Beruflich Dokumente
Kultur Dokumente
Chapter 1: Introduction to C
Chapter 1: Introduction to C
Chapter Outline: 1.1 Introduction to Computers 1.2 Computer Languages 1.3 Introduction to C 1.4 Program Development
1.1
Introduction to Computers
Computer System
Hardware
Software
System Software
Application Software
Operating System
GeneralPurpose Software
ApplicationSpecific Software
A computer is a system made up of two major components: o Hardware, the physical equipment. o Software, the collection of programs (instructions) that tell the hardware how to perform tasks. Computer software is divided into two broad categories: o System software, which manages the computer resources. o Application software, which helps users solve their problems. System software consists of: o Operating system o System support software o System development software Application software is divided into: o General-purpose software o Application-specific software
Page 1 of 10
Chapter 1: Introduction to C
1.2
Computer Languages Computers understand only machine language, which consists of 0s and 1s. The evolution of computer languages is as follows: Uses binary codes (0s and 1s). The only language that computers can understand. No translation required. Platform-dependent and very hard to program. 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. 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 user wants to run the program, the object program is loaded into the computer memory and the program instructions are executed. Platform-independent and very easy to program. E.g. C, Pascal, Cobol, etc.
Source Program
Compiler
Object Program
Results
Data
1.3
Introduction to C C is a general-purpose, structured programming language. Can be used for systems programming and application programming.
Page 2 of 10
Chapter 1: Introduction to C
C has a large number of operators and library functions and allows the user to extend on the library. C programs 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 Standards Institute) developed a standardized definition of C language that all commercial C compilers and interpreters adhere to this standard.
Exercise 1.1: 1. Start Microsoft Visual C++ 6.0 as instructed by your instructor. Follow the instructions to create a new source program named Hello.c. 2. Type in the following source code:
Include the necessary library file. The printf() function needs <stdio.h>.
Every C program must have the main() function. The printf() function prints the text on the screen.
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. 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!
Page 3 of 10
Chapter 1: Introduction to C
1.4
Program Development An old programming proverb: Resist the temptation to code. A multi-steps process that requires you to: 1. Understand the problem 2. Develop a solution 3. Write the program 4. Test the program To develop the solution, the following tools are normally used: o Structure charts o Pseudocode or flowcharts Structure chart: o A hierarchy chart that shows the functional flow through the program. o Shows how the program is broken into logical steps. Each step will be a separate module. Pseudocode: o A condensed form of English to convey program logic. o Contains the detailed steps to accomplish the task. o The detailed algorithm in the pseudocode is precise, such that the programmer can convert the pseudocode into a computer program. o Algorithm, the logical steps necessary to solve a problem in a computer. Flowchart: o 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.
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: o Only the living space will be carpeted. The garage and closets will not be considered. o 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 the division of task into subtasks, smaller subtasks and even smaller subtasks (see Figure 1.3).
Page 4 of 10
Chapter 1: Introduction to C
For each module, use pseudocode (see Figure 1.4) or a flowchart (see Figure 1.5) to specify the steps to be followed (algorithm) in order to accomplish the task.
Page 5 of 10
Chapter 1: Introduction to C
calcBathRooms
Read linoPrice
Read numBath
Read linoleum price and number of bathrooms from user. The values are represented as linoPrice and numBath.
bathArea = 0 bathProc = 0
Yes
bathProc = bathProc + 1
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.
Return
Page 6 of 10
Chapter 1: Introduction to C
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 results
Sum Two Numbers
Calculate Sum
Display Sum
Figure 1.6: Structure chart for calculate the sum of two numbers
Start 1. 2. 3. 4. End
Prompt and read number1 Prompt and read number2 sum = number1 + number2 Display sum
Page 7 of 10
Chapter 1: Introduction to C
Use terminal symbol to indicate the start of a flowchart. Write start, begin, etc in the symbol.
Start
Read number1 Use input/output symbol for reading and displaying of data values.
Display sum
End
Use terminal symbol to indicate the end of a flowchart. Write end, stop, etc in the symbol.
#include <stdio.h> int main(void) { int number1, number2, sum; printf("Enter 1st number > "); scanf("%d", &number1); printf("Enter 2nd number > "); scanf("%d", &number2); sum = number1 + number2;
Calculate the sum. Prompt and read the 1st number from user.
Page 8 of 10
Chapter 1: Introduction to C
Appendix 1
(A) Flowchart A flowchart is a pictorial representation of the logical steps it takes to solve 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.
Input/Output
Processing
Module
Terminal
Decision
Flowlines
On-page Connector
Off-page Connector
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 connects with another connector on a different page with the same symbol. Pseudocode
(B)
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.
Page 9 of 10
Chapter 1: Introduction to C
Each set of instructions is written from top to bottom, with only 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.
Page 10 of 10