COMP104 : Programming

Lab 2 : More C++ Basics


For Lab2 you will write two programs. Please raise your hand if you need any help.

1.    Write a weight program for a hawker selling fruit. First ask the user for the amount of fruit to buy, such as 3 apples and 2 oranges. Then find the minimum number of 100g, 50g, 20g, 10g and 5g weights for weighing the fruit. An apple has a weigh of 105g and an orange has a weight of 120g. Your program should produce input/output that looks like the following:

    Weight program
    Enter the number of apples to buy: 3
    Enter the number of oranges to buy: 2
    100g-weight: 5
    50g-weight : 1
    20g-weight : 0
    10g-weight : 0
    5g-weight  : 1

2.   Write a program that takes the year as input, and report the number of days for that year. The number of days in February is 29 in a leap year and is 28 in a non-leap year. A year is NOT leap year if it is not divisible by 4. If the year is divisible by 100, it's not a leap year UNLESS it is also divisible by 400. Hint: you can use if-else (or switch) statements to classify the case.

Here is some sample input/output. Here is some sample input/output.
 

    Leap Year Calculation
    Enter the year: 1900
    1900 is NOT a leap year.
    The number of days in 1900 is 365.
 

To get full marks, the indentation in your C++ program must be reasonable. For example, starting all the lines in the first column is not acceptable for if statements. See the programs in the lecture notes for examples of reasonable indentation.



Demo your working programs for your TA (the TA will also check the indentation of your source program for part 2). If for some reason you cannot finish before the end of the lab period, email your program to your TA by Sunday 18September 5PM (please include your name and lab section as a comment on line 1 of your program).