UNIVERSITI TUN HUSSEIN ONN MALAYSIA
         FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING
         BTI 10202: COMPUTER PROGRAMMING


                      LAB 1 : Computer Programming Basics
 NAME       : _________________________________
 MATRICS NO.: _______________ DATE : _________                                      (MARKS)

Objectives:
   1. Identify the variables, input/output statements and program symbolic constant
   2. Learn how to write pseudo code
   3. Learn how to draw flowchart

Notes:
                 Algorithms is a procedure or a set of plan for problem solving
    Basic rules for design method:
     1.      Consist of a statement of instructions in sequence.
     2.      Every step consists of keyword.
     3.      Every step should be written in different line, if continued, next row must be
         indented.
     4.      if/else for condition, while/do for repetition
     5.      Every step must contain clear statement and easy to understand
     6.      Use start for beginning of operation, and end/halt for finishing it.


    Pseudo code is an artificial and informal            Flow chart is a graphical informal set of
    set of instruction that helps to develop a              instruction that help to develop a
          program which written in line                 program by using shapes representation
Example:                                               Example:           Start


 Start                                                                    Display

                                                                       “Enter Value
 Display “Enter value X: ”                                                  X”

                                                                           Get X
 Get value X

 Calculate result = X x 3                                                Calculate

                                                                       Result = X x 3
 Display Result
                                                                          Display

                                                                          Result
 End
                                                                           End



Exercise 1:
Determine the purpose of each of the following C programs. Identify all the variables within
each program. Identify all input and output statements, all assignment statements and any
other special features that you recognize.

    (a) main( )
        {
                   printf(“Welcome to this computer programming class!n”);
         }

         Purpose: ________________________________________________________________
         Variables: _______________________________________________________________
         Input statement: __________________________________________________________
         Output statement: ________________________________________________________
         Assignment statement_____________________________________________________
         Special feature: ___________________________________________________________

    (b) #define MESSAGE “Welcome to this computer programming class!n”

         main( )
         {
                   printf(MESSAGE);
         }

         Purpose: ________________________________________________________________
         Variables:________________________________________________________________
         Input statement:__________________________________________________________
         Output statement:_________________________________________________________
         Assignment statement:_____________________________________________________
         Special feature:___________________________________________________________
(c) main( )
       {
                 float gross, tax, net;
                 printf(“Gross salary: “);
                 scanf(“%f”, &gross);
                 tax = 0.14*gross;
                 net = gross – tax;
                 printf(“Taxes withheld: %.2fn”, tax);
                 printf(“Net salary: %.2f”, net);
       }

       Purpose: ________________________________________________________________
       Variables:________________________________________________________________
       Input statement:__________________________________________________________
       Output statement:_________________________________________________________
       Assignment statement:_____________________________________________________
       Special feature:___________________________________________________________


Exercise 2:
Analyze the problem below.

  Compound interest
  A common problem in personal finance is that of determining how much money will
  accumulate in a bank account after n years if a known amount, P, is deposited initially and
  the account collects interest at a rate if r percent per year, compounded annually. This can be
  determined by the well-known formula

                                             F=P(1+i)n
  where F represents the future accumulation of money (including the original sum, P, which is
  known as the principal) and i is the decimal representation of the interest rate; i.e., i = r/100
  (for example, an interest rate of r = 5% would correspond to i = 0.05).

  Consider the organization of a C program that will solve this problem. The program will be
  based upon the following general outline.
     1. Declare the required program variables.
     2. Read in values for the principal (P), interest rate (r) and the number of years (n).
     3. Calculate the decimal representation of the interest rate (i), using the formula i =
         r/100.
     4. Determine the future accumulation (F) using the formula F = P(1+i)n
     5. Display the calculated value for F.


2.1 Write down the program outline in the form of pseudo code.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________




2.2 Translate the following program into pseudo code and flowchart.


       //simple compound interest problem

       #include<stdio.h>
       #include<conio.h>
       #include<math.h>

       main()
       {
                float p, r, n, i, f;
                //read input data (including prompts)
                printf(“Please enter a value for the principal (p): “);
                scanf(“%f”, &p);
                printf(“Please enter a value for the interest rate (r): “);
                scanf(“%f”, &r);
                printf(“Please enter a value for the number of years (n): “);
                scanf(“%f”, &n);

                //calculate i, then f
                i=r/100;                //interest in decimal
                f=p*pow((1+i),n);       //future accumulation of money

                //display the output
                printf(“nThe final value (F) is: %.2fn”, f);
                getch();
       }
Flowchart

Lab sheet 1

  • 1.
    UNIVERSITI TUN HUSSEINONN MALAYSIA FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING BTI 10202: COMPUTER PROGRAMMING LAB 1 : Computer Programming Basics NAME : _________________________________ MATRICS NO.: _______________ DATE : _________ (MARKS) Objectives: 1. Identify the variables, input/output statements and program symbolic constant 2. Learn how to write pseudo code 3. Learn how to draw flowchart Notes: Algorithms is a procedure or a set of plan for problem solving Basic rules for design method: 1. Consist of a statement of instructions in sequence. 2. Every step consists of keyword. 3. Every step should be written in different line, if continued, next row must be indented. 4. if/else for condition, while/do for repetition 5. Every step must contain clear statement and easy to understand 6. Use start for beginning of operation, and end/halt for finishing it. Pseudo code is an artificial and informal Flow chart is a graphical informal set of set of instruction that helps to develop a instruction that help to develop a program which written in line program by using shapes representation
  • 2.
    Example: Example: Start Start Display “Enter Value Display “Enter value X: ” X” Get X Get value X Calculate result = X x 3 Calculate Result = X x 3 Display Result Display Result End End Exercise 1: Determine the purpose of each of the following C programs. Identify all the variables within each program. Identify all input and output statements, all assignment statements and any other special features that you recognize. (a) main( ) { printf(“Welcome to this computer programming class!n”); } Purpose: ________________________________________________________________ Variables: _______________________________________________________________ Input statement: __________________________________________________________ Output statement: ________________________________________________________ Assignment statement_____________________________________________________ Special feature: ___________________________________________________________ (b) #define MESSAGE “Welcome to this computer programming class!n” main( ) { printf(MESSAGE); } Purpose: ________________________________________________________________ Variables:________________________________________________________________ Input statement:__________________________________________________________ Output statement:_________________________________________________________ Assignment statement:_____________________________________________________ Special feature:___________________________________________________________
  • 3.
    (c) main( ) { float gross, tax, net; printf(“Gross salary: “); scanf(“%f”, &gross); tax = 0.14*gross; net = gross – tax; printf(“Taxes withheld: %.2fn”, tax); printf(“Net salary: %.2f”, net); } Purpose: ________________________________________________________________ Variables:________________________________________________________________ Input statement:__________________________________________________________ Output statement:_________________________________________________________ Assignment statement:_____________________________________________________ Special feature:___________________________________________________________ Exercise 2: Analyze the problem below. Compound interest A common problem in personal finance is that of determining how much money will accumulate in a bank account after n years if a known amount, P, is deposited initially and the account collects interest at a rate if r percent per year, compounded annually. This can be determined by the well-known formula F=P(1+i)n where F represents the future accumulation of money (including the original sum, P, which is known as the principal) and i is the decimal representation of the interest rate; i.e., i = r/100 (for example, an interest rate of r = 5% would correspond to i = 0.05). Consider the organization of a C program that will solve this problem. The program will be based upon the following general outline. 1. Declare the required program variables. 2. Read in values for the principal (P), interest rate (r) and the number of years (n). 3. Calculate the decimal representation of the interest rate (i), using the formula i = r/100. 4. Determine the future accumulation (F) using the formula F = P(1+i)n 5. Display the calculated value for F. 2.1 Write down the program outline in the form of pseudo code. ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________
  • 4.
    ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ ______________________________________________________________________________ 2.2 Translate thefollowing program into pseudo code and flowchart. //simple compound interest problem #include<stdio.h> #include<conio.h> #include<math.h> main() { float p, r, n, i, f; //read input data (including prompts) printf(“Please enter a value for the principal (p): “); scanf(“%f”, &p); printf(“Please enter a value for the interest rate (r): “); scanf(“%f”, &r); printf(“Please enter a value for the number of years (n): “); scanf(“%f”, &n); //calculate i, then f i=r/100; //interest in decimal f=p*pow((1+i),n); //future accumulation of money //display the output printf(“nThe final value (F) is: %.2fn”, f); getch(); }
  • 5.