1 dimensional array of 1000 pseudo-random integers

hi guys pls help me with this question;
---------------------------------------------------
Write a C++ program that uses a 1 dimensional array of 1000 pseudo-random integers (between 1
and 50, inclusive). Write value returning functions that return:
· The sum of the value in the array ( Σ
-
=
=
1
0
[ ]
n
i
sum values i )
· The average of the values in the array ( Σ
-
=
= ´
1
0
[ ]
1 n
i
values i
n
average )
· The maximum value in the array
· The minimum value in the array
Write a void function to return
· The most frequently occurring number in the array (you will need to make another array for
this) and the number of times it occurs.
Each function should only take the array as a parameter (and possibly the array size – be careful not
to do this unnecessarily – you will lose marks).
Separate your functions into a library.


================================================================
i need help with the void function to return
· The most frequently occurring number in the array (you will need to make another array for
this) and the number of times it occurs.
Firstly, void functions don't return anything, but anyway...
To get the most frequent number you will have to create another array (as it is written) that holds the number of occurrences of every value. The length of the array should be 50 since you have 50 different values.
Iterate through your first array (the one with random values) and for each value increment the appropriate element of the counter array.
Now you have the array with occurrences of every value. Good luck
ok im lost ....
Each function should only take the array as a parameter (and possibly the array size – be careful not
to do this unnecessarily – you will lose marks).
Separate your functions into a library.

....how do i make functions with the array as the parameter compiler keeps returning error |error: invalid conversion from `int' to `int*'|

and btw hw do i genertae the Random numbers Using the srand() method ONCE

heres all the code i gt so far.......
----------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <cstdlib>
#include <ctime>


using namespace std;

int MAX(int num[10])
{
int max;
max = num[1000];

for(int i = 0 ; i < 1000 ; i++)
{
if (num[i] > max)
{
max = num[i];
}
}

return max;
}

int MIN(int num[1000])
{

int min;

min = num[1];
for(int i = 0 ; i < 1000 ; i++)
{
if (num[i] < min)
{
min = num[i];
}
}

return min;
}



int SUM(int num[1000])
{
int a, b[1000];
for(int i = 0 ; i < 1000 ; i++)
{
a = a + b[i];
}

return a;
}

double AVE(int num[1000])
{
double ave;

ave = sum(num[1000]) / 1000;

return ave;
}


int main()
{
int num[1000] = {0} ;
int sum,max,min;
double ave;


srand((unsigned)time(0));
for(int i=0; i<1000; i++)
{
num[i] = rand()%50+1;

}

sum = SUM(num[1000]);
max = MAX(num[1000]);
min = MIN(num[1000]);
ave = AVE(num[1000]);


return 0;
}
Firstly, do use [code] tags please. And some indentation too.
Since I'm going to discuss your code now, I'll do that for you:
1
2
3
4
5
6
7
8
9
10
11
int MAX(int num[10]){
    int max;
    max = num[1000];

    for(int i = 0 ; i < 1000 ; i++){
        if (num[i] > max){
            max = num[i];
        }
    }
    return max;
}

To declare a function that takes an array as an argument you do not need to specify its length. You can write "int num[]" or "int* num".
On line 3 you're doing something odd. num has 1000 elements: from 0 to 999. It does not have a 1000th element. The value of num[1000] is undefined. This could result in error. "max = 0" or "max = num[0] would be healthy alternatives.

1
2
3
4
5
6
7
8
9
10
11
int MIN(int num[1000]){
    int min;

    min = num[1];
    for(int i = 0 ; i < 1000 ; i++){
        if (num[i] < min){
            min = num[i];
        }
    }
    return min;
}

The same problems here. "min = num[1]" is alright, but I don't understand why did you use num[1000] in MAX(). Did you expect it to be greater that num[1]?

1
2
3
4
5
6
7
int SUM(int num[1000]){
    int a, b[1000];
    for(int i = 0 ; i < 1000 ; i++){
        a = a + b[i];
    }
    return a;
}

Why are you using the b array? It is undefined. It has no relation to num array. Write "a = a + num[i]".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(){
    int num[1000] = {0} ;
    int sum,max,min;
    double ave;

    srand((unsigned)time(0));
    for(int i=0; i<1000; i++){
        num[i] = rand()%50+1;
    }

    sum = SUM(num[1000]);
    max = MAX(num[1000]);
    min = MIN(num[1000]);
    ave = AVE(num[1000]);

    return 0;
}

Why did you ask how to fill the array with only one srand()? You're doing it above...

For the occurrences counting function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void COUNT(int num[]){
    int frequencies[50];
    for(int i = 0; i < 1000; i++){
        //increment the appropriate element of frequencies array
    }
    int max = 0;
    for(int j = 0; j < 50; j++){
        //very much like your MAX function
        //you have to find the highest value of frequencies[j]
        //and store 'j' into 'max'
    }
    //now the most common value is 'max+1' and the number
    //of its occurrences is 'frequencies[max]
}
could you please explain the void count i dont get it at all plssssss
Be more specific about which parts you don't understand.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void COUNT(int num[]){
    int frequencies[50];
    for(int i = 0; i < 1000; i++){
        //increment the appropriate element of frequencies array
       //WHAT SHOUD I INCREMENT HERE
    }
    int max = 0;
    for(int j = 0; j < 50; j++){
          if (frequencies[j] > max)
              {
                max = frequencies[j]; 
              }
        //very much like your MAX function
        //you have to find the highest value of frequencies[j]
        //and store 'j' into 'max'
    }
    //now the most common value is 'max+1' and the number
    //of its occurrences is 'frequencies[max](i dont understandthis part also)
}


could you please rewrite the void COUNT function with the code that i should put in plssss
//WHAT SHOUD I INCREMENT HERE
if num[i] == x then increment the x'th element in 'frequencies'
frequencies array will hold the number of occurrences of every value from 1 to 50. Arrays start from 0 so you have to subtract 1. That means increment frequencies[x-1].

(i dont understandthis part also)
Thats because you didn't understand the middle part (I hope).
max = frequencies[j] is wrong. I wrote
store 'j' into 'max'
that means max = j. max and j are both indices then, so how can you compare frequencies[j] to max? compare it to frequencies[max] instead.

Of course you could do the way you did, but then you'd have to add some more code in the end.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <valarray>



using namespace std;


int max(int  num[])
{

    int max;


    max = 0;
    for(int i = 1 ; i <= 1000 ; i++)
    {
      if (num[i] > max)
      {
         max = num[i];

      }
    }

     cout<<"Maxium Number is :"<<max<<endl;

return max;

}

int min(int  num[])
{

    int min;


    min = 50;
    for(int i = 1 ; i <= 1000 ; i++)
    {
      if (num[i] < min)
      {
         min = num[i];

      }
    }

     cout<<"Minimum Number is :"<<min<<endl;

return min;

}

int sum(int  num[])
{

 int sum ;

    for(int i = 1 ; i <= 1000 ; i++)
    {
        sum = sum + num[i];
    }

     cout<<"Sum is :"<<sum<<endl;

return sum;

}

double ave(int  num[])
{

 double ave;


        ave  = sum(num)/1000.00;


     cout<<"Ave is :"<<ave<<endl;



}

void COUNT(int num[])
{
   int frequencies[50]= {0} ;
   int max= 0;

   for(int i = 1; i <= 1000; i++)
      {
       for(int j = 1 ; j <=50 ; j++)
            {
              if (num[i]== j)
              {
                frequencies[j] = frequencies[j]+1;
              }
                if ( frequencies[j] >= max )
                {
                 max = j;
                }


            }
       }


   cout<<max<<"apeared"<<frequencies[max-1]<<endl;
}





int main()
{
    int num[1000] = {0} ;
    //srand((unsigned)time(0));
    for(int i=1; i<=1000; i++)
    {
      num[i] = rand()%50+ 1;
      cout<<i<<"="<<num[i]<<endl;
    }

   sum(num);
   max(num);
   min(num);
   ave(num);
   COUNT(num);

    return 0;
}



ok so this waht new code looks like could you check and tell me if evrything is coorect
does it work?
im not sure bcoz it does return 50 as most common number all the time is thre any way if could pls chek if it works for me
Topic archived. No new replies allowed.