1. ## Random numbers generator

Hello folks...
I'm working on a program that will generate 100 numbers in range of 1-999 and then there are few things I have to do....
Here is what I have so far and I'm hoping that someone can explain further to me...

a. Print the odd values, ten to a line
b. Return the count of the number of even values
c. Return the location of the smalles value in the array

Now, so far I got program that generates numbers, I have code that will print 10 to a line but I'm stuck on the loop that will check for odd values and print them to line... Please dissregard b and c and help me with a, I'll try to work on the rest...

Thanks a lot,

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 100

int main (void)
{

int number[SIZE];
int i;
int lineCount = 0;

double tmp;

srand(time( NULL ));

for(i =0; i < SIZE; i++)
{
tmp=(double)rand()/((double)RAND_MAX+1);

number[i] = (int)(999*tmp+0.5);
} // Generates numbers in the range 1-999

for(i=0; i < SIZE; i ++)
{
printf(" %d",number[i]);
lineCount++;
if (lineCount > 9)

{
printf("\n\n");
lineCount = 0;
}

} // printArray a. Print the array ten values to a line.

return 0;
} 2. Most of use do it the easy way:

rand() % 1000;

But then, most of use actually read the FAQ and sticky posts here, and search the board when we have questions...

Use code tags.

Quzah. 3. you can test if the number is a odd number with this:

if (number % 2) != 0
printf ("odd"); 4. Hmm.... See if you can adjust any of this to meet your needs.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
int i, nums, count;
char key;

count = 1;

srand((unsigned)time(NULL));

for(i=0; i < 100; i++)
nums[i] = rand() % 1000 + 1;

for(i=0; i < 100; i++)
{
if((nums[i] % 2) != 0)
{
if(count == 10)
{
printf("\npress enter...\n");
scanf("%*c", &key);
count = 1;
}

printf("nums[%d] was odd -> %d\n", i, nums[i]);
count++;
}
}

return EXIT_SUCCESS;
}``` 5. Ronin,
Thats a nice code you got there.... Works real nice...
What I really need help with is the count of even numbers....
After the numbers are generated and listen I need to get
exact count of how many even numbers (2,4 etc) are there....

And I also need to find the location of the smallest number, not the number but physical location in the array....
Any ideas how to do this???

Thanks a lot 6. Originally posted by murand
What I really need help with is the count of even numbers....
It's in the for loop.... you just don't see it yet *hint* add another variable for the even counter.

And I also need to find the location of the smallest number, not the number but physical location in the array....
Any ideas how to do this???Thanks a lot
not too bad... consider the following

Code:
```#include <stdio.h>

int main(void)
{
int array[] = { 5, 7, 2, 4, 1, 3, 2 };
int found, i, lowest;

lowest = 999;

for(i=0; i < 7; i++)
{
if(lowest > array[i])
{
lowest = array[i];
found = i;
}
}

for(i=0; i < 7; i++)
printf("array[%d] had %d. \n", i, array[i]);

printf("\narray[%d] had the smallest value -> %d",
found, array[found]);

return EXIT_SUCCESS;
}```
There are potential problems such as two or more elements having the same lowest value. If that is the case then you would need to write the appropriate fix.

It's late for me now... 3 am so my brain is 99% zonked. Hopefully that will compile. HTH 7. quote
It's in the for loop.... you just don't see it yet
*hint* add another variable for the even counter.

Ronin, In which for loop???? I went through all program and I couldnt get it going....

Thanks man,

MD 8. Ronin, thanks for help...I figured the rest out and here is my code...

MD

/* This program randomly generates 100 numbers each time that is run.
After the numbers are generated it will:

a. Print the array ten values to a line. The values will be aligned.
b. Print the odd values, ten to a line.
d. Return a count of the number of even values.
f. Return the location of the smallest value in the array.

*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 100

int main (void)
{

int number[SIZE];
int i;
int lineCount = 0;
int numOdd;
int numEven = 0;
int found;
int lowest;

double tmp;

srand(time( NULL ));

printf(" Randomly Generated Numbers Are:\n\n");
printf(" ================================================== =========\n\n");
for(i = 0; i < SIZE; i++)
{
tmp=(double)rand()/((double)RAND_MAX+1);

number[i] = (int)(999*tmp+0.5);
printf(" *%d*",number[i]);
lineCount++;
if (lineCount > 9)
{
printf("\n\n");
lineCount = 0;
}
} // a. Generates numbers in the range 1-999 and prints the array ten values to a line.

printf(" ================================================== =========\n\n");
printf(" The odd numbers are:\n\n");
printf(" ================================================== =========\n\n");
for (i=0;i < SIZE; i++)
{
numOdd++;

if (number[i] % 2)
{
printf(" *%d*", number[i]);
lineCount++;
}

if (lineCount > 9)
{
printf("\n\n");
lineCount = 0;
}

} // b. Print the odd values, ten to a line.
printf("\n\n");
printf(" ================================================== =========\n\n");

for (i = 0; i < SIZE; i++)
{
if (number[i] %2 ==0)
{
numEven++;
}
}// d. Return a count of the number of even values.

printf(" There are %d even numbers.\n\n", numEven);

printf(" ================================================== =========\n\n");

lowest = 999;
for(i=0; i < SIZE; i++)
{
if(lowest > number[i])
{
lowest = number[i];
found = i;
}
}

for(i=0; i < 1; i++)

printf(" Array[%d] had the smallest value of %d.\n\n", found, number[found]);
printf(" ================================================== =========\n\n\n");
// f. Return the location of the smallest value in the array.

return 0;

} 9. Use code tags when posting code. You can use the "edit" button to change your previous post. Popular pages Recent additions 