Friday, March 10, 2017

find index number of the largest element using class

Problem

Start with a program that allows the user to input a number of integers, and then stores them in an int array. Write a function called maxint() that goes through the array, element by element, looking for the largest one. The function should take as arguments the address of the array and the number of elements in it, and return the index number of the largest element. The program should call this function and then display the largest element and its index number.

Code

// p[4].cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<conio.h>
#include<iostream>
#include<string.h>
using namespace std;

class intgrs
{
private:
int a[5];
int i,j,z;

public:
intgrs():j(0),i(0),z(0)
{ }

void maxint()
{
cout<<"Enter Integers: ";
for(i=0; i<5; i++)
{
cin>>a[i];
}
for(i=0;i<5;i++)
{
if(a[i]>j)
{
j = a[i];
z=i;
}
}
cout<<"\n\nLargest Element = "<<j;
cout<<"\n\nIndex Number = "<<(z+1)<<endl;
}
};

int main()
{
intgrs i;

i.maxint();

cout<<"\n\nPress any key to close.";
getch();
return 0;
}

Output

find index number of the largest element using class

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact