ES084 - Lecture Notes MADE4 Learning Part 1 - A
ES084 - Lecture Notes MADE4 Learning Part 1 - A
ES084 - Lecture Notes MADE4 Learning Part 1 - A
Prepared by:
Roel Lauron
Instructional Resources/ Textbook
References:
005.133/B789/2013 Bronson, Gary J. C++ Programming: Principles and Practices for Scientists and Engineers. Cengage
Learning, 2013.
005.133/M295/2011 Malik, D.S. C++ Programming: Problem Analysis to Program Design. Cengage Learning, 2011.
005.133/Sch644/2011 Scholl, T., Nugent, G. C++ Programming Problem Analysis to Program Design (Lab Manual). Cengage
Learning, 2011
www.course.com
www.thomsonlearning.com
www.tutorialspoint.com/cprogramming/
www.cprogramming.com/tutorial/c-tutorial.html
www.iu.hio.no/~mark/CTutorial/CTutorial.html
http://fresh2refresh.com/c-tutorial-for-beginners
What is computer Programming?
Computer programming is creating a sequence of
instructions to enable the computer to do
something.
1. Syntax Errors
- occurs when your code violates one or more
grammar rules of C and is detected by the compiler
as it attempts to translate your program.
Note: If a statement has a syntax error, it cannot be
translated and your program will not be executed.
Common Programming Errors
2. Run-time Errors
- are detected errors and displayed by the compiler during the
execution of the program.
- occurs when the program directs the computer to perform
illegal operation, such as dividing a number by zero.
- an attempt to perform an invalid operation, detected during
program execution.
Note: When a run-time error occurs, the computer will stop
executing your program and will display a diagnostic message
that indicates the line where the error was detected.
Common Programming Errors
3. Logic Errors
- occur when a program follows a faulty algorithm.
- do not cause a run-time error and do not display
error messages, so are very difficult to detect.
C is a concise language
⚫ it has only about 32
keywords
⚫ it encourages concise code.
General Description of C
C is weakly typed
⚫ allowing quite a bit of data
conversion and providing
minimal run time checking.
C Language Elements
#include <stdio.h>
#include <conio.h>
main()
{
printf("Hello World!\n"); //syntax of printf
getch();
}
C Language Elements
Reserved Words
In C, a reserved word is defined as
the word that has special meaning
in C and cannot be used for other
purposes.
➢ double/float
➢ int
Variables, Data Types and Constants
Seven Basic C Data Types:
1. Text (data type char) – made up of single characters (example x,#,9,E)
and strings (“Hello”), usually 8 bits, or 1 byte with the range of 0 to 255.
2. Integer values – those numbers you learned to count with.
3. Floating-point values – numbers that have fractional portions such as
12.345, and exponents 1.2e+22.
4. Double-floating point values – have extended range of 1.7e-308 to
1.7e+308.
5. Enumerated data types – allow for user-defined data types.
6. void – signifies values that occupy 0 bit and have no value. You can also
use this type to create generic pointers.
7. Pointer – does not hold information as do the other data types. Instead,
each pointer contains the address of the memory location.
Variables, Data Types and Constants
int - data type
int is used to define integer numbers.
Ex.
{ int Count;
Count = 5; }
Examples:
1. Character constants – enclosed between
single quotes. Ex. ‘A’, ‘+’
2. Integer constants – specified as numbers
without fractional components.
Ex. 5 and -160
Variables, Data Types and Constants
Floating constants – require the use of
decimal point followed by the number’s
fractional components. Ex. 16.234
String constants – set of characters
enclosed by double quotes. Ex. “bag”
and “this is good”
Backslash character constants –
enclosing all character constants in
single quotes that works for most
printing characters. Ex. g = ‘\t’
Variables, Data Types and Constants
SEQUENCE NAME MEANING
#define preprocessor
- allows us to define symbolic names and
constants.
A quick example:
#define PI 3.14159
This statement will translate every occurrence of PI in
the program to 3.14159.
Here is a more complete example:
#define PI 3.14159 main() { int r=10; float cir; cir =
PI * (r*r); }
Variables, Data Types and Constants
Defining Constants
Prefix increment
⚫ is when the ++ is placed immediately
in front of its operand.
⚫ the value of the expression is the
variable’s value after incrementing.
Increment (++) and Decrement(--) Operators
Postfix increment
⚫ is when the expression’s value is the
value of the variable before it is
incremented.
Increment (++) and Decrement(--) Operators
Prefix Postfix
After… x y x y
Example
main(){
int a=3, b;
b=a++;
printf(“b is %d, and a is %d\n”, b, a);
b=++a;
printf(“Now b is %d, and a is %d\n”, b, a);
b=5%--a;
printf(“Now b is %d, and a is %d\n”, b, a);
printf(“Now b is %d, and a is %d\n”, ++b, a--);
printf(“Now b is %d, and a is %d\n”, b, a);
}
Output
b is 3, and a is 4
Now b is 5, and a is 5
Now b is 1, and a is 4
Now b is 2, and a is 4
Now b is 2, and a is 3
Operators
Prefix increment/decrement - when the ++
or -- is placed immediately in front of its
operand. Meaning the value of the
expression is the variables value after
incrementing or decrementing.
Example:
gets(name);
I/O Functions
Output Command
printf – writes formatted output to the standard output device such as the
monitor.
syntax:
printf(“format code”,var_name);
Example:
printf(“%d”,x);
puts – writes the string to the standard output device such as the monitor and
positions the cursor to the next line.
syntax:
puts(“string expression”);
Example: puts(“CIT”);
FIGURE 2-17 Output Stream Formatting Example
I/O Functions
Output Command
Example:
putchar(a);
Introduction to C Programming
CONTROL FLOW
Control Flow
Control Structures - specify the sequence of
execution of a group of statements.
3 Types:
1. Sequential
2. Conditional
3. Iterative
Control Flow
1. Sequential Logic Structure
1. Terminal Symbol
- used to designate the beginning and end of a
program.
2. Input Symbol or
- represents an instruction to an input device
3. Processing Symbol
- used to represent a group of program instructions
that perform a processing function or activity such
as mathematical operations or logical comparisons.
Algorithms and Flowcharting
Flowchart Symbols:
4. Output Symbol or
- represents an instruction to an output device.
5. Decision Symbol
- denotes a point in the program where more
than one path can be taken or used to
designate a decision making process.
Algorithms and Flowcharting
Flowchart Symbols:
9. Predetermined Symbol
- used as a subroutine symbol
- inner procedure needs to be repeated
several times.
Flowchart Symbols:
10. Preparation/Initialization Symbol
Most Commonly Used Symbol
Flowchart (Applications)
Sequential/Linear Flowchart
Sequential/Linear Flowchart
Sequential/Linear Flowchart
Sample Flowchart
Sample Flowchart
Sample Flowchart
Sample Flowchart
System Flowcharts
A system flowchart shows in general
terms the operations that will be
performed on information in an
information system. The arrows on a
system flowchart show the direction that
data will flow in around the system rather
than the order in which the operations will
be carried out.
System Flowchart Symbols
System Flowchart Example : Car Repair Garage
OTHER TYPES OF FLOWCHARTS
High-Level Flowchart
A high-level (also called first-level or top-down) flowchart shows the major steps in a process. It
illustrates a "birds-eye view" of a process, such as the example in the figure entitled High-Level
Flowchart of Prenatal Care. It can also include the intermediate outputs of each step (the product or
service produced), and the sub-steps involved. Such a flowchart offers a basic picture of the process
and identifies the changes taking place within the process. It is significantly useful for identifying
appropriate team members (those who are involved in the process) and for developing indicators for
monitoring the process because of its focus on intermediate outputs.
Most processes can be adequately portrayed in four or five boxes that represent the major steps or
activities of the process. In fact, it is a good idea to use only a few boxes, because doing so forces
one to consider the most important steps. Other steps are usually sub-steps of the more important
ones.
Detailed Flowchart
Area = 4 × pi × radius2
Volume= 3/4 × pi × radius3.
Sample Problem
13. Make a flowchart and a C program to input
2 integers. If the 1st integer is greater than the
nd
2 integer, compute the sum of the 2
integers, otherwise compute the product.
Output the sum or product of the 2 integers.
Sample Problem
13. Any customer whose total PURCHASE
is at least P1000 will be given a 10%
discount. Make a flowchart that would
input the customer’s PURCHASE and
output his net BILL.
Computer Programming 1
THE END
PART A