Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Module 4:
Copyright 2013
Page
M O D U LE 4: ALGORITHMS PROGRAMMING
1|BASIC THEORY
AND
Before writing a program to solve a particular problem, its essential to have a thorough understanding of the problem and a carefully planned approach to solving the problem. The solution to any computing problem involves executing a series of actions in a specific order. A procedure for solving a problem in terms of 1. The actions to be executed, and 2. The order in which these actions are to be executed is called an algorithm.
2| ALGORITHMS
There are two types of algorithms are commonly used, Pseudocode and Flowchart. Though so we will only use the flowchart in this experiment. Flowchart Flowchart is a set of symbols that form a diagram to illustrate a process. Flowchart is an algorithm that describes the closest to the real situation in a process. The following are the symbols that are often used in constructing a flowchart: Terminator/Oval: Denotes the beginning or end of a program.
Page
Rectangle: Denotes a process to be carried out. Diamond: Denotes a decision (or branch) to be made. The program should continue one of the two route (Yes/No) Circle: Denotes a connector.
Try to answer this question for better understanding of flowchart: 1. Illustrate a process to withdraw money from ATM machines using FLOWCHART. 2. Draw a flowchart of a program to display the following series of numbers: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024. (Nb: Use the Diamond/ Decision Maker symbol).
Page
3 | PROGRAMMING
The Advantages of C Language: C is a building block for many other currently known languages. Take a look at Python for example a fully Object-Oriented High-Level programming language. It is written in C (perhaps C++ too). That tells you if you ever want to know what is going on under the hood in other languages; understanding C and how it works is essential.
C is a compiled language versus an interpreted language. Explained simply, 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. This feature also lends heavily to the speed of C programs.
The main advantage of C language is that there is not much vocabulary to learn, and that the programmer can arrange for the program is very fast. C programming language is very easier to learn. C programming language is still a practical and compact language. It comprises a good semantic. Syntax is of C is clear also. C language is very near to assembly programming i.e. the hard use of pointers for example is a very powerful mechanism.
C has features that allow the programmer to organize programs in a clear, easy, logical way. For example, C allows meaningful names for variables without any loss of efficiency, yet it gives a complete freedom of programming style, including flexible ways of making decisions, and a set of flexible commands for performing tasks repetitively (for, while, do).
C is a portable language (Programs written in one system can be run on other systems with little or no change), efficient (Produce programs that compact and fast), modern, powerful, and flexible.
Page
Disadvantages of C Language: C does not have OOPS feature that's why C++ is developed. If you know any other modern programming language then you already know its disadvantages. 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 doesn't have the concept of namespace. C doesn't have the concept of constructors and destructors.
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. ------------------------------------------------------------------------------------------------------------------------------------------#include <stdio.h> int main(){ /* My first program */ printf("Hello, World! \n"); return 0; } ------------------------------------------------------------------------------------------------------------------------------------------Output: Hello, World! -------------------------------------------------------------------------------------------------------------------------------------------
Page
Preprocessor Commands: These commands tell the compiler to do preprocessing before doing actual compilation. Like #include <stdio.h> is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation. Functions: are main building blocks of any C Program. Every C Program will have one or more functions and there is one mandatory function which is called main() function. This function is prefixed with keyword int which means this function returns an integer value when it exits. This integer value is retured using return statement. The C Programming language provides a set of built-in functions. In the above example printf() is a C built-in function which is used to print anything on the screen. Variables: are used to hold numbers, strings and complex data for manipulation. Statements & Expressions: Expressions combine variables and constants to create new values. Statements are expressions, assignments, function calls, or control flow statements which make up C programs. Comments: are used to give additional useful information inside a C Program. All the comments will be put inside /*...*/ as given in the example above. A comment can span through multiple lines.
Note the followings: C is a case sensitive programming language. It means in C printf and Printf will have different meanings. C has a free-form line structure. End of each C statement must be marked with a semicolon. Multiple statements can be one the same line. White Spaces (i.e. tab space and space bar) are ignored. Statements can continue over multiple lines.
Before discussing the Variables and Constants, 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. The definition of a variable will assign storage for the variable and define the type of data that will be held in the location.
The value of the variable can be changed at any time, while the value of the constants declared in the beginning.
You can declare the const before or after the type. Choose one and stick to it.
int - data type int is used to define integer numbers. Variable int Count; Count = 5;
float - data type float is used to define floating point numbers. Variable float Miles; Miles = 5.6;
double - data type double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes. Variable double Atoms; Atoms = 2500000;
Operator in C Language: What is Operator? Simple answer can be given using expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and + is called operator. C language supports following type of operators. Arithmetic Operators Logical (or Relational) Operators Bitwise Operators Assignment Operators Misc. Operators
Lets have a look on all operators one by one. 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
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 yes then condition becomes true. Checks if the value of two operands is equal or not, if values are not equal then condition becomes true. Example (A == B) is not true.
!=
(A != B) is true.
>
Checks if the value of left operand is greater than the value of right (A > B) is not true. operand, if yes then condition becomes true. Checks if the value of left operand is less than the value of right operand, (A < B) is 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. right operand, if yes then condition becomes true. Checks if the value of left operand is less than or equal to the value of (A <= B) is true. right operand, if yes then condition becomes true. Called Logical AND operator. If both the operands are non-zero then then condition becomes true. (A && B) is true.
<
>=
<=
&&
||
Called Logical OR Operator. If any of the two operands is non-zero then (A || B) is true. then condition becomes true. Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
10
Bitwise Operators:
Bitwise operator works on bits and performs bit by bit operation. Assume if A = 60; and B = 13; 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
Operato r & | ^ ~
Description Binary AND Operator copies a bit to the result if it exists in both operands. Binary OR Operator copies a bit if it exists in either operand.
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. which is 0011 0001 Binary Ones Complement Operator is (~A ) will give -60
unary and has the effect of 'flipping' bits. << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.
which is 1100 0011 A << 2 will give 240 which is 1111 0000
>>
11
Assignment Operators:
Description Simple assignment operator, Assigns values from right side operands to left side operand Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand
+=
-=
Subtract AND assignment operator, 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, 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, It divides left operand with the right operand and assign the C /= A is equivalent to C=C/A
*= /=
result to left operand %= Modulus AND assignment operator, 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
Misc. Operators: There are few other operators supported by C Language. Operat or sizeof() Description Example sizeof(a), where a is integer, will return 4. &a; will give actual address of the variable. *a; will pointer to a variable. If Condition is true? Then value X : Otherwise value Y
& * ?:
13
4 | EXPERIMENT
Tools Important: In this experiment you will need a Computer/Laptop with an IDE (Integrated Development Environment) Installed. Actually, a lot of IDE that can be used in C programming, for example: Code::Block, Dev-C++, Code Lite, Net Bean, etc. But we will be using Code::Blocks for experiment this time. Here is the download link: http://www.codeblocks.org/downloads. But, you can also use another IDE if you want. Trivial: Microsoft Visio. You can also use paper and pen to draw a flowchart.
I.
Create a flowchart of a program that asks the user to enter two numbers, both numbers have the datatype "int,
after that, print out: The Sum Different Multiplication Division And, Modulo
Of the two numbers. Build the program! (Nb: use the IDE that has been installed)
II.
Create a FlowChart and program that can compare the five numbers that the user entered. Then, printout the maximum and minimum number. (Nb: use the IDE that has been installed)
14
III.
Build the app/program to print a string accordance with the number entered into the program. Number (max=100, min=0). For example:
------------------------------------------Number Input by User: 1, Output String: satu Or Number Input by User: 11, Output String: sebelas Or Number Input by User:50, Output String: lima puluh Or Number Input by User:71, Output String: tujuh puluh satu
--------------------------------------------
Page
15