Question

In: Computer Science

its a c++ programme. In your program, you will randomly generate integer numbers between -50 and...

its a c++ programme.

In your program, you will randomly generate integer numbers between -50 and +50 (including -50 and +50). You will repeat this process 1000 times. While generating these numbers, you need to count the numbers based on their signs separately, as positive and negative. For instance, if your program generate +25 15 times, and -25 19 times. Then this should be reported as,

Num PosFre NegFre
25  15     19

For this problem, you need to use array of struct, similar to depicted below.

Number

PosFre

NegFre

Number

PosFre

NegFre

Number

PosFre

NegFre

Number

PosFre

NegFre

…..

…..

…..

Report all number from 1 to 50 in an ascending order. Note that you will not use any sort algorithm in this assignment.

A sample output is given as

Num PosFre NegFre
1   13     45

2   54     59

3   55     16

4   35     9

...

...

...

49 44    29

50 35    69

Solutions

Expert Solution

#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

//structure
struct number
{
int num;
int posFre;
int negFre;
};

int main()
{
struct number n[100];
  
int randNum;
  
for(int i = 0; i<=50; i++)
{
n[i].num = i;
n[i].negFre = 0;
n[i].posFre = 0;
}
  
for(int i = 0; i<1000; i++)
{
//generate random number
randNum = rand() % 101 - 50;

if(randNum<0)
{
n[abs(randNum)].negFre = n[abs(randNum)].negFre + 1;
}
else
{
n[randNum].posFre = n[randNum].posFre + 1;
}
}
  
//display message
cout<<"Num"<<setw(10)<<"posFre"<<setw(10)<<"negFre"<<endl;
  
//display the list
for(int j = 1; j<=50; j++)
{
cout<<n[j].num<<setw(7)<<n[j].posFre<<setw(10)<<n[j].negFre<<endl;
}
  
return 0;
}

OUTPUT:

Num posFre negFre
1 8 11
2 4 8
3 10 10
4 8 12
5 9 4
6 5 12
7 14 12
8 10 5
9 14 10
10 9 10
11 12 17
12 11 10
13 11 13
14 12 8
15 11 12
16 10 8
17 9 12
18 8 11
19 10 10
20 10 8
21 14 8
22 9 11
23 9 11
24 3 9
25 11 13
26 13 11
27 19 8
28 11 6
29 6 8
30 9 8
31 13 11
32 7 10
33 13 6
34 10 8
35 6 11
36 9 12
37 12 11
38 6 7
39 11 9
40 4 17
41 8 11
42 8 16
43 8 14
44 13 6
45 9 14
46 9 11
47 15 9
48 5 10
49 8 7
50 12 15



Related Solutions

Write a C++ program that randomly generates N integer numbers (such that N is entered by...
Write a C++ program that randomly generates N integer numbers (such that N is entered by the user) and then stores them to a text file (myNumbers.txt) sorted in increasing (non-decreasing) order. Again, please notice that the size of the data (N) is known during the run time, not the compile-time (needs to be entered by the user after running the program).
C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The...
C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The program prompts the user to enter a number continuously until the number matches the randomly generated number. For each user input, the program tells the user whether the input is too low or too high, so the user can choose the next input intelligently. Here is a sample run:
Let a , b , c be three integer numbers. Write a C++ program with a...
Let a , b , c be three integer numbers. Write a C++ program with a functions void rotate1(int* a,int* b,int* c) void rotate2(int& a,int& b,int& c) such that a -> b , b -> c and c -> a. Thus we use two different approaches (pointers in rotate1 and references in rotate2).
Design a program that uses an array to store 10 randomly generated integer numbers in the...
Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: 1. Display 10 random numbers stored in the array 2. Compute and display the largest number in the array 3. Compute and display the average value of all numbers 4. Exit The options 2...
For this assignment, write a program that will generate three randomly sized sets of random numbers...
For this assignment, write a program that will generate three randomly sized sets of random numbers using DEV C++ To use the random number generator, first add a #include statement for the cstdlib library to the top of the program: #include <cstdlib> Next, initialize the random number generator. This is done by calling the srand function and passing in an integer value (known as a seed value). This should only be done ONE time and it MUST be done before...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100 and 300. Pick a number at random out of these 50 random variables. a. Determine the probability of the chosen number being greater than 200. This may be achieved by counting the numbers that are greater than 200 and dividing the count by 50. Make sure you, i.Formulate the appropriate if-conditions to check for a number being greater than 200 ii. Use a for-loop...
Write a java program which can randomly generate a permutation of the integer {1, 2, 3,...
Write a java program which can randomly generate a permutation of the integer {1, 2, 3, ..., 48,49,50}. Use the most efficient sorting algorithm to sort the list in an acceding order.
The Sum and The Average In C++, Write a program that reads in 10 integer numbers....
The Sum and The Average In C++, Write a program that reads in 10 integer numbers. Your program should do the following things: Use a Do statement Determine the number positive or negative Count the numbers of positive numbers, and negative Outputs the sum of: all the numbers greater than zero all the numbers less than zero (which will be a negative number or zero) all the numbers Calculate the average of all the numbers. The user enters the ten...
[C#] Randomly generate two numbers that user chooses and then ask the user what the answer...
[C#] Randomly generate two numbers that user chooses and then ask the user what the answer is when some operator is applied. If user is correct, they will be congratulated. If they are wrong, they will be given the correct answer. This should be repeated based on how many times is chosen by user. I have the code which will do the following below: User wants to know the answer to x % y, What is smallest value of x:...
For this problem, you will write a program using two queues. Generate n random numbers between...
For this problem, you will write a program using two queues. Generate n random numbers between 10 and 100 (both inclusive), where n>9. The value of n should be taken as input from the user and n should be >9. The numbers could be duplicated. Enqueue all these numbers to the first queue. The objective is to find the numbers whose sum of digits is odd and enqueue them to the second queue. The remaining numbers (whose sum of digits...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT