+ Reply to Thread
Results 1 to 9 of 9

Thread: Looping statement problem : TOTAL Course fee

  1. #1
    Newbie syarif20 is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    5

    Looping statement problem : TOTAL Course fee

    ~Write a complete C program using while loop to compute total course fees for students.
    ~Ask the user to enter their name(string), id no(string) and duration of study(integer).
    ~Suppose that the course fee is starting from RM10000 in the first year and increases 5% the following years.
    ~Calculate the annual fee and totala course fees for the whole duration of study.
    ~Dsplay yhe information as shown below.


    Enter name : Ahmad bin Ali
    Enter ID : K12000000
    Enter Duration of study [year] : 3
    --------------------------------------------------------------
    JAPAN MALAYSIA TECHNICAL INSTITUTE
    ---------------------------------------------------------------
    Student name : Ahmad bin Ali
    Student ID :K12000000
    Duration of study :3
    Year Course Fee
    1 RM10000.00
    2 RM10500.00
    3 RM11025.00
    ------------------------------------------------
    Total Course Fees: RM31525.00
    ------------------------------------------------


    I have tried so many time to solve the problem.
    my code isnt working. The total course fees cannot calculated.

  2. #2
    Moderator ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon's Avatar
    Join Date
    Jul 2009
    Location
    Nowhere, Washington
    Posts
    1,766
    Blog Entries
    40

    Re: Looping statement problem : TOTAL Course fee

    What code do you have so far, it helps to give us what you've got so far?
    Should I get a userbar here?

  3. #3
    Newbie syarif20 is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    5

    Re: Looping statement problem : TOTAL Course fee

    the calculation is not correct..
    total course fee is same as the last fee.

    does i need to use nested loop?

    i need to program with C

  4. #4
    Moderator ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon's Avatar
    Join Date
    Jul 2009
    Location
    Nowhere, Washington
    Posts
    1,766
    Blog Entries
    40

    Re: Looping statement problem : TOTAL Course fee

    Do you have any code? I'd make a double called "courseFee" and "totalFee", then assign courseFee 10000. After that, I'd make a for loop to iterate over each year, however many years that is, and you multiply "courseFee" by 1.05, and then += that value to totalFee, and print out courseFee on each iteration. Something similar to this:
    [highlight=C]double courseFee, totalFee;
    int iii;
    courseFee = 10000;
    for (iii = 0; iii < numYears; ++iii) {
    if( printf("RM%.2f", courseFee) < 0 ) exit (1);
    totalFee += courseFee;
    courseFee *= 1.05;
    }[/highlight]
    What do you have so far?
    Should I get a userbar here?

  5. #5
    Newbie syarif20 is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    5

    Re: Looping statement problem : TOTAL Course fee

    could you explain this line?
    Code:
        if( printf("RM%.2f", courseFee) < 0 ) exit (1);
    here's my code:
    Code:
    for(count=0; count <=year; count+=1)
    {
    printf("Year\t\tCourse Fee:");
    total=coursefee*0.5;
    all+=total;
    }
    Last edited by WingedPanther; 09-12-2009 at 04:27 PM. Reason: add code tags (the # button)

  6. #6
    Moderator ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon's Avatar
    Join Date
    Jul 2009
    Location
    Nowhere, Washington
    Posts
    1,766
    Blog Entries
    40

    Re: Looping statement problem : TOTAL Course fee

    Alright, you're not too far away from what I wrote.

    Code:
    if( printf("RM%.2f", courseFee) < 0 ) exit(1);
    What this code does is simple, it prinfs the string "RM" then follows it with the float (double) value of courseFee with two numbers after the decimal point. The "%" tells printf that it's an argument character (one of the arguments that follow it on printf), then the .2 means print two and only two numbers after the decimal, then "f" means that the value to expect is a float or double. The reason it's inside an if statement is to check printf's return value, which could be negative, and if it is that means it failed to print. If it failed to print, you want the program to quit with status 1 (EXIT FAILURE).

    In your code, the count variable in the for loop should be checked if it's less than (<) year, not less than or equal to (<=), since it starts at zero. That's a common error known as an "Off by one" error. Also, you should iterate count with "count++" instead of "count+=1" since it's easier to write and read.

    The main problem you're having is that coursefee is being multiplied by 0.5, which is half of what it started at, and you're not updating coursefee, so it'll always be the same value. Instead of having an intermittent "total" value, you should instead just do what I did, using *= 1.05 to get the desired effect of increasing coursefee by 5 percent. Also, your "all" variable should be added before changing coursefee (since it starts at 10000 and you don't want to start "all" off at 10500).
    Should I get a userbar here?

  7. #7
    Newbie syarif20 is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    5

    Re: Looping statement problem : TOTAL Course fee

    waaa....thanks you so much! i appreaciated the explaination so much..my problem solved now..

    wish you luck and have a good day!

  8. #8
    Newbie syarif20 is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    5

    Re: Looping statement problem : TOTAL Course fee

    i still had the problem for the total course fee..

    here's my code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    void main()
    { 
    
    	int numYears;
    	double courseFee=10000, totalFee;
    	double grandFee;
    	int iii;
    	char name[40], id[20];
    
    	printf("----------------\n");
    	printf("USER INFORMATION\n\n");
    	printf("----------------\n");
    
    
    	printf("Enter name:");
    	gets(name);
    
    	printf("Student id:");
    	gets(id);
    
    	printf("\n\nDuration of study(year):");
    	scanf("%d",&numYears);
    
    	printf("\n\n----------------------------------\n");
    	printf("JAPAN MALAYSIA TECHNICAL INSTITUTE\n");
    	printf("----------------------------------\n");
    
    	printf("\nYear\t\tCourse Fee\n");
    	
    for (iii = 1; iii <= numYears; ++iii)
    {
        if( printf("\n%d\t\tRM%.2f",iii, courseFee) < 0 ) exit (1);
    
    	totalFee += courseFee;
        courseFee *= 1.05;
    	grandFee+=courseFee;
    
    } 
    printf("\n\nTOTAL\n");
    printf("-------\n");
    printf("Total Course Fee:\nRM%.2lf\n\n",grandFee);
    
    }
    Last edited by WingedPanther; 09-12-2009 at 04:28 PM. Reason: add code tags (the # button)

  9. #9
    Moderator ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon's Avatar
    Join Date
    Jul 2009
    Location
    Nowhere, Washington
    Posts
    1,766
    Blog Entries
    40

    Re: Looping statement problem : TOTAL Course fee

    Please, for future reference, place your code in between [code] and [/code] tags, to make reading your code easier and to maintain whitespace, similarly to how I had it. You can use the # symbol on the top of the reply box to insert those tags.

    Now to the answer:
    First, I highly discourage the use of void main, and I don't care what your professor said. Use int main in every situation, it's standard, and it will work on all computers without any problems. At the end of main you'll have to "return 0;", no big deal, just do it. Second, what is grandFee for? I don't see what it accomplishes except to cause you problems, if you eliminate it entirely, and instead of printing grandFee on the bottom you print totalFee (which should be the right amount) you'll be fine. grandFee is getting the courseFee added to it AFTER your 5% addition to courseFee, which makes it so grandFee is greater than totalFee. Here, let me show you more clearly, assuming 3 years:
    Code:
    courseFee = 10000;
    // year 1:
    totalFee += courseFee; // totalFee == 10000
    courseFee *= 1.05; // courseFee == 10500
    grandFee += courseFee; // grandFee == 10500
    
    // year 2:
    totalFee += courseFee; // totalFee == 20500
    courseFee *= 1.05; // courseFee == 11025
    grandFee += courseFee; // grandFee == 21750
    
    // year 3:
    totalFee += courseFee; // totalFee == 31750 - CORRECT!
    courseFee *= 1.05; // courseFee == 11576.25
    grandFee += courseFee // grandFee == 33326.25 - INCORRECT!
    
    printf(Total Course Fee:\nRM%.2f\n\n", grandFee); // Will end up printing "33326.25", not "31750"!
    Finally, for the love of all that is holy don't use gets()! You know how to use scanf, AT LEAST use that instead, but you really should use fgets() over any other! You'll need to parse the string yourself, but trust me, you'll be better off that way.
    Should I get a userbar here?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. [Win7] Problem Steps Recorder
    By Termana in forum Tutorials
    Replies: 1
    Last Post: 04-14-2009, 09:24 AM
  2. Looping problem ( C )
    By lithiummethoxide in forum C and C++
    Replies: 7
    Last Post: 03-22-2009, 08:08 PM
  3. If Statement Problem
    By .Sin in forum Visual Basic Programming
    Replies: 3
    Last Post: 03-08-2009, 08:53 AM
  4. Replies: 3
    Last Post: 10-18-2008, 12:05 PM
  5. Help! Rlink32 Error
    By Sparky in forum Pascal/Delphi
    Replies: 6
    Last Post: 10-16-2008, 10:31 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts