Thread: output rand() in array

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    16

    Smile output rand() in array

    Hi everyone, this my first post. I'm wanting to output six unique integers, but the output is one integer. Any help please.
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main()
    {
       int a, n;
       char m[6];
       char num[6] = {0};
       srand((unsigned)time (NULL));
       for(a = 0;a < 6;a++)
       num[a] = rand() % 40 + 1;{
       for(n = a + 1;n < 6; n++)
          m[n];
          {
           if(num[a] != m[n])
           printf("%d\n", num[a]);
           }
         }
    
       return 0;
    }
    Last edited by anaer0bic; 09-17-2010 at 02:21 PM. Reason: pasted wrong

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your program is actually equivalent to:
    Code:
    int main()
    {
        int a, n;
        char m[6];
        char num[6] = {0};
        srand((unsigned)time (NULL));
        for(a = 0;a < 6;a++)
        {
            num[a] = rand() % 40 + 1;
        }
    
        {
            for(n = a + 1;n < 6; n++)
            {
                m[n];
            }
    
            {
                if(num[a] != m[n])
                    printf("%d\n", num[a]);
            }
        }
    
        return 0;
    }
    In other words, your for loops have a single statement each as the body. You need to fix that by proper brace placement.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    16
    just did some gymnastics. thanks laserlight.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
       int a, n;
       char m[6];
       char num[6] = {0};
       srand((unsigned)time (NULL));
       for(a = 0;a < 6;a++){
       num[a] = rand() % 40 + 1;
       
       for(n = a + 1;n < 6; n++){
          m[n];
           }
           if(num[a] != m[n])
           printf("%d\n", num[a]);
           }
           
         
    
       return 0;
    }

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's really a lot easier to read if you indent it nicely. At least make the opening and closing curly braces line up (or the closest equivalent if you like braces on the same line as for loops etc), and indent the code inside the braces.

    What is this supposed to do?
    Code:
    m[n];
    [edit] One way you could indent your code (4 spaces of indentation, K&R style braces):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main() {
        int a, n;
        char m[6];
        char num[6] = {0};
        srand((unsigned)time (NULL));
        for(a = 0;a < 6;a++) {
            num[a] = rand() % 40 + 1;
       
            for(n = a + 1;n < 6; n++) {
                m[n];
            }
            if(num[a] != m[n])
                printf("%d\n", num[a]);
        }
        return 0;
    }
    Much easier to see what's going on, isn't it? If you wrote your code in this style out of habit, you wouldn't need laserlight's alignment skills to find bugs.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  2. Output an array in a textbox
    By Diablo02 in forum C# Programming
    Replies: 5
    Last Post: 10-18-2007, 03:56 AM
  3. Array output strange
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2006, 06:58 AM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM