Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Lecture# 15 Programming Concepts PowerPoint Presentation
Download Presentation
Lecture# 15 Programming Concepts

Lecture# 15 Programming Concepts

117 Vues Download Presentation
Télécharger la présentation

Lecture# 15 Programming Concepts

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Lecture# 15 Programming Concepts

  2. Arrays • They are special kind of data type • They are like data structures in which identical data types are stored • In C++each array has: • Name • Data type • Size • They occupy continuous area of memory

  3. Array Declaration • arrayType arrayName[numberOfElements ]; • For example : intage [ 10 ] ;

  4. Array Declaration • arrayType arrayName[numberOfElements ]; • For example : intage [ 10 ] ; age[0] age[1] age[2] age[3] age[4] .72 age[5] .94 age[6] age[7] age[8] age[9]

  5. Array Declaration • More than one array can be declared on a line: • int age [10] , height [10] , names [20] ; • Mix declaration of variables with: declaration of arrays • int i , j , age [10] ;

  6. 0 0 32 0 1 1 34 0 2 2 0 3 3 0 4 4 0 .72 .72 5 5 0 .94 .94 0 6 6 7 7 0 40 8 8 0 9 9 0 Initializing Array There are several ways to initialize an array: • int age[10]; age[0]=32;age[1]=34;age[7]=40; • int age [ 10 ] = { 0,0,0,0,0,0,0,0,0,0} ; • int age [ 10 ] = { 0 } ; • int age [ ] = {10,15,30,20,34} ;

  7. Initializing Array Perhaps the most common way is the use of loops for initialization: • int c[10]; • for(int i=0;i<10;i++) • c[i]=0; • for(int i=0;i<10;i++) • cin>>c[i];

  8. Example# 1 • int main(){ • int age[5]; • for(int i=0;i<5;i++){ • cout<<"Please enter your age:"; • cin>>age[i]; • } • for(int i=0;i<5;i++){ • cout<<"You Entered:"<<age[i]<<endl; • } • getch(); • return 0; • }

  9. Copying Arrays • To copy from array “ a ” to array “ b ” : b [ 0 ] = a [ 0 ] ; b [ 1 ] = a [ 1 ] ; b [ 2 ] = a [ 2 ] ; b [ 3 ] = a [ 3 ] ; … … … … … … b [ 10 ] = a [ 10 ] ;

  10. int main(){ • int age[]={56,23,45,57,98}; • int c[5]; • for(int i=0;i<5;i++){ • c[i]=age[i]; • } • cout<<"Elements of c are now:"; • for(int i=0;i<5;i++){ • cout<<c[i]<<" "; • } • getch(); • return 0; • }

  11. Problem Declare an array of ten integers.Input the numbers of ten students. Calculate average of these numbers.Output the average

  12. int main(){ • const int ArraySize=10; • int numbers[ArraySize]; • int add=0; • for(int i=0;i<ArraySize;i++){ • cout<<"Plz Enter ur Number:"; • cin>>numbers[i]; • add+=numbers[i]; • //Exactly Equal to add=add+numbers[i] • } • cout<<"\nAverage is:"<<(add/10); • getch(); • return 0; • }

  13. Compound Statements • Statement like c=c+3; can be abbreviated using addition assignment operator += as: c+=3; • In short an statement of type: • variable=variable operator expression;can be written as: • variable operator=expression; • Operators can be: +,-,%,*,/

  14. #include<iomanip> • int main(){ • const int arraySize=10; • int n[arraySize]={19,3,15,11,9,13,5,17,11,5}; • cout<<"Elements"<<setw(13) <<"Value"<<setw(17)<<"Histogram"<<endl; • for(int i=0;i<arraySize;i++){ cout<<setw(7)<<i<<setw(13)<<n[i]<<setw(9); • for(int j=0;j<n[i];j++) • cout<<"*"; • cout<<endl; • } • getch(); • return 0; • }

  15. Search • Important area in computer science • The process of finding particular element in an array is called searching • The technique we use for searching and element from the array is called linear search • The element to be searched is also sometimes called key

  16. #include<conio> • #include<iostream> • int main(){ • const int arraySize=10; • int Record[arraySize]={19,3,15,11,9,13,5,17,11,5}; • cout<<"Enter the element you want to search:"; • int i,n,found=0; • cin>>n;

  17. for(i=0;i<arraySize;i++){ • if(n==Record[i]){ • found=1; • break; • } • } • if(found==1) • cout<<"Element is found at location Record["<<i<<"]"; • else • cout<<"Element not found in record"; • getch(); • return 0; • }

  18. Character Arrays • Character array can be initialized as: char string[]=“first”; • This string contains 5 character and a special character • This character is ‘\0’

  19. Character Arrays • This character is appended in the end of string by compiler • We can store character by character in this kind of array like • char string1[]={‘f’,’i’,’r’,’s’,’t’,’\0’}; • We can use cin for input • For output of string cout work and display char array up to Null character

  20. int main(){ • char c[100],d[]={'T','h','i','s',' ','i','s','\0'}; • cout<<"Plz Enter c string:"; • cin>>c; • cout<<"\nd is: "<<d; • cout<<"\nc is: "<<c; • cout<<"\nc from Loop: "; • for(int i=0;c[i]!='\0';i++){ • cout<<c[i]; • } • cout<<"\nd from Loop: "; • for(int i=0;d[i]!='\0';i++) • cout<<d[i]; • getch(); • return 0; • }

  21. int main(){ • char c[]="Two roads diverged\n in a yellow wood,"; • cout<<c; • int i=0; • while(c[i]!='\0'){ • if(c[i]=='d') • c[i]='e'; • i++; • } • getch(); • return 0; • }

  22. Problem • Declare an array of characters.The size of the array is 100.Input a string from the user and return it with more spaces on the location where spaces already exists.

  23. #include<stdio.h> • int main(){ • char a[100]; • cout<<"Plz Enter A String:"; • gets(a); • cout<<"\nThe text with more spaces:\n"; • for(int i=0;a[i]!='\0';i++){ • if(a[i]==' ') • a[i]='\t'; • } • cout<<a; • getch(); • return 0; • }

  24. Sorting • Arrangement of data in an array is called sorting • We can sort data in ascending or descending order • Lot of techniques used for sorting purpose • Technique we use for sorting in called “bubble sort”

  25. Bubble Sort • Array: 4 3 5 2 1 • Ascending order: 1 2 3 4 5 • Descending order: 5 4 3 2 1

  26. Bubble Sort • Array: 2 1 3 4 5 • Pass 1: 2 3 4 5 1 • Pass 2: 3 4 5 2 1 • Pass 3: 4 5 3 2 1 • Pass 4: 4 5 3 2 1

  27. #include<conio> • #include<iostream> • int main(){ • const int Size=10; • int number[Size]; • cout<<"Enter 10 Numbers:"; • for(int i=0;i<Size;i++) • cin>>number[i]; • cout<<"\nYou Have Entered:"; • for(int i=0;i<Size;i++) • cout<<number[i]<<" ";

  28. for(int i=0;i<Size-1;i++){ • for(int j=0;j<Size-1;j++){ • if(number[j]<number[j+1]){ • int temp=number[j]; • number[j]=number[j+1]; • number[j+1]=temp; • } • } • } • cout<<"\nSorted Aray: \n\n\n"; • for(int i=0;i<Size;i++) • cout<<number[i]<<" "; • getch(); • return 0; • }