Mam problem z tym kodem, kompiluje się, wydaje się, że działa dobrze, ale średnia nie wychodzi poprawnie. Uważam, że ma to coś wspólnego z typedef lub tablicą. Po prostu uczymy się tablic w mojej klasie i nie czuję się zbyt dobrze z typedef
#include <iostream>
using namespace std;
const int numberT = 50;
typedef float temps[numberT];
float averageTemp(temps, int);
int main(){
char answer;
float average;
int number;
temps temperature;
cout << "This program will take the temperatures "
<< "of consecutive days and average them " << endl;
cout << "Would you like to start? (Press y)" << endl;
cin >> answer;
if(answer == 'y' || answer == 'Y'){
cout << "How many temperatures do you want to input?" << endl;
cin >> number;
for(int x=0;x<number;x++){
if(not(answer== 'n' || answer == 'N')){
cout << "Please input temperature for day "
<< x + 1 << endl;
cin >> temperature[x];
}
else{
cout << "You have stopped inputting temperatures";
break;
}
}
averageTemp(temperature, number);
cout << endl << endl << "The average is " << average;
}else
cout << "No temperatures to average";
return 0;
}
float averageTemp(temps array, int number){
float sum=0;
for(int x=0; x<number; x++){
array[x];
sum+=array[x];
}
float average = sum / number;
return average;
}
1 odpowiedź
Twoja funkcja:
float averageTemp(temps, int);
Zwraca średnią, ale ta linia:
averageTemp(temperature, number);
Nie używa wartości zwracanej do niczego. Musisz to przypisać:
average = averageTemp(temperature, number);
PS: Włącz ostrzeżenia w swoich kompilatorach, jest linia, która nie robi nic użytecznego.
Podobne pytania
Nowe pytania
c++
C ++ to język programowania ogólnego przeznaczenia. Pierwotnie został zaprojektowany jako rozszerzenie C i ma podobną składnię, ale teraz jest to zupełnie inny język. Użyj tego tagu w przypadku pytań dotyczących kodu (który ma zostać) skompilowany za pomocą kompilatora C ++. Użyj znacznika specyficznego dla wersji w przypadku pytań związanych z określoną wersją standardu [C ++ 11], [C ++ 14], [C ++ 17], [C ++ 20] lub [C ++ 23] itp. .