Не будет добавлять то, что он получил от другой функции каждый цикл цикла
#include <stdio.h> //function to convert letter grade into a number float GradePoints(char grade) { float gP; switch (grade) { case 'A': case 'a': gP = 4.0; break; case 'B': case 'b': gP = 3.0; break; case 'C': case 'c': gP = 2.0; break; case 'D': case 'd': gP = 1.0; break; case 'F': case 'f': gP = 0.0; break; } return gP; } //function to calculate grade point average float Gpa(char grades[], char hours[], int arrLength) { //gradepoint * time in class float totalGpHours = 0; //time in class float totalHours = 0; //final grade point average float gradePointAverage; int i; for (i = 0; i < arrLength; ++i) { //add the user input time in each class to get total time in class. this is the denominator totalHours = hours[i] + totalHours; //multiplies hours in class and number grade from that class and adds the product to totalGpHours. this is the numerator totalGpHours = totalGpHours + (GradePoints(grades[i]) * hours[i]); } //calculate grade point average gradePointAverage = totalGpHours / totalHours; return gradePointAverage; } int main() { //add one more element to the array because of the null char grades[6]; int classTime[5]; int i; //Ask user for letter grades printf("Please enter the letter grades you received (upper-case letters, no spaces):\n"); //gets input plus one for the array grades fgets(grades, 6, stdin); //Ask user for time in each class printf("Please enter the time you spent in each course(in hours)"); for (i = 0; i < 5; ++i) { //add one to i so it looks normal for the user and you don't start at course 0 printf("\n Course %d: ", i + 1); scanf("%d", &classTime[i]); } printf("\n"); printf("Your grade point average is: %f\n\n", Gpa(grades, classTime, 5)); getchar(); return 0; }
Что я уже пробовал:
всё. если у меня есть 4 в первом цикле и 4 в следующем, он должен добавить их, но это не так.