This code is reading in short integer values from a text file outfile.txt. I'm able to pass these values into an array and print the median. What i was hoping to do is repeat that process for the next 11 values and compare the medians. I was hoping for some help with the loop which would allow me to do this, i'm not sure what i should be incrementing and where?
At the moment all i'm doing is using printf statements to display the medians what i intend to do is compare the two and if one is so much greater than the other do something else but i'm leaving that for now. Any help at all is much appreciated, maybe a function would be better but passing an array into a function presents a whole new headache really doesn't it , here's my code so far
Code:
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
FILE *pFile;
int start=0,y,n=11,x;
short left[11],left2[11];
pFile = fopen ("outfile.txt", "r");
for (start; start<n; start++)
{
fscanf (pFile, "%d", &left[x]);
}
for(x=0; x<n; x++) /* Loop to repeat sort 11 times */
{
for(y=0; y<n-1; y++) /* Loop to sort the values */
{
if(left[y]>left[y+1]) /*Checks if 1st value is greater than 2nd */
{
short temp = left[y+1];
left[y+1] = left[y];
left[y] = temp;
}
}
}
for (start; start<n; start++) /* Repeat Process for next eleven values to compare with */
{
fscanf (pFile, "%d", &left2[x]);
}
for(x=0; x<n; x++) /* Loop to repeat sort 11 times */
{
for(y=0; y<n-1; y++) /* Loop to sort the values */
{
if(left2[y]>left2[y+1]) /*Checks if 1st value is greater than 2nd */
{
short temp = left2[y+1];
left2[y+1] = left2[y];
left2[y] = temp;
}
}
}
for (x=0; x < n; ++x )
printf ("left[%d] = %d\n", x, left[x]);
printf ("left2[%d] = %d\n", x, left2[x]);
printf("Median1 = %d\n", left[5]); /* Median is equal to middle value in array */
printf ("Median2 = %d\n", left2[5]);
}