I need help craeting a program in C that can sort these numbers below using arra
ID: 3928135 • Letter: I
Question
I need help craeting a program in C that can sort these numbers below using array. I also need it to find the largest, smallest, and the average of the numbers. An example out put that does not include there numbers is:
The original numbers are: -391.03 833.90 -987.66 -659.06 159.69 -732.98 675.00 947.41 -13.39 -330.47 -740.21 -389.48
The sorted numbers are: -987.66 -740.21 -732.98 -659.06 -391.03 -389.48 -330.47 -13.39 159.69 675.00 833.90 947.41
The largest number is 947.41, the smallest is -987.66, and the average is -135.69.
Using these float numbers:
-292.92, 809.92, 296.47, -866.93, -211.09, -448.18, -515.57, 994.76, -551.41, 135.89, 296.65, 648.94, 642.92, 508.61, -996.24, -18.25, -471.63, 404.86, -605.35, 877.32, -714.09, -220.99, -850.70, -585.50, -862.41, -881.97, 832.74, 753.35, -711.53, -273.35, -162.94, -115.49, -329.94, -4.45, 807.46, 404.42, 940.82, 458.97, -181.73, -708.12, -632.44, 660.30, 865.75, -885.09, 17.72, 281.37, 168.90, 140.41, 248.95, -183.02, -313.78, 563.56, 859.76, -194.25, -133.12, -164.48, -144.98, 997.35, -76.22, -300.38, -563.86, -585.62, 994.89, 760.84, 365.12, 106.20, 409.94, 802.34, 614.01, -423.18, 836.06, -771.80, -886.91, 252.46, -762.88, -27.31, -759.54, 130.81, -466.18, 406.02, 326.46, -239.70, 947.79, 220.05 ,-214.82, 186.22, 566.05, -185.33, 521.55 ,-88.91, -545.55, -510.18
Explanation / Answer
YOu can enter the required numbers in the program and execute it. The program is as follows:
/*
* C program to accept N numbers and arrange them in an ascending order
*/
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j, n;
float a,number[100],sum =0,avg;
// N is the total no of numbers
printf("Enter the value of N ");
scanf("%d", &n);
printf("Enter the numbers ");
//applying loop to accept the numbers
for (i = 0; i < n; ++i)
scanf("%f", &number[i]);
//loop to sort the number in ascending order
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order are given below ");
for (i = 0; i < n; ++i)
{
//calculating sum
sum=sum+number[i];
printf("%f ", number[i]);
}
avg=sum/n;
printf("The smallest no is %f ",number[0]);
printf("The largest no is %f ",number[n-1]);
printf("The avg of the numbers is %f",avg);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.