1 / 28

Lecture# 17 Programming Concepts

Lecture# 17 Programming Concepts. Exercise. Input your name and display it in reverse order Count the element of any character array. #include<iostream> #include<conio> #include<stdio> void main(){ char a[100]; gets(a); int count=0; for(int i=0;a[i]!='';i++,count++);

kalea
Download Presentation

Lecture# 17 Programming Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture# 17 Programming Concepts

  2. Exercise • Input your name and display it in reverse order • Count the element of any character array

  3. #include<iostream> • #include<conio> • #include<stdio> • void main(){ • char a[100]; • gets(a); • int count=0; • for(int i=0;a[i]!='\0';i++,count++); • for(int i=count-1;i>=0;i--) • cout<<a[i]; • getch(); • }

  4. 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”

  5. 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 3 1 57 3 15 1 9 9 15 57 3 15 9 1 57 Descending Ascending Bubble Sort

  6. 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: 5 4 3 2 1

  7. #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]<<" ";

  8. 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; • }

  9. Problem Write a code which calculate minimum element from an array of digits

  10. #include<conio> • #include<iostream> • void minimum(int[]); • const int Size=10; • int main(){ • int array[Size]; • cout<<"Enter 10 elements:"; • for(int i=0;i<Size;i++) • cin>>array[i]; • minimum(array); • getch(); • return 0; • }

  11. void minimum(int a[Size]){ • int min; • min=a[0]; • for(int i=1;i<Size;i++){ • if(a[i]<min) • min=a[i]; • } • cout<<"\nMinimum Element :"<<min; • }

  12. Passing Arrays to Functions • We should mention the size of the array when pass to the function • Arrays are passed by reference • int MyArray[5] MyArray

  13. Passing Array To Function • #include<conio> • #include<iostream> • void f(int[],int,int); • int main(){ • int number[10]={0},a=15; • f(number,10,a); • for(int i=0;i<10;i++) • cout<<number[i]; • cout<<a; • getch(); • return 0; • }

  14. Passing Array To Function • void f(int x[],int arraySize,int a){ • a=100; • int i; • for(i=0;i<arraySize;i++) • x[i]=i; • }

  15. #include<conio> • #include<iostream> • void check(int); • int main(){ • int a[5]={5,4,6,7,9}; • cout<<"Third element is::"<<a[2]; • check(a[2]); • cout<<"\nThird element after call:"<<a[2]; • getch(); • return 0; • } • void check(int x){ • x=35; • }

  16. Assignment • #include<conio> • #include<iostream> • void menu(); • void input(int[]); • void output(int[]); • void large(int[]); • void small(int[]); • void sortA(int[]); • void sortD(int[]); • void search(int[]); • const int Size=10; • int main(){

  17. int main(){ • int x[Size],choice; • while(1){ • clrscr(); • menu(); • cin>>choice; • switch(choice){ • case 1: input(x);break; • case 2: output(x);getch();break; • …….. • case 8: return 0;break; • default: cout<<"\n\n\t\tYou have Entered Wrong Choice"; • } • } • }

  18. Two Dimensional Array • int x [ 4 ] [4 ] ; 0 1 2 3 0 1 2 3

  19. Two Dimensional Array • int x [ 2 ] [ 3 ] ;

  20. int main(){ • const int row=2; • const int col=3; • int a[row][col]; • for(int i=0;i<row;i++){ • for(int j=0;j<col;j++){ cout<<"\nCell["<<i<<"]"<<"["<<j<<"]="; • cin>>a[i][j]; • } • }

  21. for(int i=0;i<row;i++){ • cout<<"\n\n\t\t"; • for(int j=0;j<col;j++){ • cout<<a[i][j]<<" "; • } • cout<<endl; • } • getch(); • return 0; • }

  22. Example

More Related