dimanche 11 janvier 2015

Need help calculating AVG of array values (minus the lowest)


Vote count:

0




So I have succeeded in confusing the hell out of myself in doing this. I am trying to get it to calculate the average of the weights entered into the array minus the lowest weight in the array. I'm using functions and somewhere along the line I confused myself with passing variables. It would be much appreciated if someone could give me a pointer and tell me if I'm way off base or not. Also how would I compare the values entered to a validation code? I have a line commented out that I was fiddling with, but never got working.



#include <iostream>

using namespace std;

int getWeight();
int findLowest(int arrayWeight);
double calcAverage(int weight);
bool askToContinue();

int main(){

do{
int weights = getWeight();
double lowest = findLowest(weights);
double average = calcAverage(weights);
}
while(askToContinue());
}

int getWeight() {

//Variables
int weights[5]; //array
double enterWeight = 0;
bool validAmount = false;


//For loop to gather info and place amounts in an array
cout << "Please enter the weights of the Tulbuks: " << endl;
cout << endl;
for (int counter = 0; counter < 5; counter++)
{
cin >> weights[counter];
//validAmount = (weights[index] > 5) && (weights[index] <= 500);
}

//Test to redisplay the entered information
cout << "Entered information: " << endl;
for(int index = 0; index < 5; index++)
{
cout << "\nThe entered information for Tulbuk #" << (index+1) << " is: " << weights[index];
cout << endl;
}


return -1;


/*

do
{

//Gather user input of amount of discs
cout << "How many discs do you wish to purchase?" << endl;
cout << "Please enter a number between 1 and 1,000,000" << endl;
cin >> weights;
cout << endl;
validAmount = (weights > 5) && (weights <= 500); // Tests if the amount entered is valid
if (!validAmount) // Prompts user amount entered was invalid
{
cout << "Invalid Amount. Please try again!" << endl;
}
}
while(!validAmount); // Runs loop again if the amount entered was not valid



return discs;

*/

}

int findLowest(int arrayWeight){

int lowWeight = 999999;

if(lowWeight > arrayWeight)
{
lowWeight = arrayWeight;
}

cout << arrayWeight;

system("PAUSE");

return arrayWeight;
}

double calcAverage(int weight){

//Variables
float avgWeight = 0;
int sumWeight = 0;

//Calls findLowest function to find lowest value
int lowestWeight = findLowest(weight);

//Calculates the average score


return weight;
}

bool askToContinue() // Asks the user if they want to continue. If yes, the loop restarts. If no, the program exits.
{

char userResponse = ' ';
bool validInput = false;

do
{
cout << endl;
cout << "Do you wish to continue?" << endl;
cout << "Enter y for 'yes' or n for 'no'" << endl;
cin >> userResponse;
validInput = (userResponse == 'y') || (userResponse == 'n');
if (!validInput)
{
cout << "Invalid response. Please try again!" << endl;
}
} while (!validInput);
return(userResponse == 'y');
}


asked 1 min ago







Need help calculating AVG of array values (minus the lowest)

Aucun commentaire:

Enregistrer un commentaire