I\'m familiar with C, and not with the C++ header, cpp, and driver as wierd as t
ID: 3659373 • Letter: I
Question
I'm familiar with C, and not with the C++ header, cpp, and driver as wierd as that sounds - PROBLEM 3 - create a test file called bio.txt - the file will have the following format: - lastname age - lastname age - ... - read this file and create two arrays - one array should contain the last name of all seniors (age >= 65) - the other array should contain the last name of all children (age < 18) - print out both arrays - example names: - jones 84 - michaels 16 - fredrickson 22 - mitchell 7 - franklin 45 - johnson 77 - the result would be: - seniors: - jones - johnson - children: - michaels - mitchell Thank you,Explanation / Answer
include <stdio.h>
#include <string.h>
#define MAX 100
#define NUM 10
int main(){
int i, cnt, ageInt, junCnt, senCnt;
FILE *fp;
char line[MAX], seg1[NUM][MAX], seniors[NUM][MAX], juniors[NUM][MAX];
char *seg, *name, *age;
fp = fopen("bio.txt","r");
if(fgets(line, MAX, fp) != NULL){
seg = strtok(line, "-");
cnt = 0; junCnt = 0; senCnt = 0;
while(seg != NULL){
strcpy(seg1[cnt],seg);
seg = strtok(NULL,"-");
cnt++;
}
for(i = 0; i < cnt; i++){
name = strtok(seg1[i], " ");
age = strtok(NULL, " ");
ageInt = atoi(age);
if(ageInt >= 65){
strcpy(seniors[senCnt],name);
senCnt++;
}
else if(ageInt < 18){
strcpy(juniors[junCnt],name);
junCnt++;
}
}
printf("seniors:");
for(i = 0; i < senCnt; i++)
printf("- %s ",seniors[i]);
printf("- children:");
for(i = 0; i < junCnt; i++)
printf("- %s ",juniors[i]);
printf(" ");
}
fclose(fp);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.