programing fundamental 1 2 3 4 5 6 7 8

Upload: muhammad-ali

Post on 05-Apr-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    1/23

    Department of Information System Engineering

    (CS211: Programming Fundamental)Lab No. 01

    Introduction to Programming

    Objective: To familiarize the students with ComputerProgramming.

    Approach: Use the available Source C\C++ Software.

    What is Programming Language

    A sequence of instructions that a computer can interpret andexecute;

    If I tell you the way from Chib Plaza to Executive Block I willtell sequence of instructions. Any wrong instruction leads to aundesired result.

    A program is something that runs on your computer. In case ofMS 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? >

    If there was no such standard then everyone would have to writehis/her own compiler. Or use machine language

    On problem with using machine language is the machinelanguage expert of one machine cannot be an expert of othermachine as both machines might have totally differentarchitectures and calls

    1

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    2/23

    Another use is that makers of programming language oftensupply 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 thedevelopment of high-level languagesso called because theypermitted a programmer to ignore many low-level details of thecomputer's hardware

    Details of procedural, non-procedural will follow in the lectures

    How people used to Program

    Machine Language.. Damn! It was difficultAssembly Language. Remember ADD?

    Required too much user involvementTo much to rememberLess semantic

    C LanguageB Language.. Bell LabsImproved to C LanguageIs a compiled language

    2

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    3/23

    Department of Information System Engineering

    (CS211: Programming Fundamental)Lab No. 02

    Writing Program

    A programmer uses a text editor to create or modify filescontaining 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 mustinvoke the C compiler before the program can be executed(run).

    3

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    4/23

    Three Stages of Compilation

    Stage 1: Preprocessing

    Performed by a program called the preprocessor

    Modifies the source code (in RAM) according topreprocessor directives (preprocessor commands)embedded in the source code

    Strips comments and white space from the code

    4

    Compiler convertshuman readable

    language to alanguage which is

    understandable bythe operating

    system/hardware

    Examples ofC/C++

    compilers oftoday:

    Visual C++GCC/G++

    DJGPP (opensource for

    windows likeGCC)

    Borland CTurbo

    (obsolete andnot

    recommended)

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    5/23

    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 intoobject code (machinecode)

    Checks forsyntax 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 filewill be generated.

    An object code file will be generated if only warnings,not errors, arereceived.

    Stage 3: Linking

    Combines the program object code with other objectcode to produce the executable file.

    The other object code can come from the Run-TimeLibrary, other libraries, or object files that you havecreated.

    Saves the executable code to a disk file. On the Linuxsystem, that file is called a.out.

    If any linker errors are received, no executablefile will be generated.

    Object Code

    It is machine language code containing various callsspecific to operating system e.g the object code writtenby compiler is not only hardware dependent but alsooperating system dependent.

    5

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    6/23

    So if you have linux and windows both operating systemsthen 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

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    7/23

    Programming Development Using C

    Program Development Using gccProgram Development Using gcc

    Source File pgm.c

    Program Object Code File pgm.o

    Executable File a.out

    Preprocessor

    Modified Source Code in RAM

    Compiler

    Linker

    Other Object Code Files (if any)

    Editor

    Anatomy of C Program

    program header comment

    preprocessor directives (if any)

    int main ( ){

    statement(s)return 0 ;

    }

    A Simple C Program

    #include //This is preprosessor directive

    7

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    8/23

    int main ( void ) //this tells the starting point of yourprogram{

    cout

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    9/23

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

    Stdio.h

    When we write our programs, there are libraries of functions tohelp us so that we do not have to write the same code over andover.

    Some of the functions are very complex and long. Not having towrite them ourselves make it easier and faster to writeprograms.

    Using the functions will also make it easier to learn to program!

    int main (void)

    Every program must have a function called main. This iswhere program execution begins.

    main() is placed in the source code file as the first function forreadability.

    The reserved word int indicates that main() returns aninteger value.

    The parentheses following the reserved word main indicatethat it is a function.

    The reserved word void means nothing is there.

    Function Body

    9

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    10/23

    A left brace (curly bracket) -- { -- begins the body of everyfunction. A corresponding right brace -- } -- ends the functionbody.

    The style is to place these braces on separate lines in column 1and to indent the entire function body 3 to 5 spaces.

    Cout

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    11/23

    A value of 0 indicates that the program successfully terminatedexecution.

    Do not worry about this concept now. Just remember to use thestatement.

    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: [email protected]

    **** This program prompts the user for two integer values thendisplays** their product.*************************************************/

    11

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    12/23

    #include 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 availableon the 104 course homepage.

    You are expected to conform to these standards for allprogramming projects in this class and in CMSC 201. (This willbe part of your grade for each project!)

    The program just shown conforms to these standards, but isuncommented (later).

    Subsequent lectures will include more Good ProgrammingPractices slides.

    Token

    The smallest element in the C language is the token.

    12

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    13/23

    It may be a single character or a sequence of characters to forma 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 (andmay contain a period). They never contain a comma.

    Examples:

    123

    98.6

    1000000

    13

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    14/23

    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

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    15/23

    Can be of anything length, but on the first 31 are significant (toolong is as bad as too short).Are case sensitive:abc is different from ABCMust begin with a letter and the rest can be letters, digits, andunderscores.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

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    16/23

    void myfunction (int I, int *j);main(){

    int I = -5, j=-2;

    myfunction(I, &j);count

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    17/23

    c=&b;

    d=&c;

    e=&d;

    cout

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    18/23

    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

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    19/23

    while (n

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    20/23

    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, the

    call digit (n,0) would return the digit 5, and the call digit(n, 2) would return the digit 4. Note that th

    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 siz

    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

    inch for each 10 years over age 30. (note that the adjustment only takes place after a full 10 years. Sthere 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 ov

    age 28. (Note that the adjustment only takes place after a full 2 years. So there is no adjustment for a

    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 oft

    as the user wishes.

    Department of Information System Engineering

    (CS211: Programming Fundamental)Lab No. 8

    20

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    21/23

    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) He should get 55 percent or more in A and 45 percent or more in

    B.

    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, supplywhat 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

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    22/23

    for (j=1; j

  • 8/2/2019 Programing Fundamental 1 2 3 4 5 6 7 8 .

    23/23

    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)

    b. Write a program to give a message, The balance is insufficient for

    specified withdrawal.

    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.