Write a program that takes a maximum and minimum temperature of the city from Monday to Sunday, and three functions that calculate on average temperature, maximum temperature and minimum temperature of that week.
This week:
minimum temperature is: 9.0
maximum temperature is: 24.5
average temperature is: 16.21
So I have to ask the user for 14 numbers, and then create a function that does minimum, maximum and average temperature. The thing is, we haven't been taught arrays so I was wondering if I need to declare 14 integers and then divide them by 14 to get the average, without using a function I got this:
So I'm wondering how I could put this into a function considering that I have only done functions with 2 integers, and to get the the minimum and maximum temperature I would have to do a BubbleSort right?
You say you haven't been taught arrays, but do you know how to use them and are you allowed to use them? That would make this problem 10x easier to code as you could pass the whole array as a function parameter.
You'll need to learn about array and loop :) to get a min and max value you can use sequencial search, or sort the array of temp and get array at 0 as min and the last as max
I don't know how to use array 100% yet, I know just a little bit however I don't use it that often and I have never used an array as a function parameter and yes I guess we are allowed to use them but I would have to research and find out because I have no idea hehe.
In that task I did a sort using loops and array, but that would be only to sort numbers I don't know how I would go for minimum, maximum and average numbers from that, and that was using function overloaded so I'm completely lost :(.
The reason we ask is because your assignment says to create three functions. Without using an array, that means each function would require you use 14 parameters as opposed to requiring no more than two (an array and its size). I'd suggest researching if you're up for it. :P
Ok this is being very hard and I have already spent 6 hours in front of this computer trying to find a solution.
I DON'T know how to declare the function for average, mine is:
1 2 3 4 5 6 7 8 9 10 11 12 13
int getAverage (int tempNumbers[], int size)
{
int i, sum = 0;
int average;
for (i = 0; i < size; i++)
sum +- tempNumbers[i];
average = int(sum) / size;
return average;
}
But then I don't know what to put in my void main()...
Your function looks fine. It might be because you're not inputting numbers correctly. Your main function has you inputting values into the indexes 0 through 14, excluding 3 (line 14). So you're not putting them in sequence and you're going beyond the bounds of the array.
Also, your syntax is wrong from main. You're defining it as void main() when it should be int main (void)
int getAverage (int tempNumbers[], int size)
{
int i, sum = 0;
int average;
for (i = 0; i < size; i++)
sum += tempNumbers[i];
average = int(sum) / size;
return average;
}
Warning 1 warning C4244: '+=' : conversion from 'double' to 'int', possible loss of data e:\c++\assigment2-right\assigment2-right\functions.cpp 51 1 Assigment2-Right
What @LendraDwi said. Also, since you're using doubles, it's important to preserve your data.
tempNumbers, theAverage in main and sum, average in the fuction should all be of type double. Without that, you'd lose significant precision. The average of temperatures of 2.5 and 3.5 would result in 2 the way your program is currently written instead of 3 as it should be.