Thread: Question on Homework

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    6

    Question on Homework

    how would I store a random number generator into the array?

    Code:
    #include <stdio.h>
    #include <conio.h>
    #define MAX_SIZE 100//part 2
    int main(void)
    
    
    
    //Declaring Statements
    {
        int i;
        int list[MAX_SIZE]={i};//part 2
        int numPrinted=0;//part 2
        srand (time(NULL));
    {
        printf("Numbers Unsorted:\n");
    }
    
        while(i<100)
        for (i=0;i<MAX_SIZE; i++)
        {
    
            printf("%3d, %3d", rand()%1000, list[i]);
            if (numPrinted <9)
                numPrinted++;
            else
                {
            printf("\n");
            numPrinted=0;
            }
    
    
        }
    
    
        return 0;
    
    }
    because if i tried to implement the random number generator to store into the array, the output would keep resulting in printing out 1 number. my goal is to print out 100 random numbers and store them while still having the out put to have at least 10 numbers per line.

    for example: when i put in for(i=0; i<100; i++)
    list[MAX_SIZE]=rand()%1000;

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    try
    Code:
    list[i]
    You must read again the theory for array's indexing (if not the whole chapter)!

    //A bit advanced (first focus on the indexing of the arrays!), the result of rand is pseudorandom.. More.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    C and C++ don't have the declarative power for that style of approach to work.

    They are both, at the core, simple procedural languages.

    Simply put, you've not "told" the program to store the random values in the array so it, very naturally, does not store the values into the array.

    [Edit]
    Also, your indenting is atrocious and inconsistent. Fix it.
    [/Edit]

    [Edit]
    You must read again the theory for array's indexing (if not the whole chapter)!
    That certainly would not be a bad idea, but I'm compelled, by nearly infinite curiosity this time, to ask: what are you referring to? The indexing is the one thing he seems to have right.
    [/Edit]

    Soma
    Last edited by phantomotap; 04-22-2013 at 07:02 PM.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    At this line
    Code:
    list[MAX_SIZE]=rand()%1000;
    he or she has it outside code tags.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    he or she has it outside code tags
    O_o

    Ah, well, there's my problem; I was looking all over the code! ^_^;

    Soma

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    appreciate the critism and the opinions will apply it thanks

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Line 22, you have both the thing you want to assign and the thing you want to assign it to right there inside the printf.
    Take those bits, and make use of the assignment operator, and you're away laughing!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There appears to be a parallel discussion with an equally generic topic name: Question Need Help:
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question on homework
    By camel-man in forum C Programming
    Replies: 2
    Last Post: 02-02-2011, 11:30 AM
  2. Homework Question
    By Jozrael in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2008, 05:42 AM
  3. Homework question
    By Jozrael in forum C++ Programming
    Replies: 7
    Last Post: 02-07-2008, 09:30 PM
  4. Another Homework Question
    By Trekkie in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2005, 06:57 AM
  5. Homework Question...
    By djz3r0 in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 06:36 AM