0

So basically I'm taking an intro tp programming class and we've been taught the basics (loops, if statements, variable types etc.) I'm solving a program where I have to ask the user to enter 6 different temperature values, and then print out the maximum, average and range of the 6 values.

How and where should I store these 6 numbers?

cout<< "Enter 6 diff numbers" << endl;
float numbers;
cin >> numbers;

for ( .... i_++)

max = ;
min = ;


cout << .. << .... << endl;

//This should not help because float can only keep one number stored and not 6. How should I do this without using any sort of arrays, functions etc. ?

I was thinking using substrings and declaring it like a string or something??

Thanks for all your help.

3
  • 2
    Imagine there are 6 millions numbers, and that you don't have room in memory to store them. Imagine that the program is actually you. How would you solve the task? Commented Mar 19, 2016 at 23:27
  • Unless the number you get as input needs to be unique, you don't actually have to store each value. What @Dialecticus is essentially saying is that you only need the current value and compare it to the max and min values. For the average all you need is the sum and the count. Commented Mar 19, 2016 at 23:31
  • You can just store the running totals (number of values entered, current average, minimum so far, maximum so far etc...) Commented Mar 19, 2016 at 23:32

3 Answers 3

1

This is one way to do it without arrays without spoiling the rest of your homework.

cout<< "Enter 6 different numbers" << endl;

float num1, num2, num3, num4, num5, num6, max, min, sum, avg;

cin >> num1 >> num2 >> num3 >> 
       num4 >> num5 >> num6;

Goodluck!

Sign up to request clarification or add additional context in comments.

1 Comment

This is just an array in another form. An array is a collection of related items. This just introduces a collection of related variable names i.e. an array of names in source code. This also doesn't work well if the number of inputs ever changes. A good solution would work for one input or a million, is easy to achieve, and is actually the intent of questions like this. This solution would require a million variables named num1, .... num1000000 if a million inputs were to be read.
1

I'm putting up the basic algorithm you can use without using arrays. Assuming everything in Kelvin.

float max = 0; // Minimum Value Set for comparing with larger values
float min;
float sum = 0;
float avg = 0;
float tmp;
string number, alltheNumbers;
for( int i = 0; i < 6; i++ ){
   cin>>number;
   tmp = <float> number;
   if( tmp > max ){
      max = tmp;
   }
   sum += tmp;
   alltheNumbers += ',' + number; // Save all the numbers in comma seperated Strings
}
min = max;    // Maximum Found value set for finding minimum
std::string delimiter = ",";
size_t pos = 0;
while ((pos = alltheNumbers.find(delimiter)) != std::string::npos) {
    number = alltheNumbers.substr(0, pos); // Use the comma to retrieve all those numbers
    tmp = <float> number;
    if( tmp < min ){
      min = tmp;
    }
    alltheNumbers.erase(0, pos + delimiter.length());
}
avg = sum / 6;

So, You have the following variables with the required data.

max <- will have the maximum value
min <- will have the minimum value
avg <- will have the average value.

4 Comments

Two things: The max should be initialized to a very small value to begin with; And the min variable should be initialized to a very large value. Otherwise, think of what happens if the largest input is never larger than zero, or the smallest input is never smaller than zero.
Ya, correct. As, the question refers to saving temperatures. I could use the 0 Kelvin value for max. But for min will the boiling temperature be enough. and it would also depend on type of scale they have in there test cases.
@Joachim Does it look more convincing now?
@zion why would you suggest this? If the Op's assignment prohibits him/her from using an array he/she should just take the input as 6 different variables from there finding the sum,average,min,max etc.. is basic math. Aside from the fact that a variable of type string is technically an array of characters the above code is far more than you need to solve this issue. Op It seems like you're trying to go beyond the scope of your assignment keep it simple :)
0

An improvement on @Zion's the first answer, I think initializing the min and max to the first digit should suffice. Before the for loop input number, then set max = number and min = number and then start for loop from i=1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.