I have an issue with my code but I have no idea what is wrong with it, here my code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main() {
char M[1000];//stores the sex
int age[1000];//stores the age
char np[1000];//stores whether negative or positive
char ch, cn;
int h, w, z;
float am = 0, af = 0;
char st;
int i = 0, j = 0, k = 0, l = 0;//i for number of males, j for number of females
int ip = 0, jp = 0;//ip-> number of positive males, jp->no of positive females
int young = 0, old = 0;//youngest and oldest person
FILE *covid;
covid = fopen("HW #1 Data.txt", "r");
if (covid == NULL) {
printf("Error opening file\n");
return 1;
}
while (!feof(covid)) {
ch = getc(covid);//reading sex
M[k] = ch;
ch = getc(covid);//reading space
fscanf(covid, "%d", &age[k]);//reading age to the array
ch = getc(covid);//reading space
fscanf(covid, "%d", &h);//reading height
ch = getc(covid);//reading space
fscanf(covid, "%d", &w);//reading weight
ch = getc(covid);//reading space
ch = getc(covid);//reading negative/positive
np[k] = ch;//assigns to the array
fscanf(covid, "%d", &st);//reading zip
ch = getc(covid);//reading end of line
k++;
}
young = age[0];
old = age[0];
for (l = 0; l < k; l++) {
if (M[l] == 'M') {
i++;
if (np[l] == '+') {
ip++;
am = am + age[l];
}
} else if (M[l] == 'F') {
j++;
if (np[l] == '+') {
jp++;
af = af + age[l];
}
}
if (age[l] < young) {
young = age[l];
}
if (age[l] > old) {
old = age[l];
}
}
printf("\n%d males have been tested and %d have been tested positive", i, ip);
printf("\n%d females have been tested and %d have been tested positive", j, jp);
am = (float) am / ip;
af = (float) af / jp;
printf("\nAverage age of postive men is %f", am);
printf("\nAverage age for postive female is %f", af);
printf("\noldest is %d", old);
printf("\nYoungest is %d", young);
fclose(covid);
return 0;
}
My output is:
395 males have been tested and 38 have been tested positive
355 females have been tested and 30 have been tested positive
Average age of positive men is 51.868420
Average age for positive female is 54.433334
Oldest is 6420633
Youngest is 12
The oldest is supposed to be 97. I don't know what I'm doing wrong.
The youngest had no issues and I checked the file and there is nothing wrong with everything is fine except the old variable.