1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

My array is not working..

Discussion in 'C' started by bouzigue, Oct 10, 2011.

  1. bouzigue

    bouzigue New Member

    Joined:
    Oct 10, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    my program generate numbers but it does not store it in the array.. what function must i include..
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int rndm_array [10];
         
    int main(int argc, char *argv[])
    {
        unsigned int iseed = (unsigned int)time(NULL);
        srand (iseed);
    
         int i,idx;
          for (i=1; i<7; i++)
          printf ("Number[%d]= %u\n",
          i, rand () % 40+1,&rndm_array[idx]);
          {
           rndm_array [idx] = i, rand () % 40+1;
          }
          
          for (i=1; i<7; i++)
            {
              printf("\nrandom_array [%d]=",i, rand()%40+1);
              printf("%d\n", rand()%40+1);
            }
      system("PAUSE");
      return 0;
    }
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    You're displaying the contents of rndm_array[idx] before you assign it a value. Also lookup the comma operator, because the code "rndm_array[idx]=i, rand()%40+1" might not do what you think it's going to do.
     
  3. bouzigue

    bouzigue New Member

    Joined:
    Oct 10, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    in fact, m program is to generate 6 random numbers, zen store the results in the array so that the next the regenerated numbers can be compared to the previous one..
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    What output do you get, and what output did you expect?
     
  5. bouzigue

    bouzigue New Member

    Joined:
    Oct 10, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    i did get the generated random numbers but the array did not keep record of any previous generated numbers... it generate some kind of random numbers by itself.. :(
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Well I can't help if you won't answer my questions.
     
  7. bouzigue

    bouzigue New Member

    Joined:
    Oct 10, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Number[1]=27
    Number[2]=9
    Number[3]=27
    Number[4]=6
    Number[5]=19
    Number[6]=12

    random_array[1]=14

    random_array[2]=19

    random_array[3]=12

    random_array[4]=35

    random_array[5]=4

    random_array[6]=194
     
  8. bouzigue

    bouzigue New Member

    Joined:
    Oct 10, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    last one is 19..
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Your first printf has too many parameters.
    Code:
    printf ("Number[%d]= %u\n",
          i, rand () % 40+1,&rndm_array[idx]);
    
    %d prints i, %u prints rand()%40+1, and nothing displays &rndm_array[idx].

    You do that in a loop, 7 times, then after the loop ends there is an open brace, and you do
    Code:
    rndm_array [idx] = i, rand () % 40+1;
    
    which is equivalent to
    Code:
    rndm_array [idx] = i;
    
    because of the way the comma operator works. This writes the number 8, which is the current value of i, to an undefined location in memory (because idx is undefined) and therefore your program exhibits Undefined Behaviour - the most difficult to find bug of all.

    The next loop performs:
    Code:
    printf("\nrandom_array [%d]=",i, rand()%40+1);
    
    which doesn't access rndm_array at all.
     

Share This Page