Output is always zero
The output for my "biggest" (Line 45) and "median"(Line 78) functions always returns zero. Could someone point me in the right direction?
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include <iostream>
using namespace std;
int i = 0;
int testArray[100] = {0};
bool loopFlag = true;
void numbers(int testArray[], int& i)
{
cin >> testArray[i];
i++;
cout << endl;
cout << "OK" << endl;
return ;
}
void calcSum(int testArray[])
{
int sum = 0;
for(int j = 0; j < 100; j++)
{
sum += testArray[j];
}
cout << sum;
return;
}
void findAverage (int testArray[])
{
int averageCount = 0;
int averageSum = 0;
for(int k = 0; k < 100; k++)
{
if(testArray[k] > 0)
{
averageCount++;
averageSum += testArray[k];
}
}
cout << static_cast<double>(averageSum) / averageCount;
return;
}
void biggest(int testArray[])
{
int largest = 0;
for(int i; i < 100; i++)
{
if(testArray[i] > largest)
{
largest = testArray[i];
}
}
cout << endl;
cout << largest;
return;
}
void mode(int testArray[])
{
}
void numCount(int testArray[])
{
int howMany = 0;
for(int j = 0; j < 100; j++)
{
if(testArray[j] > 0)
{
howMany++;
}
}
cout << howMany;
}
void median(int testArray[])
{
int tmp;
for (int i = 0; i < 5 -1; i++)
{
for (int j = i+1; j < 5; j++)
{
if (testArray[i] > testArray[j])
{
tmp = testArray[i];
testArray[i] = testArray[j];
testArray[j] = tmp;
}
}
}
int howMany = 0;
for(int j = 0; j < 100; j++)
{
if(testArray[j] > 0)
{
howMany++;
}
}
int medianNum = (howMany / 2) + 1;
int median = testArray[medianNum];
cout << median;
}
int main()
{
char command;
cout << "Welcome to Stats Array!" << endl;
cout << "These are your choices:" << endl;
cout << "N - Numbers" << endl;
cout << "S - Sum of all" << endl;
cout << "A - Average of all" << endl;
cout << "B - Biggest of all" << endl;
cout << "F - Most Frequent of all" << endl;
cout << "H - How many numbers" << endl;
cout << "M - Median of all" << endl;
cout << "Q - Quit" << endl;
do
{
cin >> command;
cout << endl;
switch(command)
{
case 'N':
numbers(testArray, i);
break;
case 'S':
calcSum(testArray);
break;
case 'A':
findAverage(testArray);
break;
case 'B':
biggest(testArray);
break;
case 'F':
mode(testArray);
break;
case 'H':
numCount(testArray);
break;
case'M':
median(testArray);
break;
default:
cout << "END";
}
}while(loopFlag);
return 0;
}
| |
Firstly, do you understand you have to input 100 numbers? Have you considered a smaller number, like 10 for example?
You don't appear to input 100 numbers, but you still attempt to find the largest value in the 100 values.
Topic archived. No new replies allowed.