?
Solved

Randomize numbers Question for gj62

Posted on 2003-03-13
20
Medium Priority
?
205 Views
Last Modified: 2010-04-15
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;
}

}
0
Comment
Question by:dmuran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 6
20 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8132984
do want answer from gj62 only .. or will u accept other's answer also

gj62 .. come on ur fans are waiting ..
0
 
LVL 6

Expert Comment

by:gj62
ID: 8132999
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
 

Author Comment

by:dmuran
ID: 8133206
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Accepted Solution

by:
akshayxx earned 80 total points
ID: 8133332
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8133370
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
 

Author Comment

by:dmuran
ID: 8133411
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
 

Author Comment

by:dmuran
ID: 8133418
Good Job....
Thanks a lot...gj is excellent too, but he give me less code :)

Thanks guys,

DM
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8133477
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
 

Author Comment

by:dmuran
ID: 8133612
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
 
LVL 6

Expert Comment

by:gj62
ID: 8133715
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
 

Author Comment

by:dmuran
ID: 8140173
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
 
LVL 6

Expert Comment

by:gj62
ID: 8140748
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
 

Author Comment

by:dmuran
ID: 8141025
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
 
LVL 6

Expert Comment

by:gj62
ID: 8142363
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
 
LVL 6

Expert Comment

by:gj62
ID: 8142388

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
 

Author Comment

by:dmuran
ID: 8155257
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8155989
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
 
LVL 6

Expert Comment

by:gj62
ID: 8156143
Yeah, but it's not used, so it should actually just be removed...
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8156204
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
 
LVL 6

Expert Comment

by:gj62
ID: 8156226
No, just print them out 10 to a line...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question