Write a program that lets the user enter 10 values into an array. The program should then display the largest and smallest values stored in the array. You will loop once to read in the numbers, then another loop to find the smallest and largest number in the array and display/print the largest and smallest numbers to the screen.

Respuesta :

Answer:

The program in c language are as follows-

Program:

#include <stdio.h>

int main()

{

int a[10],i,largest_number,smallest_number;//declare the variable

for(i=0;i<10;i++)// loop to assign the value of array.

scanf("%d",&a[i]); //Statement to assign the value.

largest_number=a[0]; // assign first array value to be largest.

smallest_number=a[0]; // assign first array value to be smallest.

for(i=1;i<10;i++)// loop to check the value to smallest or highest.

{

if(a[i]>largest_number) // compare the new array value and largest value

largest_number=a[i]; // replace the value of largest_number to new largest value.

if(a[i]<smallest_number) // compare the new array value and smallest value

smallest_number=a[i]; // replace the value of largest_number to new largest value.

}

printf("largest number is %d and smallest number is %d", largest_number,smallest_number); // Print the largest and smallest value.

return 0; // return statement.

}

Output:

Firstly the user needs to give the 10 value as inputs--

1

2

0

3

4

2

7

3

9

10

Then it will print "largest number is 10 and smallest number is 0".

Explanation:

  • Firstly we declare the four variables of integer type - first array variable of size 10 named "a", Second variable for iteration named "i", Third is to store the Largest number named "largest_number" and Fourth is to store the smallest number named "smallest_number".
  • Then we declare a first "for loop" of 10 iterations to assign the value of array variables.
  • Then in the loop body, one "scanf" statement is used to take the value of an array variable.
  • Then we assign the first index value of an array to be the smallest and highest both
  • Then we declare the second loop which compares the value to be the highest or the smallest.
  • Then we print the value of the highest and the smallest with the help of the "print" statement.