174k views
1 vote
Write a C++ console application that outputs the largest and smallest element of an array. Your program should have three functions: a) initializeArray: this function takes an integer array and its index and initializes each element using the rand() function; b) smallestIndex: this functions takes as parameter an integer array and its size and returns the index of the first occurance of the smallest element in the array; c) largestIndex: this functions takes as parameter an integer array and its size and returns the index of the first occurance of the largest element in the array; d) outputArray: this function takes as parameters an integer array, its size, the indeces to the largest and smallest element and the elements contained at those locations in the array. Your program should not call all the functions in main.

1 Answer

3 votes

Answer:

#include<iostream>

#include<stdlib.h>

#include<time.h>

using namespace std;

//output function which print the output

void outputArray(int arr1[],int size1,int min1_Index,int max1_Index){

//display

cout<<"The minimum element is: "<<arr1[min1_Index]<<endl;

cout<<"The maximum element is: "<<arr1[max1_Index]<<endl;

}

//create the largest index calculate function

int largestIndex(int arr1[],int size1){

//initialize

int max1 = INT_MIN;

int max1_Index;

//find the min index

for(int i=0;i<size1;i++){

if(arr1[i]>max1){

max1=arr1[i];

max1_Index=i;

}

}

return max1_Index;

}

//create the smallest index calculate function

int smallestIndex(int arr1[],int size1){

//initialize

int min1 = INT_MAX;

int min1_Index;

//find the min index

for(int i=0;i<size1;i++){

if(arr1[i]<min1){

min1=arr1[i];

min1_Index=i;

}

}

return min1_Index;

}

//function for initialize the array

void initializeArray(int arr1[],int size1){

srand (time(NULL));

//store random number in the array

for(int i=0;i<size1;i++){

arr1[i] = rand()%100;

}

int min1_Index = smallestIndex(arr1,size1); //call the function

int max1_Index = largestIndex(arr1,size1); //call the function

outputArray(arr1,size1,min1_Index,max1_Index); //call the function

}

//main function

int main(){

//initialization

int arr1[20];

initializeArray(arr1,20); //call the function

return 0;

}

Step-by-step explanation:

Create the function outputArray() which takes four parameters and the return type is void it is used to print the output.

Create the function largestIndex() which takes two parameters and return type is int. it returns the integer value.

first, initialize the variable and then take a loop for traversing into the array and check for the largest element. if condition true, then update the value and also store the index. this process continues for all elements in the array.

after that, return the index.

Create the function smallestIndex() which takes two parameters and return type is int. it returns the integer value.

this is the same as the largestIndex() function just change in the check it check for the smallest element in the array.

and return the smallest index.

create the function initializeArray() which having two parameters and then initialize the array with random numbers by using the function rand() which generates the random number and srand() is used for seeding the rand() function.

and then, it calls the all above functions.

Create the main function which declares the array with size and calls the initializeArray() function with passing two arguments.

User Parikshita
by
5.6k points