Largest/Minor/Sum in C++ using for

Asked

Viewed 5,055 times

0

I want to make a program that reads 5 numbers and tells me the largest number, the smallest number and the sum of the numbers, in C++ using the loop for.

I’ve tried and done so:

#include <iostream>
using namespace std;

int main()
{
  int n, i, h, l, s;

  cout<<"Digite 5 numeros:\n";

  for (i=0; i<5; i++)
  {
      cin>>n;
      s+=n;

      if (n>l)
      {
          n=h;
      }
      else
      {
          n=l;
      }
  }

  cout<<"O maior numero e "<<h<<", o menor e "<<l<<" e a soma e "<<s<<".\n";

  return 0;

}
  • Use more intuitive variable names. This increases the readability of the code. Usually in this type of exercise the variable menorvalor starts receiving a very high value and maiorvalor gets -1. Then comparisons are made and if they meet the cases they are replaced.

  • 1

    There are 3 problems with your code. 1) The s variable is not being initialized, so its initial value will be junk, not what you want. 2) You should pay attention to what to do in the first loop, because the variables h and l also have no value yet, only junk, so any comparison made between them and n will not have the expected result. 3) Check better which comparisons you have to make in each loop, and what action to take in each case. You will want to update the h and l variables inside if/Else, not n, as you are doing now.

  • using namespace std;&#xA;&#xA;int main()&#xA;{&#xA; int n, i, h=0, l=0, s=0;&#xA;&#xA; cout<<"Digite 5 numeros:\n";&#xA;&#xA; for (i=0; i<5; i++)&#xA; {&#xA; cin>>n;&#xA; s+=n;&#xA; &#xA; if(i==0)&#xA; {&#xA; l=n;&#xA; }&#xA;&#xA; if (n<l)&#xA; {&#xA; l=n;&#xA; }&#xA; if (n>h)&#xA; {&#xA; h=n;&#xA; }&#xA; }&#xA;&#xA; cout<<"O maior numero e "<<h<<", o menor e "<<l<<" e a soma e "<<s<<".\n";&#xA;&#xA; return 0;&#xA;&#xA;}

  • Well, you have to check if the number typed n is greater than h (initialized as 0), if yes, you override the value of h (h=n); Qnd is the first loop (i==0), initializes the value of l equal to the number entered n; Check if the number typed n is less than l (initialized as the first number entered), if yes, you override the value of l (l=n);

  • @Jessika I got it, I did it the way you said it and it worked! Thank you!

2 answers

1

#include <iostream>
using namespace std;

int main(){
    int n, i, h, l, s = 0; // Inicializar a soma "s" em zero
    cout << "Digite 5 números:\n";

    for(i=0; i<5; i++){
        cin >> n;
        s+=n;
        if (i==0) {l=n; h=n;} // Inicializar os valores de "l" e "h" no primeiro loop
        if (n<l) { l=n; }
        if (n>h) { h=n; }
    }
}

With this code, the sum is started at zero, and the highest and lowest value is initialized with the first value, and we begin to make comparisons.

0

1 - Variables are not receiving initial value. 2 - Instead of n>l, do n>h. 3 - When n is greater than the higher current value, you say h=n, not the other way around. Try to do so:

#include <iostream>
using namespace std;

int main() {
    int n, i, high=0, low=1e10, s=0;

    cout<<"Digite 5 numeros:\n";
    for (i=0; i<5; i++)
    {
        cin>>n;
        s+=n;
        if (n>high)
            high=n;
        if (n<low)
            low=n;
    }
    cout<<"O maior numero e "<<high<<", o menor e "<<low<<" e a soma e "<<s<<".\n";

    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.