Você está na página 1de 4

1 9

#include <stdio.h> #define STUDENTS_NUMBER 6 #define STR_SIZE 101

void input (char names[][STR_SIZE], int grades[]) /* This function receives two empty arrays with STUDENTS_NUMBER of rows, "names" being a two dimensional array with STR_SIZE columns. It receives input from the user and puts them in the respective arrays: "names" is a two dimensional array that holds strings representing students' names. "grades" is a one dimensional array holding said students' grades. The grade in "grades[i]" belongs to the student whose name is in the "i" row in "names". *****This function assumes that each string only holds one word, and that the grades entered are valid.***** */ { int i=0; for (i=0; i < STUDENTS_NUMBER; i++) { printf("Please insert the name for student number %d: ",i+1); scanf("%100s", names[i]); printf("%s, please insert your grade: ", names[i]); scanf("%d", &grades[i]); printf("\n"); } return; }

void convert_to_pointers (char names[][STR_SIZE], char *ptr_names[]) /* This function receives all the students' names from the "names" array, and creates an array of char-type pointers. Each pointer holds the address of an appropriate string from "names", so that we can have "char *names[]" for the assignment. The arrays have STUDENTS_NUMBER amount of rows, as DEFINED, so it doesn't need to receive it from the main program. */ { int i=0; for (i=0; i < STUDENTS_NUMBER; i++) ptr_names[i]= &names[i][0]; return; }

int find_failures (int grades[], char *names[], int number_of_students, char *failed[], double *average, int *maximal) /* This is the function as required in the assignment. It returns the number of students that failed. It also "returns" the class average and what the highest grade was, with the help of pointers. Important: Since it wasn't specifically defined, I decided that "char *failed[]" will receive the appropriate pointers, and all the text output will be handled in the main program, as the term "output" in this assignment is vague. */ { int j=0, max=0, fails=0, sum=0; for (j=0; j < number_of_students; j++) { sum+= grades[j]; if (grades[j] > max) max= grades[j]; if (grades[j] < 60) failed[fails++]= names[j]; } *average= (((double)(sum))/((double)(number_of_students))); *maximal= max; return fails; } int main() //*********************** Main Program **************************** { int i=0, fails=0, maximal=0, grades[STUDENTS_NUMBER]; char names[STUDENTS_NUMBER][STR_SIZE]; char *ptr_names[STUDENTS_NUMBER], *failed[STUDENTS_NUMBER], *j; double average=0.0; input(names, grades); convert_to_pointers(names, ptr_names); fails= find_failures (grades, ptr_names, STUDENTS_NUMBER, failed, &average, &maximal); printf("Here are the results: \n"); printf("Average grade is %g, Maximal grade is %d, %d students failed:\n", average, maximal, fails); for (i=0; i < fails; i++) { for (j=failed[i]; *j != '\0'; j++) printf("%c", *j); printf("\n"); } printf("\n"); return 0; }

2 9
#include <stdio.h> #include <string.h> #define STR_LENGTH 101 /*Note: The 101 character length is not long enough for the exercise example... The example has 122 characters. */ int check_case(char *str, char *wrongCase[]) /* This function receives a string (as a pointer) from the main program, and returns the following: "*wrongCase[]" (An array of char pointers) receives the locations of the grammatically incorrect sentences. The function returns the amount of grammatically incorrect sentences.*/ { int i=0; while ((*str == ' ') || (*str == ',') || (*str == '.')) str++; while (*str != '\n') { if ((*str >= 'a') && (*str <= 'z')) wrongCase[i++]= str; str = strchr(str, '.'); while ((*str == ' ') || (*str == ',') || (*str == '.')) str++; } return i; } int main() //************************************** Main Program ************************************** // Note: The string inserted assumes the conditions are as noted in the assignment. { int i=1, j=0; char str[STR_LENGTH]; char *pstr, *wrongCase[STR_LENGTH]; pstr = str; printf("Please insert your sentence: \n"); scanf("%c", &str[0]); for (i=1; str[i-1] != '\n'; i++) scanf("%c", &str[i]); str[i]= '\0'; j= check_case(pstr, wrongCase);

if (j==0) printf("Your grammar was correct, good job!\n"); else { printf("The following sentences were grammatically incorrect:\n\n"); for (i=0; i < j; i++) { while (*wrongCase[i] != '.') { printf("%c", *wrongCase[i]); wrongCase[i]++; } printf(".\n"); } } return 0; }

Você também pode gostar