-
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.
-
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]);?
-
Try it out and tell us :D
-
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]?
-
still having a problem with it ._.
-
*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
http://cboard.cprogramming.com/c-pro...-homework.html
-
I'll close this one instead, otherwise we might get a jumbled up thread.