Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**********This is the 4th lab in a beginning programming class so make sure not

ID: 1814861 • Letter: #

Question

**********This is the 4th lab in a beginning programming class so make sure not to use any complicated methods (focus on using arrays), also use comments on the sides with "/*" telling what is happening***********

Array Manipulation

Step 1: Initialization and display

Write a program as follows:

Declare an array of size 5.

Read 5 integers from the user and store them in the array.

Using a while loop, print the content of the array.

Verify that all the elements entered by the user are printed correctly.

Modify your program so the array has size 12. How many changes did you have to apply to your

program?

Verify again that your program prints all the array content correctly.

Now replace the while loop that prints the array with a for loop.

Test and fix if you have any errors.

Switch the size of the array back to 5.

Update your program so the content is initialized using random numbers between 0 and 100 instead of

asking the user for input.

Try to print elements 0 through 7. Does your program compile?

Step 2: Min, Max, Average

Compute the smallest element in the array, the largest element and the average of all elements.

Print these values with a precision of 2 digits. Verify that your results are correct.

Update your program so it prints the position of each of the previous values such as:

The largest element is 11 and is at index 3

The smallest element is 2 and is at index 4

The average is 4.53

Step 3: Shuffling

Add one or more statements to swap the smallest element with the element at position 0 (i.e, put the

smallest element at position 0 and the element that was at position 0 in place of the smallest element).

Print your array.

Add one or more statements to swap the second smallest element with the element at position 1. Note

that the smallest element is now at position 0, so the second smallest element is the smallest of

elements at positions 1 to 4. Print your array.

Repeat by swapping the third smallest element with the element at position 2. Print your array.

What are these three blocks of code doing?

Replace the three blocks you added by nested loops.

Update your loops so the shuffling is executed for all elements.

Clean up any extra print statements in your code so your program prints the array once after

initialization, then prints the min, max, and average information, then the array at different steps of

shuffling. When printing the content of the array, use at least 3 places for each integer to the elements

are right aligned in every step as the example below:

6 12 5 97 1

1 12 5 97 6

1 5 12 97 6

1 5 6 97 12

1 5 6 12 97

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

int main()

{

printf("STEP-1: ");

int i;

int A[5]; /*declaration of array of size 5*/

for(i=0;i<5;i++) /*scanniing 5 elements and storing in array A*/

scanf("%d",&A[i]);

i=5; /*printing using a while loop*/

while(i>0)

{

printf("%d ",A[i]);

i--;

}

printf(" Number of changes = 3 ");


for(i=0;i<5;i++) /*randomly generating 5 numbers and storing in array A*/

A[i] = rand()%101;


return 0;

}