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;
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]
}
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
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.