Friday, June 18, 2010

cs101 assignment 4 solution



solution:

 

The following can help explain the concept of algorithms in relation to programming.

Problem: Calculate the average of three given numbers.
Let the given numbers be a, b, c
Step 1: Set aside memory for all values (a, b, c,)
Step 2: Assign the values to a memory location (the given numbers)
Step 3: Calculate the average of the values using the formula (a+b+c)/3
Step 4: Display the answer in the proper format, in this case the average.
The problem statement is programmatically solved with the use of four steps in the algorithm.

Problem:Find the gain or loss of an item, as the case may be, and compute the gain or loss percentage, given the cost price and the selling price.

Gain = S.P. (sell price) - C.P (cost price).
Gain % = (Gain * 100)/C.P.
Loss = C.P. - S.P.
Loss % = (Loss * 100)/C.P.
(Percentage gain or loss is calculated on the C.P.)

Algorithm:
Step 1: Obtain the values of S.P. and C.P. Set aside memory for S.P. and C.P., gain percent and loss percent.
Step 2: Assign values to memory location.
Step 3: Perform mathematical computations.
Step 4: Display results in the proper format.

If C.P. = S.P, then
Declare "There is neither gain nor loss in the transaction"
Stop
If S.P. > C.P., then
Calculate Gain = S.P - C.P
Calculate Gain % = (Gain * 100)/C.P
Write "Answer: Gain is $" followed by value of Gain
Write "Answer: Gain % is" followed by value of Gain %
Stop
Else
Calculate Loss = C.P. - S.P
Calculate Loss % = (Loss * 100)/C.P.
Write "Answer: Loss is $" followed by value of Loss
Write "Answer: Loss % is" followed by values of Loss %
Stop


The algorithm is executed in order, starting with Step 1:
In step 1, we set aside memory locations for S.P. and C.P
In step 2, we assign the numerical values of S.P. and C.P. to the memory locations.
In step 3, the answer to the phrase If C.P. - S.P. could be yes or no. If the answer is yes, then only the instructions following the word "then" are executed.
In step 3, if the answer to the phrase S.P. > C.P. is yes, then steps to a to e before else are executed. If the answer to S.P. > C.P. is no, implying that C.P. >= S.P., steps a to e after else are executed.
Step 4 is the last in the process. The result of the computations is displayed and the algorithm is terminated after its execution.

Words such as "IF", "THEN", and "ELSE are used to emphasize their distinctive nature in executing the algorithm.
Flowcharts

When thinking of a Flowchart think of a visual representation of how a process works, depicting the sequence of steps at a minimum. A flowchart offers many advantages including the following.
Helps locate the key elements of a process. It draws clears lines between the end of one process and the start of another.
Brings clarity and improvement to a process.
Helps identify appropriate areas for improvement in a program.

The following shows a format of a sample flowchart that identifies the starting and ending points of a sequential process with decision points.

 

 

Flowcharts depict different types of actions and steps in a process through special shapes. The sequence of steps and the relationships between these steps is shown using arrows. The following is a list of the different shapes in a flow chart and what they represent.

The following shows the flowchart of gain and loss for a business.

 Pseudocode

Pseudocode, aka false code, is an informal language for relating problem solving steps. The steps used in pseudocode are simple and can be implemented with ease into any programming language. Pseudocode uses a combination of English words, mathematical notations, and a set of capitalized keywords such as BEING, END, READ, PRINT, FREAD, FPRINT, IF, ELSE, ELSEIF, ENDIF, WHILE, DO and ENDWHILE.

Pseudocode consists of:
Fundamental control structures such as Sequence, Selection and Repetition.
Three other types of statements, which include Input, Output and Assignment.
Subroutines or small programs that can be added to modularize pseudocode. The subroutines are also written using the pseudocode notation.

Here is an example of pseudocode.

Begin procedure
Input Widget Price
Taxes = 0.06 x Widget Price
Amount Due = Widget Price + Taxes
Output Taxes and Amount Due
End procedure

.....................

CS 101 Introduction to Computing

Assignment # 04

 

Deadline

Your assignment must be uploaded / submitted before or on 23-06-2010.

Upload Instructions

Please view the assignment submission process document provided to you by the Virtual University to upload the assignment.

Rules for Marking

Please note that your assignment will not be graded if:
·         It is submitted after due date
·         The file you uploaded does not open
·         The file you uploaded is copied from some one else
·         It is in some format other than .doc

Objective

The assignment has been designed to enable you:
  • To learn how to write algorithm for Problems.

  • To get knowledge of flowcharting.

  • To enhance the thinking and vision of student for specific problem domains.


Write an algorithm in pseudo code form for the following problem and also draw a Flow chart for the same problem statement (5+5)

There is a multinational company. In the company, there are deductions from the salary of the employee according to following rules:
i)  If salary is less than 10,000 then no tax deduction and 4% of salary will be deducted as Well-fare fund every month.
ii) If salary is more than 10,000 and less than 20,000 then 3% of salary as tax deduction and 6% of remaining salary as employee well-fare fund will be deducted every month.

iii) If salary is equal to or more than 20,000 then  5 % of the salary as tax deduction and 8% of remaining salary as employee well-fare fund  will be deducted every month .

Take salary input from user and after appropriate deduction show the net payable amount for month.
Using Loop structure calculate the total salary; paid tax and well fare fund of that particular employee for two years and display these values at end.

0 comments:

Post a Comment