Thread: Parking Charges problem using sorting

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    9

    Parking Charges problem using sorting

    im working on a parking charges problem and im stuck.

    here is my code so far. the only problem im stuck with is that it should display the final result with cars sorted from the highest charged to the lowest.

    CURRENT OUTPUT:
    Car Hours Charge
    1 9.00 185
    2 1.00 45
    3 3.00 65
    4 7.00 145



    TARGET OUTPUT SAMPLE:

    Car Hours Charge
    1 9.00 185
    4 7.00 145
    3 3.00 65
    2 1.00 45


    Code:
    #include <stdio.h>
    
    
    float calculateTimeSpent(float a, float b, float c, float d){
    	float timeSpent, x;
    	if (d < b){
    		c--;
    		d +=60;
    	}
    	timeSpent = (c - a) + (d - b)/60;
    	return timeSpent;
    }
    
    float timeEntryCars(){
    	int hhEntry, mmEntry, hhExit, mmExit, hour, minute;
    	printf("Enter time of entry: ");
    	scanf("%d:%d", &hhEntry, &mmEntry);
    	printf("Enter time of exit: ");
    	scanf("%d:%d", &hhExit, &mmExit);
    	return calculateTimeSpent(hhEntry, mmEntry, hhExit, mmExit);
    }
    
    float calculateCharges(float x){
    	int ans = 45;
    	int remainder, quotient, y;
    	if (x<=2) ans = 45;
    	else{
    		x -=2;
    		y = x*100;
    		remainder = y%50;
    		quotient = y/50;
    		if (remainder >= 0) ans = ans + (quotient*10);
    		else ans = ans + ((quotient+1)*10);
    	}
    	return ans;
    	}
    	int main(){
    	int Cars;
    	printf("input number of Cars: ");
    	scanf("%d", &Cars);
    		float car[Cars];
    		int charge;
    		int i, j;
    		float totalHours = 0;
    		int totalCharge = 0;
    		
    		for (i = 0; i<=Cars-1; i++) {
    			printf("For car %d>> \n", i + 1);
    			car [i] = timeEntryCars();
    		}
    		
    		printf("Car \t Hours \t Charge\n");
    		
    		for (j = 0; j<=Cars-1; j++) {
    			charge = calculateCharges(car[j]);
    			printf("%d\t %.2f\t %d\n", j + 1, car[j], charge);
    			totalHours +=car[j];
    			totalCharge +=charge;
            }
    		printf("TOTAL\t %.2f\t %d\n", totalHours, totalCharge);
    		
    		return 0;
    	}




    i desperately need your help. thanks!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You mean you don't know how to sort them?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    Quote Originally Posted by Sipher View Post
    You mean you don't know how to sort them?
    yes. that's my only problem. can you please help me? im desperate.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by stealthblade View Post
    yes. that's my only problem. can you please help me? im desperate.
    Try googling bubblesort, heapsort or quicksort to find the most popular sorting algorithms.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    Quote Originally Posted by Sipher View Post
    Try googling bubblesort, heapsort or quicksort to find the most popular sorting algorithms.

    i dont know how to apply those techniques here in my code. would you mind to help me do it?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Using an array of structs would make your program a lot easier to write.

    For sorting, since you'll never have a huge number, a simple substitution sort should be fine. This is a basic example:

    Code:
    for(i=0;i<numberOfCars-1; i++) {
      for(j = i+1;j<numberOfCars; j++) {
        if(charges[i] > charges[j]) {
          tempcar = car[i];       //save the values at index i
          tempcharge = charges[i];
          temphours= hours[i];
    
          car[i] = car[j]; //swap car number  and all values at index i
          charges[i] = charges[j]; //swap charges
          hours[i] = hours[j]; //swap hours used
    
          car[j] = tempcar;  //reassign the temp values from i, down to index j
          chartes[j] = tempcharge;
          hours[j] = temphours;
        }
      }
    }
    You need to change your code so you are saving each car's charges (into a charges array), and each amount of time that they're being charged, so that data is available (if you want it all).

    As it is now, you calculate the charges for each car, but you don't save them into an array, so you can't sort by them.

    Just a btw: using an array of struct for each car, which included the time being charged, the car number, and the charge, as struct members, would make the program a lot easier.
    Last edited by Adak; 02-04-2011 at 12:14 PM.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    Quote Originally Posted by Adak View Post
    Using an array of structs would make your program a lot easier to write.

    For sorting, since you'll never have a huge number, a simple substitution sort should be fine. This is a basic example:

    Code:
    for(i=0;i<numberOfCars-1; i++) {
      for(j = i+1;j<numberOfCars; j++) {
        if(charges[i] > charges[j]) {
          tempcar = car[i];       //save the values at index i
          tempcharge = charges[i];
          temphours= hours[i];
    
          car[i] = car[j]; //swap car number  and all values at index i
          charges[i] = charges[j]; //swap charges
          hours[i] = hours[j]; //swap hours used
    
          car[j] = tempcar;  //reassign the temp values from i, down to index j
          chartes[j] = tempcharge;
          hours[j] = temphours;
        }
      }
    }
    You need to change your code so you are saving each car's charges (into a charges array), and each amount of time that they're being charged, so that data is available (if you want it all).

    As it is now, you calculate the charges for each car, but you don't save them into an array, so you can't sort by them.

    Just a btw: using an array of struct for each car, which included the time being charged, the car number, and the charge, as struct members, would make the program a lot easier.

    thank you so much Adak!

    but im kind of clueless in how will i change my code so that i could sort each car and where will i insert the code you've done?

    thanks so much.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    would you mind to edit my code? im just new in C programming and im totally lost with this code. if it's okay to you. thank you very much in advance

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I just can't remember the name of that algorithms. It's notation is O(n^2) but its actual iterations are ((n^2 + n)/2). ( i think )
    Devoted my life to programming...

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by stealthblade View Post
    would you mind to edit my code? im just new in C programming and im totally lost with this code. if it's okay to you. thank you very much in advance
    That's a no no! If you're too lazy to at least try to change your code, you'd better quit programming!
    Devoted my life to programming...

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    Quote Originally Posted by Sipher View Post
    That's a no no! If you're too lazy to at least try to change your code, you'd better quit programming!
    that's not laziness. what im trying to say is that i need everybody's help in solving the problem of my code. i didnt requested for a new code. i asked for your help through editing my code.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You use the sorting block of code, only AFTER you've collected all the data you need. Obviously, you can't sort according to the charges for the cars, if you don't *have* the charges for each car, already calculated and saved somewhere, for reference.

    Straight up, coding takes time, and it takes a lot of practice. I'd be doing you no favors if I did it for you.

    Reread what I wrote, and make those other arrays (charges[] and hours[]), and by all means, put in the work to make it all make sense.

    Meter by meter, life is sweeter, and programming is that way, as well. No one is born with the ability to write code -- it takes work and practice, and recurse().

    If you have further questions, repost your updated code. It's improper to simply ask for someone to edit your code, however.

    Put some skin into this.

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by stealthblade View Post
    that's not laziness. what im trying to say is that i need everybody's help in solving the problem of my code. i didnt requested for a new code. i asked for your help through editing my code.
    In other words, you want us to write your code! Why don't you try first, and if you get stuck somewhere you ask again for advise.

    EDIT: And what Adak said!
    Devoted my life to programming...

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    Code:
    #include <stdio.h>
    
    void swapValues();
    int a, b;
    float tempcharge, tempcar, temphours, charges[], hours[];
    
    float calculateTimeSpent(float a, float b, float c, float d){
    	float timeSpent, x;
    	if (d < b){
    		c--;
    		d +=60;
    	}
    	timeSpent = (c - a) + (d - b)/60;
    	return timeSpent;
    }
    
    float timeEntryCars(){
    	int hhEntry, mmEntry, hhExit, mmExit, hour, minute;
    	printf("Enter time of entry: ");
    	scanf("%d:%d", &hhEntry, &mmEntry);
    	printf("Enter time of exit: ");
    	scanf("%d:%d", &hhExit, &mmExit);
    	return calculateTimeSpent(hhEntry, mmEntry, hhExit, mmExit);
    }
    
    float calculateCharges(float x){
    	int ans = 45;
    	int remainder, quotient, y;
    	if (x<=2) ans = 45;
    	else{
    		x -=2;
    		y = x*100;
    		remainder = y%50;
    		quotient = y/50;
    		if (remainder >= 0) ans = ans + (quotient*10);
    		else ans = ans + ((quotient+1)*10);
    	}
    	return ans;
    	}
    	int main(){
    	int Cars;
    	printf("input number of Cars: ");
    	scanf("%d", &Cars);
    		float car[Cars];
    		int charge;
    		int i, j;
    		float totalHours = 0;
    		int totalCharge = 0;
    
    void swapValues(){
        for(a=0;a<=Cars-1; a++) {
        for(j = a+1;b<=Cars; b++) {
        if(charges[a] > charges[b]) {
          tempcar = car[a];       //save the values at index i
          tempcharge = charges[a];
          temphours= hours[a];
    
          car[a] = car[b]; //swap car number  and all values at index i
          charges[a] = charges[b]; //swap charges
          hours[a] = hours[b]; //swap hours used
    
          car[b] = tempcar;  //reassign the temp values from i, down to index j
          charges[b] = tempcharge;
          hours[b] = temphours;
        }
      }
    }
    }
    		for (i = 0; i<=Cars-1; i++) {
    			printf("For car %d>> \n", i + 1);
    			car [i] = timeEntryCars();
    		}
    
    		printf("Car \t Hours \t Charge\n");
    
    swapValues();
    		for (j = 0; j<=Cars-1; j++) {
    			charge = calculateCharges(car[j]);
    
    			printf("%d\t %.2f\t %d\n", j + 1, car[j], charge);
    			totalHours +=car[j];
    			totalCharge +=charge;
            }
    		printf("TOTAL\t %.2f\t %d\n", totalHours, totalCharge);
    
    		return 0;
    	}
    still not working properly. :'(

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For anything but the most trivial of programs, you can expect to have some bugs, initially. The trick is, to go through each function, then each block of code within the suspect function, and finally, get it down to just one suspect line of code.

    The more you code, the better you'll get at troubleshooting your code, you can be sure.

    So what function is not working right? Insert print statements where needed to check the value of the variables before your program leaves the function you're checking. Also, using a debugger, and watching the value of some variables as you step through the code, can be a real god-send.

    One problem is you've made a, and b global variables. Don't do that, because your program wasn't designed to work that way. Declare the variables in main(), and then pass them around as parameters, ONLY.

    The reason is that when you have any two variables with the same name, in the same function, the program will use ONE of them (usually the local variable only), and the result will be an inaccurate program.


    Also, this:
    Code:
    a<=Cars-1;
    should be:
    Code:
    a<Cars;
    to avoid mind-numbing confusion.
    Last edited by Adak; 02-04-2011 at 09:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Book Sorting Problem - Help
    By The Conqueror in forum C Programming
    Replies: 3
    Last Post: 11-20-2010, 01:18 PM
  2. Sorting problem?
    By audinue in forum C Programming
    Replies: 5
    Last Post: 01-06-2009, 02:16 PM
  3. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  4. problem in sorting
    By ashish singh in forum C Programming
    Replies: 2
    Last Post: 07-17-2008, 02:24 AM
  5. What is problem about the sorting?
    By Mathsniper in forum C Programming
    Replies: 2
    Last Post: 04-17-2005, 07:00 AM