0

i am currently having problem getting largest values from an array, here is my code:

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


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

    }
    max = a[0];
    for(int i=0;i<10;i++)
        {
        if(a[i]>max){
            max=a[i];
            j=i;
        }
    }
    secondbig=a[10-j-1];
    for(int i=0;i<10;i++)
        {
        if(secondbig <a[i] && j != i)
          secondbig =a[i];
    }
    cout<<max<<"\n"<<secondbig;
    return 0;
}

What i want to do is to first get maximum value from an array and then leave one array value and then get second largest value and same for third largest value, for example :

200
100
50
300
400
500
600
700
800
900

If in the above test values 900 is the largest value then the subsequent second and third largest value should be 700 and 500, is there anyway to do that?

user2891869
  • 569
  • 1
  • 4
  • 12
  • 1
    Is array sorted ? If not simplest would be sort it and then get the values . – ameyCU Sep 12 '15 at 03:46
  • Why do you do not sort? And you can take n-th largest values....? – dennitorf Sep 12 '15 at 03:46
  • 1
    possible duplicate of [Which is the fastest algorthm for selecting kth largest number in an unsorted array containing non -unique elements?](http://stackoverflow.com/questions/11412023/which-is-the-fastest-algorthm-for-selecting-kth-largest-number-in-an-unsorted-ar) – NathanOliver Sep 12 '15 at 03:48

4 Answers4

0

maybe you can sort the array

int *pbeg = begin(a);
int *pend = end(a);
sort(pbeg, pend);

this will sort all elements in the range[pbeg, pend) with operator <, or sort all elements by using the binary predicate

Ti Lami
  • 9
  • 2
0

Given your original code, I presume this is in the style of what you were attempting. it will retrun the three highest vaule. Notice that you only needed to create a loop to repeat the inital computation. You want the max remaining value less than the prior max value.

#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;
}
Michael Cresta
  • 337
  • 1
  • 14
0

Assuming that you're happy to get the values in place, I'd use a vector.

std::sort(std::begin(a), std::end(a), std::greater<int>());

then print or copy the first n elements of the array (assuming n is no more than your array size, 10). They will be in reverse order, but that can be easily corrected.

Peter
  • 35,646
  • 4
  • 32
  • 74
0

As per your follow on question, If you want to list all the max values, change the j<3 to j<10. If you want the numeric sum of the max values that you have extracted from the array, you need to add one more variable:

int a[10], total, max, prior_max = numeric_limits<int>::max();

then within the outer loop, place at the end of the inner loop this statement:

total += max;

Then after the execution of the loops are complete, you can print out the total or otherwise use it. as in:

cout << 'Total: ' << total << endl;
Michael Cresta
  • 337
  • 1
  • 14
  • thanks but why it gives 900, 800 and 700 but i want 900, 800 and 500 as output? – user2891869 Sep 13 '15 at 13:50
  • The specs call for the top highest 3 values to be the output in declining value. Your sample data is: 200, 100, 50, 300, 400, 500, 600, 700, 800, 900. The output based on the specs is 900, 800, 700; as they are the highest 3 values in the sample population; that is what is being delivered. You state that you would like the output, to be 900, 800, and 500; that statement is inconsistant with the combination of the specs and the sample data. – Michael Cresta Sep 13 '15 at 15:14
  • Actually even your statement in your comment stating that you want the output to be 900, 800 and 500 is inconsistant with your first statement of expected output of 900, 700, 500. Which is the correct desired output, your first version of the expected output or your second version, or what the specs call for? If it is the spec, it has been delivered. If it is one of the statements of expected output, then which one? – Michael Cresta Sep 13 '15 at 15:50
  • Will you be continuing this line of questions, here or in the parallel question thread that you are also running on this topic? http://stackoverflow.com/questions/32535874/how-to-get-sum-of-all-values-a-variable-holds-after-for-loop/32557048#32557048 – Michael Cresta Sep 14 '15 at 03:55