Anda di halaman 1dari 60

COMPUTER PROGRAMMING

Lab # 1

Farrah Ambreen

2
Computer Programming in C++

How we will Proceed


Attendance first 15 min
Announcements, and for other course materials on yahoo group <will announce later> Ask questions, participate actively in class Copying assignments results in zero No late assignments will be accepted Quizzes would be unannounced, expect a quiz in every lab

3
Computer Programming in C++

Lab Marks distribution


Lab exam Quiz Assignments Lab participation Total 10% 5% 3% 2% 20%

Or as according to Course instructor

4
Computer Programming in C++

Platform

Microsoft Visual Studio C++

5
Computer Programming in C++

Building our first C++ Project

6
Computer Programming in C++

7
Computer Programming in C++

8
Computer Programming in C++

9
Computer Programming in C++

Select Win32 Console Application

10
Computer Programming in C++

Name of the project

Hit to save

11
Select theComputer Programming in C++ location

12
Computer Programming in C++

After Selection

Hit Ok

13
Computer Programming in C++

Hit Finish

14
Computer Programming in C++

Hit Ok

15
Computer Programming in C++

Class Generated

16
Computer Programming in C++

17
Computer Programming in C++

18
Computer Programming in C++

Project name

Write file name

Location

19
Computer Programming in C++

Project name

Select Write file name

Location

Hit Ok

20
Computer Programming in C++

Text Editor Workspace window

21
Computer Programming in C++

Tools of the trade


Editor Interpreter and Compilers Debuggers

22

Integrated Development Environment (IDE)


It contains
Editor Compilers Debugger Linkers Loaders

Computer Programming in C++

23
Computer Programming in C++

Editor
Preprocessor Compiler Linker

Disk
Disk Disk Disk

Program is created in the editor and stored on disk. Preprocessor program processes the code. Compiler creates object code and stores it on disk. Linker links the object code with the libraries

Primary Memory

Loader Disk Loader puts program in memory.


.. .. ..

Primary Memory

CPU

.. .. ..

CPU takes each instruction and executes it, possibly storing new data values as the program executes.

24
Computer Programming in C++

First Program

No we are going to write first program in C++

25
Computer Programming in C++

#include <iostream.h> void main (void ) { cout << Welcome to Air University; }

Out put of the program


Welcome to Air UniversityPress any key to continue

cout<< can be used with


Variable
cout<<num;

String
cout<<Hello Word;

Expression
cout<<a + b;

Constant
cout<<20;

28
Computer Programming in C++

Escape Sequence
\a \b \n \r \t Bell (beep) Backspace New Line Return Tab (8 spaces)

29
Computer Programming in C++

#include <iostream.h> void main (void) { cout << \n\t Welcome to Air University \n; cout<< \nSchool of Engineering\n; }

30
Computer Programming in C++

Practice Que 1
Write a program to display following output, ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ################################# ################################# #################################

31
Computer Programming in C++

Practice Que 2
Write a program which prints following output, $******************************************$ $******************************************$ $* Welcome to Air University *$ $* *$ $* School of Engineering *$ $* *$ $* lslamabad. *$ $****************************************** $ $****************************************** $

32
Computer Programming in C++

Variable
Variable

33

Pic of the memory

Variable

Computer Programming in C++

25

10323

name of the variable

34

Variable
Rules to Write Variable.
1. 2. 3.

Computer Programming in C++

First Letter Character Underscore _ (Not Recommended) Cant use real numbers (Syntax Errorr) int num1;

35

Variable
Small post box

Computer Programming in C++

36
Computer Programming in C++

Variable
Variable is the name of a location in the memory e.g. x= 2;

37
Computer Programming in C++

Variable
In a program a variable has: 1. Name 2. Type 3. Size 4. Value

38
Computer Programming in C++

Variable
i.e

int x = 2; float y = 3.6

39
Computer Programming in C++

Assignment Operator

=
x=2
X

40
Air University

Assignment Operator
L.H.S = R.H.S. X+ 3 = y + 4 Wrong
Z = x +4

x +4 = Z

Wrong

41
Air University

X = 10 ;

X X

10
30

X = 30 ;

42
Air University

X = X + 1;
10
+ 1
11

=
X

43
Air University

Example Of Assignment Operator


#include <iostream.h> void main (void) { int a, b; a = 10; b = 4; a = b; b = 7; cout << "a :-"; cout << a; cout << " b :-"; cout << b; }

44

Data type
Air University

int i ; -> Declaration line

45

#include <iostream.h> void main (void ) { int x ; int y ; int z ; x = 10 ; y = 20 ; z=x+y;


cout << " x = " ; cout << x ; cout<< \n Y = " <<y;
}

Air University

cout << \n z =x + y " << x+y ;

46
Air University

int x, y, z ; int x; int y; int z ;

47

Data Types
1. 2. 3. 4. 5. 6. int short long float double char

Air University

48
Air University

Arithmetic operators
Plus Minus Multiply Divide Modulus + * /
%

49
Air University

Arithmetic operators
i+j x*y a/b a%b

50
Air University

% = Remainder

5%2=1 2%2=0

51
Air University

4/2=2 5/2=?

52
Air University

Precedence
Highest: Next: Lowest: () *,/,% +,-

53
Air University

21.2

Precedence 192 19.2


5 2 3 1

24 8 X = 2 + 4 * 6 ( 10 2 ) / 10 Y = 10 / 5 * 2 ( 8 - 2 ) + 6

54

Quadratic Equation
In algebra
y = ax2 + bx + c

In C
y = a*x*x + b*x + c

55

a*b%c +d

56

a*(b%c) = a*b%c

57
Air University

Practice Que 3
Write a program in C++, which takes radius from the user and calculate the area of sphere i.e Area=4pr2 (Hint p = 3.1416 Area = 4 * 3.1416 * r * r)

58
Air University

Practice Que 4

Write a program to find the number of bytes occupied by various data types using the sizeof operator?

59
Air University

Practice Que 4 (Code)


#include<iostream.h> void main () { int a; type char b; float c; long int d; bool e; short f; double g; unsigned char h; unsigned short i; unsigned int j; unsigned long k; //Declaration of Variables for all data

60
Air University

Practice Que 4 (Code)


cout<<"\nThe Size of Integer is = "<<sizeof(a); cout<<"\nThe Size of Chacter is = "<<sizeof(b); cout<<"\nThe Size of Float is = "<<sizeof(c); cout<<"\nThe Size of Long Integer is = "<<sizeof(d); cout<<"\nThe Size of Boolean is = "<<sizeof(e); cout<<"\nThe Size of Short is = "<<sizeof(f); cout<<"\nThe Size of Double is = "<<sizeof(g); cout<<"\nThe Size of Unsigned char is = "<<sizeof(h); cout<<"\nThe Size of Unsigned short is = "<<sizeof(i); cout<<"\nThe Size of Unsigned int is = "<<sizeof(j); cout<<"\nThe Size of Unsigned Long is = "<<sizeof(k)<<endl; }

Anda mungkin juga menyukai