Originally Posted by
jocdrew21
@CSharpener, thank you and I will use that advice and I will implement that very shortly. Since the semester is over and I have some free time I am trying to apply what I have learned,brain is in fun over drive at the moment. I will post the code in question below.
I am doing the same thing, but now I have two loops & when they both are == the number is outputted, else keep looping.
I will have a different function later for using the advice @CSharpener gave me.
I have a feeling I the code is not working correctly because my first number is always 20 which is why I switched it to 7 numbers instead of 6. Seems the numbers below 20 are random, but the first number is not.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUMBERS 7
int main(int argc, const char * argv[])
{
float randValue = 0.0;
float randValue2=0.0;
int i=0,j = 0;
srand(time(NULL));
while (randValue!=randValue2)
for (i=0; i<NUMBERS; i++) {
randValue= 1 + (int)rand() % 49;
printf("%6.0f\n",randValue );
}
for (j=0; j<NUMBERS; j++) {
randValue2= 1 + (int)rand() % 49;
printf("%6.0f\n", randValue2 );
}
return 0;
}
Why are you trying to use floats with this? Rand() doesn't return a float, so there is no point. Also, if you are trying to just print numbers and find the average, you don't need two loops. You could handle all of it in one loop.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUMBERS 7
int main( )
{
int randValues[NUMBERS] = {0}, sum = 0, i = 0;
srand(time(NULL));
for (i = 0; i < NUMBERS; sum += randValues[i], i++)
printf("%d ", randValues[i] = ((rand() % 49)+1));
sum /= NUMBERS;
printf("\nAverage : %d", sum);
return 0;
}
Edit : I just read that you wanted mode not average, but my advice about floats still applies.