In: Computer Science
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.
|
|||
|
|||
|
|||
|
|||
….. |
|||
….. |
|||
….. |
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
#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