I am creating a simple program, for the entry of an array, which will then sort the array into ascending order, printing out the sorted array, its min, max and median. I have started the code but after many attempts it seems to being going futher backwards each time i change anything.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
main()
{
int a[100];
int i,j, value, n;
int min = 100;
int max = 0;
float median;
// Upto 100 items can be created in a[i]
for(i=0; i<100; i++)
{
// Input of the numbers into array a[i]
scanf("%d", &a[i]);
n = i;
if(a[i]<100)
{
// Terminates input and performs the sort; prints the min, max and median values
if(a[i] == -42)
{
printf("%s", "Sorted List: ");
// Simple insertion sort
for(i = 1; i <100; i++)
{
value = a[i];
for (j = i - 1; j >= 0 && a[j] > value; j--)
{
a[j + 1] = a[j];
}
a[j + 1] = value;
}
}
// Find the min of array
if(a[i] < min)
{
min = a[i];
}
else
{
min = min;
}
// Find the max of array
if(a[i] > max)
{
max = a[i];
}
else
{
max = max;
}
//Find the median of array
//Prints the sorted array
for(i=0; i<n; i++)
{
printf("%d ", a[i]);
}
printf("\n");
printf("%s %d\n ", "Min: ", min);
printf("%s %d\n ", "Max: ", max);
printf("%s %d\n ", "Median: ", median);
}
}
//Error if the number entered is above 100
if(a[i]>100)
{
printf("Number must be between 0 and 100");
a[i] = 0;
}
}
The functions are meant to be performed when -42 is entered without that also being added to the array.
Your help would be greatly appriciated, i am eager to learn how to accomplish these things.