280 likes | 438 Views
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++);
E N D
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]!='\0';i++,count++); • for(int i=count-1;i>=0;i--) • cout<<a[i]; • getch(); • }
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”
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
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
#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]<<" ";
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; • }
Problem Write a code which calculate minimum element from an array of digits
#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; • }
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; • }
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
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; • }
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; • }
#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; • }
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(){
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"; • } • } • }
Two Dimensional Array • int x [ 4 ] [4 ] ; 0 1 2 3 0 1 2 3
Two Dimensional Array • int x [ 2 ] [ 3 ] ;
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]; • } • }
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; • }