# Digital Laboratory Electrical Engineering Department Engineering Faculty Universitas Indonesia

PRACTICUM MODULE COMPUTER BASIC
Module 4: Algorithms and C
Programming
Objectives: 1. Learn how to draw flowchart properly to understand the logic behind the programming. 2. Know the advantages and disadvantages of C language. 3. Understand the C language syntax. 4. Able to create a simple program using C language.

Computer Basic Practicum Module

Digital Laboratory Universitas Indonesia

Module 4:

Algorithms and C Programming
1 | BASIC THEORY 2 2 | ALGORITHMS 3 | C PROGRAMMING 4 4 | EXPERIMENT 14 2

Page 1 .

2| ALGORITHMS There are two types of algorithms are commonly used. The following are the symbols that are often used in constructing a flowchart: Terminator/Oval: Denotes the beginning or end of a program. Pseudocode and Flowchart.Computer Basic Practicum Module M O D U LE 4: ALGORITHMS PROGRAMMING 1|BASIC THEORY AND C Before writing a program to solve a particular problem. Though so we will only use the flowchart in this experiment. Flowchart is an algorithm that describes the closest to the real situation in a process. and 2. A procedure for solving a problem in terms of 1. Flowchart Flowchart is a set of symbols that form a diagram to illustrate a process. The solution to any computing problem involves executing a series of actions in a specific order. The actions to be executed. it’s essential to have a thorough understanding of the problem and a carefully planned approach to solving the problem. . The order in which these actions are to be executed is called an algorithm.

Flow Line: Denotes the direction of logic flow in a program. Page 2 .

32. 256. Draw a flowchart of a program to display the following series of numbers: 1. 2.Computer Basic Practicum Module Parallelogram: Denotes either an input or an output operation in a program. 1024. Rectangle: Denotes a process to be carried out. 128. Illustrate a process to withdraw money from ATM machines using FLOWCHART. The program should continue one of the two route (Yes/No) Circle: Denotes a connector. 64. 16. Try to answer this question for better understanding of flowchart: 1. 2. Diamond: Denotes a decision (or branch) to be made. 512. 8. (Nb: Use the Diamond/ Decision Maker symbol). Page 3 . 4.

Take a look at Python for example a fully Object-Oriented High-Level programming language. the hard use of pointers for example is a very powerful mechanism. • C is a portable language (Programs written in one system can be run on other systems with little or no change).e. modern. while. do). C allows meaningful names for variables without any loss of efficiency.Computer Basic Practicum Module 3 | PROGRAMMING C The Advantages of C Language: • C is a building block for many other currently known languages. powerful. and a set of flexible commands for performing tasks repetitively (for. • A lot of libraries are written in C. Syntax is of C is clear also. C programming language is very easier to learn. . this means that the code is compacted into executable instruction (in the case of windows anyway) rather than being "translated" on the fly at run time. • C is a compiled language versus an interpreted language. It is written in C (perhaps C++ too). including flexible ways of making decisions. For example. This feature also lends heavily to the speed of C programs. Explained simply. • C has features that allow the programmer to organize programs in a clear. yet it gives a complete freedom of programming style. That tells you if you ever want to know what is going on under the hood in other languages. and flexible. and that the programmer can arrange for the program is very fast. logical way. understanding C and how it works is essential. efficient (Produce programs that compact and fast). It comprises a good semantic. C language is very near to assembly programming i. easy. C programming language is still a practical and compact language. • The main advantage of C language is that there is not much vocabulary to learn.

Page 4 .

World! ------------------------------------------------------------------------------------------------------------------------------------------- . } ------------------------------------------------------------------------------------------------------------------------------------------Output: Hello. • • There is no runtime checking in C language There is no strict type checking (for example: we can pass an integer value for the floating data type). C Language Structure and Syntax: C program basically has the following form: • • • • • Preprocessor Commands Functions Variables Statements & Expressions Comments The following program is written in the C programming language.Computer Basic Practicum Module Disadvantages of C Language: • C does not have OOPS feature that's why C++ is developed. ------------------------------------------------------------------------------------------------------------------------------------------#include <stdio. • • C doesn't have the concept of namespace. World! \n"). C doesn't have the concept of constructors and destructors.h> int main(){ /* My first program */ printf("Hello. If you know any other modern programming language then you already know its disadvantages. return 0.

Page 5 .

Statements can continue over multiple lines.*/ as given in the example above. Statements & Expressions: Expressions combine variables and constants to create new values. strings and complex data for manipulation. It means in C printf and Printf will have different meanings. Comments: are used to give additional useful information inside a C Program. White Spaces (i.Computer Basic Practicum Module Preprocessor Commands: These commands tell the compiler to do preprocessing before doing actual compilation. . assignments. This integer value is retured using return statement.. All the comments will be put inside /*.e. Every C Program will have one or more functions and there is one mandatory function which is called main() function. Note the followings: • • • • • C is a case sensitive programming language. Multiple statements can be one the same line. function calls.h> is a preprocessor command which tells a C compiler to include stdio. The C Programming language provides a set of built-in functions. tab space and space bar) are ignored. Statements are expressions. Like #include <stdio. Variables: are used to hold numbers. In the above example printf() is a C built-in function which is used to print anything on the screen. or control flow statements which make up C programs. Functions: are main building blocks of any C Program. C has a free-form line structure.. A comment can span through multiple lines.h file before going to actual compilation. This function is prefixed with keyword int which means this function returns an integer value when it exits. End of each C statement must be marked with a semicolon.

Count = 5. Variable int Count. The value of the variable can be changed at any time. while the value of the constants declared in the beginning.Page Computer Basic Practicum Module 6 Variables and Constants in C Language: Before discussing the Variables and Constants. . const int a =2. These following are commonly used data-types in c language: • • • • int float double char C has a concept of 'data types' which are used to define a variable before its use.data type int is used to define integer numbers. You can declare the const before or after the type. Choose one and stick to it. Constant int const a = 1. The definition of a variable will assign storage for the variable and define the type of data that will be held in the location. int .

. Variable char Letter. Variable double Atoms.data type char defines characters. Variable float Miles.data type double is used to define BIG floating point numbers. It reserves twice the storage for the number. letter = ‘x’. Atoms = 2500000. const double Atoms =2500000. Constant double const Atoms = 2500000.6. double . Constant char const Letter = ‘x’.6. On PCs this is likely to be 8 bytes. const char Letter =’x’.6.data type float is used to define floating point numbers. Constant float const Miles = 5. char . Miles = 5. const float Miles =5.Page Computer Basic Practicum Module 7 float .

Arithmetic Operators: There are following arithmetic operators supported by C language: Assume variable A holds 10 and variable B holds 20 then: Operator + * / % ++ Description Adds two operands Subtracts second operand from the first Multiply both operands Divide numerator by enumerator Modulus Operator and remainder of after an integer division Increment operator. increases Example A + B will give 30 A .B will give -10 A * B will give 200 B / A will give 2 B % A will give 0 A++ will give 11 . • • • • • Arithmetic Operators Logical (or Relational) Operators Bitwise Operators Assignment Operators Misc. Here 4 and 5 are called operands and + is called operator.Page Computer Basic Practicum Module 8 Operator in C Language: What is Operator? Simple answer can be given using expression 4 + 5 is equal to 9. Operators Let’s have a look on all operators one by one. C language supports following type of operators.

!= (A != B) is true. if values are not equal then condition becomes true. Called Logical AND operator. > Checks if the value of left operand is greater than the value of right (A > B) is not true. operand. Checks if the value of left operand is less than the value of right operand. right operand. Checks if the value of left operand is less than or equal to the value of (A <= B) is true.integer value by one -Decrement operator. < >= <= && .will give 9 integer value by one Page Computer Basic Practicum Module 9 Logical (or Relational) Operators: There are following logical operators supported by C language Assume variable A holds 10 and variable B holds 20 then: Operator == Description Checks if the value of two operands is equal or not. If both the operands are non-zero then then condition becomes true. if yes then condition becomes true. right operand. if yes then condition becomes true. if yes then condition becomes true. Checks if the value of left operand is greater than or equal to the value of (A >= B) is not true. (A && B) is true. Checks if the value of two operands is equal or not. Example (A == B) is not true. if yes then condition becomes true. if yes then condition becomes true. decreases A-. (A < B) is true.

! !(A && B) is false. Page Computer Basic Practicum Module 10 Bitwise Operators: Bitwise operator works on bits and performs bit by bit operation. Now in binary format they will be as follows: A = 0011 1100 B = 0000 1101 ----------------------A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~A = 1100 0011 There are following Bitwise operators supported by C language: Operato r & | ^ ~ Description Binary AND Operator copies a bit to the result if it exists in both operands.|| Called Logical OR Operator. Called Logical NOT Operator. Binary OR Operator copies a bit if it exists in either operand. Use to reverses the logical state of its operand. and B = 13. Assume if A = 60. If a condition is true then Logical NOT operator will make false. If any of the two operands is non-zero then (A || B) is true. which is 0011 0001 Binary Ones Complement Operator is (~A ) will give -60 . Example (A & B) will give 12 which is 0000 1100 (A | B) will give 61 which is 0011 1101 Binary XOR Operator copies the bit if it (A ^ B) will give 49 is set in one operand but not both. then condition becomes true.

It divides left operand with the right operand and assign the C /= A is equivalent to C=C/A *= /= . It subtracts right C -= A is equivalent to operand from the left operand and C = C .A assign the result to left operand Multiply AND assignment operator. << Binary Left Shift Operator. It multiplies right operand with the C *= A is equivalent to left operand and assign the result C=C*A to left operand Divide AND assignment operator. The left operands value is moved right by the number of bits specified by the right operand. The left operands value is moved left by the number of bits specified by the right operand. which is 1100 0011 A << 2 will give 240 which is 1111 0000 >> A >> 2 will give 15 which is 0000 1111 Page Computer Basic Practicum Module 11 Assignment Operators: There are following assignment operators supported by C language: Operat or = Description Simple assignment operator.unary and has the effect of 'flipping' bits. It adds right operand to the left operand and assign the result to left operand Example C = A + B will assigned value of A + B into C C += A is equivalent to C=C+A += -= Subtract AND assignment operator. Assigns values from right side operands to left side operand Add AND assignment operator. Binary Right Shift Operator.

will pointer to a variable. Pointer to a variable. & * ?: Returns the address of an variable. It takes modulus using two operands and assign the result to left operand Left shift AND assignment operator Right shift AND assignment operator Bitwise AND assignment operator bitwise exclusive OR and assignment operator bitwise inclusive OR and assignment operator C %= A is equivalent to C=C%A C <<= 2 is same as C = C << 2 C >>= 2 is same as C = C >> 2 C &= 2 is same as C = C &2 C ^= 2 is same as C = C ^2 C |= 2 is same as C = C | 2 <<= >>= &= ^= |= Page 12 Computer Basic Practicum Module Misc.result to left operand %= Modulus AND assignment operator. Operat or sizeof() Description Example sizeof(a). *a. If Condition is true? Then value X : Otherwise value Y Returns the size of an variable. where a is integer. Operators: There are few other operators supported by C Language. will return 4. &a. will give actual address of the variable. Conditional Expression .

• Trivial: Microsoft Visio.codeblocks.org/downloads. But. Dev-C++. Code Lite. You can also use paper and pen to draw a flowchart. a lot of IDE that can be used in C programming.Page Computer Basic Practicum Module 13 4 | EXPERIMENT Tools • Important: In this experiment you will need a Computer/Laptop with an IDE (Integrated Development Environment) Installed. . Actually. Here is the download link: http://www. for example: Code::Block. But we will be using Code::Blocks for experiment this time. Net Bean. etc…. you can also use another IDE if you want.

print out: The Sum Different Multiplication Division And. Create a FlowChart and program that can compare the five numbers that the user entered. after that. Modulo Of the two numbers. min=0). Build the program! (Nb: use the IDE that has been installed) II. Number (max=100. For example: . Then. Build the app/program to print a string accordance with the number entered into the program.I. Create a flowchart of a program that asks the user to enter two numbers. both numbers have the datatype "int”. printout the maximum and minimum number. (Nb: use the IDE that has been installed) Page Computer Basic Practicum Module 14 III.

Output String: sebelas Or Number Input by User:50. Output String: satu Or Number Input by User: 11. Output String: lima puluh Or Number Input by User:71. Output String: tujuh puluh satu -------------------------------------------- -have a good time- .------------------------------------------Number Input by User: 1.

Page 15 .