x.Hm Im working this program by parts and I could really use someone help. Ive b
ID: 3618942 • Letter: X
Question
x.Hm Im working this program by parts and I could really use someone help. Ive been tryin to figure out the how to delete a line that was put into a string from a file line because it has invalid data in it. Let me show you a input file so you can see what i mean: sample input:Gates, William ,H, 1.89 Jobs,Steven,P,2.56 Brin , Sergey , , 3.89 Page, Lawrence ,, 3.77 Buffett, Warren, E, 3.34 Short, Sue, , 3.11 Cisneros, Juan, G, 3.67 Andrew, T, 5.67
Do you see where it says andrew, T, 5.67 ? well at scan time I would like to ignore that line and continue to the next one. After I have done so, my program will produce the folllowing output file: output file: Highest gpa: 3.89 Average gpa: 3.18 (only 7 numbers were inputted since one of them were delete it) lowest gpa: 1.89
William H. Gates 1.89Steven P. Jobs 2.56Sergey Brin 3.89Lawrence Page 3.77Warren E. Buffett 3.34Sue Short 3.11Juan G. Cisneros 3.67Thats what I would like it to do but I get this: my outoutfile: Highest gpa: 5.67 Average gpa: 3.49 Lowest gpa: 1.89
William H. Gates 1.89 Steven P. Jobs 2.56 Sergey . Brin 3.89 Lawrence . Page 3.77 Warren E. Buffett 3.34 Sue . Short 3.11 Juan G. Cisneros 3.67 T . Andrew 5.67
Any suggestions?? THis is my code #include <stdio.h> #include <string.h> #include <stdlib.h> #define TEN 10 #define THREE 3 #define TWENTY 20 #define FIFTY 50 #define ONE_THOU 1000
typedef struct { char first_name[TWENTY]; char middle_name[TWENTY]; char last_name[TWENTY]; double users_gpa; } format_of_file;
int main () { /* Declarations: File I/O */ FILE *inp, *outp; double sum,place_temp,min_value,max_value,average_value; char place_holder[TWENTY],first_file[FIFTY],second_file[FIFTY],str[ONE_THOU],*ptr; format_of_file organized[ONE_THOU]; int i,j, count,f; /* Get users inputs */ printf("Enter the input file name: "); scanf("%s", first_file); printf("Enter the output file name: "); scanf("%s", second_file);
inp = fopen(first_file, "r"); outp = fopen(second_file, "w");
/* Test if both files for validity */ if (inp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); } if (outp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); }
/* Begin Program execution and get each name/gpa until , and then continue */ count = 0; while (fgets(str,sizeof(str),inp) != NULL) { /* Get last name */ if ((ptr = strtok(str,", ")) != NULL) { strcpy(organized[count].last_name,ptr); } else { organized[count].last_name[0]=''; } /* Get first name */ if ((ptr = strtok(NULL,", ")) != NULL) { strcpy(organized[count].first_name,ptr); } else { organized[count].first_name[0]=''; } /* Get Middle or gpa */ if ((ptr = strtok(NULL,", ")) != NULL ) { /* Checks to see if it is a middle name or double value */ strcpy(place_holder,ptr); place_temp = atof(place_holder); if(place_temp == 0) { strcpy(organized[count].middle_name,ptr); } else { /* Convert string to double */ organized[count].users_gpa = atof(ptr); organized[count].middle_name[0] = ''; } } else { organized[count].middle_name[0]=''; } /* Get gpa */ if ((ptr = strtok(NULL,", ")) != NULL) { /* Convert string to double */ organized[count].users_gpa = atof(ptr); } count++; /* Check to see if the size of the file is under a thousand */ if (count > ONE_THOU) { printf("Error with file size "); return 0; } } /* Close first File */ fclose(inp); /* Find the highest/lowest value/sum and Initialize */ max_value = organized[0].users_gpa; min_value = organized[0].users_gpa; sum = 0; for (i=0;i < count;i++) { if (max_value < organized[i].users_gpa) { max_value = organized[i].users_gpa; } if (min_value > organized[i].users_gpa) { min_value = organized[i].users_gpa; } sum = sum + organized[i].users_gpa; } /* Find the average value */ average_value = (sum/count); printf("%d ", count); /* Display Results */ fprintf(outp,"Highest gpa: %.2f ", max_value); fprintf(outp,"Average gpa: %.2f ", average_value); fprintf(outp,"Lowest gpa: %.2f ", min_value); f = 0; for (i=0;i < count;i++) { for (j=0;j < sizeof(organized[i].first_name);j++) { if ((organized[i].first_name[j] != '') && (organized[i].first_name[j] != ' ')) { fprintf(outp,"%c", organized[i].first_name[j]); } } place_holder[0]=''; fprintf(outp,"%s ", place_holder); for (j=0;j < sizeof(organized[i].middle_name);j++) { if ((organized[i].middle_name[j] != '') && (organized[i].middle_name[j] != ' ')) sample input:
Gates, William ,H, 1.89 Jobs,Steven,P,2.56 Brin , Sergey , , 3.89 Page, Lawrence ,, 3.77 Buffett, Warren, E, 3.34 Short, Sue, , 3.11 Cisneros, Juan, G, 3.67 Andrew, T, 5.67
Do you see where it says andrew, T, 5.67 ? well at scan time I would like to ignore that line and continue to the next one. After I have done so, my program will produce the folllowing output file: output file: Highest gpa: 3.89 Average gpa: 3.18 (only 7 numbers were inputted since one of them were delete it) lowest gpa: 1.89
William H. Gates 1.89Steven P. Jobs 2.56Sergey Brin 3.89Lawrence Page 3.77Warren E. Buffett 3.34Sue Short 3.11Juan G. Cisneros 3.67Thats what I would like it to do but I get this: my outoutfile: Highest gpa: 5.67 Average gpa: 3.49 Lowest gpa: 1.89
William H. Gates 1.89 Steven P. Jobs 2.56 Sergey . Brin 3.89 Lawrence . Page 3.77 Warren E. Buffett 3.34 Sue . Short 3.11 Juan G. Cisneros 3.67 T . Andrew 5.67
Any suggestions?? THis is my code #include <stdio.h> #include <string.h> #include <stdlib.h> #define TEN 10 #define THREE 3 #define TWENTY 20 #define FIFTY 50 #define ONE_THOU 1000
typedef struct { char first_name[TWENTY]; char middle_name[TWENTY]; char last_name[TWENTY]; double users_gpa; } format_of_file;
int main () { /* Declarations: File I/O */ FILE *inp, *outp; double sum,place_temp,min_value,max_value,average_value; char place_holder[TWENTY],first_file[FIFTY],second_file[FIFTY],str[ONE_THOU],*ptr; format_of_file organized[ONE_THOU]; int i,j, count,f; /* Get users inputs */ printf("Enter the input file name: "); scanf("%s", first_file); printf("Enter the output file name: "); scanf("%s", second_file);
inp = fopen(first_file, "r"); outp = fopen(second_file, "w");
/* Test if both files for validity */ if (inp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); } if (outp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); }
/* Begin Program execution and get each name/gpa until , and then continue */ count = 0; while (fgets(str,sizeof(str),inp) != NULL) { /* Get last name */ if ((ptr = strtok(str,", ")) != NULL) { strcpy(organized[count].last_name,ptr); } else { organized[count].last_name[0]=''; } /* Get first name */ if ((ptr = strtok(NULL,", ")) != NULL) { strcpy(organized[count].first_name,ptr); } else { organized[count].first_name[0]=''; } /* Get Middle or gpa */ if ((ptr = strtok(NULL,", ")) != NULL ) { /* Checks to see if it is a middle name or double value */ strcpy(place_holder,ptr); place_temp = atof(place_holder); if(place_temp == 0) { strcpy(organized[count].middle_name,ptr); } else { /* Convert string to double */ organized[count].users_gpa = atof(ptr); organized[count].middle_name[0] = ''; } } else { organized[count].middle_name[0]=''; } /* Get gpa */ if ((ptr = strtok(NULL,", ")) != NULL) { /* Convert string to double */ organized[count].users_gpa = atof(ptr); } count++; /* Check to see if the size of the file is under a thousand */ if (count > ONE_THOU) { printf("Error with file size "); return 0; } } /* Close first File */ fclose(inp); /* Find the highest/lowest value/sum and Initialize */ max_value = organized[0].users_gpa; min_value = organized[0].users_gpa; sum = 0; for (i=0;i < count;i++) { if (max_value < organized[i].users_gpa) { max_value = organized[i].users_gpa; } if (min_value > organized[i].users_gpa) { min_value = organized[i].users_gpa; } sum = sum + organized[i].users_gpa; } /* Find the average value */ average_value = (sum/count); printf("%d ", count); /* Display Results */ fprintf(outp,"Highest gpa: %.2f ", max_value); fprintf(outp,"Average gpa: %.2f ", average_value); fprintf(outp,"Lowest gpa: %.2f ", min_value); f = 0; for (i=0;i < count;i++) { for (j=0;j < sizeof(organized[i].first_name);j++) { if ((organized[i].first_name[j] != '') && (organized[i].first_name[j] != ' ')) { fprintf(outp,"%c", organized[i].first_name[j]); } } place_holder[0]=''; fprintf(outp,"%s ", place_holder); for (j=0;j < sizeof(organized[i].middle_name);j++) { if ((organized[i].middle_name[j] != '') && (organized[i].middle_name[j] != ' ')) Gates, William ,H, 1.89 Jobs,Steven,P,2.56 Brin , Sergey , , 3.89 Page, Lawrence ,, 3.77 Buffett, Warren, E, 3.34 Short, Sue, , 3.11 Cisneros, Juan, G, 3.67 Andrew, T, 5.67
Do you see where it says andrew, T, 5.67 ? well at scan time I would like to ignore that line and continue to the next one. After I have done so, my program will produce the folllowing output file: output file: Highest gpa: 3.89 Average gpa: 3.18 (only 7 numbers were inputted since one of them were delete it) lowest gpa: 1.89
William H. Gates 1.89Steven P. Jobs 2.56Sergey Brin 3.89Lawrence Page 3.77Warren E. Buffett 3.34Sue Short 3.11Juan G. Cisneros 3.67Thats what I would like it to do but I get this: my outoutfile: Highest gpa: 5.67 Average gpa: 3.49 Lowest gpa: 1.89
William H. Gates 1.89 Steven P. Jobs 2.56 Sergey . Brin 3.89 Lawrence . Page 3.77 Warren E. Buffett 3.34 Sue . Short 3.11 Juan G. Cisneros 3.67 T . Andrew 5.67
Any suggestions?? THis is my code #include <stdio.h> #include <string.h> #include <stdlib.h> #define TEN 10 #define THREE 3 #define TWENTY 20 #define FIFTY 50 #define ONE_THOU 1000
typedef struct { char first_name[TWENTY]; char middle_name[TWENTY]; char last_name[TWENTY]; double users_gpa; } format_of_file;
int main () { /* Declarations: File I/O */ FILE *inp, *outp; double sum,place_temp,min_value,max_value,average_value; char place_holder[TWENTY],first_file[FIFTY],second_file[FIFTY],str[ONE_THOU],*ptr; format_of_file organized[ONE_THOU]; int i,j, count,f; /* Get users inputs */ printf("Enter the input file name: "); scanf("%s", first_file); printf("Enter the output file name: "); scanf("%s", second_file);
inp = fopen(first_file, "r"); outp = fopen(second_file, "w");
/* Test if both files for validity */ if (inp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); } if (outp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); }
/* Begin Program execution and get each name/gpa until , and then continue */ count = 0; while (fgets(str,sizeof(str),inp) != NULL) { /* Get last name */ if ((ptr = strtok(str,", ")) != NULL) { strcpy(organized[count].last_name,ptr); } else { organized[count].last_name[0]=''; } /* Get first name */ if ((ptr = strtok(NULL,", ")) != NULL) { strcpy(organized[count].first_name,ptr); } else { organized[count].first_name[0]=''; } /* Get Middle or gpa */ if ((ptr = strtok(NULL,", ")) != NULL ) { /* Checks to see if it is a middle name or double value */ strcpy(place_holder,ptr); place_temp = atof(place_holder); if(place_temp == 0) { strcpy(organized[count].middle_name,ptr); } else { /* Convert string to double */ organized[count].users_gpa = atof(ptr); organized[count].middle_name[0] = ''; } } else { organized[count].middle_name[0]=''; } /* Get gpa */ if ((ptr = strtok(NULL,", ")) != NULL) { /* Convert string to double */ organized[count].users_gpa = atof(ptr); } count++; /* Check to see if the size of the file is under a thousand */ if (count > ONE_THOU) { printf("Error with file size "); return 0; } } /* Close first File */ fclose(inp); /* Find the highest/lowest value/sum and Initialize */ max_value = organized[0].users_gpa; min_value = organized[0].users_gpa; sum = 0; for (i=0;i < count;i++) { if (max_value < organized[i].users_gpa) { max_value = organized[i].users_gpa; } if (min_value > organized[i].users_gpa) { min_value = organized[i].users_gpa; } sum = sum + organized[i].users_gpa; } /* Find the average value */ average_value = (sum/count); printf("%d ", count); /* Display Results */ fprintf(outp,"Highest gpa: %.2f ", max_value); fprintf(outp,"Average gpa: %.2f ", average_value); fprintf(outp,"Lowest gpa: %.2f ", min_value); f = 0; for (i=0;i < count;i++) { for (j=0;j < sizeof(organized[i].first_name);j++) { if ((organized[i].first_name[j] != '') && (organized[i].first_name[j] != ' ')) { fprintf(outp,"%c", organized[i].first_name[j]); } } place_holder[0]=''; fprintf(outp,"%s ", place_holder); for (j=0;j < sizeof(organized[i].middle_name);j++) { if ((organized[i].middle_name[j] != '') && (organized[i].middle_name[j] != ' ')) Gates, William ,H, 1.89 Jobs,Steven,P,2.56 Brin , Sergey , , 3.89 Page, Lawrence ,, 3.77 Buffett, Warren, E, 3.34 Short, Sue, , 3.11 Cisneros, Juan, G, 3.67 Andrew, T, 5.67
Do you see where it says andrew, T, 5.67 ? well at scan time I would like to ignore that line and continue to the next one. After I have done so, my program will produce the folllowing output file: output file: Highest gpa: 3.89 Average gpa: 3.18 (only 7 numbers were inputted since one of them were delete it) lowest gpa: 1.89
William H. Gates 1.89Steven P. Jobs 2.56Sergey Brin 3.89Lawrence Page 3.77Warren E. Buffett 3.34Sue Short 3.11Juan G. Cisneros 3.67Thats what I would like it to do but I get this: my outoutfile: Highest gpa: 5.67 Average gpa: 3.49 Lowest gpa: 1.89
William H. Gates 1.89 Steven P. Jobs 2.56 Sergey . Brin 3.89 Lawrence . Page 3.77 Warren E. Buffett 3.34 Sue . Short 3.11 Juan G. Cisneros 3.67 T . Andrew 5.67
Any suggestions?? THis is my code #include <stdio.h> #include <string.h> #include <stdlib.h> #define TEN 10 #define THREE 3 #define TWENTY 20 #define FIFTY 50 #define ONE_THOU 1000
typedef struct { char first_name[TWENTY]; char middle_name[TWENTY]; char last_name[TWENTY]; double users_gpa; } format_of_file;
int main () { /* Declarations: File I/O */ FILE *inp, *outp; double sum,place_temp,min_value,max_value,average_value; char place_holder[TWENTY],first_file[FIFTY],second_file[FIFTY],str[ONE_THOU],*ptr; format_of_file organized[ONE_THOU]; int i,j, count,f; /* Get users inputs */ printf("Enter the input file name: "); scanf("%s", first_file); printf("Enter the output file name: "); scanf("%s", second_file);
inp = fopen(first_file, "r"); outp = fopen(second_file, "w");
/* Test if both files for validity */ if (inp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); } if (outp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); }
/* Begin Program execution and get each name/gpa until , and then continue */ count = 0; while (fgets(str,sizeof(str),inp) != NULL) { /* Get last name */ if ((ptr = strtok(str,", ")) != NULL) { strcpy(organized[count].last_name,ptr); } else { organized[count].last_name[0]=''; } /* Get first name */ if ((ptr = strtok(NULL,", ")) != NULL) { strcpy(organized[count].first_name,ptr); } else { organized[count].first_name[0]=''; } /* Get Middle or gpa */ if ((ptr = strtok(NULL,", ")) != NULL ) { /* Checks to see if it is a middle name or double value */ strcpy(place_holder,ptr); place_temp = atof(place_holder); if(place_temp == 0) { strcpy(organized[count].middle_name,ptr); } else { /* Convert string to double */ organized[count].users_gpa = atof(ptr); organized[count].middle_name[0] = ''; } } else { organized[count].middle_name[0]=''; } /* Get gpa */ if ((ptr = strtok(NULL,", ")) != NULL) { /* Convert string to double */ organized[count].users_gpa = atof(ptr); } count++; /* Check to see if the size of the file is under a thousand */ if (count > ONE_THOU) { printf("Error with file size "); return 0; } } /* Close first File */ fclose(inp); /* Find the highest/lowest value/sum and Initialize */ max_value = organized[0].users_gpa; min_value = organized[0].users_gpa; sum = 0; for (i=0;i < count;i++) { if (max_value < organized[i].users_gpa) { max_value = organized[i].users_gpa; } if (min_value > organized[i].users_gpa) { min_value = organized[i].users_gpa; } sum = sum + organized[i].users_gpa; } /* Find the average value */ average_value = (sum/count); printf("%d ", count); /* Display Results */ fprintf(outp,"Highest gpa: %.2f ", max_value); fprintf(outp,"Average gpa: %.2f ", average_value); fprintf(outp,"Lowest gpa: %.2f ", min_value); f = 0; for (i=0;i < count;i++) { for (j=0;j < sizeof(organized[i].first_name);j++) { if ((organized[i].first_name[j] != '') && (organized[i].first_name[j] != ' ')) { fprintf(outp,"%c", organized[i].first_name[j]); } } place_holder[0]=''; fprintf(outp,"%s ", place_holder); for (j=0;j < sizeof(organized[i].middle_name);j++) { if ((organized[i].middle_name[j] != '') && (organized[i].middle_name[j] != ' ')) Highest gpa: 5.67 Average gpa: 3.49 Lowest gpa: 1.89
William H. Gates 1.89 Steven P. Jobs 2.56 Sergey . Brin 3.89 Lawrence . Page 3.77 Warren E. Buffett 3.34 Sue . Short 3.11 Juan G. Cisneros 3.67 T . Andrew 5.67
Any suggestions?? THis is my code #include <stdio.h> #include <string.h> #include <stdlib.h> #define TEN 10 #define THREE 3 #define TWENTY 20 #define FIFTY 50 #define ONE_THOU 1000
typedef struct { char first_name[TWENTY]; char middle_name[TWENTY]; char last_name[TWENTY]; double users_gpa; } format_of_file;
int main () { /* Declarations: File I/O */ FILE *inp, *outp; double sum,place_temp,min_value,max_value,average_value; char place_holder[TWENTY],first_file[FIFTY],second_file[FIFTY],str[ONE_THOU],*ptr; format_of_file organized[ONE_THOU]; int i,j, count,f; /* Get users inputs */ printf("Enter the input file name: "); scanf("%s", first_file); printf("Enter the output file name: "); scanf("%s", second_file);
inp = fopen(first_file, "r"); outp = fopen(second_file, "w");
/* Test if both files for validity */ if (inp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); } if (outp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); }
/* Begin Program execution and get each name/gpa until , and then continue */ count = 0; while (fgets(str,sizeof(str),inp) != NULL) { /* Get last name */ if ((ptr = strtok(str,", ")) != NULL) { strcpy(organized[count].last_name,ptr); } else { organized[count].last_name[0]=''; } /* Get first name */ if ((ptr = strtok(NULL,", ")) != NULL) { strcpy(organized[count].first_name,ptr); } else { organized[count].first_name[0]=''; } /* Get Middle or gpa */ if ((ptr = strtok(NULL,", ")) != NULL ) { /* Checks to see if it is a middle name or double value */ strcpy(place_holder,ptr); place_temp = atof(place_holder); if(place_temp == 0) { strcpy(organized[count].middle_name,ptr); } else { /* Convert string to double */ organized[count].users_gpa = atof(ptr); organized[count].middle_name[0] = ''; } } else { organized[count].middle_name[0]=''; } /* Get gpa */ if ((ptr = strtok(NULL,", ")) != NULL) { /* Convert string to double */ organized[count].users_gpa = atof(ptr); } count++; /* Check to see if the size of the file is under a thousand */ if (count > ONE_THOU) { printf("Error with file size "); return 0; } } /* Close first File */ fclose(inp); /* Find the highest/lowest value/sum and Initialize */ max_value = organized[0].users_gpa; min_value = organized[0].users_gpa; sum = 0; for (i=0;i < count;i++) { if (max_value < organized[i].users_gpa) { max_value = organized[i].users_gpa; } if (min_value > organized[i].users_gpa) { min_value = organized[i].users_gpa; } sum = sum + organized[i].users_gpa; } /* Find the average value */ average_value = (sum/count); printf("%d ", count); /* Display Results */ fprintf(outp,"Highest gpa: %.2f ", max_value); fprintf(outp,"Average gpa: %.2f ", average_value); fprintf(outp,"Lowest gpa: %.2f ", min_value); f = 0; for (i=0;i < count;i++) { for (j=0;j < sizeof(organized[i].first_name);j++) { if ((organized[i].first_name[j] != '') && (organized[i].first_name[j] != ' ')) { fprintf(outp,"%c", organized[i].first_name[j]); } } place_holder[0]=''; fprintf(outp,"%s ", place_holder); for (j=0;j < sizeof(organized[i].middle_name);j++) { if ((organized[i].middle_name[j] != '') && (organized[i].middle_name[j] != ' ')) #include <stdio.h> #include <string.h> #include <stdlib.h> #define TEN 10 #define THREE 3 #define TWENTY 20 #define FIFTY 50 #define ONE_THOU 1000
typedef struct { char first_name[TWENTY]; char middle_name[TWENTY]; char last_name[TWENTY]; double users_gpa; } format_of_file;
int main () { /* Declarations: File I/O */ FILE *inp, *outp; double sum,place_temp,min_value,max_value,average_value; char place_holder[TWENTY],first_file[FIFTY],second_file[FIFTY],str[ONE_THOU],*ptr; format_of_file organized[ONE_THOU]; int i,j, count,f; /* Get users inputs */ printf("Enter the input file name: "); scanf("%s", first_file); printf("Enter the output file name: "); scanf("%s", second_file);
inp = fopen(first_file, "r"); outp = fopen(second_file, "w");
/* Test if both files for validity */ if (inp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); } if (outp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); }
/* Begin Program execution and get each name/gpa until , and then continue */ count = 0; while (fgets(str,sizeof(str),inp) != NULL) { /* Get last name */ if ((ptr = strtok(str,", ")) != NULL) { strcpy(organized[count].last_name,ptr); } else { organized[count].last_name[0]=''; } /* Get first name */ if ((ptr = strtok(NULL,", ")) != NULL) { strcpy(organized[count].first_name,ptr); } else { organized[count].first_name[0]=''; } /* Get Middle or gpa */ if ((ptr = strtok(NULL,", ")) != NULL ) { /* Checks to see if it is a middle name or double value */ strcpy(place_holder,ptr); place_temp = atof(place_holder); if(place_temp == 0) { strcpy(organized[count].middle_name,ptr); } else { /* Convert string to double */ organized[count].users_gpa = atof(ptr); organized[count].middle_name[0] = ''; } } else { organized[count].middle_name[0]=''; } /* Get gpa */ if ((ptr = strtok(NULL,", ")) != NULL) { /* Convert string to double */ organized[count].users_gpa = atof(ptr); } count++; /* Check to see if the size of the file is under a thousand */ if (count > ONE_THOU) { printf("Error with file size "); return 0; } } /* Close first File */ fclose(inp); /* Find the highest/lowest value/sum and Initialize */ max_value = organized[0].users_gpa; min_value = organized[0].users_gpa; sum = 0; for (i=0;i < count;i++) { if (max_value < organized[i].users_gpa) { max_value = organized[i].users_gpa; } if (min_value > organized[i].users_gpa) { min_value = organized[i].users_gpa; } sum = sum + organized[i].users_gpa; } /* Find the average value */ average_value = (sum/count); printf("%d ", count); /* Display Results */ fprintf(outp,"Highest gpa: %.2f ", max_value); fprintf(outp,"Average gpa: %.2f ", average_value); fprintf(outp,"Lowest gpa: %.2f ", min_value); f = 0; for (i=0;i < count;i++) { for (j=0;j < sizeof(organized[i].first_name);j++) { if ((organized[i].first_name[j] != '') && (organized[i].first_name[j] != ' ')) { fprintf(outp,"%c", organized[i].first_name[j]); } } place_holder[0]=''; fprintf(outp,"%s ", place_holder); for (j=0;j < sizeof(organized[i].middle_name);j++) { if ((organized[i].middle_name[j] != '') && (organized[i].middle_name[j] != ' '))
Explanation / Answer
#include #include #include #define TEN 10#define THREE 3#define TWENTY 20#define FIFTY 50#define ONE_THOU 1000typedef struct {char first_name[TWENTY];char middle_name[TWENTY];char last_name[TWENTY];double users_gpa;} format_of_file;int main () {/* Declarations: File I/O */FILE *inp, *outp;double sum,place_temp,min_value,max_value,average_value;char place_holder[TWENTY],first_file[FIFTY],second_file[FIFTY],str[ONE_THOU],*ptr;format_of_file organized[ONE_THOU];int i,j, count,f;/* Get users inputs */printf("Enter the input file name: ");scanf("%s", first_file);printf("Enter the output file name: ");scanf("%s", second_file);inp = fopen(first_file, "r");outp = fopen(second_file, "w");/* Test if both files for validity */if (inp == NULL) {perror("Invalid File");exit(EXIT_FAILURE);}if (outp == NULL) {perror("Invalid File");exit(EXIT_FAILURE);}/* Begin Program execution and get each name/gpa until , and then continue */count = 0;while (fgets(str,sizeof(str),inp) != NULL) {/* Get last name */ if ((ptr = strtok(str,", ")) != NULL) {strcpy(organized[count].last_name,ptr);}else {organized[count].last_name[0]='';} /* Get first name */ if ((ptr = strtok(NULL,", ")) != NULL) {strcpy(organized[count].first_name,ptr);}else {organized[count].first_name[0]='';}/* Get Middle or gpa */if ((ptr = strtok(NULL,", ")) != NULL ) {/* Checks to see if it is a middle name or double value */strcpy(place_holder,ptr);place_temp = atof(place_holder);if(place_temp == 0) {strcpy(organized[count].middle_name,ptr);}else {/* Convert string to double */organized[count].users_gpa = atof(ptr);organized[count].middle_name[0] = '';}}else {organized[count].middle_name[0]='';}/* Get gpa */if ((ptr = strtok(NULL,", ")) != NULL) {/* Convert string to double */organized[count].users_gpa = atof(ptr);}count++;/* Check to see if the size of the file is under a thousand */if (count > ONE_THOU) {printf("Error with file size ");return 0;}}/* Close first File */fclose(inp);/* Find the highest/lowest value/sum and Initialize */max_value = organized[0].users_gpa;min_value = organized[0].users_gpa;sum = 0;for (i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.