• C

Randomize numbers Question for gj62

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;
}

}
dmuranAsked:
Who is Participating?
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.

akshayxxCommented:
do want answer from gj62 only .. or will u accept other's answer also

gj62 .. come on ur fans are waiting ..
0
gj62Commented:
This sounds like homework, so I am only going to give you hints in most places...

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...
0
dmuranAuthor Commented:
akshayxx....
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
0
Ultimate Tool Kit for Technology Solution Provider

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.

akshayxxCommented:
refer to what gj62 said .. and here is the routine that u need to generate random numbers .. ihope u can figure out the rest

int number[SIZE];
int i;
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);
}
for(i=0;i<SIZE;i++)printf("%d ",number[i]);
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..

0

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 trial
akshayxxCommented:
just in case u miss my statement in the above posting ..
WATCH 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
0
dmuranAuthor Commented:
akshay....

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
0
dmuranAuthor Commented:
Good Job....
Thanks a lot...gj is excellent too, but he give me less code :)

Thanks guys,

DM
0
akshayxxCommented:
sorry gj . u deserved this one .. rather it was for u only . but i couldnt help posting small code..when i saw some cute things in the code.

and good luck DM
0
dmuranAuthor Commented:
Guys,
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()/((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;
          } // Prints an array of ten values per line
}    
     return 0;

}
0
gj62Commented:
Impress your prof and at make sure your program only has 2 loops.

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....
0
dmuranAuthor Commented:
gj62....
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
0
gj62Commented:
You keep track of the number of odds you print per line - you said in the instructions you need to print 10 per line.

Each time you have an odd number, you increase numOdd.  When you get to 10, you insert a linefeed...
0
dmuranAuthor Commented:
ahhh ok...
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
0
gj62Commented:
You keep track of the number of odds you print per line - you said in the instructions you need to print 10 per line.

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

numOdd=lineCount=minLocation=0;
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;
  }
}        
0
dmuranAuthor Commented:
Guys, thank you all for help, gj62 and akshayxx....
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(" ===========================================================\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;

}
0
akshayxxCommented:
check your code where u have written .printing Odd numbers

u r incrementing numOdd++ before checking the number is odd or not ... check it out
0
gj62Commented:
Yeah, but it's not used, so it should actually just be removed...
0
akshayxxCommented:
yes he should remove the numOdd, if he doesnt intend to use it ..
 i thought he want to keep track of how many odd numbers u have.
0
gj62Commented:
No, just print them out 10 to a line...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
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.