Solved

randomized

Posted on 1999-01-08
11
292 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

762 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

17 Experts available now in Live!

Get 1:1 Help Now