Ok i have this assignment almost complete. But after the standard deviation, i am supposed to show the minimum and maximum temperatures and the number of the position of the temperature. I just do not understand how to to that....

//Assignment 6: Computing with Arrays
//By: Curtis Davenport 2/24/08

#include <iostream>

#include <cmath>

#define MAXTEMPREADINGS 100

using namespace std;

int AveOut(float);
int SdevOut(float);

int main()
{
	int i,numTemps;
    float temp[MAXTEMPREADINGS];
    float ave  = 0.0;
    float sdev = 0.0;

    do
    {
        cout << "Enter the number of temperature readings:  ";
        cin  >> numTemps;

        if (numTemps < 1) 
        {
            cout << "The number of readings is too LOW!!! \n";
			cout << "Keep the number of readings greater than 0. \n";
        }

		if (numTemps > MAXTEMPREADINGS)
		{
			cout <<"The number of readings is too HIGH!!! \n";
			cout << "Limit the number of temperature readings to 100 \n";
		}

    } while ((numTemps < 1) || (numTemps > MAXTEMPREADINGS));

    for (i = 0;i < numTemps;i++)
    {
        cout << "Enter the temperature reading #" << i+1 << ": ";
        cin  >> temp[i];
    }

    for (i = 0; i < numTemps;i++)
    {
        ave = ave + temp[i];
    }
    ave = ave / numTemps;

    for (i = 0; i < numTemps;i++)
    {
        sdev = sdev + pow((temp[i] - ave),2);
    }
    sdev = sdev / numTemps;
    sdev = sqrt(sdev);

    AveOut(ave);
    SdevOut(sdev);

    return 0;

}
int AveOut(float ave)
{
	cout  << "The Temperature average is: " << ave << "\n";

	return 0;

}
int SdevOut(float sdev)
{
	cout << "The class standard deviation is: " << sdev << "\n";

	return 0;
}

I have the output of what it should look like attached. That is all i need help with, i just cant figure out how to do the minimum and maximum and what temperature reading number it is.....thanks for any assistance

Recommended Answers

All 24 Replies

Create four more variables, min, mintempnum, max, and maxtempnum then on line 46 check to see if the temp just read is greater than max then set max to temp, and if temp is less than min set min to be temp. set mintempnum and maxtempnum to be the value of i whenever you change min and max.

int min = 0, mintempnum = 0;
int max = 0, maxtempnum = 0;
if( temp[i] > max)
{
    max = temp[i];
    maxtempnum = i;
}
// do the same with min

When you may not know the actual range of data to be used, better to start min and max as the first data item's value. Then you always have a valid comparison.

//updating AD's code
int min = temp[0], mintempnum = 0;
int max = temp[0], maxtempnum = 0;
if( temp[i] > max)
{
    max = temp[i];
    maxtempnum = i;
}
// do the same with min

Where in my code would this go?Would it be at the end because it needs to pop up after my standard deviation?? Thanks with the fast response by the way, this little thing was stumping me...

Once you have the data in the array, you can place your min/max code sections wherever you want, prior to the point at which you will display the results. This chunk has no direct relationship to the standard deviation calculation - they all simply work upon the same data.

If you're feeling particularly clever and you want to reduce the number of times you traverse the array, you could do the min/max within the loop that adds up the values for the average.

Ok thanks a lot. You really helped me out here. Slowly getting this stuff...and its because of ya'lls help...

Well I am having problems getting the min and max to display in the temp number altogether. It is not even coming up after the standard deviation part...It still displays the same things i had before...how do i get this to display properly...

//Assignment 6: Computing with Arrays
//By: Curtis Davenport 2/24/08

#include <iostream>

#include <cmath>

#define MAXTEMPREADINGS 100

using namespace std;

int AveOut(float);
int SdevOut(float);

int main()
{
	int i,numTemps;
    float temp[MAXTEMPREADINGS];
    float ave  = 0.0;
    float sdev = 0.0;

    do
    {
        cout << "Enter the number of temperature readings:  ";
        cin  >> numTemps;

        if (numTemps < 1) 
        {
            cout << "The number of readings is too LOW!!! \n";
			cout << "Keep the number of readings greater than 0. \n";
        }

		if (numTemps > MAXTEMPREADINGS)
		{
			cout <<"The number of readings is too HIGH!!! \n";
			cout << "Limit the number of temperature readings to 100 \n";
		}

    } while ((numTemps < 1) || (numTemps > MAXTEMPREADINGS));

    for (i = 0;i < numTemps;i++)
    {
        cout << "Enter the temperature reading #" << i+1 << ": ";
        cin  >> temp[i];
    }

    for (i = 0; i < numTemps;i++)
    {
        ave = ave + temp[i];

    }
    ave = ave / numTemps;

    for (i = 0; i < numTemps;i++)
    {
        sdev = sdev + pow((temp[i] - ave),2);
    }
	
	int max = temp[0], int maxtempnum = 0;
	int min = temp[0], int mintempnum = 0; 

	if ( temp[i] > max)
	{
		max = temp[i];
		int maxtempnum = i;

		cout <<"The maximum temperature is:" << max << "in reading #" << i << "\n";
	}

	if ( temp[i] > min)
	{
		min = temp[i];
		int mintempnum = i;

		cout <<"The minimum temperature is:" << min << "in reading #" << i << "\n";
	}

    sdev = sdev / numTemps;
    sdev = sqrt(sdev);

    AveOut(ave);
    SdevOut(sdev);

    return 0;

}
int AveOut(float ave)
{
	cout  << "The Temperature average is: " << ave << "\n";

	return 0;

}
int SdevOut(float sdev)
{
	cout << "The class standard deviation is: " << sdev << "\n";

	return 0;
}

If your current behavior is like a previous attempt, maybe it's because this latest code you posted doesn't compile.

int max = temp[0], int maxtempnum = 0;
	int min = temp[0], int mintempnum = 0;

I dont understand what you mean by this. It compiled when i tried it last, but i just could not get the min and max stuff to work properly. I have my assignment attached to show what it should do. Thats all i cant get to work....

He is saying that these lines won't compile:

int max = temp[0], int maxtempnum = 0;
	int min = temp[0], int mintempnum = 0;

Notice that he put the commas in red. They either need to be changed into semicolons or you need to remove the word "int" after the comma.

Oh ok got that, i changed it and it now compiles correctly. Still cannot figure out how to get the min and max to display. I used the script ya'll gave me but it just will not say min and max after standard deviation. The output looks just like before when i had no min and max stuff in my code...this is what i have and it compiles...

//Assignment 6: Computing with Arrays
//By: Curtis Davenport 2/24/08

#include <iostream>

#include <cmath>

#define MAXTEMPREADINGS 100

using namespace std;

int AveOut(float);
int SdevOut(float);

int main()
{
	int i,numTemps;
    float temp[MAXTEMPREADINGS];
    float ave  = 0.0;
    float sdev = 0.0;

    do
    {
        cout << "Enter the number of temperature readings:  ";
        cin  >> numTemps;

        if (numTemps < 1) 
        {
            cout << "The number of readings is too LOW!!! \n";
			cout << "Keep the number of readings greater than 0. \n";
        }

		if (numTemps > MAXTEMPREADINGS)
		{
			cout <<"The number of readings is too HIGH!!! \n";
			cout << "Limit the number of temperature readings to 100 \n";
		}

    } while ((numTemps < 1) || (numTemps > MAXTEMPREADINGS));

    for (i = 0;i < numTemps;i++)
    {
        cout << "Enter the temperature reading #" << i+1 << ": ";
        cin  >> temp[i];
    }

    for (i = 0; i < numTemps;i++)
    {
        ave = ave + temp[i];

    }
    ave = ave / numTemps;

    for (i = 0; i < numTemps;i++)
    {
        sdev = sdev + pow((temp[i] - ave),2);
    }
	
	int max = temp[0],  maxtempnum = 0;
	int min = temp[0],  mintempnum = 0; 

	if ( temp[i] > max)
	{
		max = temp[i];
		int maxtempnum = i;

		cout <<"The maximum temperature is:" << max << "in reading #" << i << "\n";
	}

	if ( temp[i] > min)
	{
		min = temp[i];
		int mintempnum = i;

		cout <<"The minimum temperature is:" << min << "in reading #" << i << "\n";
	}

    sdev = sdev / numTemps;
    sdev = sqrt(sdev);

    AveOut(ave);
    SdevOut(sdev);

    return 0;

}
int AveOut(float ave)
{
	cout  << "The Temperature average is: " << ave << "\n";

	return 0;

}
int SdevOut(float sdev)
{
	cout << "The class standard deviation is: " << sdev << "\n";

	return 0;
}

These statements aren't in a loop.

if ( temp[i] > max )
   {
      max = temp[i];
      int maxtempnum = i;

      cout <<"The maximum temperature is:" << max << "in reading #" << i << "\n";
   }

   if ( temp[i] > min )
   {
      min = temp[i];
      int mintempnum = i;

      cout <<"The minimum temperature is:" << min << "in reading #" << i << "\n";
   }

I don't suppose you meant to put them somewhere else?

The only thing that needs to loop is entering the temps, it loops when temps are out of the acceptable range. Is placement important with this script?? I just simply cannot get it to output "The minimum temperature is _ in reading # _ " and same for the max. When i run the code it just does the standard deviation and thats it there is no min and max after it, and thats what i am trying to get...

What's i after the loop? And what's temp? How are you going to examine a loopful of values for the max and min if the check is not in a loop? Why do you need three loops identically controlled consecutively?

ok i sort of see what you are saying. I dont understand what three loops identically controlled consecutively means. Ive tried putting it in a few different places and i cant get it to work. Where should i place the min and max script then?? And for you question i have an attachment of what the output and directions that must be followed are. I am so frustrated because this is due tomorrow and this seemingly easy max min thing has taken forever to figure out...

ok i sort of see what you are saying. I dont understand what three loops identically controlled consecutively means.

For example,

for ( i = 0; i < 10; ++i )
{
   foo();
}

for ( i = 0; i < 10; ++i )
{
   bar();
}

for ( i = 0; i < 10; ++i )
{
   baz();
}

could be written

for ( i = 0; i < 10; ++i )
{
   foo();
   bar();
   baz();
}

This is not your exact case, but...

Ive tried putting it in a few different places and i cant get it to work. Where should i place the min and max script then??

Maybe in the loop used to calculate the average?

I am trying to runn this code but it exits upon entering the last temperature. Have you had this problem? This is a classmate.

No the last code i put on there was working great for me. It showed everything just like the assignment other than the max and mins is what i am still working on...any hints on that??

I am now getting the Min and Max temperatures to show up, but they are not right. Any suggestions?

#include <iostream>
#include <cmath>
#define MAXTEMPREADINGS 100

using namespace std;

int AveOut(float);
int SdevOut(float);

int main()
{
    int i,numTemps;
    float temp[MAXTEMPREADINGS];
    float ave  = 0.0;
    float sdev = 0.0;
	

    do
    {
        cout << "Enter the number of temperature readings:  ";
        cin  >> numTemps;

        if (numTemps < 1) 
        {
            cout << "The number of readings is too LOW!!! \n";
			cout << "Keep the number of readings greater than 0. \n";
        }

		if (numTemps > MAXTEMPREADINGS)
		{
			cout <<"The number of readings is too HIGH!!! \n";
			cout << "Limit the number of temperature readings to 100 \n";
		}

    } while ((numTemps < 1) || (numTemps > MAXTEMPREADINGS));
	
	for (i = 0;i < numTemps;i++)
    {
		cout << "Enter the temperature reading #" << i+1 << ": ";
        cin  >> temp[i];
		ave = ave + temp[i];
		sdev = sdev + (temp[i] - ave)*(temp[i] - ave);
	}
	ave = ave / numTemps;
	sdev = sdev / numTemps;
    sdev = sqrt(sdev); 
	
	float maxtemp = temp[0];	
	float mintemp = temp[0];
	
	for (i = 0;i < numTemps;i++)
	{
		if ( temp[i] > maxtemp)
			{
				maxtemp = temp[i];
			}
	}
	cout <<"The maximum temperature is:" << maxtemp << "in reading #" << i << "\n";
	
	for (i = 0;i < numTemps;i++)
	{
		if ( temp[i] > mintemp)
			{
				mintemp = temp[i];
			}
	}
	cout <<"The minimum temperature is:" << mintemp << "in reading #" << i << "\n";

	AveOut(ave);
    SdevOut(sdev);

    system("PAUSE");
	return 0;
}
int AveOut(float ave)
{
	cout  << "The Temperature average is: " << ave << "\n";

	return 0;

}
int SdevOut(float sdev)
{
	cout << "The class standard deviation is: " << sdev << "\n";

	return 0;
}

The two loops at lines 51 and 60 can be combined into one loop then find both min and max within the same loop because its more efficient that way.

line 62: min doesn't work because you have the test backwards. Use the < operator instead of >.

Sorry for not posting properly the first time.

I have put the changes in that you told me and now I am getting 2 min temps and 1 max temp. I am very new to this and is very confusing. Did I enter your changes in right?

#include <iostream>
#include <cmath>
#define MAXTEMPREADINGS 100

using namespace std;

int AveOut(float);
int SdevOut(float);

int main()
{
	int i,numTemps;
    float temp[MAXTEMPREADINGS];
    float ave  = 0.0;
    float sdev = 0.0;
	

    do
    {
        cout << "Enter the number of temperature readings:  ";
        cin  >> numTemps;

        if (numTemps < 1) 
        {
            cout << "The number of readings is too LOW!!! \n";
			cout << "Keep the number of readings greater than 0. \n";
        }

		if (numTemps > MAXTEMPREADINGS)
		{
			cout <<"The number of readings is too HIGH!!! \n";
			cout << "Limit the number of temperature readings to 100 \n";
		}

    } while ((numTemps < 1) || (numTemps > MAXTEMPREADINGS));
	
	for (i = 0;i < numTemps;i++)
    {
		cout << "Enter the temperature reading #" << i+1 << ": ";
        cin  >> temp[i];
		ave = ave + temp[i];
		sdev = sdev + (temp[i] - ave)*(temp[i] - ave);
	}
	ave = ave / numTemps;
	sdev = sdev / numTemps;
    sdev = sqrt(sdev); 
	
	float maxtemp = temp[0];	
	float mintemp = temp[0];
	
	for (i = 0;i < numTemps;i++)
	{
		if ( temp[i] > maxtemp)
			{
				if ( temp[i] < mintemp)
				{
					mintemp = temp[i];
				}
				maxtemp = temp[i];
			}
		cout <<"The minimum temperature is:" << mintemp << "in reading #" << i << "\n";
	}
	cout <<"The maximum temperature is:" << maxtemp << "in reading #" << i << "\n";	

	AveOut(ave);
    SdevOut(sdev);

    system("PAUSE");
	return 0;
}
int AveOut(float ave)
{
	cout  << "The Temperature average is: " << ave << "\n";

	return 0;

}
int SdevOut(float sdev)
{
	cout << "The class standard deviation is: " << sdev << "\n";

	return 0;
}
commented: Good job learning code tags :) +24

lines 53-60 are still incorrect. You should not have used nested if statements

for (i = 0;i < numTemps;i++)
{
	if ( temp[i] > maxtemp)
	{
		maxtemp = temp[i];
	}
	if ( temp[i] < mintemp)
	{
		mintemp = temp[i];
	}
	cout <<"The minimum temperature is:" << mintemp << "in reading #" << i << "\n";
}

This is what i have, But in the output, the max and min appear before the temp average and standard deviation... but mainly it outputs the max and min of the right numbers, but it does not display the proper temp number for the set of temps...

//Assignment 6: Computing with Arrays
//By: Curtis Davenport 2/24/08

#include <iostream>

#include <cmath>

#define MAXTEMPREADINGS 100

using namespace std;

int AveOut(float);
int SdevOut(float);

int main()
{
	int i,numTemps;
    float temp[MAXTEMPREADINGS];
    float ave  = 0.0;
    float sdev = 0.0;

    do
    {
        cout << "Enter the number of temperature readings:  ";
        cin  >> numTemps;

        if (numTemps < 1) 
        {
            cout << "The number of readings is too LOW!!! \n";
			cout << "Keep the number of readings greater than 0. \n";
        }

		if (numTemps > MAXTEMPREADINGS)
		{
			cout <<"The number of readings is too HIGH!!! \n";
			cout << "Limit the number of temperature readings to 100 \n";
		}

    } while ((numTemps < 1) || (numTemps > MAXTEMPREADINGS));

    for (i = 0;i < numTemps;i++)
    {
        cout << "Enter the temperature reading #" << i+1 << ": ";
        cin  >> temp[i];
    }

    for (i = 0; i < numTemps;i++)
    {
        ave = ave + temp[i];

    }
    ave = ave / numTemps;

    for (i = 0; i < numTemps;i++)
    {
        sdev = sdev + pow((temp[i] - ave),2);
    }
	
	int maxtemp = temp[0], maxtempnum = 0;
	int mintemp = temp[0], mintempnum = 0;

	for(i =0; i < numTemps; i++)

		if ( temp[i] > maxtemp)
	{
		maxtemp = temp[i];
		maxtempnum = i;
		
	}
		cout <<"The maximum temperature is:" << maxtemp << "in reading #" << i << "\n";

	for(i = 0; i < numTemps; i++)
		
		if ( temp[i] < mintemp)
	{
		mintemp = temp[i];
		maxtempnum = i;
		
	}
		cout <<"The minimum temperature is:" << mintemp << "in reading #" << i << "\n";




    sdev = sdev / numTemps;
    sdev = sqrt(sdev);

    AveOut(ave);
    SdevOut(sdev);

    return 0;

}
int AveOut(float ave)
{
	cout  << "The Temperature average is: " << ave << "\n";

	return 0;

}
int SdevOut(float sdev)
{
	cout << "The class standard deviation is: " << sdev << "\n";

	return 0;
}

This thing is due today and i have been trying to get it to work right forever...i have the attached output of what it is supposed to look like...any further assistance would help me tremendously...

This is what I have and it seems to be working fine. The order in which the min and max is different than in the assignment, but I do not think that is what is important as long as what is displayed is correct. Let me know how this works for you.

#include <iostream>
#include <cmath>

#define MAXTEMPS 100

using namespace std;

int AveOut(float);
int SdevOut(float);

int main()
{
    int i,numTemps;
    float temp[MAXTEMPS];
    float ave  = 0.0;
    float sdev = 0.0;
	float Tmax;
	float Tmin;

    do
    {
        cout << "Enter the number of temperature readings (1-" << MAXTEMPS << "): ";
        cin  >> numTemps;

        if (numTemps <= 1)
        {
            cout << "The number of readings is too LOW!\n";
			cout << "You must enter a number higher than 1. \n";
        }
		if (numTemps > MAXTEMPS)
		{
			cout << "The number of readings is too HIGH!\n";
			cout << "Limit the number of readings to 100. \n";
		}
    } while ((numTemps <= 1) || (numTemps > MAXTEMPS));

    for (i = 0;i < numTemps;i++)
    {
        cout << "Enter Temperature reading #" << i+1 << ": ";
        cin  >> temp[i];
    }

    for (i = 0; i < numTemps;i++)
    {
        ave = ave + temp[i];
    }
    ave = ave / numTemps;

    for (i = 0; i < numTemps;i++)
    {
        sdev = sdev + pow((temp[i] - ave),2);
    }
    sdev = sdev / numTemps;
    sdev = sqrt(sdev);

	Tmax = temp[0];
	int maxRead = 1;
	for (i = 0;i < numTemps;i++)
				
		{ 
			if (temp[i] > Tmax)
			{
				Tmax = temp[i];
				maxRead = i + 1;
			}
		}
	cout << "The maximum Temperature is: " << Tmax << " in reading # " << maxRead << "\n";

	Tmin = temp[0];
	int minRead = 1;
	for (i = 0;i < numTemps;i++)
				
		{ 
			if (temp[i] < Tmin)
			{
				Tmin = temp[i];
				minRead = i + 1;
			}
		}
	cout << "The minimum Temperature is: " << Tmin << " in reading # " << minRead << "\n";

	AveOut(ave);
    SdevOut(sdev);

    system("PAUSE");
	return 0;
}

int AveOut(float ave)
{
    cout << "The average temperature is: " << ave << "\n";

    return 0;
}

int SdevOut(float sdev)
{
    cout << "The standard deviation is: " << sdev << "\n";
    
    return 0;
}

Hey thanks for all of the help finally got this thing to work right!!!

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.