Anda di halaman 1dari 66

CS001P

( Week 2-2)

Programming Process
1.
2.
3.
4.

Understanding the problem


Planning the logic
Coding the program
Using software to translate the program into
machine language
5. Testing the program
6. Putting the program into production

Understanding the Programming Process

It is very important to understand the


whole programming process. From the
why to the how and then to the what,
understanding the reasons behind
programming and how it communicates
with the computer is important.

In understanding the processes you need to do the ff:

Understanding the problem


Planning the Logic
Coding the program
Using Software to translate the program
into machine languages
Testing the program
Putting the program into production

1. Understanding the task


Before you even start pressing buttons on
your keyboard and spending a lot of
time on it, you have to know why you
are doing it in the first place. What
problem do you have to solve anyway?
What process are you trying to
streamline? What error are you trying to
fix?

Understanding the task


Example:
www.nasa.gov, at the National Aeronautics
and Space Administration, scientists have to
understand every detail in a problem. Any
mistake will certainly result in a disaster.
Launching a rocket into space is certainly
something complicated. All the scientists
have to cooperate in making a successful
rocket launch

Analyze the Problem


Analyst must understand
What outputs are required
What inputs will be needed
How the inputs can be used to produce the
desired outputs

Failure to analyze and understand the


requirements leads to a failed solution!

You have to understand the task at hand


before you even start programming.
Understanding the task is crucial because if
you do not understand the problem then you
will not be able to solve it. The chances of
solving a problem that you do not understand
are very slim and more often than not,
impossible.
NOTE: Understand what the users need or want

2. Planning the Logic


The heart of the programming process lies in
planning the programs logic.
Once you understand your problem, you have to devise a plan
to solve it. Part of your plan includes the selection of an
appropriate programming language. In the case of the Hello
World problem, almost all programming languages can get
this job done. The selection of programming language
becomes more and more complex as the problem becomes
more and more complex. Learning the strengths and
weaknesses of each programming language will allow you to
choose the best course of action for a problem. Later on, you
will learn the different types of programming languages
including some of their strengths and weaknesses.

Aside from choosing an appropriate programming language, you


have to make sure to accomplish tasks within reasonable resources.
You should not be spending too much time on a project or you
should not be spending too much money on solving a problem.
You have to manage the way you address the problem. Planning a
schedule for solving the problem is important. Time and money
are limited resources. You have to be smart when you budget
these two resources in solving a problem. Do not spread yourself
too thin and do not expend too many resources. You have to be
efficient.
The approach to solving a problem is critical. Efficient programs
are made by expert programmers. These programs are
comprehensible, effective, and resource friendly. It takes time and
experience to develop programs like this. As a beginner, you can
take on simple problems and tasks and try to find the best way to
solve it. Develop good programming habits early on.
NOTE: You may hear programmers refer to
planning a program as developing an algorithm

Develop a Solution
Algorithm: the exact steps used to solve a
problem
Algorithm is usually defined at high level, then
refined to detailed lower levels
Structure level diagrams may be used to represent
the levels of analysis
Two most common planning tools:

Flowcharts
Pseudocode

Planning the logic includes thinking


carefully about all the possible data
values a program might encounter and
how you want to handle every scenario
The process of walking through a
programs logic on paper before you
actually write the program is called
DESK-CHECKING

3. Coding the Program


Programmers choose a particular language
because some languages have built-in
capabilities to make them more efficient than
others at handling certain types of operations.

3. Coding the Program


Coding is one of the most tedious parts in programming.
Some programs will take weeks or even months to code.
After the plan has been drafted, the solution will usually
come in one big chunk. This big program is subdivided
into several modules. If the modules are still too big
then they are divided even further. When coding can
already be done, the programmer thinks of the best way
a program can be made. Once the programmer knows
what to do, coding begins. Coding is literally typing
commands on the computer. Most programmers feel
that coding is the most tedious part in programming. If
you do not want to go through coding over and over
again then you have to make sure that you plan properly.

Coding the Program


Translating the program into machine language
Higher level programming languages like Java, C#, and VB are very far
fetched from the lower level programming languages like FORTRAN and
COBOL in so far as machine language is concerned. Any programming
language that is not in machine language has to be converted into
machine language in order for the computer to be able to understand it.
Computers process information in ones and in zeroes, not in our
ordinary daily language. The higher level programming languages are
more human and need to be converted all the way to machine language
in order for it to be understood by a computer. The lower level
programming languages also need to be converted to machine language
although they take up lesser resources than the higher level
programming languages.

Coding the Program


Translating the program into machine language

Translation into machine language is done by means of a


compiler or an interpreter. When a program is compiled, the
original program is turned into an executable program that
communicates directly with machine language. When a
program is interpreted, the original program is simply
interfaced with an interpreter to communicate with the
machine. Rarely will you see programs that are purely
compiled or purely translated. In order for programs to be
understood by a computer, they are usually compiled and
interpreted at different levels.

Code the Solution


Also called writing the program, and
implementing the solution
Program should contain well-defined patterns or
structures of the following types:
Sequence
Selection
Iteration

4. Testing and Running the Program


Once a program has been coded, compiled
and or interpreted, you can test if the
program will run or not. Some compilers and
interpreters will enable you to debug a code
when an error is apparent.
After the program is free of bugs and is free
from

Test and Correct the Program


Testing: method to verify correctness and that
requirements are met
Bug: a program error
Debugging: the process of locating an error, and
correcting and verifying the correction
Testing may reveal errors, but does not
guarantee the absence of errors

Data that the


program uses

Write and correct


the program code

Compile the
program

List of syntax
error messages

Executable
program

Program
Output

Creating an executable program

Testing The Program

Once a program has been coded, compiled and or interpreted,


you can test if the program will run or not. Some compilers and
interpreters will enable you to debug a code when an error is
apparent. Errors can come in the form of syntax, type mismatch, or
an error in the logic of the program itself. Syntax errors are
basically errors that have misspellings in the commands. These can
easily be corrected. Type mismatch errors are basically errors that
arise when your defined variables do not match with their
respective tasks. Errors in the logic of the program itself are the
last kinds of errors you would want to see during the testing phase.
Sometimes, you might have to code the whole program altogether
just to get it right.

Running the Program

Once the program is free of bugs and is


free from errors, you simply run it and
watch it solve your problem or perform
the task you assigned it to do. In this
case, all you have to do is watch the
Hello World message flash across your
screen.

Problem Solution and Software Development:

Relative effort for steps in Phase I

5. Putting the Program into Production


Once a program has been coded, compiled
and or interpreted, you can test if the
program will run or not. Some compilers and
interpreters will enable you to debug a code
when an error is apparent.
After the program is free of bugs and is free
from

Documentation
Many documents may be required, including
Program description
Algorithm development and changes
Well-commented program listing
Sample test runs
Users manual

Maintenance
Maintenance includes
Ongoing correction of newly discovered bugs
Revisions to meet changing user needs
Addition of new features

Maintenance usually the longest phase, and


may be the primary source of revenue
Good documentation vital for effective
maintenance

Backup
Backup: process of making copies of program
code and documentation on a regular basis
Backup copies = insurance against loss or
damage
Consider using off-site storage for additional
protection

CS001P
Week3

PROBLEM SOLVING USING C++

C++

PROGRAMMING
LANGUAGE

Programming language is used to create programs that


can be translated into machine language.
Program it is a set of Instructions

C++

History of C++
1980- began the development of the C++
language, that would receive formally this
name at the end of 1983
C++ lived a great expansion in its use and
today is the preferred language to develop
professional applications on all platforms.

Bjarne Stroustrup
-a computer scientist

C++ has been evolving, and a new version of


the standard, c++09, is being developed to be - Professor and holder of the
published before the end of 2009, with
College of Engineering Chair in
several new features.
Computer Science at Texas
A&M University

DEFINITION OF TERMS
BUG is a term used to refer for a program
mistake or error.
C++ LIBRARY provides a set of common
classes and interfaces that greatly extend the
core C++ language.
COMPILER is a program that converts your
program to low-level machine instructions.
DEBUG is a process of correcting program
mistakes.

DEFINITION OF TERMS
EDITOR is software that enables the typing of the
programs where you can easily type, change and save
programs.
IDENTIFIER is a sequence of one or more letters, digits
or underscores characters (_).
LINKER is a program that supplies needed runtime
information to the compiled program and it is also used
to combine multiple programs into one executable
program.
LOADER is a program that loads an executable
program into main memory.

DEFINITION OF TERMS
NAMESPACE is an abstract container providing
context for the items (names, or technical terms, or
words) it holds and allowing disambiguation of items
having the same name.
OBJECT CODE is the code produced by a compiler
from the source code, usually in the form of machine
language that a computer can execute directly.
PREPROCESSOR reads a programs preprocessor
directives to control the compilation.
PROGRAM is a list of detailed instructions that tells
the computer what to do.

DEFINITION OF TERMS
PROGRAMMING is the act or process of creating a
program.
SOURCE CODE is code written by a programmer in
a high-level language and readable by people but
not computers.
SYNTAX is the rules governing the formation of
statements in a programming language, usually a
typing error.
VARIABLE is an example of an identifier, it is a
location where you can store a piece of information.

STRUCTURE OF C++ PROGRAM


First Program(Console Output- cout): "Hello World

cout (pronounced as see out) is


an output statement defined in the
C++ library called <iostream>
It is used to display variables,
constants, literals and expressions
in C++ screen.

C++

Click Start
Choose All Programs
Select Microsoft Visual Studio 6.0
Highlight Microsoft Visual C++
Go to File then New
Click the Files tab
Select C++ Source File
Type the filename of your work
Check the location
Then ok.

STRUCTURE OF C++ PROGRAM


First Program(Console Output- cout): "Hello World
// my first program in C++
#include <iostream>
using namespace std;
int main()

F5- Compile
Or
Ctrl + F5 To compile and run

{
cout<<Hello World";
return 0;
}

OUTPUT:
Hello World

STRUCTURE OF C++ PROGRAM


First Program: "Hello World
(//) that indicate that the entire line is to be ignored
// my first program in C++ COMMENT (/**/) enclose more than one line of statements to
be considered as comments.

DIRECTIVE

#include <iostream> basic standard input-output library in C++

tells the preprocessor to include the iostream standard file

Declaration of elements of the standard C++ library

using namespace std;

using namespace std;


All the elements of the standard C++ library are declared within what is called a
namespace, the namespace with the name std.

int main()

MAIN FUNCTION

Start of a block

all C++ programs start their execution

Standard Output Stream

cout<<Hello World";
return 0;
}

cout (pronounced as see out) is an output statement


defined in the C++ library called <iostream>.

Terminate the main function

End of a block

OUTPUT:
Hello World

STRUCTURE OF C++ PROGRAM


PRACTICE:
// my first program in C++
#include <iostream>
using namespace std;
int main()
{
New Line
cout << Hello \n;
cout<<Malayan Colleges;
return 0;
}

OUTPUT:
Hello
Malayan Colleges

STRUCTURE OF C++ PROGRAM


PRACTICE:
// my first program in C++
#include <iostream>
using namespace std;
int main()
{
cout << Hello \n Malayan Colleges;
return 0;
}

OUTPUT:
Hello
Malayan Colleges

STRUCTURE OF C++ PROGRAM


Machine Problem:

Write a program that will serve


as your resume, put PERSONAL
INFORMATION, EDUCATIONAL
BACKGROUND, TRAININGS &
SEMINARS, CHARACTER
REFERENCES

DATA TYPES & VARIABLES


// operating with variables
#include <iostream>
using namespace std;
int main ()
{

Data
Type

Variable Name

int a;
a = 5;
cout << a;
return 0;
}

NOTE: Whenever we
use a value or a
variable, we will
declare it and its data
type

Data types are used to store various types of data


that is processed by program.
C++ supports various data types such as character, integer and floating-point
types.

Data Types
Numeric Data Types

Real

Floating point

Non-Numeric Data Types

Integral
Character

String

Fundamental data types


Data Type

Range of Values

char

256 characters

bool

true and false

int

-2,147,483,648 to +2,147,483,647

short int

-32, 768 to +32,767

long int

-2,147,483,648 to +2,147,483,647

unsigned short int

0 to 65,535

unsigned long int

0 to 4,294,967,295

float

1.40129846432481707e-45 to
3.4028234663858860e+38

double and long double

4.94065645841246544e-324 to
1.79769313486231570e+308

Variable is a meaningful name of data storage location in computer memory.


When using a variable you refer to memory address of computer.

Naming Variables
The name of variable can be called identifier or variable name in a friendly
way. It has to follow rules:

1. Use alpha characters such as A-Z and numeric


characters such as 0-9
2. Spacing in a Variable Name is NOT ALLOWED
Example:
FIRST NAME

FIRSTNAME
FIRST_NAME

3. Must begin with an alpha character or an


underscore, NOT A NUMBER
_7NAME
Example:
7names

NAME2
_NAME

4. C++ is case sensitive, upper and lowercase


characters are not treated the same
Example: total, Total, TOTAL
5. Name the variable according to what it represents
6. Reserve words are not allowed
The C++ Compiler looks for certain keywords, words with special meanings

Example: int, cout, cin,if, else

DATA TYPES & VARIABLES


// operating with variables
#include <iostream>
using namespace std;
int main ()
{

Data
Type

Variable Name

Int a;
a = 5;
cout << a;
return 0;
}

OUTPUT:
5

STRUCTURE OF C++ PROGRAM


First Program(Console Input- cin):

cin (pronounced as see in)


is an input statement defined
in the C++ library called
<iostream>. It is used to get
value for a variable.

STRUCTURE OF C++ PROGRAM


(Console Input- cin):
// my first program in C++
#include <iostream>
using namespace std;
int main()
{
Data
Type

Variable Name

int num1;
cout << Enter a number: ;
cin>>num1;
return 0;
}

OUTPUT:
Enter a number: 5

STRUCTURE OF C++ PROGRAM


Practice:
// my first program in C++
#include <iostream>
using namespace std;

OUTPUT:
Enter two numbers separated by spaces: 5 7

int main()
{
cout << Enter two numbers separated by spaces: ;
cin>>num1>>num2
return 0;
}

DECLARING VARIABLE NAME


Assigning values & Initialization

#include <iostream>
using namespace std;
int main()
{
int myAge;
cout << "My age is " << myAge << endl;
return 0;
}

NOTES!

In some languages, variables are initialized to


0 - that is, a variable's initial value will be 0.
This is not true of C++! Sometimes your
variables will be initialized to 0, but sometimes
they will be initialized with garbage.

It is always a good idea to


initialize your variables with
some value. If you don't know
what a variable's initial value
should be, initialize it to 0.

DECLARING VARIABLE NAME

#include <iostream>
using namespace std;
int main()
{
int myAge=20;
cout << "My age is " << myAge << endl;
return 0;
}

Practice 1:

#include <iostream>
using namespace std;
int main()
{
int sum;
sum=12;
sum= sum + 5;
cout << Sum is: " << sum << endl;
return 0;
}

VARIABLE DECLARATION STATEMENT


#include<iostream>
using namespace std;
int main()
{

int firstNumber=37, secondNumber = 23;

cout<<"\nThe sum is:"<<(firstNumber+secondNumber);


cout<<"\nThe difference is:"<< (firstNumber-secondNumber);
cout<<"\nThe product is:"<< (firstNumber*secondNumber);
cout<<"\nThe quotient is:"<< (firstNumber/secondNumber);

return 0;
}

VARIABLE DECLARATION STATEMENT

Output:
The sum is: 60
The difference is: 14
The product is: 851
The quotient is: 1

MACHINE PROBLEM 1
FILENAME: MP1

CREATE A PROGRAM
THAT ADD TWO
INPUT NOS. AND
SHOW THE RESULT

MACHINE PROBLEM 2
FILENAME: MP2

CREATE A PROGRAM
THAT CALCULATE AND
DISPLAY THE SUM AND
AVERAGE OF THREE
QUIZZES

LABORATORY
EXERCISES
2 01,02,
& 04

SEMI-QUIZ#1
PART 1: General Instructions. Write A if only
Statement (a) is correct, B If only Statement
(b) is correct, C if both Statements are correct,
otherwise, write D

1-5
(a) Selection is also termed as loop structure
(b) Selection Structure follows a sequence of instructions
(a) Pseudo means False
(b) Pseudocode follows a strict standardized structure
(a) Mnemonics use 0s and 1s
(b) Assembly language uses 0s and 1s
(a) Any programming language has its own compiler
(b) A compiler translates the program into machine language
(a) Sequence structure iterates as long as the condition is true
(b) Iteration structure loops as long as the condition is true

6-10
(a) Variables should be declared with data type
(b) 1x is a valid variable
(a) Hexagon is a flowchart symbol for input
(b) Circle is a flowchart symbol for start or stop
(a)
(b)

Float is a data type for fractional values


Double is a datatype for numbers with decimal points

(a)
(b)

Bool accepts character value


Bool is a data type

(a)
(b)

semi-colon terminates a statement


Double quotation mark signifies a String or Text

11-15
(a) int accepts negative numbers
(b) int y =5 is valid
(a) Equal sign is an assignment symbol
(b) Keyword can be used as variable
(a) Decision symbol in flowchart has 2 entry points and 1 exit point
(b) Long flowchart can be extended using connector
(a) Cout is a keyword from iostream
(b) Iostream is a library
(a) Square is the flowchart symbol for process
(b) Parallelogram is the flowchart symbol for preparation

Arrange them properly by specifying the correct sequence order number at


the space provided before each psuedocode statement. You must arrange all
pseudocode statements correctly in order to gain points allocated for
pseudocode

Sequence
Number

Pseudocode Statements
area = length * width
input length, width
display area, perimeter
perimeter =
2*(length+width)
display instructions asking
users to input length and
width

Examine each program statement, identify if there are


syntax errors and specify how to correct them. (10
pts.)
With
Error?
Yes/No

Program
include <iostream>
using namespace std;
int main();
{
int n, sum=0;
Cout<<Enter an integer
value: ;
cin>>n;
sum = n(n+1)/2;
cout>>The summation of
number from 1 to<< n is:
<<sum;
return;
{

Corrected
Statement

Anda mungkin juga menyukai