Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4620032
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:30:18+00:00 2026-05-22T02:30:18+00:00

I want to create a multidimensional array with just two values : 0 or

  • 0

I want to create a multidimensional array with just two values : 0 or 1.

I use the srand/rand functions but array contains only 0.
Here is the code :

#define NB_LINE 4
#define NB_COLUMN 11
int tab[NB_LINE][NB_COLUMN] ;  // global variable
    void generate() {
    srand((unsigned int)time(NULL));

    int n, i, j;
    for(n = 0; n < NB_LINE*NB_COLUMN; ++n){
        do
        {
            i = rand() % NB_LINE;
            j = rand() % NB_COLUMN;
        }
        while (tab[i][j] != 0);
        tab[i][j] = 1 ;
    }
}

I don’t know how to solve this problem ?

thanks !

Edit : Thanks for your answers. Do you think it’s possible with rand() to have juste one “1” per column and others spots contain only 0 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:30:19+00:00Added an answer on May 22, 2026 at 2:30 am

    Edited question

    Do you think it’s possible with rand() to have just one “1” per column and others spots contain only 0?

    Yes, of course.

    Instead of looping over all lines and all columns setting each individual array element to a random value of 0 or 1 do initialize all array elements to 0 (if they aren’t already); loop over all columns and choose 1 random line and set the corresponding array element to 1.

    #include <stdlib.h>
    #include <string.h> // memset
    #include <time.h>
    
    void generate(int *arr, size_t lines, size_t columns) {
        int col, row;
        memset(tab, 0, lines * columns * sizeof *arr);
        for (col = 0; col < columns; col++) {
            row = rand() % lines;
            tab[row * columns + col] = 1;
        }
    }
    
    int main(void) {
        int tab[NB_LINE][NB_COLUMN];
        srand(time(0));                           /* no casts */
        generate(&tab[0][0], NB_LINE, NB_COLUMN);
        /* ... */
    }
    

    Notice I’ve removed the srand() from the generate() function. srand() should be called no more than once per program invocation (to guarantee maximum randomness). The best way to accomplish that is to call srand from main().
    Also, instead of i and j, I named the variables row and col.
    And, I’ve made tab a local variable rather than global. Avoid globals whenever possible: it’s a favor you do yourself. With no globals the information is passed to the function through arguments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to create a multidimensional array of gtkwidgets? Specifically something like this:
I'm having issues with returning a multidimensional array. I create the array in a
I want create a drop shadow around the canvas component in flex. Technically speaking
I want to create a Java application bundle for Mac without using Mac. According
I want to create a client side mail creator web page. I know the
I want to create a function that performs a function passed by parameter on
I want to create a simple http proxy server that does some very basic
I want to create a draggable and resizable window in JavaScript for cross browser
I want to create an allocator which provides memory with the following attributes: cannot
I want to create my Rails application with MySQL, because I like it so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.