Hey can someone please help me with my project? I attached the instructions + .cpp file , I can't seem to get it to work correctly.
Thank you!!
Printable View
Hey can someone please help me with my project? I attached the instructions + .cpp file , I can't seem to get it to work correctly.
Thank you!!
Indeed, your function calls are wrong - read up on them. You use () when calling functions and you must also pass an argument for your functions.
Since this is C++, I'd rather you try to use vector instead of built-in arrays too.
Oh and array is a keyword. Best not use that.
array is not a keyword, but it is not a particularly descriptive name since it says the obvious.Quote:
Oh and array is a keyword.
array is a keyword in C++ CLI, Microsoft's own C++ crap, so it's a keyword in Visual Studio. It's not in C/C++, though, but I'd still avoid it.
Maybe that clears up some confusion.
What function calls? and I used arrays cause it is the name of the project >_<
Code:
#include <iostream>
#include <conio.h>
using namespace std;
//the function prototypes
void getData(int array[][2]);
double averageHigh(int array[][2]);
double averageLow(int array[][2]);
int indexHighTemp(int array[][2]);
int indexLowTemp(int array[][2]);
int main()
{
int months[12][2];
//telling the user the average high temperature
cout << "Average high temperature:"<<averageHigh<< endl;
//telling the user the average low temperature
cout << "Average low temperature:"<<averageLow<< endl;
//telling the user the highest temperature
cout << "Highest temperature:"<<indexHighTemp<< endl;
//telling the user the lowest temperature
cout <<"Lowest temperature:"<<indexLowTemp<< endl;
cout<<"Name"<< endl;
getch ();
return 0;
}
void getData(int array[][2])
{
/* asks the user to input the highest and lowest temperatures
and contains the for loop to store it and a counter.*/
cout << "Please enter highest temperatures for each month." << endl;
for(int i=0; i<12; i++)
cin >> array[i][0];
cout << "Please enter lowest temperatures for each month." << endl;
for(int i=0; i<12; i++)
cin >> array[i][1];
}
//does the math for the average high temperatures
double averageHigh(int array[][2])
{
int sumh = 0;
for(int i=0; i<12; i++)
sumh += array[i][0];
return sumh/12.0;
}
//does the math for the average low temperatures, also contains a for looop
double averageLow(int array[][2])
{
int suml = 0;
for(int i=0; i<12; i++)
suml += array[i][1];
return sum1/12.0;
}
//a nested for loop to find the highest and lowest temperature
int indexHighTemp(int array[][2])
{
int highTemp = 0;
for(int i=0; i<12; i++)
{
if(array[i][0] > highTemp)
{
highTemp = array[i][0];
}
}
return highTemp;
}
int indexLowTemp(int array[][2])
{
int index = 0;
int lowTemp = array[0][1];
for(int i=1; i<12; i++)
{
if(array[i][1] < lowTemp)
{
lowTemp = array[i][1];
index = i;
}
}
return lowTemp;
}
In this example it's fine, but consider std::vector otherwise.
What function calls????? What are these if not function calls?
You're doing it wrong - you're printing the address of the function (or if you'd used Visual Studio 2005 or another compiler, you'd get a compile error because that isn't standard!)!
std::vector is part of the STL. Very, very useful. I suggest you learn it when you have time.
A function call is when you call a function - simple enough
Code:int foo()
{
return 6;
}
int main()
{
int myint = foo(); // Calls function - aka function call.
cout << myint; // Prints 6 to screen.
cout << foo(); // Calls function - aka function call. Prints 6 to screen.
cout << foo; // Prints the function ADDRESS. Where the function is located in the memory (if it compiles at all)!
}
You really don't understand functions at all. Have you read tutorials/books on them?
No, I'm not majoring in Programing I just had to take the class..
http://www.cprogramming.com/tutorial/lesson4.html
You can't use a tool if you don't know how to use it. Read up, friend. Functions are your friends.
I did a function program before but not with arrays
Is it something like this?
Code:int main()
{
int months[12][2];
getData(array[i][0],array[i][1]);
averageHigh(sumh);
averageLow(suml);
indexHighTemp(highTemp);
indexLowTemp(lowTemp);
//telling the user the average high temperature
cout << "Average high temperature:"<<averageHigh[sumh]<< endl;
//telling the user the average low temperature
cout << "Average low temperature:"<<averageLow[suml]<< endl;
//telling the user the highest temperature
cout << "Highest temperature:"<<indexHighTemp[highTemp]<< endl;
//telling the user the lowest temperature
cout <<"Lowest temperature:"<<indexLowTemp[lowTemp]<< endl;
cout<<"Name"<< endl;
getch ();
return 0;
}
No, the functions take only ONE argument. Did you read the tutorial properly?
And when passing arguments, you type the NAME only. You are passing the entire array, not some subset or index. Re-read your function argument list.
I don't understand the tutorial...
It would also help to get a grasp of the language before you use it. You don't drive a car before you know how to operate it!
is this correct?
Code:#include <iostream>
#include <conio.h>
using namespace std;
//the function prototypes
void getData(int array[][2]);
double averageHigh(int array[][2]);
double averageLow(int array[][2]);
int indexHighTemp(int array[][2]);
int indexLowTemp(int array[][2]);
int main()
{
int months[12][2];
getData(months);
//telling the user the average high temperature
cout << "Average high temperature:"
<< averageHigh(months) << endl;
//telling the user the average low temperature
cout << "Average low temperature:"
<< averageLow(months) << endl;
//telling the user the highest temperature
cout << "Highest temperature:"
<< indexHighTemp(months) << endl;
//telling the user the lowest temperature
cout << "Lowest temperature:"
<< indexLowTemp(months) << endl;
getch ();
return 0;
}
void getData(int array[][2])
{
/* asks the user to input the highest and lowest temperatures
and contains the for loop to store it and a counter.*/
cout << "Please enter highest temperatures for each month." << endl;
for(int i=0; i<12; i++)
cin >> array[i][0];
cout << "Please enter lowest temperatures for each month." << endl;
for(int i=0; i<12; i++)
cin >> array[i][1];
}
//does the math for the average high temperatures
double averageHigh(int array[][2])
{
int sumHigh = 0;
for(int i=0; i<12; i++)
sumHigh += array[i][0];
return sumHigh/12.0;
}
//does the math for the average low temperatures, also contains a for looop
double averageLow(int array[][2])
{
int sumLow = 0;
for(int i=0; i<12; i++)
sumLow += array[i][1];
return sumLow/12.0;
}
//a nested for loop to find the highest and lowest temperature
int indexHighTemp(int array[][2])
{
int highTemp = 0;
for(int i=0; i<12; i++)
{
if(array[i][0] > highTemp)
{
highTemp = array[i][0];
}
}
getch();
return highTemp;
}
int indexLowTemp(int array[][2])
{
int lowTemp = array[0][1];
for(int i=1; i<12; i++)
{
if(array[i][1] < lowTemp)
{
lowTemp = array[i][1];
}
}
getch();
return lowTemp;
}
Yes, this looks correct. Does it work?
Yes it does =] lol the thing that was mostly confusing me were the arrays -_-