# New student asking for advise

Printable View

• 09-16-2003
Tier
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(); } ]```
• 09-16-2003
twm
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.
• 09-16-2003
confuted
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.
• 09-16-2003
WaltP
Re: New student asking for advise
Quote:

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.
Quote:

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?
Quote:

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!
Quote:

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(); } ]```

• 09-16-2003
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
• 09-17-2003
WaltP
Quote:

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.