# how do i get the largest, smallest, first and last values??

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-22-2003
Nikisha
how do i get the largest, smallest, first and last values??
how do i write a code to get the largest, smallest, first and last entries from a series of number that the user inputs ( the series of number is large- about 50 numbers)
• 03-22-2003
Hammer
As you read the numbers in, keep a record of the highest/lowest values. The first and last are pretty easy to spot, the first one will be the one the user enters first, and the last one will be...

No need to store all the numbers unless you want to. Just create a few variables like these:

int first, last, highest, lowest.
• 03-22-2003
Nikisha
:confused:

well i guess you're basically re- stating my question...how do i code largest, smallest, first and last entries??
im really a new programmer!:rolleyes:
• 03-22-2003
Hammer
Post some code, so we can see how far you've got, and then we'll be in a better position to help you.

For example, can you get a number from the user ?
• 03-22-2003
Nikisha
well heres the qustion:
write a program tht accepts series of numbers form the user, stopping at the first zero or negative number (n <= 0) entered. Echo-print the numbers as they are entered (or print them if you re-direct input from a file). Do not processs the non-positve number entered to stop the sequence at all. After the non-postive number entry, print the count of entries, the sum of
entries, the largest entry, the smallest entry, the first entry, the last entry, and the (floating-point) average of the entries.

well the thing is i don't know how to code the sum of
entries, the largest entry, the smallest entry, the first entry, and the last entry

the user has to enter about 50 integers...

can u help me now?:)
• 03-22-2003
Hammer
Code:

```#include <iostream> int main ( void ) {   int i,       highest,       lowest,       first,       last;     if (cin>>first)   {     highest = lowest = last = first;     while (cin>>i)     {       // More code needed here.       // Simply test the current entry with the highest.  If it is bigger,       // make the highest entry the same value as the current entry.       // Then do the same kind of thing for the lowest entry.     }   } }```
There's a starter for you.
• 03-22-2003
Nikisha
Thank You!!:)

well i have another quetion
how do i make the user enter the integer soo many times??
• 03-22-2003
RoD
a for loop. Say u want 5 values:

Code:

```for (int i = 1; i <= 5; i++) { cout << .... cin >> ... }```
• 03-22-2003
Hammer
Quote:

Originally posted by Nikisha
how do i make the user enter the integer soo many times??
--> while (cin>>i)
This loops until a bad read occurs, so if the user keeps entering, the prog will keep running. There are many ways to control a loop though. You could also add a second condition to this while statement, like loop while i is greater than 0.
• 03-23-2003
Nikisha
well here's my program so far:

Code:

```#include <iostream> using namespace std; int main() {         int n, sum, num, last;                 cout << "Enter count of entries: ";         cin  >> n;                 sum = 0;         for (int i = 0; i <= n; i++)         {                 cout << "enter an integer: ";                 cin >> num;                        sum = sum + num;         }                 cout << "Sum of integers = " << sum << endl;         last = num;         cout << "The last number is " << last << ' ';                         return 0; }```
so how do i fit in the highest, lowest and first values into this?
• 03-23-2003
funkydude9
Quote:

Originally posted by Nikisha
well here's my program so far:

Code:

```#include <iostream> using namespace std; int main() {         int n, sum, num, last;                 cout << "Enter count of entries: ";         cin  >> n;                 sum = 0;         for (int i = 0; i <= n; i++)         {                 cout << "enter an integer: ";                 cin >> num;                        sum = sum + num;         }                 cout << "Sum of integers = " << sum << endl;         last = num;         cout << "The last number is " << last << ' ';                         return 0; }```
so how do i fit in the highest, lowest and first values into this?

Well, getting the first number is easy. Look at where the user enters the first number. Right after that, you'll want to add the code
Code:

`first = num.`
But here's the problem: because of the loop, first is always getting num, every time the user enters the next number.

So how do we get around this? Well, you could do one of two things. You can either add a if statement in the loop, and checking that
Code:

```if (i == 0) { //then first gets num }```
OR you could simply place one line of cin>>num before the loop, but then you have to make some change to the loop accordingly.

The first time the user enters a number, your going to treat that number like it's the highest and the lowest (cause you don't know yet), so your going to set your two veriables, highest and lowest, to that first number. (if (i == 0)) Then when the user enters the next number, you need to re-set highest and lowest to the second number ONLY IF num > highest and the same goes with lowest. Get it?
• 03-23-2003
Nikisha
thnx ..it works!!

but then wht bout the highest n lowest values??:confused:
• 03-23-2003
-KEN-
Pretty simply...

For high:
First read in the very first value and place it in the variable high. Now, as you read in the user's input, constantly test if the value currently in high is larger than the value being inputted.

Code:

```if(high < num)     high = num;```
So your loop might look something like:

Code:

```for (int i = 0; i <= n; i++) {     cout << "enter an integer: ";     cin >> num;     sum = sum + num;     if(high < num)         high = num;     if (i == 0)         high = first = num; }```
use the same idea for lowest values
• 03-23-2003
funkydude9
I edited my post.
• 03-23-2003
Nikisha
well i seen to get the hang of the highest, first and last values but i still cant figure out the lowest value
plus i just realized tht the program should calculate the numer of counts...i made a mistake by asking the user to enter the counts.
can neone help me figure this out?:(

heres my program so far:
Code:

```// This program prompts the user for a series of numbers and then displays the // count of entries, sum of entries, largest entry, smallest entry, first entry and // the last entry #include <iostream> using namespace std; int main() {         int n, sum, num, last, count, first, high, low;         float avg;                 cout << "Enter number of entries: ";         cin  >> n;         sum = 0;         for (int i = 0; i < n; i++)         {                 cout << "enter an integer: ";                 cin >> num;                 if (i == 0)                 {                         first = num;                 }                 sum = sum + num;                 if (high < num)                 {                         high = num;                 }                 if (num < low)                 {                         low = num;                 }         }         cout << endl;         last = num;                count = n;         avg = sum / count;         cout << "The first number is " << first << endl;         cout << endl;         cout << "The last number is " << last << endl;         cout << endl;         cout << "The highest number is " << high << endl;         cout << endl;         cout << "The lowest number is " << low << endl;         cout << endl;         cout << "Your count of entries was " << count << endl;         cout << endl;         cout << "Sum of integers = " << sum << endl;         cout << endl;         cout << "Average = " << avg << endl;         cout << endl;         return 0; }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last