Thread: Question Need Help:

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

    Question Need Help:

    Well my goal right now is to learn C and so far i've been enjoying it. But I reached a problem in which I can't seem to understand how to implement. So my goal was to create a number generator where I can create 100 unique random numbers from 0 to 999 and have it be displayed as to 10 number's per line. so far for my coding I had this.

    Program to run random number generator:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <time.h>
    int main (void)
    {
    
    int i;
    
    srand (time(NULL));
    //pt1: Generating 100 random Unique Numbers
    
    
    while (i<100)
    //pt1: Generating 100 random Unique Numbers
    
    {
        printf ("%3d\n", rand()%1000);
    //pt1: Generating 100 random Unique Numbers
        i++;//pt1: Generating 100 random Unique Numbers
    
    }
    
    return ;
    
    }
    .

    And this one was to run the 10 numbers per line:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #define MAX_SIZE 100
    #include <time.h>
    int main(void)
    {
        int i;
        int list[MAX_SIZE];
        int numPrinted=0;
        srand(time(NULL));
    
        for(i=0; i<MAX_SIZE; i++)
    
        {
            printf("%3d", list[MAX_SIZE]);
            if(numPrinted<9)
                numPrinted++;
            else
            {
                printf("\n");
                numPrinted=0;
            }
    
        }
        return 0;
    
    }
    .
    now when i implemented the 10 number's per line with the random number generator. *in which i will display code as well*, it seems that for the output it would only generate 1 number.

    Question I have right now is how do i make it generate more numbers for the output? and is it because im not for looping the random numbers into the array?.


    Coding that only prints out 1 number:
    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++)
            list[MAX_SIZE]=rand()%1000;
        {
    
            printf("%3d\n",list[i]);
            if (numPrinted <9)
                numPrinted++;
            else
                {
            printf("\n");
            numPrinted=0;
            }
    
    
        }
    
    
        return 0;
    
    }
    this is the coding that actually prints out random numbers with 10 per line but some reason i have random 0's in it which throws me off:
    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;
    
    }
    I ask for help because im at a mental block as to how to solve it. Would appreciate the help if given. my overall goal as to this is to try to output these numbers and then be able to apply a insertion sort function. if possible.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    so i should remove

    Code:
    for(i=0; i<MAX_SIZE; i++)
    {
        printf("%3d", list[MAX_SIZE]);
    and replace it with
    printf("%3d", list[i]);?

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Try it out and tell us
    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

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    well doing that to the 2nd code, it generates weird numbers that are out of context which i don't understand.
    since its generating loads of weird numbers out of context for the code in which it generates the 10 numbers per line:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #define MAX_SIZE 100
    #include <time.h>
    int main(void)
    {
        int i;
        int list[MAX_SIZE];
    
        int numPrinted=0;
        srand(time(NULL));
     
        for(i=0; i<MAX_SIZE; i++)
     
        {
            printf("%3d", list[i]);
    
            if(numPrinted<9)
                numPrinted++;
            else
            {
                printf("\n");
                numPrinted=0;
            }
     
        }
        return 0;
     
    }
    am I suppose to implement the random number generator in there and have it store into the array which is [MAX_SIZE]?
    Last edited by Lenfried; 04-22-2013 at 03:22 PM.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    still having a problem with it ._.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    *grumble*

    Okay, if a moderator comes by, can we please get this thread joined with the other for the sake of context and completeness?

    Soma

    Question on Homework

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'll close this one instead, otherwise we might get a jumbled up thread.
    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. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. *szString = things question/memory question
    By Jang in forum C Programming
    Replies: 3
    Last Post: 01-20-2011, 04:59 AM
  3. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  4. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 02:02 PM
  5. A question of color...(not a racial question)
    By Sebastiani in forum Windows Programming
    Replies: 7
    Last Post: 01-15-2003, 08:05 PM