-3

i am currently having problem getting sum of all values a variable holds, here is my code i want to print the sum of max variable as it outputs 3 values at the end but i want to print the sum of those 3 values.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <limits>
using namespace std;


int main()
{
    int a[10], max, prior_max = numeric_limits<int>::max();
    for(int i=0;i<10;i++)
        {
        cin>>a[i];

    }
  for( int j = 0; j<3; j++){
    max = numeric_limits<int>::min();
    for(int i=0;i<10;i++)
        {
        if(a[i]>max && a[i]<prior_max){
            max=a[i];
        }
    }
    cout << max << endl;
    prior_max = max;

  }
  return 0;
}
user2891869
  • 569
  • 1
  • 4
  • 12
  • There is an elegant solution of this problem using priority queue built on heap data structure. – alexm Sep 12 '15 at 06:36
  • 1
    You are not honestly asking, how to calculate the sum of 3 instances of a variable, are you? – MikeMB Sep 12 '15 at 06:43
  • This issue is a parallel issue posted in this question by this member. The initial code of this question is the code that I provided as the answer for a prior part of the initial question -- How to get largest value after leaving one array value?. Then this member asked the follow on question as a comment to my answer, where the member asked about how to compute the total. As I did not answer withing an hour of his asking the follow on question, it was posted here. http://stackoverflow.com/questions/32534940/how-to-get-largest-value-after-leaving-one-array-value – Michael Cresta Sep 14 '15 at 16:43

1 Answers1

0
int main()
{
    int a[10], max, prior_max = numeric_limits<int>::max();
    for (int i = 0; i<10; i++) {
        cin >> a[i];
    }

    long int sum = 0;   // <------------------------------------

    for (int j = 0; j<3; j++) 
    {
        max = numeric_limits<int>::min();
        for (int i = 0; i < 10; i++) 
        {
            if (a[i] > max && a[i] < prior_max) 
            {
                max = a[i];
            }
        }
        cout << "max = " << max << endl;
        prior_max = max;
        sum += max;  //  <----------------------------------------
    }       
    cout << "sum = " << sum << endl;   //    <--------------------------------
}
MikeMB
  • 20,029
  • 9
  • 57
  • 102
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • it doesn't prints the sum, why? – user2891869 Sep 12 '15 at 06:42
  • Aren't you forgetting the most important thing: `sum+=max`? ;) – MikeMB Sep 12 '15 at 06:44
  • The best thing to do is to use a debugger. Step through the program and watch as the values change. You will soon sport the problem. Btw, Mike is correct, I made a typo. As it stands `sum == max;` does nothing (although sum should still be printed as 0). It should be `sum += max;` I will update the answer. BUt do learn to use the debugger. It is your best friend – Mawg says reinstate Monica Sep 12 '15 at 06:47
  • Seriously? Now sum will be `3*numeric_limits::min();` – MikeMB Sep 12 '15 at 06:51
  • 1
    Then I guess that I didn't understand the question. If you do, please help him. Or maybe he will learn to use the debugger. I'm off to bed now ... yaaaaaaaaaawnz. Nite nite :-) – Mawg says reinstate Monica Sep 12 '15 at 07:08
  • 1
    Done - you just put the summation into the wrong line – MikeMB Sep 12 '15 at 07:13