Can anyone tell me why this function is not working? It passes in an array, and the total number of values in the array (j), and the average of the array. The function is supposed to print out how many numbers are higher and lower than the average. Currently, it seems to do nothing. (I didn't post the next part of the function that finds the smaller numbers because it is identical to this loop.) I understand that means that my loop is faulty, but I can't understand why. Any insight is appreciated.
//Function definition 6
void printHigherLower(int ary[], int j, int avg)
{
int k = 0; //Declare counter for array position
int cnt = 0; //Declare counter to find how many are larger than average
int cnt2 = 0;//Declare counter to find how many are smaller than average
int largest = ary[0]; //Declare variable for largest and initialize to first value in array
int smallest = ary[0];//Declare variable for smallest and initialize to first value in array
//To find which and how many values in the array are greater than its average
while (k < j) //While k is less than j (100)
{
if (largest > avg) //If largest is greater than the average (34)
{
largest = ary[k]; //Set k to largest
cnt = cnt + 1; //Increment larger counter
k++; //Increment k
}
}
//Display message
cout << cnt << " in the array are LARGER than its average of " << avg << endl;
//Echo
outfile << cnt << " in the array are LARGER than its average of " << avg << endl;
Okay, so what about doing it this way? Am I getting closer? (It still doesn't work...) but this at least prints out the display messages, but it's only giving me 2 for smaller than, and still 0 for greater than...
//Function definition 6
void printHigherLower(int ary[], int j, int avg)
{
int k = 0; //Declare counter for array position
int cnt = 0; //Declare counter to find how many are larger than average
int cnt2 = 0;//Declare counter to find how many are smaller than average
int largest = ary[0]; //Declare variable for largest and initialize to first value in array
int smallest = ary[0];//Declare variable for smallest and initialize to first value in array
for (k = 0; k <= j; k++) //While k is less than j (100)
{
//To find all values smaller than the avg (34)
if (smallest < avg) //If smallest is less than the average (34)
{
smallest = ary[k]; //Set k to smallest
cnt2 = cnt2 + 1; //Increment smallest counter
}
//Now to find how many values in the array are greater than its average
if (largest > avg) //If largest is greater than the average (34)
{
largest = ary[k]; //Set k to largest
cnt = cnt + 1; //Increment larger counter
}
}
//Display message
cout << cnt << " in the array are LARGER than its average of " << avg << endl;
//Echo
outfile << cnt << " in the array are LARGER than its average of " << avg << endl;
//Display message
cout << cnt2 << " in the array are SMALLER than its average of " << avg << endl;
//Echo
outfile << cnt2 << " in the array are SMALLER than its average of " << avg << endl;
}
Yay! I figured it out! I changed both my if statements from testing smallest and largest to testing ary[k] and it worked. :) Thanks for the hint there, Lynx876.