| | |
Passing an Array to Function Problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 30
Reputation:
Solved Threads: 0
Hey everyone. I've been working on a homework assignment to simulate a weather station responsible for recording hourly temps and reporting an average temp. I'm supposed to be passing an array to different functions to first get the temps and input them into the array. Then, compute the average temp from the array. Finally, display the array in a table format. I've got the code all typed up, but I keep getting an error that says... "function does not take 0 arguments" for each of my call to functions in the main..
So, here's the code I wrote that's not playing nicely. Also, attached the files for viewing too.
Header File
CPP File
So, here's the code I wrote that's not playing nicely. Also, attached the files for viewing too.
Header File
C++ Syntax (Toggle Plain Text)
#ifndef Lab4_h #define Lab4_h void GetTemperatures (int Temperatures[], int NumTemperatures); double ComputeAverageTemp (int Temperatures[], int NumTemperatures); void DisplayTemperatures (int Temperatures[], int NumTemperatures, double AverageTemp); #endif
CPP File
C++ Syntax (Toggle Plain Text)
#include <iostream> using std::cin; using std::cout; using std::endl; #include "Lab4.h" int main () { const int NumTemperatures = 24; int HourlyTemperatures[NumTemperatures] = {0}; GetTemperatures (); ComputeAverageTemp (); DisplayTemperatures (); return 0; } void GetTemperatures (int Temperatures[], int NumTemperatures) { int Count = 0; int CurrentTemp = 0; while (Count < 24) { cout << "Input hourly temp: "; cin >> CurrentTemp; Temperatures[++Count] = CurrentTemp; } } double ComputeAverageTemp (int Temperatures[], int NumTemperatures) { double AverageTemp = 0.0; int Count = 0; int Sum = 0; for (Count = 0; Count < 24; Count++) { Sum = Sum + Temperatures[Count]; } AverageTemp = Sum / NumTemperatures; return AverageTemp; } void DisplayTemperatures (int Temperatures[], int NumTemperatures, double AverageTemp) { int Count = 0; cout << "*******************************\n"; cout << "Hour\t\tTemperature\n"; while (Count < 24) { cout << "0" << ++Count << ":00\t\t" << Temperatures[Count] << endl; } cout << "High Temp: \t\t\n"; cout << "Low Temp: \t\t\n"; cout << "Average Temp: \t\t" << AverageTemp << endl; cout << "*******************************\n"; }
The problem is exactly what it is saying.
For example, you have a procedure prototyped as:
...which clearly states that it requires two arguments.
but in main() you don't specify any arguments:
You must specify the arguments:
I think the problem is that you are confusing multiple variables with the same name as a single variable. For example:
In this example, there are two variables named 'answer'. The first is a global variable and it has the value 42. The second is a temporary variable local to print(). In other words, it exists inside the function but nowhere else. Since it has the same name as the global variable, the local variable takes precedence (meaning you cannot use the global variable inside the function).
Hope this helps.
PS. You could use the global if you give its full name:
For example, you have a procedure prototyped as:
void GetTemperatures (int Temperatures[], int NumTemperatures);...which clearly states that it requires two arguments.
but in main() you don't specify any arguments:
GetTemperatures ();You must specify the arguments:
GetTemperatures( HourlyTemperaturs, NumTemperatures );I think the problem is that you are confusing multiple variables with the same name as a single variable. For example:
C++ Syntax (Toggle Plain Text)
int answer = 42; void print( int answer ) { cout << "The answer is " << answer << endl; }
Hope this helps.
PS. You could use the global if you give its full name:
cout << ::answer << endl; Last edited by Duoas; May 14th, 2008 at 6:07 pm.
•
•
Join Date: Feb 2008
Posts: 30
Reputation:
Solved Threads: 0
That makes perfect sense now, Duoas. That fixed my 0 argument problem, but now I'm having a problem when I Start Without Debugging.. I get the following error:
Also, I can't seem to figure out why the average will display some funky number.. Attaching the files again so don't spam with code. If want the code to read lemme know and I'll post my current code. Thanks again for any help.
•
•
•
•
Debug Error!
Program: ...
Module ... ...\Labs\Lab 4\Lab4\debug\Lab4.exe
File:
Run-Time Check Failure #2 - Stack around the variable 'HourlyTemperatures' was corrupted.
(Press Retry to debug the application)
Abort Retry Ignore
Last edited by Metalsiege; May 15th, 2008 at 5:46 pm.
•
•
Join Date: Feb 2008
Posts: 30
Reputation:
Solved Threads: 0
•
•
•
•
You exceed the array bounds by using the pre-increment operator in
Temperatures[++Count] = CurrentTemp;
change it to
Temperatures[Count ++] = CurrentTemp;
Ahh.. yep.. guess I need to pay more attention when using pre-increment.. Thanks. Any ideas on the averaging function?
C++ Syntax (Toggle Plain Text)
cout << "Average Temp: \t\t" << AverageTemp << endl;
Is there some way that I could call the function there and just pass the value of AverageTemp?
I thought this would work but gives me a funky number still.. even if I just put all 1's which should give me 1 as the average..
C++ Syntax (Toggle Plain Text)
cout << "Average Temp: \t\t" << ComputeAverageTemp << endl;
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
•
•
•
•
Ahh.. yep.. guess I need to pay more attention when using pre-increment.. Thanks. Any ideas on the averaging function?
C++ Syntax (Toggle Plain Text)
cout << "Average Temp: \t\t" << AverageTemp << endl;
Is there some way that I could call the function there and just pass the value of AverageTemp?
I thought this would work but gives me a funky number still.. even if I just put all 1's which should give me 1 as the average..
C++ Syntax (Toggle Plain Text)
cout << "Average Temp: \t\t" << ComputeAverageTemp << endl;
Not sure where this line is above, but it is not a function call. If ComputeAverageTemp is the function, you'll need some parentheses and you'll need to pass the parameters. Your code is short enough that I think posting your revised program would be easier than the attachments. I didn't know where the line above was in the program. If you post the whole revised program here rather than by attachment, you can refer to the line number of the function call and we'll get a better idea of what's changed.
•
•
Join Date: Feb 2008
Posts: 30
Reputation:
Solved Threads: 0
Sorry lemme post the code again. :-) Anyway.. talking about line 87. Now I'm trying to call the function ComputeAverageTemp, but would I have to include all the parameters in () when calling even though I only want AverageTemp?
Header file
CPP File
Header file
C++ Syntax (Toggle Plain Text)
#ifndef Lab4_h #define Lab4_h void GetTemperatures (int Temperatures[], int NumTemperatures); double ComputeAverageTemp (int Temperatures[], int NumTemperatures); void DisplayTemperatures (int Temperatures[], int NumTemperatures, double AverageTemp); #endif
CPP File
C++ Syntax (Toggle Plain Text)
#include <iostream> using std::cin; using std::cout; using std::endl; #include "Lab4.h" int main () { const int NumTemperatures = 24; int HourlyTemperatures[NumTemperatures] = {0}; double AverageTemp = 0.0; cout << "*******************************\n"; cout << "* Welcome to CTU Weather *\n"; cout << "* ---------------------- *\n"; cout << "* *\n"; cout << "* Your source for up-to-date *\n"; cout << "* weather information! *\n"; cout << "*******************************\n\n\n"; GetTemperatures (HourlyTemperatures, NumTemperatures); ComputeAverageTemp (HourlyTemperatures, NumTemperatures); DisplayTemperatures (HourlyTemperatures, NumTemperatures, AverageTemp); return 0; } void GetTemperatures (int Temperatures[], int NumTemperatures) { int Count = 0; int CurrentTemp = 0; int TimerCount = 0; while (Count < 24) { if (CurrentTemp > -49 && CurrentTemp < 131) { cout << "Temperature at " << TimerCount++ << ":00? "; cin >> CurrentTemp; Temperatures[Count++] = CurrentTemp; } else { cout << "Incorrect temperature input...\n"; cout << "Please input between -50 and 130 degrees...\n"; cout << "Re-input temperature: "; cin >> CurrentTemp; } } } double ComputeAverageTemp (int Temperatures[], int NumTemperatures) { double AverageTemp = 0.0; int Count = 0; int Sum = 0; for (Count = 0; Count < 24; Count++) { Sum = Sum + Temperatures[Count]; } AverageTemp = Sum / NumTemperatures; return AverageTemp; } void DisplayTemperatures (int Temperatures[], int NumTemperatures, double AverageTemp) { int Count = 0; cout << "\n*******************************\n"; cout << "Hour\t\t Temperature\n"; while (Count < 24) { cout << Count++ << ":00\t\t\t" << Temperatures[Count] << endl; } cout << "High Temp: \t\t\n"; cout << "Low Temp: \t\t\n"; cout << "Average Temp: \t\t" << AverageTemp << endl; cout << "*******************************\n"; }
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
•
•
•
•
Sorry lemme post the code again. :-) Anyway.. talking about line 87. Now I'm trying to call the function ComputeAverageTemp, but would I have to include all the parameters in () when calling even though I only want AverageTemp?
Header file
C++ Syntax (Toggle Plain Text)
#ifndef Lab4_h #define Lab4_h void GetTemperatures (int Temperatures[], int NumTemperatures); double ComputeAverageTemp (int Temperatures[], int NumTemperatures); void DisplayTemperatures (int Temperatures[], int NumTemperatures, double AverageTemp); #endif
CPP File
C++ Syntax (Toggle Plain Text)
#include <iostream> using std::cin; using std::cout; using std::endl; #include "Lab4.h" int main () { const int NumTemperatures = 24; int HourlyTemperatures[NumTemperatures] = {0}; double AverageTemp = 0.0; cout << "*******************************\n"; cout << "* Welcome to CTU Weather *\n"; cout << "* ---------------------- *\n"; cout << "* *\n"; cout << "* Your source for up-to-date *\n"; cout << "* weather information! *\n"; cout << "*******************************\n\n\n"; GetTemperatures (HourlyTemperatures, NumTemperatures); ComputeAverageTemp (HourlyTemperatures, NumTemperatures); DisplayTemperatures (HourlyTemperatures, NumTemperatures, AverageTemp); return 0; } void GetTemperatures (int Temperatures[], int NumTemperatures) { int Count = 0; int CurrentTemp = 0; int TimerCount = 0; while (Count < 24) { if (CurrentTemp > -49 && CurrentTemp < 131) { cout << "Temperature at " << TimerCount++ << ":00? "; cin >> CurrentTemp; Temperatures[Count++] = CurrentTemp; } else { cout << "Incorrect temperature input...\n"; cout << "Please input between -50 and 130 degrees...\n"; cout << "Re-input temperature: "; cin >> CurrentTemp; } } } double ComputeAverageTemp (int Temperatures[], int NumTemperatures) { double AverageTemp = 0.0; int Count = 0; int Sum = 0; for (Count = 0; Count < 24; Count++) { Sum = Sum + Temperatures[Count]; } AverageTemp = Sum / NumTemperatures; return AverageTemp; } void DisplayTemperatures (int Temperatures[], int NumTemperatures, double AverageTemp) { int Count = 0; cout << "\n*******************************\n"; cout << "Hour\t\t Temperature\n"; while (Count < 24) { cout << Count++ << ":00\t\t\t" << Temperatures[Count] << endl; } cout << "High Temp: \t\t\n"; cout << "Low Temp: \t\t\n"; cout << "Average Temp: \t\t" << AverageTemp << endl; cout << "*******************************\n"; }
C++ Syntax (Toggle Plain Text)
ComputeAverageTemp (HourlyTemperatures, NumTemperatures);
C++ Syntax (Toggle Plain Text)
AverageTemp = ComputeAverageTemp (HourlyTemperatures, NumTemperatures);
There is no need to call ComputeAverageTemp at all in your DisplayTemperatures function as far as I can see. You've already called it once from main. You just didn't store the results of that function. Store it in AverageTemp, then pass AverageTemp to the Display function. No need to call ComputeAverageTemp twice.
Last edited by VernonDozier; May 15th, 2008 at 7:57 pm.
•
•
Join Date: Feb 2008
Posts: 30
Reputation:
Solved Threads: 0
Make it seem so easy, VernonDozier.
Sorry to kinda put all the program problems in one thread, but I was reading more of my assignment description and my instructor wants me to use a pause function to "pause" the output. He gives the following code:
How can that be used in a while loop that displays the data in the array? I read other threads that used both ways, but they had inputs and no output like me.. There is nothing being input so that function just goes into a never-ending loop. I've thought about using an if statement to use the pause, but don't know how to implement it in the while statement. If I use the following it just pauses after each output, which I don't want.. I want to pause after.. say 12 lines..
Sorry to kinda put all the program problems in one thread, but I was reading more of my assignment description and my instructor wants me to use a pause function to "pause" the output. He gives the following code:
C++ Syntax (Toggle Plain Text)
void Pause() { cout << "Hit any key to continue..."; cin.ignore(1); }
How can that be used in a while loop that displays the data in the array? I read other threads that used both ways, but they had inputs and no output like me.. There is nothing being input so that function just goes into a never-ending loop. I've thought about using an if statement to use the pause, but don't know how to implement it in the while statement. If I use the following it just pauses after each output, which I don't want.. I want to pause after.. say 12 lines..
C++ Syntax (Toggle Plain Text)
system ("PAUSE");
Last edited by Metalsiege; May 16th, 2008 at 3:11 pm.
just add
into the code where ever you want to stop .
And copy and paste the function somewhere outside main();
And it will work fine.
C++ Syntax (Toggle Plain Text)
Pause();
And copy and paste the function somewhere outside main();
And it will work fine.
![]() |
Similar Threads
- Passing 2D Array of Pointers into a function (C++)
- How to send a text array to a function? (C++)
- Programing functions and having a problem with one of the functions. (C++)
- bubble sort function w/ array (C++)
- Passing array of structures into a function (C++)
- Problem passing structure with array into function (C)
- Passing Arrays to function in Visual C++ (C++)
Other Threads in the C++ Forum
- Previous Thread: structures and pointers
- Next Thread: high number of objects for a data structure
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






