fueanta

largest integer on an array and describing this precisely

Sep 6th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. // Cpp program to find the largest integer on an array and describing this precisely.
  2. // created on June, 2016
  3. // Author: fueanta
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. int main(){
  9.     int x;
  10.     cout << "How many numbers do you want to store?\n\nInput: ";
  11.     cin >> x;
  12.     int arr[x];
  13.     cout << endl;
  14.     for (int i=0;i<x;i++){
  15.         cout << "\nElement No(" << i+1  <<") : ";
  16.         cin >> arr[i];
  17.    }
  18.     int maxima=arr[0];
  19.     for (int i=1;i<x;i++){
  20.         if (arr[i]>maxima){
  21.             maxima=arr[i];
  22.            }
  23.    }
  24.    int counta1=0,counta2=0;
  25.     //cout << "The largest value you have given is "<< maxima << endl;
  26.     for (int i=0;i<x;i++){
  27.         if (arr[i]==maxima){
  28.             counta1++;
  29.        }
  30.    }
  31.     cout << "\n\nElement No -> ";
  32.     for (int i=0;i<x;i++){
  33.         if (arr[i]==maxima){
  34.             cout <<i+1;
  35.             counta2++;
  36.             if (counta2!=counta1){
  37.                 if (counta2!=(counta1-1)){
  38.                     cout << ", ";
  39.                }
  40.                 else{
  41.                     cout << " and ";
  42.                }
  43.            }
  44.             else{
  45.                 cout << " ";
  46.            }
  47.        }
  48.    }
  49.     if (counta1==1){
  50.         cout << "has ";
  51.    }
  52.     else{
  53.         cout << "have ";
  54.    }
  55.    cout << "the largest value: " << maxima << endl;
  56.    return 0;
  57. }
Add Comment
Please, Sign In to add comment