Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Hi, here it is...

I first have to have this program randomly create 100 numbers in range of 1 to 999......

then:

a. Print the array ten values to the line.

b. Print the odd values, ten to a line.

c. Return count of the number of even values.

d. Return location of the smalles value in the array.....

// I believe I should be ok with a-d assignments as long as I get the numbers properly generated.//

I started off with 8 errors and after I read your first comment I'm down to 4.... Can you look at this code that I kinda fixed and tell me whats wrong here....

Oh, btw I'm posting 20 points only so please dont use too much of your time on it cause I don't have any more points, I'll make sure to give u some extra when I get more points...

I also don't understand what this is:

srand( (unsigned)time( NULL ) );

Thanks a lot... Here is what I got so far....

SOURCE CODE

********************************************************************************************************

#include <stdio.h>

#include <stdlib.h>

#define SIZE 100

int main (void)

{

int number [SIZE];

int num;

int i;

int max;

int a;

num = rand() % 999 +1;

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

{

number[i] = rand() % 999 + 1;

{

if (max % 3 ==0 || max % 7 == 0)

a[i] = -num;

else

a[i] = num;

} // to generate numbers

return 0;

}

}

I first have to have this program randomly create 100 numbers in range of 1 to 999......

then:

a. Print the array ten values to the line.

b. Print the odd values, ten to a line.

c. Return count of the number of even values.

d. Return location of the smalles value in the array.....

// I believe I should be ok with a-d assignments as long as I get the numbers properly generated.//

I started off with 8 errors and after I read your first comment I'm down to 4.... Can you look at this code that I kinda fixed and tell me whats wrong here....

Oh, btw I'm posting 20 points only so please dont use too much of your time on it cause I don't have any more points, I'll make sure to give u some extra when I get more points...

I also don't understand what this is:

srand( (unsigned)time( NULL ) );

Thanks a lot... Here is what I got so far....

SOURCE CODE

**************************

#include <stdio.h>

#include <stdlib.h>

#define SIZE 100

int main (void)

{

int number [SIZE];

int num;

int i;

int max;

int a;

num = rand() % 999 +1;

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

{

number[i] = rand() % 999 + 1;

{

if (max % 3 ==0 || max % 7 == 0)

a[i] = -num;

else

a[i] = num;

} // to generate numbers

return 0;

}

}

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

srand( (unsigned)time( NULL ) );

This statement "seeds" the random number generator with the time from your system. If you don't do this, you will ALWAYS get the same numbers in the same order when you run your program.

I think all of your errors are coming because you have declared 'a' as an integer, yet you are attempting to treat it as an array:

a[i] - this is illegal if a is an integer.

Why are you using 'a' at all in this case?

Let's start after the for loop - that is filling in the array of random numbers just fine. Now you have to print the numbers 10 to a line - that should be easy enough - give it a try and post if you are stuck.

To print the odd numbers 10 to a line, you can't easily use a for loop - I would suggest a while loop, with the test while( numOdd < 10) - and you can test for odd by doing if (number[i] % 2) - it will be true if the number is odd. Remember to increment i and numOdd correctly....

Using the same test for odd, you can have an else statement that increment numEven each time you find an even number, to print out when you are done.

The location of the smallest value can be found by scanning the array (you can do this in one of your other loops) and always testing if number[i] < min, and updating locationMin = i when you find a lower number.

Hope this helps - work on it some and then post what you've got...

Please you can respond too.... What I really need help is with the code to generate the numbers I should be ok with the rest....

gj62.... Still crashes instead of generating numbers...

Thanks guys

DM

int number[SIZE];

int i;

double tmp;

srand(time( NULL ));

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

{

tmp=(double)rand()/((doubl

number[i] = (int)(999*tmp+0.5);

}

for(i=0;i<SIZE;i++)printf(

printf("\n");

and separated the random number generation in two steps to make u understand the concept behind it ..

tmp=rand()/(RAND_MAX+1) ........this gives u a random number in the range

0 and 1.0 (u'll never get 1.0)

then u multiply this with 999 . that will give u numbers in the range 0 and 999 ( excluding 999 ) .. so i have added 0.5 so that for numbers like 998.5 - 998.99999 u will get 999 like

(int)(998.6+ 0.5) = (int)(999.1) = 999

ALSO one big mistake in ur code is

while generating the random numbers u have put semicolon just after the for loop .. due to which it never enters the random number generation blocks .. cause of which u see all the junk values ..

i hope u can figure out the rest now..

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialWATCH OUT FOR THE ';' THAT U HAVE PUT JUST AFTER THE for LOOP

IT NEVER GETS IN THE NUMBER GENERATION .. AND ALWAYS SHOWS SAME JUNK VALUES

Thanks a lot... I think I can figure the rest out...

gj and akshay you guys both deserve more points... I'll make sure to give u more credit....

DM

and good luck DM

This is what I got so far, I haven't started with the rest yet but I got good idea what should I do...

You guys are awsome, gj62 and akshayxx..................

See all I needed was beginning, thanks agin :)

DM

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define SIZE 100

int main (void)

{

int number[SIZE];

int i;

double tmp;

int lineCount = 0;

srand(time( NULL ));

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

{

tmp=(double)rand()/((doubl

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;

} // Prints an array of ten values per line

}

return 0;

}

One loop is required to generate the random numbers, the other is required to print just the odd numbers (because you want the printout of the odd after the printout of all the numbers). The count of the even numbers and the calculation of the index of the minimum can be done within one of these two loops.

If you can do this with just those 2 loops, I won't mind that you gave the points to akshayxx, who shouldn't post code for homework questions...

OK, now I will get off my high horse and say G'nite all....

Why less then 10???? while( numOdd < 10),can you explain what does < 10 represent in the code.....

From my understanding I have to grab each number in the array and %2.... If its true it will be odd and it will be printed to the screen...

DM

Each time you have an odd number, you increase numOdd. When you get to 10, you insert a linefeed...

Well, this is as far as I got... I got a and b done...

How to count how many even numbers are there???

I have this loop setup:

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

{

if (number[i] % 2 == 0)

{

numEven++;

printf(" %d", number[i]);

lineCount++;

}

if (lineCount > 9)

{

printf("\n\n");

lineCount = 0;

}

}

This only prints the even numbers to the screen...Can you tell me how the count works...

Thanks,

DM

Each time you have an odd number, you increase numOdd. When you get to 10, you insert a linefeed...

numOdd=lineCount=minLocati

minNumber = number[0];

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

{

if (number[i]<minNumber)

{

/* find the location of the smallest number*/

minLocation = i;

}

if (number[i] % 2 == 0)

{

/* you just want to count the number of even, not print them...*/

numEven++;

}

else

{

/* we have an odd number */

if (lineCount > 9)

{

/* we've got 10 on the line, so print a newline*/

printf("\n");

lineCount = -1; /* we're going to increment in 3 lines, so set to -1 (will become 0 soon...)*/

}

printf("%d ", number[i]);

++lineCount;

}

}

I finaly figured the rest out....

Thanks again,

DM

/* 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(" ==========================

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

{

tmp=(double)rand()/((doubl

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(" ==========================

printf(" The odd numbers are:\n\n");

printf(" ==========================

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(" ==========================

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(" ==========================

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(" ==========================

// f. Return the location of the smallest value in the array.

return 0;

}

u r incrementing numOdd++ before checking the number is odd or not ... check it out

i thought he want to keep track of how many odd numbers u have.

C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

gj62 .. come on ur fans are waiting ..