1
#include<iostream>
main()
{
    int limit,input;
    int sum=0;
    int i;
    std::cout<<"Please Enter the limit: ";
    std::cin>>limit;
    for(i=0;i<limit;i++)
    {
        std::cout<<"Please Input values: ";
        std::cin>>input;
        sum+=input;
    }
    std::cout<<"The sum of the values is: "<<sum;
    std::cout << std::endl;
    std::cout<<"The average of the values is: "<<sum/1+i;


}

What to do if i want to find Max and Min Values from the values input by the user?

eerorika
  • 232,697
  • 12
  • 197
  • 326

5 Answers5

5

Try this

int min_v = std::numeric_limits<int>::max();
int max_v = std::numeric_limits<int>::min();

for(i=0;i<limit;i++)
{
    std::cout<<"Please Input values: ";
    std::cin>>input;
    sum+=input;
    min_v = std::min(input, min_v);
    max_v = std::max(input, max_v);
}
Chen OT
  • 3,486
  • 2
  • 24
  • 46
1

There are multiple ways, You can store all values input from user into a std::vector<int>. Use this vector to find out sum of elements and then sort it using std::sort to get Max and Min values as well. Simple!

vector<int> v(limits);
for(int i = 0; i<limits; i++)
{
    int input;
    cin >> input;

    v.push_back(input);
}

int sum = 0;
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
    sum += *it;
}

cout << "sum = " << sum << endl;


sort(v.begin(), v.end());

cout << "Min = " << v[0] << endl;
cout << "Max = " << v[v.size() -1] << endl;
Pravar Jawalekar
  • 605
  • 1
  • 6
  • 18
0

Generally you need to do the following:

  1. Declare the variable to store Max and Min values.
  2. Initialize Min with a very big value, e.g. 2147483647, and Max to a very small value, e.g. -2147483648.
  3. Inside the loop, compare input with Min, if input is smaller than Min, update Min. Do the reverse with Max.

That's it.

rcs
  • 6,713
  • 12
  • 53
  • 75
0

Please try the code below.

int main()
{
    int limit,input;
    int sum=0;
    int i;
    std::cout<<"Please Enter the limit: ";
    std::cin>>limit;
    int min = 0;
    int max = 0;
    for(i=0;i<limit;i++)
    {
        std::cout<<"Please Input values: ";
        std::cin>>input;

        if(i == 0)
        {
            min = input;
            max = input;
        }
        else
        {
            if(input < min)
                min = input;

            if(input > max)
                max = input;
        }
        sum+=input;
    }
    std::cout<<"The sum of the values is: "<<sum<<std::endl;
    std::cout<<"The average of the values is: "<<sum/(1+i)<<std::endl;
    std::cout<<"Max: "<<max<<std::endl;
    std::cout<<"Min: "<<min<<std::endl;

    return 0;
}
ldlchina
  • 927
  • 2
  • 12
  • 31
  • You Guys are amazing!!!! thank you so much for teaching me a new skill!!! and thank you so much for making me thing the logic behind it!!! Also can you guys give me tips on how to become a better programmer? – bc150200022 Soniya Shah Noor Dec 01 '15 at 09:04
  • Just practice and practice. And some C++ books for your reference: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – ldlchina Dec 01 '15 at 09:14
0
int max=0, min=0;
for(i=0;i<limit;i++)
    {
        std::cout<<"Please Input values: ";
        std::cin>>input;
        sum+=input;

        if(i==0){
        max=input;
        min=input;
        }

        if(input>max)
          max=input;

        if(input<min)
          min=input;
    }

Now, you got the max and min value.
bivrantoshakil
  • 421
  • 1
  • 5
  • 10