You are on page 1of 16

CSC 1101

Assignment 1

Table of Contents
Question 1.................................................. 2 a. .............................................................. 2 b. .............................................................. 2 c. .............................................................. 3 Question 2.................................................. 4 a. Problem 1 ............................................. 4 b. Problem 2 .............................................7 6 Question 3................................................ 11

CSC 1101

Assignment 1

Question 1 a. Explain FOUR (4) rules of naming and using variables. 1. A variable name should not have the same exact word as the reserved word in the programming language. For example, the mathematical function of SQRT. 2. The named variables should have the exact name or word throughout the entire program. For example, if multiply is used in the counting program, it should be used consistently and should not be changed to MULTIPLY or MultiPly. 3. In naming a variable, it should be named according to what it is representing and be consistent with uppercase and lowercase characters. 4. Do not use dash or any mathematical symbol in naming a variable. The variable name should be combined or use underscore ( _ ) if there is more than one word. For example, totaldays or total_days.

b. Suggest the suitable variable name and data type to represent each of the following data. i. Number of passenger in a bus : Example 3, 41, 27 Variable name : numberpassenger Data type : numeric - integer

ii.

Clothing size : Example S, M, L Variable name : clothsize Data type : character

iii.

Zip code of a city : Example 11900, 05350, 72000 Variable name : zipcode Data type : string

CSC 1101

Assignment 1

iv.

Students CGPA : Example 2.88, 3.64, 1.65 Variable name : cgpa Data type : numeric - real

v.

Signal of an electronic circuitry pulse : Example 0, 1 Variable name : signalpulse Data type : logical

c. Give the value of each R for the values A=5, B=2, C=True, D=False, E=False. Show all the steps involved. i. R = A + 3 > B - 1 AND C OR D R = 5 + 3 > 2 1 AND True OR False R = 8 > 1 AND True OR False R = True AND True OR False R = True OR False R = True

ii.

R = A > B AND NOT (D OR C) OR NOT (93 MOD 3 < 0 AND C) R = 5 > 2 AND NOT (False OR True) OR NOT (0 < 0 AND True) R = True AND NOT True OR NOT (False AND True) R = True AND False OR NOT False R = True AND False OR True R = False OR True R = True

iii.

R = (64\12 > 5 OR NOT C) AND (3 + B = A/1) R = (5 > 5 OR NOT True) AND (3 + 2 = 5/1) R = (False OR False) AND (5 = 5) R = False AND True R = False

CSC 1101

Assignment 1

Question 2 For both problems below, produce a PAC, a structure chart, an IPO chart, an algorithm and a C++ code. a. Problem 1 John is going to present a research paper that he has done in a conference, attended by more than 20 academic and industrial people. He wanted to print handouts to all the people attending the conference. He has to know the total number of reams of paper that he needed to buy. One ream of paper consists of 400 sheets of paper. He has to calculate how many pages is the handout and multiply it by the number of people attending. Problem Analysis Chart (PAC) Given Data 400 sheets per ream Total handouts Total pages Required Processing totalsheets = totalpages*totalhandouts totalreams = totalsheets/400 Solution Alternatives 1.The totalpages and totalhandouts can be define as a user input variable. Required Results totalreams

Structure Chart
Counter CONTROL 0000

Read 1000

Calculate 2000

Display 3000

CSC 1101

Assignment 1

IPO Chart MODULE INPUT PROCESSING REFERENCE NUMBER


totalhandouts totalpages START ENTER totalhandouts, totalpages totalsheets=totalpages*totalhandouts totalreams = totalsheets/400 DISPLAY totalreams END 0000 1000 2000 2000 3000 0000 1. total reams 2. all input data

OUTPUT

Algorithm ALGORITHM Counter CONTROL (0000) START

Read (1000) ENTER totalhandouts, totalpages

Calculate (2000) totalsheets=totalpages*totalhandouts totalreams=totalsheets/400

Display (3000) DISPLAY totalreams

END

CSC 1101

Assignment 1

C++ Code

Program code

Output screen

CSC 1101

Assignment 1

b. Problem 2 A person invests RM 1000 in a saving account yielding 5% interest. Assuming that all interest is left on deposit, calculate and print the amount of money in the account at the end of each year for 10 years. The dividend of the saving is given to the investor once a year. Problem Analysis Chart (PAC) Formula : Total amount = amount * (1+ interest/dividend) ^ (dividend * years)
y1-year 1, y2-year 2, y3-year 3, y4-year 4, y5-year 5, y6-year 6, y7-year 7, y8-year 8, y9-year 9, y10-year 10, H- flexible year.

Given Data
Amount= RM 1000 Interest = 5% Years=10 Dividend=1

Required Results
H,y1, y2, y3, y4, y5, y6, y7, y8, y9, y10

Required Processing
interest1=interest/100 F=1+(interest1/dividend) G=dividend * years H= amount * pow(F,G) y1 =amount*pow(F,(dividend*1)) y2 =y1*pow(F,(dividend*1)) y3 =y2*pow(F,(dividend*1)) y4 =y3*pow(F,(dividend*1)) y5 =y4*pow(F,(dividend*1)) y6 =y5*pow(F,(dividend*1)) y7 =y6*pow(F,(dividend*1)) y8 =y7*pow(F,(dividend*1)) y9 =y8*pow(F,(dividend*1)) y10=y9*pow(F,(dividend*1))

Solution Alternatives
1.amount, interest, years and dividend can be define as user input variables.

CSC 1101

Assignment 1

Structure Chart
Invest CONTROL 0000

Read 1000

Calculate 2000

Print 3000

IPO Chart MODULE INPUT PROCESSING REFERENCE NUMBER


amount interest years dividend
START ENTER amount, interest, years, dividend interest1=interest/100 F=1+(interest1/dividend) G=dividend * years H= amount * pow(F,G) y1 =amount*pow(F,(dividend*1)) y2 =y1*pow(F,(dividend*1)) y3 =y2*pow(F,(dividend*1)) y4 =y3*pow(F,(dividend*1)) y5 =y4*pow(F,(dividend*1)) y6 =y5*pow(F,(dividend*1)) y7 =y6*pow(F,(dividend*1)) y8 =y7*pow(F,(dividend*1)) y9 =y8*pow(F,(dividend*1)) y10=y9*pow(F,(dividend*1)) PRINT H,y1, y2, y3, y4, y5, y6, y7, y8, y9, y10 END 0000 1000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 3000 0000

OUTPUT

1. H,y1, y2, y3, y4, y5, y6, y7, y8, y9, y10 2. all input data

CSC 1101

Assignment 1

Algorithm
ALGORITHM Invest CONTROL (0000) START

Read (1000) ENTER amount, interest, years, dividend

Calculate (2000) interest1=interest/100 F=1+(interest1/dividend) G=dividend * years H= amount * pow(F,G) y1 =amount*pow(F,(dividend*1)) y2 =y1*pow(F,(dividend*1)) y3 =y2*pow(F,(dividend*1)) y4 =y3*pow(F,(dividend*1)) y5 =y4*pow(F,(dividend*1)) y6 =y5*pow(F,(dividend*1)) y7 =y6*pow(F,(dividend*1)) y8 =y7*pow(F,(dividend*1)) y9 =y8*pow(F,(dividend*1)) y10=y9*pow(F,(dividend*1))

Print (3000) PRINT H,y1, y2, y3, y4, y5, y6, y7, y8, y9, y10 END

CSC 1101

Assignment 1

C++ Code

Program code

10

CSC 1101

Assignment 1

Output screen

Question 3 Ask the user to enter two dates (day, month and year); obtains the dates and calculate the number of days between the two dates. Assume that there are 30 days in every month. Check your answer with the following pairs of dates : i. ii. iii. iv. 1-5-2006 until 4-7-2007 1-5-2006 until 4-4-2007 1-5-2006 until 28-6-2007 1-3-2006 until 15-3-2007

Produce PAC, a structure chart, an IPO chart, a flowchart and a C++ code. A-start date, B-start month, C-start year, D-end date, E-end month, F-end year, G-unwanted days in start year, H-unwanted days in start month, I-wanted days in end year, J-unwanted days in end year

11

CSC 1101

Assignment 1

Problem Analysis Chart (PAC) Given Data 30 days per month Date(A, B, C, D, E, F) Required Processing years=F-C+1 month1=12-B+1 H=30-(31-A) G=360-(month1*30)+H month2=12-E+1 I=360-(month2*30)+D J=360-I numberofdays= (years*12*30) - G - J Solution Alternatives 1.Date is a variable 2.Days in a month is a constant 3.Months in a year is a constant Required Results numberofdays

Structure Chart
Calendar MODULE 0000

Read 1000

Calculate 2000

Display 3000

12

CSC 1101

Assignment 1

IPO Chart MODULE INPUT PROCESSING REFERENCE NUMBER


A B C D E F START ENTER A, B, C, D, E, F years=F-C+1 month1=12-B+1 H=30-(31-A) G=360-(month1*30)+H month2=12-E+1 I=360-(month2*30)+D J=360-I numberofdays= (years*12*30) - G J DISPLAY numberofdays END 0000 1000 2000 2000 2000 2000 2000 2000 2000 2000 3000 0000 1.number of days 2.all input data (A, B, C, D, E, F)

OUTPUT

13

CSC 1101

Assignment 1

Flowchart
START

ENTER A, B, C, D, E, F

years=F-C+1

month1=12-B+1

H=30-(31-A)

G=360-(month1*30)+H

month2=12-E+1

I=360-(month2*30)+D

J=360-I

numberofdays= (years*12*30) - G J

DISPLAY numberofdays

END

14

CSC 1101

Assignment 1

C++ Code

Program code

15

CSC 1101

Assignment 1

Output screen

16

You might also like