# Thread: Calculations on an entered array

1. 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.

2. Originally Posted by MV1
The functions are meant to be performed when -42 is entered without that also being added to the array.
put a condition to :
Code:
`scanf("%d", &a[i]);`

3. Sit back for a minute and think about the logic of the program and perhaps write a flowchart.
Then you need to fix the indentation with this piece of code:
Code:
```				// 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;
}
}```
Finally, make main return int, not nothing.

4. Apoloigies for the code not be indented correctly hope that is sorted now, the flow of the program seems to be a major issue although im notsure the code is up to scratch either.

As for the condition i thought; if(a[i] == -42) was working without including into the array in one of my other versions but i dont seem to be able to do it again.

5. MV1 - input, output, and computations or processing (sorting in your case), should *not* be mixed into the same function.

In general:

Get your input - print it (or a part of it), on the screen, so you know you've got the input right.

Call your processing function(s) - check each one that it's doing what you want,

Then output your reports to files, and check those.

I'll be back

6. Been away and looked at it and had some advice, its pretty much done.
Code:
```#include <stdio.h>
#include <stdlib.h>

#define N 100

int main()
{
int a[N] = {0};
int i, j, value, n;

// Upto 100 items can be created in a[i]
scanf("%d", &a[0]);
for(i = 1; i < N && a[i-1] < 100 && a[i-1] != -42; i++)

// Input of the numbers into array a[i]
scanf("%d", &a[i]);
n = i;

if(a[n-1] > 100)
{
printf("Number must be between 0 and 100");
exit(1);
}

if (a[n-1] == -42)
{
a[n-1] = 0;
n--;
}

printf("Sorted List: ");

// Simple insertion sort
for(i = 1; i < n; i++)
{
value = a[i];
for (j = i - 1; j >= 0 && a[j] > value; j--)
a[j + 1] = a[j];
a[j + 1] = value;
}

//Prints the sorted array
for(i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
printf("Min: %d\n", a[0]);
printf("Max: %d\n", a[n-1]);
printf("Median: %f", n % 2 ? a[n/2] : 0.5*(a[n/2] + a[n/2-1]));
}```

7. OK.

I'll edit down the above post of mine.

8. Code:
```	for(i = 1; i < N && a[i-1] < 100 && a[i-1] != -42; i++)

//...

for(i = 0; i < n; i++)```
These two looks suspect, because they have no starting or ending brace. And your indentation is failing, so it's difficult to see if it's supposed to be a one-line for loop or not.
And the condition in the first for might just be a little too difficult.

Popular pages Recent additions