Solved

randomized

Posted on 1999-01-08
11
293 Views
Last Modified: 2010-04-15
generate 100 unique (just one time only) integer random numbers between 1000 and 10000 in an array rn[100]. How many of these numbers fall between 4000 and 7000?
0
Comment
Question by:samuelccs
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1255640
Is this a homework assignment?
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1255641
Post the code you have so far.
0
 

Expert Comment

by:aj1070798
ID: 1255642
Since I am the one who set this question in class I will be very interested to see the answer.

Have fun folks!
0
 

Expert Comment

by:rsongyl
ID: 1255643
Can be done easily. You want the short way or the long way?
The long way is set all the number with counters and if anything hits 2, reset it.
The short way?? Press F1 if you need help. I think they have online help services.


0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1255644
Well, now that you're already in trouble with your teacher, it couldn't get any worse!!

for(i=0;i<9000;i++) x[i]=i+1000;
for(i=0;i<100;i++) { r=random(9000); t=x[i]; x[i]=x[r]; x[r]=t; }
for(i=0;i<100;i++) { if((x[i]>=4000)&&(x[i]<=7000)) n++; }
0
The curse of the end user strikes again      

You’ve updated all your end user’s email signatures. Hooray! But guess what? They’re playing around with the HTML, adding stupid taglines and ruining the imagery. Find out how you can save your signatures from end users today.

 

Accepted Solution

by:
tato earned 30 total points
ID: 1255645

for(between=i=0;i<100;i++) if (array[i]>=4000 & array <=7000) between++;

printf("between 4000 and 7000 = %d",between);

0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1255646
Tato:  please do not post as an answer something somebody else already posted as a comment.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1255647
especially when it's incorrect
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1255648
I know that my code did not meet samuelccs's specifications *exactly*...my intention was for a general "hint".
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1255649
OK, guys, here is my code... Maybe not the best, but who cares....

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

int main()
{
      int array[100];
      int ran, n = 0;
      bool dup;

      randomize();
      for (int i = 0; i < 100; i++) {
            dup = FALSE;
            ran = random(9000) + 1001;
            for (int j = 0; j <= i; j++) {
                  if (array[j] == ran)
                        dup = TRUE;
            }
            if (!dup) {
                                      array[i] = ran;
                                                      cout << array[i] << ' ';
                  if (array[i] >= 4000 && array[i] <= 7000) n++;
            } else i--;
      }
      cout << "\n\nThere were " << n << " in the range of 4000 and 7000";
      return 0;
}
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1255650
Oppps....

THis line

cout << "\n\nThere were " << n << " in the range of 4000 and 7000";

should have been

cout << "\n\nThere were " << n << " numbers in the range of 4000 and 7000";
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now