So for my school program, I simply have to read in a listing of integers from a text file that is in the format below, with the first line indicating the amount of integers. Then sort the integers(they are unsorted in the txt file), and then find the mean. I'm stuck just trying to read in the integers into an array though from the test .txt files I've been using.
9
1
3
2
4
...
#include<stdio.h>
#include<stdlib.h>
int amtValues;
int *values;
float find_median (int* values){
if(amtValues%2==0){
return ((values[amtValues/2] + values[amtValues/2 -1]) /2.0);
}
else
return values[amtValues/2];
}
int main(int argc, char **argv){
//open the file, copy the data from the file, and determine the number of values
//open the filestream
FILE* fp = fopen(argv[1], "r");
if(!fp){
printf("error reading file\n");
return 1;
}
//determine the number of values
char *lineOne;
if(fgets(lineOne, 80, fp)!=1)
puts (lineOne);
lineOne++;
amtValues = atoi(lineOne);
printf("\nThe amount of values is: %d\n",amtValues);
/*allocate memory for values array, and copy the values from file.*/
values = (int*)malloc(amtValues*sizeof(int));
int i=0;
while(!feof(fp)){
int curNum;
fscanf(fp, "%d", &curNum);
values[i] = curNum;
i++;
}
fclose(fp);
for(i=0; i<amtValues; i++)
printf("\n%d");
}
This is what I have going at the moment, I get an error saying segmentationFault(core Dumped). I'm new to C so I'm really not sure what that means.