Thread: New student asking for advise

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    2

    Talking New student asking for advise

    I am a new programming student and am having a bit of diffulty on an assignment. I do not want someone to finish writing my code, just some advise on what to do or how to accomplish a task. Our assignment was to generate 'x' amount of random numbers and then sort them into rows and columns as determined by the user. I know this is basic array manipulation, I can generate numbers and ask the user how many rows and columns, but I can not put those numbers into an array, every time I use a statement like "inary[100] = rand()" and test it with a printf function later it outputs a different number than what was generated. I have listed a small piece of code , if you could help me in any way I would be very thankful.

    Code:
    [
    
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    
    
    void main(void);
    void main(void)
    
    {
    
    	int i, no, rowno, colno;
            int inary[100] = {rand()}, outary[100][100] = {rand()};
            srand(0);
    
            printf ("How many random numbers to be generated?");
            scanf ("%d", &no);
            {
    
            	for (i=1; i<=no; i++)
    		printf("%d\n", rand());
                    }
    
            printf ("How many rows?");
            scanf ("%d", &rowno);
    
            printf ("How many columns?");
            scanf ("%d", &colno);
    
    
    
            getch();
    
    }
    ]

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    You don't need an array, just print the numbers out as you go and when you reach the end of the row, print a newline:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( ) {
      int i;
      int j;
      int r;
      int c;
    
      printf("Enter rows: ");
      fflush(stdout);
      scanf("%d", &r);
      printf("Enter cols: ");
      fflush(stdout);
      scanf("%d", &c);
      for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++)
          printf("%4d ", rand() % 9999);
        printf("\n");
      }
    
      return 0;
    }
    >void main(void);
    There's no need to prototype main, and it returns int.

    >void main(void)
    Ditto, main returns int.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    void main() details:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    srand() details: (you used it poorly)
    http://www.cplusplus.com/ref/cstdlib/srand.html

    Oh, and you appear to have [ /*your code*/ ]. The square brackets might have just been you trying to use code tags... they don't belong in the code though, if you don't know.
    Away.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: New student asking for advise

    Originally posted by Tier
    I am a new programming student and am having a bit of diffulty on an assignment. I do not want someone to finish writing my code, just some advise on what to do or how to accomplish a task.
    Good. many noob don't want help, they want the handout.
    Our assignment was to generate 'x' amount of random numbers and then sort them into rows and columns as determined by the user.
    Determined in what way? Sorted how?
    I know this is basic array manipulation, I can generate numbers and ask the user how many rows and columns, but I can not put those numbers into an array, every time I use a statement like "inary[100] = rand()" and test it with a printf function later it outputs a different number than what was generated. I have listed a small piece of code , if you could help me in any way I would be very thankful.
    Code Tags on a first post? This has got to be a red-letter day! Thank you Tier!


    Code:
    [
    
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    
    
    void main(void);
    void main(void)
    
    {
    
    	int i, no, rowno, colno;
    Don't load values here, just create the arrays
            int inary[100] = {rand()}, outary[100][100] = {rand()};
    
    The seed should not be a constant, it will produce the same sequence of 
    random numbers each time the program is run.
    Use srand(time()); and include time.h
            srand(0);
    
            printf ("How many random numbers to be generated?");
            scanf ("%d", &no);
            {
    
            	for (i=1; i<=no; i++)
    
    Here you are not storing any random values, so you have nothing for later.  
    You might want to store the rand value in inaray then print that value.
    		printf("%d\n", rand());
                    }
    
            printf ("How many rows?");
            scanf ("%d", &rowno);
    
            printf ("How many columns?");
            scanf ("%d", &colno);
    
    
    
            getch();
    
    }
    ]
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    2
    WaltP,

    Thanks for the advise, in responce to your question "Determined in what way? Sorted how?" the instructor wanted us to ask the user how many numbers to be generated, then ask how many rows and columns to display them in, say you generate 20 numbers you could have 4 rows and 5 columns, he wanted us to keep the product of the row and column the same amount of the number generated (4 * 5 = 20) to avoid things going haywire.

    Thanks

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Tier
    WaltP,

    Thanks for the advise, in responce to your question "Determined in what way? Sorted how?" the instructor wanted us to ask the user how many numbers to be generated, then ask how many rows and columns to display them in, say you generate 20 numbers you could have 4 rows and 5 columns, he wanted us to keep the product of the row and column the same amount of the number generated (4 * 5 = 20) to avoid things going haywire.

    Thanks
    Then I'd suggest simply asking for the number of rows and colums and calculate the number of values to generate. That way the user doesn't have to do any math himself, or enter values that don't coincide.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 03:56 AM
  3. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 01:21 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. A few tips please...
    By gems in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 02:28 PM