Monday, September 27, 2010

Appendix 1

(A) Flowchart
- A Flowchart is a pictorial representation of the logical steps it takes to solves a problem . Almost every program involves the steps of input , processing , & output . When you create a flowchart , you draw geometric shapes around the individual statements and connect them with arrows .

SYmbol used in flowcharting

-Arithmetic operation statements are examples of processing .
-TO show the correct sequence of these statements , use flowlines to connect the steps
-To be complete , a flowchart should include a terminal (start/stop) symbol at each end .
-Use connector when a flowchart cannot physically fit on one page . The letters or numbers on the connectors match up so you can determine the next step in the flowchart .
-The off-page connector can be used when the flowchart is at the bottom of a page and must be continued on a new page . As with the on-page connector , the off-page connector has a symbol in it ( a letter or number ) and connect with another connector on a different page with the same symbol .

B. Pseudocode
-Pseudocode is an English-like representation of the same thing .
-Statements are written in simple English
-Each instruction is written on a separate line.
-Keywords and indention are used to signify particular control structures .
-Each set of instruction is written from top to bottom , with olny one entry and one exit .
-Groups of statements may be formed into modules , and that group given a name .

Exercise 1.3
-By using structure chart , pseudocode and flowchart , design the solution for a program that reads three (3) numbers from user , calculates their total and average , and displays the calculated total and average on the screen .

Flowchart for calculattes the sum of two numbers

1. Use terminal symbols to indicate the start of a flowchart . Write start , begin , etc in the symbol
2.Use input/output symbols for reading and displaying of data values .
3.Use processing symbol for calculations
4.Use terminal symbol to indicate the end of flowchart .Write end , stop , etc in the symbol .




-Prompt and read the 1st number from user .
-Prompt and read 2nd number from user
-Calculate the sum
-Display the sum


#include

int main (void)
{
int number 1 , number 2, sum;

printf ("Enter 1st number > ") ;
scanf ( "%d", &number1);

printf ("Enter 2nd number > " ) ;
scanf ("%d" , &number2);

sum = number1 + numbers2;

printf ("Result = %d\n",sum);

return 0;

Pseudocode for calculate the sum of two numbers

Start the pseudocode witht he words start , begin etc
Use english words to describe the algorithms in steps
Numbering are optional
ENd of the pseudocode with the words end , stop , etc

Start
1.Prompt and read number 1
2.Prompt and read number 2
3.sum = number1 + number2
4.display sum
End

Review : Steps for program development include :

1. Understand the problem
2.Develop a solution
a) Design the program : structure chart
b)Design the algorithms : flowchart or pseudocode
3. Write the program
4. Test the program

Example 1.2
-Problem : calculate the sum of two numbers
a)Design the solution using a structure chart
b)Normally , each module in the structure chart would have its own pseudocode . However , as this is your first attempt and this is a pretty easy problem , write the pseudocode to solve this entire problem .
C) Draw the flowchart to represent the program logic for this problem .
-HInt : the general algorithm for a programming problem is normally :
1. Get the data
2. Perform the computations
3. Display the result

Sum two number consist of three , that is get user input , calculate sum , and display sum

Flowchart for calcbathrooms module

Start of calcBathrooms then read linoleum price and number of bathrooms from user . THe values are represented as linoPrice and numBath.Then set the values of bathArea and bathProc to 0 , whie there are still any bathrooms need to be process .Read the length and width of bathroom . Accumulate the total area of bathrooms . One more bathroom has been processed . So add 1 to the counter bathProc .Calculate cost of linoleum based on the total bathrooms area and linoleum price . End of calcBathrooms module

Pseudocode for calcBathRooms module

-For each module , use pdesudocode ( see Firgure 1.4 ) or a flowchart ( see Figure 1.5 ) to specify the steps to be followed ( algorithm ) in order to accomplish the task


Algorithm Calculation Bathrooms
1 prompt user and read linoleum price
2 Prompt user and read number of bathrooms
3 set total bath area and baths processed to zero
4 While (baths processed < number of bathrooms )
1 prompt user and read linoleum price
2 prompt user and read number of bathrooms
3 set total bath area and baths processed to zero
4 while ( baths processed < number of bathrooms )
1 Prompt user and read bath length and width
2 total bath area =
3 total bath area + bath length * bath width
4 add 1 to baths processed
5 bath cost = total bath area * linoleum price
6 return bath cost
end Algorithm Calculate BathRooms

Programming concepts and design 1

Flooring cost consist of get user info , calculate and print report , and the calculate is separate to calcilate linoleum , calc carpeting , whereas calc linoleum consist of calc kitchen and calc bath rooms ,,alc carpenting consist of calc bedrooms and calc living areas , calc living areas consist of calc family room and calc dine living

Sunday, September 26, 2010

Example 1.1

-You would like to install new floor covering in your home and would like to calculate the cost that would be involved . Your plan is as follows :

1.Olny the living space will be carpeted . The garage and closets will not be considered .
2.The kitchen and bathrooms will be covered with linoleum . THe rest of the house is to be carpeted .

-You need a computer program to compute the flooring cost . How can you approach this problem ? "Divide and Conquer : !
a)Break down the tasks into a few big subtasks
b)Decompose each big subtask into smaller subtasks.
c)Further break down each smaller subtask into even smaller subtasks
d)Eventually the subtasks become so small that they are trivial to implement in C language .

-A.k.a stepwise refinement or top-down design
-Use a structure chart to represent or top-down design
-Use a structure chart to represent the division of task into subtasks , smaller subtasks and even smaller subtasks ( see Figure 1.3 ).

Program development

An old programming proverb : "Resist the temptation to code ".

-A multi-steps process that requires you to :
1. undestand the problem
2.Develop a solution
3.Write the program
4.Test the program

-To develop the soulution , the following tools are normally used :
1.Structure chart
2.Pseudocode or flowcharts

-Structure chart
1.A hierarchy chart that shows the functional flow through the program.
2.Shows how the program is broken into logical steps . Each step will be a separate module

-Pseudocode:
1.A condensed from of ENglish to convey program logic .
2.Contains the detailed steps to accomplish the task
3.The detailed algorithm in the pseudocode is precise , such that the programmer can convert the pseudocode into a computer program
4.Algorithm , the logical steps nevessary to solve a problem in a computer .

-Flowchart:
A program design tool in which standard graphical symbols ( refer to Appendix 1 ) are used to represent the logical flow of data through a function

Exercise 1.2

-Create a new program to print the following output :

welcome , welcome all of you
Glad you are with us
SHake hands , no need to be blue
Welcome to you !

Exericise 1.1

1. Start Microsoft Visual C++ 6.0 as instructed by your instructor . Follow the instruction to create a new source program named " Hello.c".

2.Type in the following source code :

INcluding the necessaru library file . The printf() function needs Every C program must have main () function .
The printf () function prints the text on the screen.

#include

int main (void)
{
printf ("Hello World!\n");
return 0;
}

Statements/commands are written within the body of function , as indicated by a pair of braces , the { and } symbols.

3. Compile the program . IF there are errors , verify and correct them . Compile the program again , until there is no error .

4.Build and execute the program . FInish your task by closing the project workspace .


Introduction to C

  • C is a general-purpose , structured programming language .
  • Can be used for sustems programming and application programming .
  • C has a large number of operators and library functions and allows the user to extend on the library .
  • C program are portable . They can be processed on many different computers with no or little alterations
  • Developed in 1970s by Dennis Ritchie at Bell telephone Lab .Inc
  • Originated from BCPL ( Basic Combined Programming Language ).
  • ANSI ( American National Standard Institute ) developed a standardized definition of C language that all commerical C compilers and interpreters adhere to this standard

Compiling and running a program

SOurce Program -> COmpiler -Program error listing & object program (data ) -> results

Computer language

-COmputer understand olny machine language , which consists of 0s and 1s .
-The evolution of computer languages is as follows :

First Generation : MACHINE LANGUAGE
-Uses binary codes (0s and 1s).
-The olny language that computers can understand . No translation required
-Platform-dependent and very hard to program
-
Second generation : Symbolic language / Assembly Language
-Uses symbolic instruction codes and mnemonics ( meaningful abbreviations )
-Source program has to be translated to machine language using an assembler
-PLatform-dependent but easier to program
-E.g. MASM 6.15 , Turbo Assembler , etc

Third Generation :High level Language /Procedural Language
-Uses ENglish-like instruction codes and mathematical operators
-Source program has to be translated to machine language using a compiler or interpreter .
-With a compiler ( see Figure 1.2 ) , the source program is converted into a machine language object program . If the compiler encounters any errors , it will be shown as a program error listing .When the users wants to run the program , the object program is loaded into the computer memory and the program instruction are executed .
-Platform-independent and very easy to program
-E.g C , Pascal , Cobol , ETC

interoduction to computer

A computer is a system made up of two major components
-hardware , the physical equipment
-software , the collection of programs ( instruction ) that tell the hardware how to perform tasks

Computer softwate is divided into two broad categories :
-system software , which manages the computer resources .
-Application software , which helps users solve their problems

System softwate consists of :
-Operating system
-System support software
-System development software

Application software is divided into :
-General-purpose software
-Application-specific software