Chapter One: Introduction To Computer Programs
Chapter One: Introduction To Computer Programs
Upon completion of this chapter, each student must understand the concepts of: Objective1#
Objective2#
Objective3#
Objective4#
Important terminology
Objective5#
1.1
must
be
able
to
conquer
the
concepts
of
programming.
1.2
1.3
Proper documentation.
Comments
Eg : 3
Every program you write that you intend to keep around for more
than a couple of hours ought to have documentation in it. Don't talk
yourself into putting off the documentation. A program that is
perfectly clear today is clear only because you just wrote it. Put it
away for a few months, and it will most like take you a while to
figure out what it does and how it does it. If it takes you a while to
figure it out, how long would it take someone else to figure it out?
4.
5.
1.4
Important terminology
Logic errors the real result is not the same as with the expected
result. Or wrong output in a syntactically correct program
30.
31.
32.
33.
34.
1.5
For CSC425, we focus on the first phase of PDLC only. This phase
is consists of several tasks :o
Problem 1 (e.g):
Calculate the product of two numbers.
i.
ii.
Program specification.
Input : first number, second number
Process : multiply the first number with the second number
Output : Display the result of multiplication.
iii.
Algorithm (Pseudo-code)
1. Input first number
2. Input second number
3. result = first number * second number
4. Output The result of multiplication for both numbers result
Problem 2 (e.g) :
Calculate the average of 5 marks
i.
ii.
Program Specification.
Input : mark1, mark2, mark3, mark4, mark5
Process : average = (mark1 + mark2 + mark3 + mark4 +
mark5) / 5
Output : Display the average
iii.
Algorithm
1. Input mark1, mark2, mark3, mark4, mark5
2. average = (mark1 + mark2 + mark3 + mark4 + mark5) / 5
3. Output The average for 5 numbers average
#include <iostream>
Programming Concepts
int main()
{
int a, b, result;
cin >> a;
int main()
: ";
cin >> b;
int a, b;
result = a * b;
cout << "\n Result of " << a << " * "
cin >> a;
return 0;
: ";
cin >> b;
cout << "\n Result of " << a << " * "
<< b << " is " << multiply(a,b);
First
Sample
of
Procedureal
Programming Concepts
// Author : Norlis Othman
return 0;
}
// two numbers
{
return x * y;
#include <iostream>
file
private :
int a,b, result;
public :
// two numbers
#include <iostream>
int result1();
void displayresult();
class multiply {
~multiply();
};
{
cout << "\n end ";
{
a = x;
b = y;
#include <iostream.h>
#include "multiply.h"
void main()
int multiply::result1()
int a, b;
result = a * b;
multiply t;
return result;
cin >> a;
cout << "\n Enter the second number
void multiply::displayresult()
: ";
cin >> b;
cout
<<
"\n
The
result
after
t.getdata(a,b);
t.result1();
result;
t.displayresult();
t.~multiply();
}
multiply::~multiply()
Second Sample of Non Procedural Programming Concepts
// Author : Norlis Othman
// Date : 18 May 2014
//Program to get the average of 5 marks
#include <iostream>
void main()
10
{
float mark1, mark2, mark3, mark4, mark5, average;
cout << "\n Please enter the first mark : ";
cin >> mark1;
cout << "\n Please enter the second mark : ";
cin >> mark2;
cout << "\n Please enter the third mark : ";
cin >> mark3;
cout << "\n Please enter the fourth mark : ";
cin >> mark4;
cout << "\n Please enter the fifth mark : ";
cin >> mark5;
average = (mark1 + mark2 + mark3 + mark4 + mark5)/5;
cout << "\n The average mark is : " << average;
}
mark5, ave;
void input(void);
marks
float average(void);
#include <iostream>
void main()
{
11
input();
<< average();
mark : ";
void input()
mark : ";
mark : ";
cin >> mark1;
float average()
mark : ";
mark4 + mark5)/5;
return ave;
mark : ";
12