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