Thread: Swapping Printf

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    33

    Swapping Printf

    Hey guys, this is my code and I need to print the student who must pay most at the top, while the person that receives the most is at the bottom. And if there are students that have to pay the same amount then they are printed in the same order as they are ordered in the input.
    I dont know how to swap student, i tried some code but i did not work at all. Is it possible to swap after the calculation is made, so when the program knows which student and how much a student has to receive or pay? Is this possible?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    typedef struct student {
      int who;    
      float paid;  
    } student;
      
    student firstStudent, secondStudent, thirdStudent;
    
    void readStudent(student *stud) {
      scanf("%d %f", &stud->who, &stud->paid);
    }
     
    void printNameOfStudent(student stud) {
      printf("%s", (stud.who == 0 ? "John" : (stud.who == 1 ? "Peter" : "William")));
    }
     
    float paidByStudent(student stud) {
      return stud.paid;
    }
     
    void swapStudents(student *a, student *b) {
      student h = *a;
      *a = *b;
      *b = h;
    }
    
    void readInput() {
      readStudent(&firstStudent);
      readStudent(&secondStudent);
      readStudent(&thirdStudent);
    }
     
    int main(int argc, char *argv[]) {
      float fairShare, difference;
      readInput();
     
     fairShare = ((paidByStudent(firstStudent) + paidByStudent(secondStudent) + paidByStudent(thirdStudent))/3);
      
      if(fairShare == paidByStudent(firstStudent)){
        printNameOfStudent(firstStudent);
        printf(" receives 0.00\n");
      }
        
      if(fairShare < paidByStudent(firstStudent)){
        difference =  paidByStudent(firstStudent) - fairShare;
        printNameOfStudent(firstStudent);
        printf(" receives %3.2f\n", difference);
      }
      
      if(fairShare > paidByStudent(firstStudent)){
        difference = fairShare - paidByStudent(firstStudent);
        printNameOfStudent(firstStudent);
        printf(" pays %3.2f\n", difference);
      }
      
      if(fairShare == paidByStudent(secondStudent)){
        printNameOfStudent(secondStudent);
        printf(" receives 0.00\n");
      }
      if(fairShare < paidByStudent(secondStudent)){
        difference = paidByStudent(secondStudent) - fairShare;
        printNameOfStudent(secondStudent);
        printf(" receives %3.2f\n", difference);
      }
      if(fairShare > paidByStudent(secondStudent)){
        difference = fairShare - paidByStudent(secondStudent);
        printNameOfStudent(secondStudent);
        printf(" pays %3.2f\n", difference);
      }
      
     if(fairShare == paidByStudent(thirdStudent)){
        printNameOfStudent(thirdStudent);
        printf(" receives 0.00\n");
     }
      if(fairShare < paidByStudent(thirdStudent)){
        difference = paidByStudent(thirdStudent) - fairShare;
        printNameOfStudent(thirdStudent);
        printf(" receives %3.2f\n", difference);
      }
      if(fairShare > paidByStudent(thirdStudent)){
        difference = fairShare - paidByStudent(thirdStudent);
        printNameOfStudent(thirdStudent);
        printf(" pays %3.2f\n", difference);
     }
      
      return 0;
    }
    Last edited by Se7enUp; 09-29-2013 at 10:01 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The first thing to think about is the logic you need to print 3 integers in order.

    Code:
    int main ( ) {
      int a = 3, b = 7, c = 1;
      // Through several swaps, can you arrange that a=1, b=3, c=7 ?
    }
    When you can do this, you can do the same for students.

    Also, when you get past 3, you'll realise an array is much more convenient.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    33
    Quote Originally Posted by Salem View Post

    Also, when you get past 3, you'll realise an array is much more convenient.
    Wat is an array?

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    33
    Quote Originally Posted by Salem View Post
    Code:
    int main ( ) {
      int a = 3, b = 7, c = 1;
      // Through several swaps, can you arrange that a=1, b=3, c=7 ?
    }
    When you can do this, you can do the same for students.
    So I need to do this
    Code:
    int main(int argc, char *argv[]) {
      int John = 0, Peter = 1, William = 2;
      float fairShare, difference;
      readInput();
    And then swap when the paidbyStudent is more than the rest of when they paid less

  5. #5
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Salem is saying that u have to learn the swap logic,

    Example:
    int a = 5;
    int b = 7;
    int c = 10;

    and the question is how to change the value a to b, b to c and c to a

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    33
    Quote Originally Posted by loserone+_+ View Post
    Salem is saying that u have to learn the swap logic,

    Example:
    int a = 5;
    int b = 7;
    int c = 10;

    and the question is how to change the value a to b, b to c and c to a
    int a to b
    int d = int a;

    int a = int b;

    int b = int d;

    right? I already have a void method swapStudent

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Se7enUp View Post
    int a to b
    int d = int a;
    int a = int b;
    int b = int d;
    right?
    Except for all the "int"s that's correct.

    I already have a void method swapStudent
    You can use the swapStudents function something like this:
    Code:
    swapStudents(&firstStudent, &secondStudent);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    33
    Thx that was my question, i was already typing this:
    Code:
    void swapStudents(student *a, student *b) {
      student h = *a;
      *a = *b;
      *b = h;
    }
    
     if(fairShare > paidByStudent(secondStudent)){
        difference = fairShare - paidByStudent(secondStudent);
        printNameOfStudent(secondStudent);
        printf(" pays %3.2f\n", difference); 
           if ((paidByStudent(secondStudent) > paidByStudent(firstStudent)) &&     
           (paidByStudent(secondStudent) > paidByStudent(thirdStudent))){
            swapStudents(student firstStudent, student secondStudent);
        }
      }
    How must swapStudents be typed?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Something like
    Code:
    swapStudents(&firstStudent, &secondStudent);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Sep 2013
    Posts
    33
    Jesus Christ! I made it work. But please tell me there is a easy and shorter way than this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
     
    typedef struct student {
      int who;    
      float paid;  
    } student;
     
    /* global variables */
    student firstStudent, secondStudent, thirdStudent;
    
    void readStudent(student *stud) {
      scanf("%d %f", &stud->who, &stud->paid);
    }
     
    void printNameOfStudent(student stud) {
      printf("%s", (stud.who == 0 ? "John" : (stud.who == 1 ? "Peter" : "William")));
    }
     
    float paidByStudent(student stud) {
      return stud.paid;
    }
     
    void swapStudents(student *a, student *b) {
      student h = *a;
      *a = *b;
      *b = h;
    }
    
    void readInput() {
      readStudent(&firstStudent);
      readStudent(&secondStudent);
      readStudent(&thirdStudent);
    }
     
    int main(int argc, char *argv[]) {
      float fairShare, difference;
      readInput();
      
      /* first swap the students and then calculate*/
      if (paidByStudent(firstStudent) > paidByStudent(secondStudent) || paidByStudent(firstStudent) > paidByStudent(thirdStudent)) {
      if (paidByStudent(firstStudent) > paidByStudent(secondStudent) && paidByStudent(firstStudent) > paidByStudent(thirdStudent)) {
      if (paidByStudent(thirdStudent) > paidByStudent(secondStudent)) {
         swapStudents(&firstStudent, &thirdStudent);
         swapStudents(&thirdStudent, &secondStudent);
                }
                
      if (paidByStudent(thirdStudent) == paidByStudent(secondStudent)) {
         swapStudents(&secondStudent, &firstStudent);
         swapStudents(&thirdStudent, &secondStudent);
                }
                
      if (paidByStudent(secondStudent) > paidByStudent(thirdStudent)) {
         swapStudents(&firstStudent, &thirdStudent);
                }
            }
            
      if (paidByStudent(firstStudent) > paidByStudent(secondStudent) && paidByStudent(thirdStudent) > paidByStudent(firstStudent)) {
         swapStudents(&firstStudent, &secondStudent);
            }
            
      if (paidByStudent(firstStudent) > paidByStudent(thirdStudent) && paidByStudent(secondStudent) > paidByStudent(firstStudent)) {
          swapStudents(&firstStudent, &thirdStudent);
          swapStudents(&secondStudent, &thirdStudent);
            }
        }
        
     if (paidByStudent(secondStudent) > paidByStudent(firstStudent) && paidByStudent(secondStudent) > paidByStudent(thirdStudent)) {
     if (paidByStudent(firstStudent) > paidByStudent(thirdStudent)) {
          swapStudents(&thirdStudent, &secondStudent);
          swapStudents(&firstStudent, &secondStudent);
            }
            
     if (paidByStudent(thirdStudent) > paidByStudent(firstStudent)) {
          swapStudents(&secondStudent, &thirdStudent);
            }
           
     if (paidByStudent(firstStudent) == paidByStudent(thirdStudent)) {
          swapStudents(&secondStudent, &thirdStudent);
            }
        }
     
     fairShare = ((paidByStudent(firstStudent) + paidByStudent(secondStudent) + paidByStudent(thirdStudent))/3);
     
      if(fairShare > paidByStudent(firstStudent)){
        difference = fairShare - paidByStudent(firstStudent);
        printNameOfStudent(firstStudent);
        printf(" pays %3.2f\n", difference);
      }
      
       if(fairShare > paidByStudent(secondStudent)){
        difference = fairShare - paidByStudent(secondStudent);    
        printNameOfStudent(secondStudent);
        printf(" pays %3.2f\n", difference); 
      }
      
      if(fairShare > paidByStudent(thirdStudent)){
        difference = fairShare - paidByStudent(thirdStudent);
        printNameOfStudent(thirdStudent);
        printf(" pays %3.2f\n", difference);
        
     }
     
      if(fairShare == paidByStudent(firstStudent)){
        printNameOfStudent(firstStudent);
        printf(" receives 0.00\n");
      }
      
       if(fairShare == paidByStudent(secondStudent)){
        printNameOfStudent(secondStudent);
        printf(" receives 0.00\n");
      } 
      
       if(fairShare == paidByStudent(thirdStudent)){
        printNameOfStudent(thirdStudent);
        printf(" receives 0.00\n");
     }
     
      if(fairShare < paidByStudent(firstStudent)){
        difference =  paidByStudent(firstStudent) - fairShare; 
        printNameOfStudent(firstStudent);
        printf(" receives %3.2f\n", difference);
        
      }
    
      if(fairShare < paidByStudent(secondStudent)){
        difference = paidByStudent(secondStudent) - fairShare;
        printNameOfStudent(secondStudent);
        printf(" receives %3.2f\n", difference);
      }
    
      if(fairShare < paidByStudent(thirdStudent)){
        difference = paidByStudent(thirdStudent) - fairShare;
        printNameOfStudent(thirdStudent);
        printf(" receives %3.2f\n", difference);
      }
      
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping structure
    By ncode in forum C Programming
    Replies: 6
    Last Post: 02-29-2012, 08:22 AM
  2. Swapping 2 numbers
    By Rakesh Kp in forum C Programming
    Replies: 8
    Last Post: 09-12-2011, 12:07 PM
  3. swapping help
    By mouse666666 in forum C Programming
    Replies: 3
    Last Post: 03-24-2010, 10:38 AM
  4. swapping pointers
    By csvraju in forum C Programming
    Replies: 17
    Last Post: 04-01-2009, 03:18 AM
  5. swapping
    By metallicaau in forum C Programming
    Replies: 2
    Last Post: 10-08-2002, 08:17 AM