Lab # 1

1,157 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,157
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
41
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lab # 1

  1. 1. Introduction to Programming Lab No. 1 saqib.rasheed@mail.au.edu.pk Air University 1
  2. 2. Platform Air University 2
  3. 3. Tools of the trade• Editor• Interpreter and Compilers• Debuggers Air University 3
  4. 4. Integrated Development Environment (IDE) It contains • Editor • Compilers • Debugger • Linkers • Loaders Air University 4
  5. 5. Program is created in the Editor Disk editor and stored on disk. Preprocessor programPreprocessor Disk processes the code. Compiler creates Compiler Disk object code and stores it on disk. Linker Disk Linker links the object code with the libraries Primary Memory Loader Loader puts program in memory. Disk .. .. .. Primary Memory CPU takes each CPU instruction and executes it, possibly storing new data .. .. values as the program .. executes. Air University 5
  6. 6. First ProgramNo we are going to write first program in C++ Air University 6
  7. 7. #include <iostream.h>main ( ){ cout << “ Welcome to Air University “;} Air University 7
  8. 8. cout<< can be used with• Variable – cout<<num;• String – cout<<“Hello Word”;• Expression – cout<<a + b;• Constant – cout<<20;• Call of Function – cout<<pow(a);
  9. 9. Escape Sequencea Bell (beep)b Backspacen New Liner Returnt Tab (8 spaces) 9
  10. 10. #include <iostream.h>main ( ){cout << “nt Welcome to Air University n“;cout<<“ nSchool of Engineeringn”;} 10
  11. 11. Practice Que 1Write a program to display following output, ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ################################# ################################# ################################# Air University 11
  12. 12. Practice Que 2Write a program which prints following output,$******************************************$$******************************************$$* Welcome to Air University *$$* School of Engineering *$$* lslamabad. *$$****************************************** $$****************************************** $ Air University 12
  13. 13. VariableVariable X Air University 13
  14. 14. • Pic of the memory Variable• 25 name• 10323 of the variable Air University 14
  15. 15. VariableRules to Write Variable. 1. First Letter Character 2. Underscore _ (Not Recommended) 3. Can’t use real numbers (Syntax Errorr) int num1; Air University 15
  16. 16. Variable• Small post box X Air University 16
  17. 17. VariableVariable is the name of a location inthe memory e.g. x= 2; Air University 17
  18. 18. VariableIn a program a variable has:1. Name2. Type3. Size4. Value Air University 18
  19. 19. Variablei.e int x = 2; float y = 3.6 Air University 19
  20. 20. Assignment Operator = x=2X 2 Air University 20
  21. 21. Assignment OperatorL.H.S = R.H.S.X+ 3 = y + 4 WrongZ = x +4x +4 = Z Wrong Air University 21
  22. 22. X = 10 ; X 10X = 30 ; X 30 Air University 22
  23. 23. X = X + 1; 10 + 1 = 11X Air University 23
  24. 24. Example Of Assignment Operator#include <iostream.h>void main (){ int a, b; a = 10; b = 4; a = b; b = 7; cout << "a :"; cout << a; cout << " b :"; cout << b;} Air University 24
  25. 25. Data type• int i ; -> Declaration line i Air University 25
  26. 26. #include <iostream.h>main ( ){ int x ; int y ; int z ; x = 10 ; y = 20 ; z=x+y; cout << " x = " ; cout << x ; cout<< " Y = " <<y; cout << " z =x + y " << x+y ;} Air University 26
  27. 27. int x, y, z ;int x; int y; int z ; Air University 27
  28. 28. Data Types 1. int 2. short 3. long 4. float 5. double 6. char Air University 28
  29. 29. Arithmetic operatorsPlus +Minus -Multiply *Divide /Modulus % Air University 29
  30. 30. Arithmetic operators i+j x*y a/b a%b Air University 30
  31. 31. % = Remainder 5%2=1 2%2=0 Air University 31
  32. 32. 4/2=25/2=? Air University 32
  33. 33. Precedence• Highest: ()• Next: *,/,%• Lowest: +,- Air University 33
  34. 34. Precedence21.2 5 192 19.2 3 24 8 2 1 X = 2 + 4 * 6 ( 10 – 2 ) / 10 Y = 10 / 5 * 2 ( 8 - 2 ) + 6 Air University 34
  35. 35. Quadratic Equation• In algebra y = ax2 + bx + c• In C y = a*x*x + b*x + c 35
  36. 36. a*b%c +d 36
  37. 37. a*(b%c) = a*b%c 37
  38. 38. Practice Que 3Write a program in C++, which takes radius from the user and calculate the area of sphere i.e Area=4pr2(Hint p = 3.1416Area = 4 * 3.1416 * r * r) Air University 38
  39. 39. Practice Que 4Write a program to find the number of bytes occupied by various data types using the sizeof operator? Air University 39
  40. 40. Practice Que 4 (Code)#include<iostream.h>void main (){ int a; //Declaration of Variables for all data 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; Air University 40
  41. 41. 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;} Air University 41
  42. 42. Simple Program In C++ using 5 Arithmetic Operators1) + (Plus)2) - (Minus)3) * (Multiply)4) / (Divide)5) % (Modulo) Air University 42
  43. 43. Assignment No 1Write a program that take two values from user and perform all arithmetic operation on them and display the result. Air University 43
  44. 44. Instructions of Assignment • Take help from books • Take help from Internet • Do not copy paste • Copied assignments will be marked Zero • Viva will be conducted in next lab • Total Marks 15 • Last date Next Lab • Late assignment will not be considered44
  45. 45. Simple Program In C++ using 5 Arithmetic Operators#include<iostream.h> //Header File Used for Input/Outputvoid main () //Mian Funcation Starting With Braces{ int num1, num2, res; //Declaring Three Variables of type int cout<<"Enter the First Number= "; //Display the Statement cin>>num1; //Stores the value in memory cout<<"Enter the Second Number= "; cin>>num2; res = num1 + num2; //Adding two Integers cout<<"Addition = "<<res<<endl; res = num1 - num2; //Subtracting two Integers Air University 45
  46. 46. Simple Program In C++ using 5 Arithmetic Operatorscout<<"Subtraction ="<<res<<endl; res = num1 * num2; //Multiplication two Integers cout<<"Multiplication ="<<res<<endl; res = num1 / num2; //Dividing two Integers cout<<"Division = "<<res<<endl; res = num1 % num2; //Modulus of two Integers cout<<"Modulus = "<<res<<endl;} //End of Mian Funcation Air University 46
  47. 47. For Slideshttps://sites.google.com/site/saqibrashied/cplusplus

×