Hello,
for our school project, we have to make a program which involves three students and the prices each of them paid for the meals. The program has to calculate how much each of the students has to pay/receive to/from the others.
Most of it, we got from our teacher. The error we are getting is that we have to 'take care of the input/output order'. So if you type in the names/numbers of the students in a different way, so for example if you type first number 1, then 0, then 2 and 0 and 1 have to pay money to number 2, number 0 has to be in front of 1. Our output is that number 1 is in front of number 0.
insertDo you have any ideas on how to solve this?Code:#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> /* type definition of student */ typedef struct student { int who; /* 0=John, 1=Peter, 2=William */ float paid; /* amount that student paid */ } student; /* global variables */ student firstStudent, secondStudent, thirdStudent; /************************************************************************ * ABSTRACT DATA TYPE: the following functions form the ADT student. * Do not change the ADT part of this program ! */ /* read the info of a student from the keyboard and return it. */ void readStudent(student *stud) { scanf("%d %f", &stud->who, &stud->paid); } /* print the name of a student. */ void printNameOfStudent(student stud) { printf("%s", (stud.who == 0 ? "John" : (stud.who == 1 ? "Peter" : "William"))); } /* returns the amount that a student paid. */ float paidByStudent(student stud) { return stud.paid; } /* swaps two students */ void swapStudents(student *a, student *b) { student h = *a; *a = *b; *b = h; } /************************************************************************/ /* remainder of the program in which the ADT student is used. */ /* * Reads the entire input from the keyboard and stores * it in the appropriate global variables. */ void readInput() { readStudent(&firstStudent); readStudent(&secondStudent); readStudent(&thirdStudent); } int main(int argc, char *argv[]) { readInput(); float average, difference; readInput(); average = ((paidByStudent(firstStudent) + paidByStudent(secondStudent) + paidByStudent(thirdStudent)) / 3); /*ordering*/ if (paidByStudent(thirdStudent) - average == paidByStudent(firstStudent) - average) { swapStudents(&thirdStudent, &firstStudent); } else { if (paidByStudent(thirdStudent) - average < paidByStudent(firstStudent) - average) { swapStudents(&thirdStudent, &firstStudent); } } if (paidByStudent(secondStudent) - average == paidByStudent(firstStudent) - average) { swapStudents(&secondStudent, &firstStudent); } else { if (paidByStudent(secondStudent) - average < paidByStudent(firstStudent) - average) { swapStudents(&secondStudent, &firstStudent); } } if (paidByStudent(secondStudent) - average == paidByStudent(thirdStudent) - average) { } else { if (paidByStudent(secondStudent) - average > paidByStudent(thirdStudent) - average) { swapStudents(&secondStudent, &thirdStudent); } } /* ordering when outcome is the same */ /* if (paidByStudent(thirdStudent) - average == paidByStudent(firstStudent) - average){ swapStudents(&thirdStudent, &firstStudent); } else { */ /* printing */ printNameOfStudent(firstStudent); if (paidByStudent(firstStudent) < average){ difference = average - paidByStudent(firstStudent); printf(" pays %3.2f\n", difference); } else { difference = ((average - paidByStudent(firstStudent)) *-1); printf(" receives %3.2f\n", difference); } printNameOfStudent(secondStudent); if (paidByStudent(secondStudent) > average){ difference = ((average - paidByStudent(secondStudent)) *-1); printf(" receives %3.2f\n", difference); } else { difference = average - paidByStudent(secondStudent); printf(" pays %3.2f\n", difference); } printNameOfStudent(thirdStudent); if (paidByStudent(thirdStudent) < average){ difference = average - paidByStudent(thirdStudent); printf(" pays %3.2f\n", difference); } else { difference = ((average - paidByStudent(thirdStudent)) *-1 ); printf(" receives %3.2f\n", difference); } return 0; }