?
Solved

C++ How to find the highest and lowest value of user inputs?

Posted on 2011-03-03
11
Medium Priority
?
1,202 Views
Last Modified: 2012-05-11
Hello you more experienced C++ programmers,
I am learning C++ on my own (just to keep my brain cells active); bought the C++ Programming book, and so far have been doing pretty good. The book has an exercise which I am now struggling: the program should prompt user to enter a length in feet and inches for 10 times, and display the sum of all user inputs as feet and inches. Additionally, the program should display the lowest and highest value of user inputs. I am struggling with the lowest and highest part. Code below shows how I have started the exercise, but I am not sure if  I, instead of the for loop,  should have used variables
int userFeet1, userInches1, userFeet2, userInches2  etc. until  userFeet10, userInches10 so that I can find the lowest and highest values. Any advice?
#include <iostream>

using namespace std;

int main()
{

int sumInches =0;
int feet, inches;
int lowest_value, highest_value = 0:

for(int i=1;i<=10; i++) //loop ten times
{
cout<<"Enter the lenght in feet and inches: ";
cin>>feet>>inches;

int length= feet*12 + inches; //convert to inches

sumInches = sumInches + length;//calculate total inches 

}

  feet = sumInches/12; 
  inches %= 12;

  cout <<"The sum is " << feet << " feet " << inches << " inches " <<endl; //print the sum of users inputs in feet and inches

//code for highest and lowest value........

return 0;
}

Open in new window

0
Comment
Question by:Mathilda10
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 35026851
How about, instead of storing the lengths using two values, you store them as just one value : the length in inches.

So, read the value for feet, and inches from the user, then calculate the length in inches from these two values, and use that for the rest of your calculations.

Having one value makes it easier to compare two lengths, and know which is shorter or longer.

To keep track of the shortest and longest, have two extra variables : one for the shortest, and one for the longest. For each length that the user gives, updates those two values if necessary to always have the shortest resp. longest length in them.
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 1200 total points
ID: 35026867
Hi Mathilda10,

I think you should keep the loop - just initialize the 'lowest_value' to a very large number (i.e. with VisualC++ you can use MAX_INT) and compare the last entered value somehow like this:

...
for( ... )
...
 sumInches = sumInches + length;//calculate total inches 

 if ( lenght < lowest_value )
 {
  lowest_value = length;
 }
 if ( length > highest_value )
 {
  highest_value = length;
 }
}

Open in new window


Hope that helps,

ZOPPO


PS @Infinity08: IMO what you suggested is already there ...
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 800 total points
ID: 35026872
Oh sorry : I see that you're already doing most of that. So, just focus on the last part of my post ;) For every length that the user gives, check if the lowest_value or highest_value need to be updated. There's only one special case, and that's the start : take the first length given by the user, and use it to initialize both lowest_value and highest_value.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Expert Comment

by:athomsfere
ID: 35026889
Have you covered arrays yet? I do not see one in the code.

I would say create an array, then you can either sort and pull the value from the first and last index, or I believe C++ has a method built in for highest and lowest values.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35027192
>> PS @Infinity08: IMO what you suggested is already there ...

Heh, yes. Spotted that after I posted ;)
0
 
LVL 35

Expert Comment

by:sarabande
ID: 35027321
for mimimum change to your existing code, add at end of loop:

   if (n == 1)
   {
       // initialize both lowest and highest to the first length given
       ...
   }
   else
   {
       
        if (length < lowest_value)
           ...    // set lowest to length

        // and similar for highest
        ...
   }

Sara
0
 

Author Comment

by:Mathilda10
ID: 35030730
Thank you for your postings,I followed Zoppo's advice and the code is working!  It displays the correct sum of all lenghts that  user has inputted (in feet and inches) and highest and lowest lenghts (in inches). I overlooked the exercise instruction:  the output values for highest and lowest lenghts should be displayed in feet and inches. How do I covert the inches for highest and lowest lengths back to feet and inches, so that the program displays
 Lowest lenght=  1 feet 0 inches (instead of 12 inches), and same thing for Highest length..
This is the code I have made with your help:

#include <iostream>

using namespace std;

int main()
{

int sumInches =0;
int feet, inches;
int lowest_value = 99999;
int highest_value = 0;

for(int i=1;i<=5; i++) //loop ten times
{
cout<<"Enter the lenght in feet and inches: ";
cin>>feet>>inches;
int length= feet*12 + inches; //convert to inches
sumInches = sumInches + length;//calculate total inches
  if ( length < lowest_value )
 {
  lowest_value = length;
 }
 if ( length > highest_value )
 {
  highest_value = length;
 }

}

  feet = sumInches/12;
  inches %= 12;
  cout <<"The sum is " << feet << " feet " << inches << " inches " <<endl; //print the sum of users     inputs in feet and inches

 cout <<"Lowest lenght= " << lowest_value<< " inches " <<endl;
cout <<"Highest lenght = "  << highest_value<< " inches" <<endl;

return 0;
}
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 800 total points
ID: 35031628
>> How do I covert the inches for highest and lowest lengths back to feet and inches, so that the program displays Lowest lenght=  1 feet 0 inches (instead of 12 inches), and same thing for Highest length..

The same way you did it for the total length :)
0
 

Author Comment

by:Mathilda10
ID: 35034229
Yes, of course....sometimes I stare at the coding problem (which actually is not a problem at all) and make it way too complicated.  Thanks so much.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35034233
No problem :)
0
 

Author Closing Comment

by:Mathilda10
ID: 35034245
Thank you so much for your help!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question