An array of 100 elements has been initialized to random numb

An array of 100 elements has been initialized to random numbers from 1 to 200. The program uses the following function with this prototype:

void two_largest(int[], int, int*, int*, int*, int*);

Set up a do/while loop which will be repeated until a -1 is entered from the user. Call the function two_largest to find the two largest elements in the array and their subscripts (ifthere are two equal largest elements, detect both - they are the solution to the problem). This function must leave the original array unchanged (the array can\'t be sorted). The function must return the largest elements and their subscripts using the pointers in the parameter list. Display the largest elements with subscripts in the main.

Solution

C++ program to get the two largest numbers in an array with index

#include<iostream>
using namespace std;
void two_largest(int [],int,int*,int*,int*,int*); // function prototype
int main()
{
   int array[100],count = 0,Large1,Large2,index1,index2; // variable decleration
   cout << \"Enter numbers from 1 to 200, Maximum 100 numbers\" << endl; // instruct the user to enter numbers
   cout << \"To stop entering enter -1\" << endl;
   do // do while loop to get numbers from user
   {
       cin >> array[count];// taking input
       count ++;
   }while(count < 100 && array[count-1] != -1); // loop til user enters -1 or count reach 100
   two_largest(array,count,&Large1,&Large2,&index1,&index2);// function calling to get the largest numbers
   cout << \"The First largest number\" << Large1 << \" is at position \"<< index1+1<< endl;// output the first largest number
   cout << \"The Second largest number\" << Large2 << \" is at position \"<< index2+1<< endl;// output the second largest number
   return 0;
}// end of main code
// function definition
void two_largest(int arr[],int c,int* L1,int* L2,int* Ind1,int* Ind2)
{
   *L1 = 0;
   *L2 = 0;
   for(int i = 0; i<c; i++)// loop to check each element
   {
       if(arr[i]> *L1)// checking the array element is larger than L1
       {
          *L1 = arr[i];// if yes update l1 with array element
          *Ind1 = i;// and change index
       }
       else if(arr[i] > *L2)// if the array element is larger than L2
       {
          *L2 = arr[i];// update L2
          *Ind2 = i;// and index
       }
   }
}


OUTPUT

Enter numbers from 1 to 200, Maximum 100 numbers
To stop entering enter -1
100
45
3
46
7
55
8
100
101
34
3
101
45
4
-1
The First largest number101 is at position 9
The Second largest number101 is at position 12

An array of 100 elements has been initialized to random numbers from 1 to 200. The program uses the following function with this prototype: void two_largest(int
An array of 100 elements has been initialized to random numbers from 1 to 200. The program uses the following function with this prototype: void two_largest(int

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site