Sign in with
Sign up | Sign in
Your question

C++ program 2 help

Last response: in Applications
Share
May 9, 2010 4:04:30 AM

Okay, found out how to get the months to display when entering the rainfall but I cant figure out how to get it to display the name of the month for the lowest and highest rainfall( Lines 51 and 66). The way I have it when I enter the last rainfall it displays the total and average then beeps and shows weird characters for the highest and lowest.

  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. double total;
  7.  
  8. int main()
  9. {
  10. const int array_size = 12;
  11. string name[array_size] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
  12. double rain[array_size];
  13. int count;
  14. double average;
  15. double lowest;
  16. double highest;
  17.  
  18. cout << "Please enter the rainfall for all 12 months" << endl;
  19. cout << "After each input please press enter to input the next number" << endl << endl;
  20. for (count = 0; count < array_size; count++)
  21. {
  22. cout << "Please enter " << name[count] << " rainfall: ";
  23. cin >> rain[count];
  24. if (rain[count] < 0)
  25. {
  26. cout << "Rainfall can not be a negative number" << endl;
  27. cout << "Please re-enter " << name[count] << " rainfall: ";
  28.  
  29. cin >> rain[count];
  30. }
  31. total += rain[count];
  32. }
  33.  
  34. average = total/12;
  35.  
  36. cout << "The total rainfall for the year is: " << total << endl;
  37. cout << "The average rainfall for the year is: " << average << endl;
  38.  
  39.  
  40. highest = rain[0];
  41. for (count = 1; count < array_size; count++)
  42. {
  43. if (rain[count] > highest)
  44. {
  45. highest = rain[count];
  46. }
  47.  
  48. }
  49. cout << "The highest rainfall is in " << name[count] << " with: " << highest << " inches" << endl;
  50.  
  51.  
  52.  
  53.  
  54.  
  55. lowest = rain[0];
  56. for (count = 1; count < array_size; count++)
  57. {
  58. if (rain[count] < lowest)
  59. {
  60. lowest = rain[count];
  61. }
  62.  
  63. }
  64. cout << "The lowest rainfall is in " << name[count] << " with: " << lowest << " inches" << endl;
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. return 0;
  73. }

More about : program

May 9, 2010 7:25:22 AM

The error you're making is in your use of the count variable. If this were a managed language you'd get an index out of bounds exception, but C++ is just letting you access some random piece of the address space. In the for loop to find the highest rain count, you determine the largest amount of rain. However, you do not determine the index of this month. Later, when you cout, name[count] is actually asking for name[12], which is not within your array!
What you want is to declare
int highest_index = 0;
and have
if (rain[count] > highest)
{
highest = rain[count];
highest_index = count;
}

Then when you cout, ask for name[highest_index]
Likewise for the lowest rainfall.
Cheers,
May 9, 2010 4:08:34 PM

Ohhh okay yea I see. Nice Thank You
Related resources
May 9, 2010 4:13:51 PM

One last question, I made the total variable a global because when I had it as a local it didn't recognize my running total (line 31). I know its find to have as a global but just confused why it wasn't working as a local.
May 10, 2010 7:33:41 PM

Can you show me the code that doesn't work with that? It should work as a local for sure.
a b L Programming
May 10, 2010 8:40:33 PM

IMHO, try to avoid global variables like the plague. In larger programs they tend to create more problems then they solve.
a b L Programming
May 12, 2010 1:03:23 AM

flamethrower205 said:
The error you're making is in your use of the count variable. If this were a managed language you'd get an index out of bounds exception, but C++ is just letting you access some random piece of the address space. In the for loop to find the highest rain count, you determine the largest amount of rain. However, you do not determine the index of this month. Later, when you cout, name[count] is actually asking for name[12], which is not within your array!
What you want is to declare
int highest_index = 0;
and have
if (rain[count] > highest)
{
highest = rain[count];
highest_index = count;
}

Then when you cout, ask for name[highest_index]
Likewise for the lowest rainfall.
Cheers,
Good solution there, but you can go a bit further. Since you keep the index of the highest rainfall, you don't need to also keep the value.
  1. int highest_index = 0;
  2. ...
  3. if (rain[count] > rain[highest_index])
  4. {
  5. highest_index = count;
  6. }
Then if you need the month name, use "name[highest_index]" as suggested and for the value "rain[highest_index]".
May 12, 2010 2:54:53 AM

I think the difference in code is in part stylistic, I prefer to be upfront about what I'm keeping track of since it's one less thing to worry about when implementing some heinous algorithm.
Keeping track of the local variable also helps performance though (I do a lot of high performance code in which saving a couple of cycles or memory accesses can result in hours less processing time).
To that end, you can minimize array accesses by directly keeping track of the best value. You'll also help the compiler realize that it ought to cache highest.
I did a small test to find the smallest element in a random array of 10^7 elements in which code 1 explicitly stores the value of the smallest element while code 2 refers to the array for the value (as in you suggestion). The average runtimes with managed and unmanaged code, respectively, are

Managed:
Code 1 - 51.9 ms
Code 2 - 90.6 ms

Unmanaged:
Code 1 - 36.8
Code 2 - 44.6

Thus, keeping track of the best value gives you a 42.7% and 17.5% performance advantage for managed and unmanaged code, respectively. Granted this makes no difference with 12 elements, but it's good coding practice for larger programs.
May 12, 2010 5:45:28 AM

When I try to make double total (line 6( inside main it always said like total not declared
May 13, 2010 3:45:03 AM

Can you post the problematic code? The total declaration shouldn't cause problems, so maybe there's something particular about where you declared it.
May 13, 2010 3:43:01 PM

laserpp said:
When I try to make double total (line 6( inside main it always said like total not declared


it's because he is using total without initializing it, ie he is declaring it without setting it, and then in line 31 using it

line 6 should be:
  1. double total = 0;
May 13, 2010 4:16:41 PM

Fair enough, although the value ought to default to 0 when not initialized. It is a compiler preference - MS VS freaks out at you when you do that in debug, but works fine in production.
a b L Programming
May 13, 2010 6:03:37 PM

flamethrower205 said:
Fair enough, although the value ought to default to 0 when not initialized.
Unless stated in the language specifications, this can be a dangerous assumption.
May 14, 2010 3:44:43 AM

O it's definitely sketchy business, especially in C++. it's just amusing that the compiler didn't care when he declared it (but didn't explicitly initialize) as a global variable - technicalities of the language aside, more user consistency from the compiler would be nice.
!