You are on page 1of 23

Department of Information System Engineering

(CS211: Programming Fundamental) Lab No. 01 Introduction to Programming

Objective: To Programming. Approach:

familiarize

the

students

with

Computer

Use the available Source C\C++ Software.

What is Programming Language A sequence of instructions that a computer can interpret and execute; If I tell you the way from Chib Plaza to Executive Block I will tell sequence of instructions. Any wrong instruction leads to a undesired result. A program is something that runs on your computer. In case of MS Windows program is of .EXE or .COM extensions MS Word, Power point, Excel are all computer programs Why We Need Programming Language Writing machine language code is very difficult if not impossible Standard manner to type instructions on computers Why standard? << Any comments >> If there was no such standard then everyone would have to write his/her own compiler. Or use machine language On problem with using machine language is the machine language expert of one machine cannot be an expert of other machine as both machines might have totally different architectures and calls
1

Another use is that makers of programming language often supply us with pre-built functions that help us save time (hence money J )

Evaluation of Programming Language The lack of portability between different computers led to the development of high-level languagesso called because they permitted a programmer to ignore many low-level details of the computer's hardware Details of procedural, non-procedural will follow in the lectures How people used to Program Machine Language.. Damn! It was difficult Assembly Language. Remember ADD? Required too much user involvement To much to remember Less semantic C Language B Language.. Bell Labs Improved to C Language Is a compiled language

Department of Information System Engineering


(CS211: Programming Fundamental) Lab No. 02 Writing Program

A programmer uses a text editor to create or modify files containing C code. Code is also known as source code. A file containing source code is called a source file. After a C source file has been created, the programmer must invoke the C compiler before the program can be executed (run).

understandable by

Compiler converts human readable language to a language which is

the operating system/hardwa re Examples of C/C++ compilers of today: Visual C++ GCC/G++ DJGPP (open source for windows like GCC) Borland C Turbo (obsolete and not recommended)

Three Stages of Compilation Stage 1: Preprocessing Performed by a program called the preprocessor Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code Strips comments and white space from the code
4

The source code as stored on disk is not modified. Stage 2: Compilation Performed by a program called the compiler Translates the preprocessor-modified source code into object code (machine code)
Checks for syntax errors and warnings

Saves the object code to a disk file, if instructed to do so (we will not do this). If any compiler errors are received, no object code file will be generated. An object code file will be generated if only warnings, not errors, are received. Stage 3: Linking Combines the program object code with other object code to produce the executable file. The other object code can come from the Run-Time Library, other libraries, or object files that you have created. Saves the executable code to a disk file. On the Linux system, that file is called a.out. If any linker errors are received, no executable file will be generated. Object Code It is machine language code containing various calls specific to operating system e.g the object code written by compiler is not only hardware dependent but also operating system dependent.

So if you have linux and windows both operating systems then object file of compiled by one Operating System (OS) will not get executed on the other OS

Department of Information System Engineering


(CS211: Programming Fundamental) Lab No. 03 6

Programming Development Using C

Program De lopme Using gcc ve nt


Editor
Source File pgm.c

Preprocessor
Modified Source Code in RAM

Compiler
Program Object Code File pgm.o Other Object Code Files (if any)

Linker
Executable File a.out

Anatomy of C Program program header comment preprocessor directives (if any) int main ( ) { statement(s) return 0 ; }

A Simple C Program #include <stdio.h> //This is preprosessor directive


7

int main ( void ) //this tells the starting point of your program { cout << Hello World <<endl ; //print the text on monitor return 0 ; //return to operating system } Note: cout is an object given to you by the creators of C++. This function saves you From the complexity of writing your own function of how to display text on the computer Screen. Hence you are more productive with the actual program rather than worrying About such issues.

Program Header Comments A comment is descriptive text used to help a reader of the program understand its content. All comments must begin with the characters /* and end with the characters */ These are called comment delimiters The program header comment always comes first. Look at the class web page for the required contents of our header comment.

Processor Directive Lines that begin with a # in column 1 are called preprocessor directives (commands). Example: the #include <stdio.h> directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code.
8

This header file was included because it contains information about the printf ( ) function that is used in this program.

Stdio.h When we write our programs, there are libraries of functions to help us so that we do not have to write the same code over and over. Some of the functions are very complex and long. Not having to write them ourselves make it easier and faster to write programs. Using the functions will also make it easier to learn to program!

int main (void) Every program must have a function called main. This is where program execution begins. main() is placed in the source code file as the first function for readability. The reserved word int indicates that main() returns an integer value. The parentheses following the reserved word main indicate that it is a function. The reserved word void means nothing is there.

Function Body

A left brace (curly bracket) -- { -- begins the body of every function. A corresponding right brace -- } -- ends the function body. The style is to place these braces on separate lines in column 1 and to indent the entire function body 3 to 5 spaces.

Cout << Hello, World! <<endl ; This line is a C++ statement. It is a call to an object with a single argument (parameter), Even though a string may contain many characters, the string itself should be thought of as a single quantity. Notice that this line ends with a semicolon. All statements in C/C++ end with a semicolon.

Return 0; Because function main() returns an integer value, there must be a statement that indicates what this value is. The statement return 0 ; indicates that main() returns a value of zero to the operating system.

10

A value of 0 indicates that the program successfully terminated execution. Do not worry about this concept now. Just remember to use the statement.

Department of Information System Engineering


(CS211: Programming Fundamental) Lab No. 04 Two Integer Values Program

/***************************************** ** File: proj1.c ** Author: Joe Student ** Date: 9/15/01 ** SSN: 123-45-6789 ** Section: 0304 ** E-mail: jstudent22@umbc.edu ** ** This program prompts the user for two integer values then displays ** their product. ** ***********************************************/

11

#include <stdio.h> int main( void ) { int value1, value2, product ; printf(Enter two integer values: ) ; scanf(%d%d, &value1, &value2) ; product = value1 * value2 ; printf(Product = %d\n, product) ; return 0 ; }

Good Programming Practices C programming standards and indentation styles are available on the 104 course homepage. You are expected to conform to these standards for all programming projects in this class and in CMSC 201. (This will be part of your grade for each project!) The program just shown conforms to these standards, but is uncommented (later). Subsequent lectures will include more Good Programming Practices slides.

Token The smallest element in the C language is the token.

12

It may be a single character or a sequence of characters to form a single item. Token are following: Tokens can be: Numeric constants Character constants String constants Keywords Names (identifiers) Punctuation Operators

Department of Information System Engineering


(CS211: Programming Fundamental) Lab No. 05 Numeric, Character & String Constant and Key words

Numeric constants Numeric constants are an uninterrupted sequence of digits (and may contain a period). They never contain a comma. Examples: 123 98.6 1000000
13

Character Constants Singular! One character defined character set. Surrounded on the single quotation mark. Examples: A a $ 4 String Constants A sequence characters surrounded by double quotation marks. Considered a single item. Examples: UMBC I like ice cream. 123 CAR car

Keywords Sometimes called reserved words. Are defined as a part of the C language. Can not be used for anything else! Examples: int while for Names Sometimes called identifiers.
14

Can be of anything length, but on the first 31 are significant (too long is as bad as too short). Are case sensitive: abc is different from ABC Must begin with a letter and the rest can be letters, digits, and underscores. Must follow the standards for this course! Punctuation Semicolons, colons, commas, apostrophes, quotation marks, braces, brackets, and parentheses. ; : , [ ] { } ( ) Operators There are operators for: assignments mathematical operations relational operations Boolean operations bitwise operations shifting values calling functions subscripting obtaining the size of an object obtaining the address of an object referencing an object through its address choosing between alternate subexpressions
Department of Information System Engineering
(CS211: Programming Fundamental) Lab No. 06 Pointer Function Output

What will be output of following Function? a)


15

void myfunction (int I, int *j); main() { int I = -5, j=-2; myfunction(I, &j); count<<i<<j; } void myfunction (int i, int *j) { i = i * i; *j= *j * *j; }

b) main() { float a= 7.999999; float *b, *c; b=7a; c=b; cout<<&a<<b<<c; cout<<a<<*(&a)<<*b<<*c; }

c) main() { int a,*b, **c, ***d, ****e; a=10; b=&a; 16

c=&b; d=&c; e=&d; cout<<a<<b<<c<<d<<e; cout<<a<<*b<<**c<<***d<<****e; }

d) main() { int a,*b; float c; b=&c; cout<<b; }

e) main() { float a=10,*b; cout<<b; } f) main() { float a=10,*b; cout<<*b; } 17

Q2. Write a function name power that calculates the power of a number. Make use of pointers. Q3. Try the swap and increment function given in slides

Department of Information System Engineering


(CS211: Programming Fundamental) Lab No. 07 Loop Function 1. What is wrong with following code: 18

while (n<=100) sum+=n*n; 2. Write a program that uses a while loop to compute and print the sum of a given number of squares. For example, if 5 is input, then the program will print 55, which equals 12 + 22 + 32 + 42 + 52 . 3. Write the output of the following program. int main() { for( int i=0; i<8;i++) { if( i%2==0) cout<< i + 1<<endl; else if ( i%3==0) continue; else if (i% 5==0) break;

cout<<End of the program.\n; } cout<<end of the program.\n; } 4. Pascal Triangle is a triangular array of numbers that begins like this:

Each number in pascals Triangle is one of the combination C(n,k). If we count the rows and the diagonal columns starting with 0, then the number C(n,k) is in row n and column k. For example, the number C (6,2)= 15 is in row number 6 and column number 2. Write a program that usese the comb() function to print Pascals Triangle down to row number 12. 19

5. Write and test digit ( ) function: int digit (int n, int k) This function returns the kth digit of the positive integer n. For example, if n is the integer 29415, then call digit (n,0) would return the digit 5, and the call digit(n, 2) would return the digit 4. Note that the digits are numbered from right to left beginning with the zeroth digit. 6.Write a program that asks for the users height, weight, and the age and then computes clothing sizes according to the formulas: Hat Size = weight in pounds divided by height in inches and all that multiplied by 2.9 Jacket Size (chest in inches)= height times weight divided by 288 and then adjusted by adding 1/8 of an inch for each 10 years over age 30. (note that the adjustment only takes place after a full 10 years. So there is no adjustment for age 30 through 39, but 1/8 of an inch is added for age 40). Waist in inches= weight divided by 5.7 and then adjusted by adding 1/10 of an inch for each 2 years over age 28. (Note that the adjustment only takes place after a full 2 years. So there is no adjustment for age 29, but 1/10 of an inch is added for age 30) Use functions for each calculation. Your program should allow the user to repeat this calculation as often as the user wishes.

Department of Information System Engineering


(CS211: Programming Fundamental) Lab No. 8

20

1. A university has the following rules for a student to quality for a degree with A as the main subject and B as the subsidiary subject: a) B. He should get 55 percent or more in A and 45 percent or more in

b) If he gets less than 55 percent in A he should get 55 percent or more in B. However, he should get at least 45 percent in A. c) If he gets less than 45 percent in B and 65 percent or more in A he is allowed to reappear in an examination in B to qualify. d) In all other cases he is declared to have failed. Write a program to receive marks in A and B and output whether the students has passed, failed or allowed to reappear in B. 2. A policy followed by company to process customer orders is given by following rule: a) If a customer order is less than or equal to that in stock and his credit is OK, supply his requirement. b) If his credit is not OK do not supply. Send him intimation. c)If his credit is OK but the item in stock is less than his order, supply what is in stock. Intimate to him the date the balance will be shipped. Write a program to implement the company policy.

3.

Try the following codes and check the output:


a) main() { int i,j; for (i=1; i<=2; i++) { 21

for (j=1; j<=2; j++) { if (i==j) continue; cout<<i<<j; } } } b) main() { int i=10,j; i>=5?(j=10):(j=15) cout<<i<<j; }

Department of Information System Engineering


(CS211: Programming Fundamental) Lab No. 9

22

1.

Create a structure to specify data on students given below: Roll Number, Name, Department, Course, and Year of joining Assume that there are no more than 450 students in the school.

Write a program to print names of all students who joined in a particular year and have a particular roll number. (Year and roll number is input from the user). 2. Create a structure to specify data of customers in a bank. The data to be stored is: Account Number, Name, and Balance in account. Assume maximum of 200 customers in the bank. a. Write a function to print the Account number and name of each customer with balance below Rs. 100. If a customer request for withdrawal or deposit, it is given in the form: Acct.no, amount, (1 for deposit, 0 for withdrawal) Write a program to give a message, The balance is insufficient for specified withdrawal.

b.

3. An automobile company has serial numbers for engine parts starting from AA0 to FF9. The other characteristics of parts to be specified in structure are: year of manufacture, material and quantity manufactured. a. Specify a structure to store information corresponding to a part. b. Write a program to retrieve information on parts with serial numbers between BB1 and CC6.

23

You might also like