Thread: How to arrange the input/output order?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    1

    How to arrange the input/output order?

    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.
    insert
    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;
    }
    Do you have any ideas on how to solve this?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your problem statement is not all that clear but - if you mean what I think you do ...

    Read up on arrays (and use one array with 3 elements, rather than 3 distinct variables) to represent your data. Then look up sorting.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-26-2013, 01:15 AM
  2. Replies: 1
    Last Post: 11-17-2012, 05:34 PM
  3. Pointers help needed. Arrange fractions in ascending order
    By jollykiddo in forum C Programming
    Replies: 1
    Last Post: 12-14-2011, 01:30 AM
  4. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  5. Replies: 1
    Last Post: 09-24-2009, 01:28 AM

Tags for this Thread