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");

}

Recommended Answers

[QUOTE=Aghtar;1672317]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];[/QUOTE]
You have an array of 7 elements indexed from 0 to 6. The 7th element (ie. highs[7] or lows[7]) does not exist. …

Jump to Post

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 [B]declarations[/B] of your arrays.

What Narue is saying is that when you are [B]accessing[/B] your arrays, as in Line 65, use of …

Jump to Post

All 13 Replies

Re: Arrays and average with a constant. Help!! 80 80

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];
}
Re: Arrays and average with a constant. Help!! 80 80

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.
Re: Arrays and average with a constant. Help!! 80 80

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.

Re: Arrays and average with a constant. Help!! 80 80

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];

Re: Arrays and average with a constant. Help!! 80 80

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.

Re: Arrays and average with a constant. Help!! 80 80

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");

}
Re: Arrays and average with a constant. Help!! 80 80

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.

Re: Arrays and average with a constant. Help!! 80 80

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.

Re: Arrays and average with a constant. Help!! 80 80

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
}
Re: Arrays and average with a constant. Help!! 80 80

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

Re: Arrays and average with a constant. Help!! 80 80

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.

Re: Arrays and average with a constant. Help!! 80 80

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

Re: Arrays and average with a constant. Help!! 80 80

@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 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.