Reputation: 47
I am currently working on some code for my C++ class, and I cant find what I'm doing wrong. My code will not output the correct number value. My code is supposed to be able to Finding the Maximum, Minimum, Sum and Average of any set number array.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
const int ARRAY_SIZE = 12; // number of elements
int userVals[ARRAY_SIZE];// Array of input numbers
int numArray[] ={24, -5, 72, 104, 0, 5, 89, -225, 19, 17, 16,
81};//Elements
int i; // loop index
int maxVal; // Max Value
int minVal; // Min Value
int sumVal; // Sum Value
int aveVal; // Average Value
sumVal = 0; //* Sum Value
for (i = 0; i < ARRAY_SIZE; ++i) {
sumVal = sumVal + userVals[i];
}
cout << " Sum Value: " << sumVal << endl;
maxVal = userVals[0];//* Max Value
for (i = 0; i < ARRAY_SIZE; ++i) {
if (userVals[i] > maxVal) {
maxVal = userVals[i];
}
}
cout << " Max Value: " << maxVal << endl;
minVal = userVals[0];//* Min Value
for (i = 0; i < ARRAY_SIZE; ++i) {
if (userVals[i] > minVal) {
minVal = userVals[i];
}
}
cout << " Min Value: " << minVal << endl;
aveVal = userVals[0];//* Average Value
for (i = 0; i < ARRAY_SIZE; ++i) {
aveVal = aveVal + userVals[i];
}
cout << " Average Value: " << aveVal << endl;
return 0;
}
Like I stated above, my code is supposed to be able to be able to Finding the Maximum, Minimum, Sum and Average of any set number array. Currently, when I run my code it outputs these results:
Sum Value: 447815718
Max Value: 914973176
Min Value: 914973176
Average Value: 1362788894
Edits 7/13/19:
These are the changes I made to the code to make it work.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
const int ARRAY_SIZE = 12; // number of elements
int userVals[ARRAY_SIZE];// Array of input numbers
int numArray[] ={24, -5, 72, 104, 0, 5, 89, -225, 19, 17, 16, 81}; //
Elements
int i; // loop index
int maxVal; // Max Value
int minVal; // Min Value
int sumVal; // Sum Value
int aveVal; // Average Value
sumVal = 0; //* Sum Value
for (i = 0; i < ARRAY_SIZE; ++i) {
sumVal = sumVal + numArray[i];
}
cout << " Sum Value: " << sumVal << endl;
maxVal = numArray[0];//* Max Value
for (i = 0; i < ARRAY_SIZE; ++i) {
if (numArray[i] > maxVal) {
maxVal = numArray[i];
}
}
cout << " Max Value: " << maxVal << endl;
minVal = numArray[0];//* Min Value
for (i = 0; i < ARRAY_SIZE; ++i) {
if (numArray[i] < minVal) {
minVal = numArray[i];
}
}
cout << " Min Value: " << minVal << endl;
aveVal = numArray[0];//* Average Value
for (i = 0; i < ARRAY_SIZE; ++i) {
if (aveVal = numArray[0] + numArray[1] + numArray[2] + numArray[3] +
numArray[4] + numArray[5] + numArray[6] + numArray[7] +
numArray[8] + numArray[9] + numArray[10] + numArray[11])
aveVal = aveVal / 12;
}
cout << " Average Value: " << aveVal << endl;
return 0;
}
Upvotes: 0
Views: 1325
Reputation: 31465
The standard library provides everything you need in the algorithm header.
To find the minimum and maximum, use std::minmax_element.
To find the sum, use std::accumulate.
To find the average, use std::accumulate and divide by the number of elements.
Upvotes: 0
Reputation: 371
The maximum value and the sum should be OK.
You are finding the maximum instead of the minimum because for a value to be the new minimum value it should be less than the current minimum so it should be:
...
if (userVals[i] < minVal) {
...
instead of:
...
if(userVals[i] > minVal){
...
And for the average you should divide the sum by the ARRAY_SIZE.
Also be careful because you are not initializing userVals.
Upvotes: 1
Reputation: 35154
In your code, you are referring to userVals
, which never gets initialized. Use numArray
instead.
BTW: You should probably rethink the way you calculate the average...
Upvotes: 3