Hello, first time here in the forum, though Ive used it before for help.

Okay so I have to create a constant to define the size of two float arrays and
Create two one dimensional float arrays in the function main() called highs and lows.
const int MAX = 7
float highs[MAX]; // like this
float lows[MAX];


Though I can get to print out the high and low temperatures, I cannot get the program to average the low or the high temperatures. Can anyone explain to me what I did wrong on the for loop for the average?

P.S. In my code I only have the for loop to average the low temperatures.

Thanks in advance.

#include <iostream>
#include <iomanip>
using namespace std;

void main(void)
{
const int	MAX = 7;	    // max size of the array
float		highs[MAX];    
float		lows[MAX];	
int			count;
int			counts;         // # of temps entered
int			x;				// loop control variable (LCV)
int			i = MAX;        

	// load the array from the keyboard
	
	do {	
		cout << "Enter the high temperatures ";
		cin >> highs[count];
		
		count++;
		cout << endl;
	} while ( ( count < MAX) && ( highs[count-1] != 0) );
	if ( highs[count-1] == 0)
	{
		count--;
	}

	// print them back out
	for (x= 0; x < count; x++)
	{
		cout << "High Temperature[" << x << "] = " << highs[x] << endl;
	}



	cout << "\n\n";
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

	do {	
		cout << "Enter the low temperatures ";
		cin >> lows[counts];
		counts++;
		cout << endl;
	} while ( ( counts < MAX) && ( lows[counts-1] != 0) );	

	if ( lows[counts-1] == 0)
	{
		counts--;
	}

	for (x= 0; x < counts; x++)
	{
		cout << "Low Temperature[" << x << "] = " << lows[x] << endl;
	}

	double sum = 0;
	for (int i=0; i <7; i++)
	{
		sum += lows[MAX];
	}


	cout << "The average low temperatures is: " << sum/7<< "\n";



	system("pause");

}

If you use a function, you only have to write the averaging code once. This will save you time and make your code easier to read.

In order for the average to calculate correctly, you have to base the index you're using to access the array on the control variable in your for loop, not the constant you used to declare it.

const int SIZE = 20;

float array[SIZE] = {0.0f};
float sum = 0.0f;

for (int i = 0; i < SIZE; ++i) {
  sum += array[i];
}

Two biggest problems:

  1. count and counts are never initialized. You should initialize them both to 0.
  2. In your averaging loop, you always use lows[MAX] . It should be lows[i] . lows[MAX] doesn't exist.

Okay, but in my code, do I use something like float array [MAX]? or how can I make them fit with the lows and highs? I still dont know about functions. That is in module 5, and I am currently in module 4 of my book.

Two biggest problems:

  1. count and counts are never initialized. You should initialize them both to 0.
  2. In your averaging loop, you always use lows[MAX] . It should be lows[i] . lows[MAX] doesn't exist.

but I have the const int Max = 7; isnt MAX 7? Thats what the teacher instructed to use:

const int MAX = 7;
float highs[MAX];
float lows[MAX];

but I have the const int Max = 7; isnt MAX 7? Thats what the teacher instructed to use:

const int MAX = 7;
float highs[MAX];
float lows[MAX];

You have an array of 7 elements indexed from 0 to 6. The 7th element (ie. highs[7] or lows[7]) does not exist. Aside from the two things I mentioned, your code is acceptable. Those are the two problems that are holding you back.

I get it now. however I get error C2065: 'i' : undeclared identifier in this:

Though I have the int i; in there. can you help me out with that? :)?

#include <iostream>
#include <iomanip>
using namespace std;

void main(void)
{
const int	MAX = 7;	    // max size of the array
float		highs[i];    
float		lows[i];	
int			count = 0;
int			counts = 0;         // # of temps entered
int			x, i;				// loop control variable (LCV)
       




	// load the array from the keyboard
	
	do {	
		cout << "Enter the high temperatures ";
		cin >> highs[count];
		
		count++;
		cout << endl;
	} while ( ( count < MAX) && ( highs[count-1] != 0) );
	if ( highs[count-1] == 0)
	{
		count--;
	}

	// print them back out
	// note: x< count - if our count is 4, then subscripts 0,1,2,3 are in use
	for (x= 0; x < count; x++)
	{
		cout << "High Temperature[" << x << "] = " << highs[x] << endl;
	}



	cout << "\n\n";
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

	do {	
		cout << "Enter the low temperatures ";
		cin >> lows[counts];
		counts++;
		cout << endl;
	} while ( ( counts < MAX) && ( lows[counts-1] != 0) );	

	if ( lows[counts-1] == 0)
	{
		counts--;
	}

	for (x= 0; x < counts; x++)
	{
		cout << "Low Temperature[" << x << "] = " << lows[x] << endl;
	}

	float sum = 0.0;

	for (int i=0; i < MAX; i++)
	{
		sum += lows[i];
	}


	cout << "The average low temperatures is: " << sum/7<< "\n";



	system("pause");

}

Lines 8 and 9. You changed them and you shouldn't have; you'll need to change them back. Those lines should be based on MAX because they are the declarations of your arrays.

What Narue is saying is that when you are accessing your arrays, as in Line 65, use of MAX as the index is not allowed because that would be accessing a nonexistent element of the array. That Line should be as you now have it.

OMG. Thank you Narue. Thank you Fbody. Thank you !! I Got it now.. You guys are pros.

Now I just have to make the same for the highs. I really appreciated.

Thanks again.

As long as the arrays are the same size (which yours currently are), you should be able to do it in the same loop, all you have to do is add another accumulator to the body of the loop. Simply declare sumHigh, sumLow, avgHigh, and avgLow and do both the high and low at the same time:

const int SIZE = 20;

float array1[SIZE] = {0.0f};
float array2[SIZE] = {0.0f};
float sum1 = 0.0f, sum2 = 0.0f;

for (int i = 0; i < SIZE; ++i) {
  sum1 += array1[i];  //accumulate the first sum
  sum2 += array2[i];  //accumulate the second sum
}

I'm finished now. Thank you both. :) Should I close this thread?

I'm finished now. Thank you both. :) Should I close this thread?

Glad you figured it out. :)

I don't generally like to campaign for solves or votes, but since you asked, yes.

Generally, if you feel your problem is adequately solved, it is appreciated by the posters if you mark a thread as "solved", and/or give positive votes where you feel warranted. It's not an absolute must though.

Could I see the finished work? I have the same problem but I don't know how to do it.

@prglili5994 - If you are having a problem with the code that you have start a new thread and post your code, a description of what it should do, a description of what it is doing, any error messages you are receiving.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.