Sarah_Xx
Sarah_Xx

Reputation: 133

how is the input stored?

i am confused about how does this code flows especially after inputtedin the set of integers. for example how will the input be stored and then compared to find the largest among the set?

#include <iostream>

using namespace std;

int main()
{
   int n, num, max, k=1;
   cout << " Enter how many integers " << endl;
   cin >> n;

   cout << " enter " << n << " integers: "; // where input be stored
   cin >> max; // this will input the last number right?
   // if i entered 50 55 60 where they will be stored dont i need to store them in in 3 seprate places
   while (k<n)
   { 
      cin >> num; // what is the function of this line? from where the input will be 
      if (num > max)
          max = num;
      k++;
   }
   cout << " largest integer is :" << max << endl;

   return 0;
}

Upvotes: 2

Views: 72

Answers (3)

nightfury1204
nightfury1204

Reputation: 4654

Problem statement of this program will be like : You are given n integers. Now you have to print the largest integer among all these integers.

  • cin >> max will take only one integer as input. max will hold the value.
  • cout << " enter " << n << " integers: "; will print this output in the console. For example, if value of n is 2, then this will print: enter 2 integers:

Look in the comment for further details:

#include <iostream>

using namespace std;

int main() {
int n, num, max, k = 1;
cout << " Enter how many integers " << endl; // print
cin >> n; // number of integer to input;

cout << " enter " << n << " integers: ";  // print how many integers to enter as input

cin >> max;  // input for 1st integer, assume it is the maximum integer

// this while loop will take input of the remaining n-1 intergers
// initially k=1, while loop will run until k is less than n
// while loop will run for n-1 times
while (k < n) {
    cin >> num;  // input for 1 integer
    if (num > max) max = num; // if this input integer 'num' is greater than 'max', then update 'max'
    k++; // increment 'k'
}

cout << " largest integer is :" << max << endl; // print the largest integer

return 0;
}

Upvotes: 0

jacob
jacob

Reputation: 4956

Let's walk through this. Let's consider the case the user selects n >= 1. (note also k = 1). We first need the user to enter one number.

cin >> max;

We say that this number is the max, we don't know if it's true or not but we make this assumption.

We then read in integers while k < n is true.

while (k < n)
{ 
    cin >> num;
    if (num > max)
        max = num;
    k++;
}

So, we read a number into num (which we declared outside the while loop). We then check if this number is greater than our assumption that the first number was the biggest, if it is we reassign max to be equal to num. We then increment k.

We do this until we have read in n integers. Resulting in max being the largest number we entered.

As for storage, we're not needing to store anything, inside the scope of the while loop we can do the check if the number is larger than max or not, if it wasn't we just discard it with the next iteration.

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234665

It doesn't store the entire set of numbers read.

It compares each new one entered to the current maximum. The initial maximum value is set to the first number read.

Upvotes: 0

Related Questions