Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

no need to make program just have a look on this program and make it error free. #include using namespace std; // Q. # 01:

no need to make program just have a look on this program and make it error free.

#include

using namespace std;

// Q. # 01: returns the maximum value occurring in the array A

// between position i and j.

int findMax(int A[], int i, int j)

{

int max = A[i];

while(i <= j)

{

if (A[i] > max)

{

max = A[i];

}

i++;

}

return max;

}

// Q. # 02: returns the position of the maximum value in the array A

// between position i and j.

int findMaxPos(int A[], int i, int j)

{

int maxPos = i;

while(i <= j)

{

if (A[i] > A[maxPos])

{

maxPos = i;

}

i++;

}

return maxPos;

}

// Q. # 03: returns the minimum value in the array A

// between position i and j.

int findMin(int A[], int i, int j)

{

int min = A[i];

while(i <= j)

{

if (A[i] < min)

{

min = A[i];

}

i++;

}

return min;

}

// Q. # 04: returns the position of the minimum value in the array A

// between position i and j.

int findMinPos(int A[], int i, int j)

{

int minPos = i;

while(i <= j)

{

if (A[i] < A[minPos])

{

minPos = i;

}

i++;

}

return minPos;

}

// Q. # 05: Swaps the elements in position i and j in the array A.

void swap(int A[], int i, int j)

{

int temp = A[i];

A[i] = A[j];

A[j] = temp;

}

// Q. # 06: shifts to the right all the elements of the array A

// starting from position i and until position j

void shiftRight(int A[], int i, int j)

{

while(j > i)

{

A[j] = A[j-1];

j--;

}

}

// Q. # 07: shifts to the left all the elements of the array A

// from position j down to position i

void shiftLeft(int A[], int i, int j)

{

while(i < j)

{

A[i] = A[i+1];

i++;

}

}

// prints the array elements

string print(int A[], int size)

{

string str = "";

for (int i = 0; i < size; i++)

{

str += to_string(A[i]) + " ";

}

return str;

}

// main function

int main()

{

// create array for testing

int A[] = { 3, 7, 9, 1, 8, 0, 2, 4, 6, 5};

// print array

cout << "Array Elements : " << print(A, 10) << endl;

// Q. #01 findMax

cout << endl << "Q# 01 - Find Max Element: ";

cout << findMax(A, 0, 9) << endl;

// Q. #02 findMaxPos

cout << endl << "Q# 02 - Find Max Position : ";

cout << findMaxPos(A, 0, 9) << endl;

// Q. #03 findMin

cout << endl << "Q# 03 - Find Min Element: ";

cout << findMin(A, 0, 9) << endl;

// Q. #04 findMinPos

cout << endl << "Q# 04 - Find Min Position : ";

cout << findMinPos(A, 0, 9) << endl;

// Q. #05 swap

cout << endl << "Q# 05 - Swap Position 2 and 5: " << endl;

cout << "Initial Array : " << print(A, 10) << endl;

swap(A, 2, 5);

cout << "Updated Array : " << print(A, 10) << endl;

// Q. #06 shiftRight

cout << endl << "Q# 06 - Shift Right " << endl;

cout << "Initial Array : " << print(A, 10) << endl;

shiftRight(A, 0, 9);

cout << "Updated Array : " << print(A, 10) << endl;

// Q. #07 shiftLeft

cout << endl << "Q# 07 - Shift Left " << endl;

cout << "Initial Array : " << print(A, 10) << endl;

shiftLeft(A, 0, 9);

cout << "Updated Array : " << print(A, 10) << endl;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions