Passing an Array to Function Problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 30
Reputation: Metalsiege is an unknown quantity at this point 
Solved Threads: 0
Metalsiege Metalsiege is offline Offline
Light Poster

Passing an Array to Function Problem

 
0
  #1
May 14th, 2008
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
  1. #ifndef Lab4_h
  2. #define Lab4_h
  3.  
  4. void GetTemperatures (int Temperatures[], int NumTemperatures);
  5. double ComputeAverageTemp (int Temperatures[], int NumTemperatures);
  6. void DisplayTemperatures (int Temperatures[], int NumTemperatures, double AverageTemp);
  7.  
  8. #endif


CPP File
  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5.  
  6. #include "Lab4.h"
  7.  
  8. int main ()
  9. {
  10. const int NumTemperatures = 24;
  11. int HourlyTemperatures[NumTemperatures] = {0};
  12.  
  13. GetTemperatures ();
  14. ComputeAverageTemp ();
  15. DisplayTemperatures ();
  16.  
  17. return 0;
  18. }
  19.  
  20.  
  21. void GetTemperatures (int Temperatures[], int NumTemperatures)
  22. {
  23. int Count = 0;
  24. int CurrentTemp = 0;
  25.  
  26. while (Count < 24)
  27. {
  28. cout << "Input hourly temp: ";
  29. cin >> CurrentTemp;
  30.  
  31. Temperatures[++Count] = CurrentTemp;
  32. }
  33.  
  34. }
  35.  
  36. double ComputeAverageTemp (int Temperatures[], int NumTemperatures)
  37. {
  38. double AverageTemp = 0.0;
  39. int Count = 0;
  40. int Sum = 0;
  41.  
  42. for (Count = 0; Count < 24; Count++)
  43. {
  44. Sum = Sum + Temperatures[Count];
  45. }
  46.  
  47. AverageTemp = Sum / NumTemperatures;
  48.  
  49.  
  50. return AverageTemp;
  51. }
  52.  
  53. void DisplayTemperatures (int Temperatures[], int NumTemperatures, double AverageTemp)
  54. {
  55. int Count = 0;
  56.  
  57. cout << "*******************************\n";
  58. cout << "Hour\t\tTemperature\n";
  59.  
  60. while (Count < 24)
  61. {
  62. cout << "0" << ++Count << ":00\t\t" << Temperatures[Count] << endl;
  63. }
  64.  
  65. cout << "High Temp: \t\t\n";
  66. cout << "Low Temp: \t\t\n";
  67. cout << "Average Temp: \t\t" << AverageTemp << endl;
  68. cout << "*******************************\n";
  69. }
Attached Files
File Type: cpp Lab4.cpp (3.8 KB, 1 views)
File Type: h Lab4.h (2.4 KB, 0 views)
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Passing an Array to Function Problem

 
1
  #2
May 14th, 2008
The problem is exactly what it is saying.
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:
  1. int answer = 42;
  2.  
  3. void print( int answer )
  4. {
  5. cout << "The answer is " << answer << endl;
  6. }
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:
cout << ::answer << endl;
Last edited by Duoas; May 14th, 2008 at 6:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 30
Reputation: Metalsiege is an unknown quantity at this point 
Solved Threads: 0
Metalsiege Metalsiege is offline Offline
Light Poster

Re: Passing an Array to Function Problem

 
0
  #3
May 15th, 2008
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:

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
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.
Last edited by Metalsiege; May 15th, 2008 at 5:46 pm.
Attached Files
File Type: cpp Lab4.cpp (4.3 KB, 3 views)
File Type: h Lab4.h (2.4 KB, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 979
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 209
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Passing an Array to Function Problem

 
0
  #4
May 15th, 2008
You exceed the array bounds by using the pre-increment operator in

Temperatures[++Count] = CurrentTemp;

change it to

Temperatures[Count ++] = CurrentTemp;
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 30
Reputation: Metalsiege is an unknown quantity at this point 
Solved Threads: 0
Metalsiege Metalsiege is offline Offline
Light Poster

Re: Passing an Array to Function Problem

 
0
  #5
May 15th, 2008
Originally Posted by mitrmkar View Post
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?

  1. 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..
  1. cout << "Average Temp: \t\t" << ComputeAverageTemp << endl;
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Passing an Array to Function Problem

 
0
  #6
May 15th, 2008
Originally Posted by Metalsiege View Post
Ahh.. yep.. guess I need to pay more attention when using pre-increment.. Thanks. Any ideas on the averaging function?

  1. 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..
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 30
Reputation: Metalsiege is an unknown quantity at this point 
Solved Threads: 0
Metalsiege Metalsiege is offline Offline
Light Poster

Re: Passing an Array to Function Problem

 
0
  #7
May 15th, 2008
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
  1. #ifndef Lab4_h
  2. #define Lab4_h
  3.  
  4. void GetTemperatures (int Temperatures[], int NumTemperatures);
  5. double ComputeAverageTemp (int Temperatures[], int NumTemperatures);
  6. void DisplayTemperatures (int Temperatures[], int NumTemperatures, double AverageTemp);
  7.  
  8. #endif

CPP File
  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5.  
  6. #include "Lab4.h"
  7.  
  8. int main ()
  9. {
  10. const int NumTemperatures = 24;
  11. int HourlyTemperatures[NumTemperatures] = {0};
  12. double AverageTemp = 0.0;
  13.  
  14. cout << "*******************************\n";
  15. cout << "* Welcome to CTU Weather *\n";
  16. cout << "* ---------------------- *\n";
  17. cout << "* *\n";
  18. cout << "* Your source for up-to-date *\n";
  19. cout << "* weather information! *\n";
  20. cout << "*******************************\n\n\n";
  21.  
  22.  
  23. GetTemperatures (HourlyTemperatures, NumTemperatures);
  24. ComputeAverageTemp (HourlyTemperatures, NumTemperatures);
  25. DisplayTemperatures (HourlyTemperatures, NumTemperatures, AverageTemp);
  26.  
  27. return 0;
  28. }
  29.  
  30.  
  31. void GetTemperatures (int Temperatures[], int NumTemperatures)
  32. {
  33. int Count = 0;
  34. int CurrentTemp = 0;
  35. int TimerCount = 0;
  36.  
  37. while (Count < 24)
  38. {
  39. if (CurrentTemp > -49 && CurrentTemp < 131)
  40. {
  41. cout << "Temperature at " << TimerCount++ << ":00? ";
  42. cin >> CurrentTemp;
  43.  
  44. Temperatures[Count++] = CurrentTemp;
  45. }
  46. else
  47. {
  48. cout << "Incorrect temperature input...\n";
  49. cout << "Please input between -50 and 130 degrees...\n";
  50. cout << "Re-input temperature: ";
  51. cin >> CurrentTemp;
  52. }
  53. }
  54.  
  55. }
  56.  
  57. double ComputeAverageTemp (int Temperatures[], int NumTemperatures)
  58. {
  59. double AverageTemp = 0.0;
  60. int Count = 0;
  61. int Sum = 0;
  62.  
  63. for (Count = 0; Count < 24; Count++)
  64. {
  65. Sum = Sum + Temperatures[Count];
  66. }
  67.  
  68. AverageTemp = Sum / NumTemperatures;
  69.  
  70. return AverageTemp;
  71. }
  72.  
  73. void DisplayTemperatures (int Temperatures[], int NumTemperatures, double AverageTemp)
  74. {
  75. int Count = 0;
  76.  
  77. cout << "\n*******************************\n";
  78. cout << "Hour\t\t Temperature\n";
  79.  
  80. while (Count < 24)
  81. {
  82. cout << Count++ << ":00\t\t\t" << Temperatures[Count] << endl;
  83. }
  84.  
  85. cout << "High Temp: \t\t\n";
  86. cout << "Low Temp: \t\t\n";
  87. cout << "Average Temp: \t\t" << AverageTemp << endl;
  88. cout << "*******************************\n";
  89. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Passing an Array to Function Problem

 
0
  #8
May 15th, 2008
Originally Posted by Metalsiege View Post
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
  1. #ifndef Lab4_h
  2. #define Lab4_h
  3.  
  4. void GetTemperatures (int Temperatures[], int NumTemperatures);
  5. double ComputeAverageTemp (int Temperatures[], int NumTemperatures);
  6. void DisplayTemperatures (int Temperatures[], int NumTemperatures, double AverageTemp);
  7.  
  8. #endif

CPP File
  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5.  
  6. #include "Lab4.h"
  7.  
  8. int main ()
  9. {
  10. const int NumTemperatures = 24;
  11. int HourlyTemperatures[NumTemperatures] = {0};
  12. double AverageTemp = 0.0;
  13.  
  14. cout << "*******************************\n";
  15. cout << "* Welcome to CTU Weather *\n";
  16. cout << "* ---------------------- *\n";
  17. cout << "* *\n";
  18. cout << "* Your source for up-to-date *\n";
  19. cout << "* weather information! *\n";
  20. cout << "*******************************\n\n\n";
  21.  
  22.  
  23. GetTemperatures (HourlyTemperatures, NumTemperatures);
  24. ComputeAverageTemp (HourlyTemperatures, NumTemperatures);
  25. DisplayTemperatures (HourlyTemperatures, NumTemperatures, AverageTemp);
  26.  
  27. return 0;
  28. }
  29.  
  30.  
  31. void GetTemperatures (int Temperatures[], int NumTemperatures)
  32. {
  33. int Count = 0;
  34. int CurrentTemp = 0;
  35. int TimerCount = 0;
  36.  
  37. while (Count < 24)
  38. {
  39. if (CurrentTemp > -49 && CurrentTemp < 131)
  40. {
  41. cout << "Temperature at " << TimerCount++ << ":00? ";
  42. cin >> CurrentTemp;
  43.  
  44. Temperatures[Count++] = CurrentTemp;
  45. }
  46. else
  47. {
  48. cout << "Incorrect temperature input...\n";
  49. cout << "Please input between -50 and 130 degrees...\n";
  50. cout << "Re-input temperature: ";
  51. cin >> CurrentTemp;
  52. }
  53. }
  54.  
  55. }
  56.  
  57. double ComputeAverageTemp (int Temperatures[], int NumTemperatures)
  58. {
  59. double AverageTemp = 0.0;
  60. int Count = 0;
  61. int Sum = 0;
  62.  
  63. for (Count = 0; Count < 24; Count++)
  64. {
  65. Sum = Sum + Temperatures[Count];
  66. }
  67.  
  68. AverageTemp = Sum / NumTemperatures;
  69.  
  70. return AverageTemp;
  71. }
  72.  
  73. void DisplayTemperatures (int Temperatures[], int NumTemperatures, double AverageTemp)
  74. {
  75. int Count = 0;
  76.  
  77. cout << "\n*******************************\n";
  78. cout << "Hour\t\t Temperature\n";
  79.  
  80. while (Count < 24)
  81. {
  82. cout << Count++ << ":00\t\t\t" << Temperatures[Count] << endl;
  83. }
  84.  
  85. cout << "High Temp: \t\t\n";
  86. cout << "Low Temp: \t\t\n";
  87. cout << "Average Temp: \t\t" << AverageTemp << endl;
  88. cout << "*******************************\n";
  89. }
Line 87 looks fine now to me. Perhaps change line 24 from this:
  1. ComputeAverageTemp (HourlyTemperatures, NumTemperatures);
to this?
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 30
Reputation: Metalsiege is an unknown quantity at this point 
Solved Threads: 0
Metalsiege Metalsiege is offline Offline
Light Poster

Re: Passing an Array to Function Problem

 
0
  #9
May 16th, 2008
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:

  1. void Pause()
  2. {
  3. cout << "Hit any key to continue...";
  4. cin.ignore(1);
  5. }

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..

  1. system ("PAUSE");
Last edited by Metalsiege; May 16th, 2008 at 3:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Passing an Array to Function Problem

 
0
  #10
May 16th, 2008
just add

  1. Pause();
into the code where ever you want to stop .

And copy and paste the function somewhere outside main();

And it will work fine.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC