7 Replies - 15398 Views - Last Post: 18 October 2006 - 06:27 PM Rate Topic: -----

#1 csstarter9  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 18-September 06

Array Program

Posted 17 October 2006 - 07:14 AM

I'm writing a program that computes arrays and asks the user to enter a number of Temperature readings, stores them in an array, calculates the average temperature, the standard deviation, the minimum temperature and its corresponding reading, and the maximum temperature and its corresponding reading.
- The temperature array must be declared with a size of 100 dimensions provided by a constant value.
- The program must first ask the user to enter the number of temperature readings and verify that this number is no greater than 100 (constant value). If the number is greater, the program must notify this to the user and ask for the number again.
- The program must then request the user to enter all the temperature readings and must allow for floating-point (float) values.
- The program must then calculate average and standard deviation and output these values to the console.
- The program must calculate the minimum temperature and its corresponding reading as well as the maximum temperature and its corresponding reading, and output these quantities to the console.


This is what i have so far:
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <conio.h>
using namespace std;

int main()
{
	const int TEMPERATURE = 120;
	float ave = 0.0;
	float sdev = 0.0;   
	int temp_read;
	int read[TEMPERATURE];

	do
	{
		cout << "Enter the number of Temperarure readings [1-120]: ";
		cin >> temp_read;
		if ((temp_read < 1) || (temp_read > TEMPERATURE))
			cout << "Incorrect choice. Try again... \n";
	} while ((temp_read< 1) || (temp_read > TEMPERATURE));

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

	ave = ave / temp_read;

	cout << "The average of all the temperature readings is: " << ave << "\n";

	for (int i = 0;i < temp_read;i++)
	{
		sdev = sdev + (read[i] - ave)*(read[i] - ave);
	}
	sdev = sdev / temp_read;
	sdev = sqrt(sdev);

	cout << "The standard deviation of all the temperature readings is: " << sdev << "\n";
	
	cout << "Press any key to continue..";

	_getch();
	return 0;
}


Does my code look ok and have I fulfilled all the requirements for the code

Is This A Good Question/Topic? 0
  • +

Replies To: Array Program

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Array Program

Posted 17 October 2006 - 07:22 AM

Well, your specifications note that you should accept no more than 100 temperature readings, but you have set a limit of 120. The specifications also state that floating point numbers are acceptable, but you have decalred the array to be of type int, not float. You are not tracking or reporting on the minimum and maximum temperatures at all.

You've got a good start...just a few tweaks to make, and some additional functionality to add. Feel free to ask any questions you wish.
Was This Post Helpful? 0
  • +
  • -

#3 csstarter9  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 18-September 06

Re: Array Program

Posted 17 October 2006 - 12:18 PM

View PostAmadeus, on 17 Oct, 2006 - 07:22 AM, said:

Well, your specifications note that you should accept no more than 100 temperature readings, but you have set a limit of 120. The specifications also state that floating point numbers are acceptable, but you have decalred the array to be of type int, not float. You are not tracking or reporting on the minimum and maximum temperatures at all.

You've got a good start...just a few tweaks to make, and some additional functionality to add. Feel free to ask any questions you wish.




so it would look something like this:
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <conio.h>
using namespace std;

int main()
{
	const int TEMPERATURE = 100;
	float ave = 0.0;
	float sdev = 0.0;   
	int temp_read;
	int read[TEMPERATURE];

	do
	{
		cout << "Enter the number of Temperarure readings [1-120]: ";
		cin >> temp_read;
		if ((temp_read < 1) || (temp_read > TEMPERATURE))
			cout << "Incorrect choice. Try again... \n";
	} while ((temp_read< 1) || (temp_read > TEMPERATURE));

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

	ave = ave / temp_read;

	cout << "The average of all the temperature readings is: " << ave << "\n";

	for (int i = 0;i < temp_read;i++)
	{
		sdev = sdev + (read[i] - ave)*(read[i] - ave);
	}
	sdev = sdev / temp_read;
	sdev = sqrt(sdev);

	cout << "The standard deviation of all the temperature readings is: " << sdev << "\n";
	
	cout << "Press any key to continue..";

	_getch();
	return 0;
}


what would the code be for tracking the minimum and maximum temperature values

and what else do i need? i have to submit this assignment by tomorrow and whatever help i can recieve i would appreciate it

edit: added [code] tags ~ jayman9
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Array Program

Posted 17 October 2006 - 01:41 PM

I'd suggest declaring two more variables...one for the min, and one for the max. As each temperature is entered, you can check to see if it's lower than the current min, or higher than the current max - if so, replace the approriate value.

Another, more efficient way to do it would be to sort the array once all values have been entered - then you minimum value witll be in the first element, and the maximum in the last.
Was This Post Helpful? 0
  • +
  • -

#5 csstarter9  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 18-September 06

Re: Array Program

Posted 18 October 2006 - 05:17 AM

View PostAmadeus, on 17 Oct, 2006 - 01:41 PM, said:

I'd suggest declaring two more variables...one for the min, and one for the max. As each temperature is entered, you can check to see if it's lower than the current min, or higher than the current max - if so, replace the approriate value.

Another, more efficient way to do it would be to sort the array once all values have been entered - then you minimum value witll be in the first element, and the maximum in the last.



so like this:
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <conio.h>
using namespace std;

int main()
{
	const int TEMPERATURE = 100;
	float ave = 0.0;
	float sdev = 0.0;   
	int temp_read;
	int read[TEMPERATURE];
	int min;
	int max;

	do
	{
		cout << "Enter the number of Temperarure readings [1-120]: ";
		cin >> temp_read;
		if ((temp_read < 1) || (temp_read > TEMPERATURE))
			cout << "Incorrect choice. Try again... \n";
	} while ((temp_read< 1) || (temp_read > TEMPERATURE));

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

	ave = ave / temp_read;

	cout << "The average of all the temperature readings is: " << ave << "\n";

	for (int i = 0;i < temp_read;i++)
	{
		sdev = sdev + (read[i] - ave)*(read[i] - ave);
	}
	sdev = sdev / temp_read;
	sdev = sqrt(sdev);

	cout << "The standard deviation of all the temperature readings is: " << sdev << "\n";
	
	cout << "Press any key to continue..";

	_getch();
	return 0;
}


i declared the 2 min and max variables but i dont' know how to have them checked each time a temperature is entered
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Array Program

Posted 18 October 2006 - 05:22 AM

Set the to zero initially and then check the value coming in using an if statement...you are already doing something similar for the number of temperatures enetered.
if(read[i]>max)
   max=read[i];


Was This Post Helpful? 0
  • +
  • -

#7 csstarter9  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 18-September 06

Re: Array Program

Posted 18 October 2006 - 01:55 PM

View PostAmadeus, on 18 Oct, 2006 - 05:22 AM, said:

Set the to zero initially and then check the value coming in using an if statement...you are already doing something similar for the number of temperatures enetered.
if(read[i]>max)
   max=read[i];




where would i stick the if statement in my code?
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Array Program

Posted 18 October 2006 - 06:27 PM

You would place it where you are taking the input, or wait until you've got all the inputs, then run a loop that checks them all.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1