>array[i]; } cout<array[j+1]) { hold=array[j]; array[j]=array[j+1]; array[j+1]=hold; } } } cout<<"Sorted Array is: "< >array[i]; } cout<array[j+1]) { hold=array[j]; array[j]=array[j+1]; array[j+1]=hold; } } } cout<<"Sorted Array is: "<

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array & Matrix Selected topics. 1. It will compare two adjacent elements, if second element is smaller than the first then it will swap them, if we wanted.

Similar presentations


Presentation on theme: "Array & Matrix Selected topics. 1. It will compare two adjacent elements, if second element is smaller than the first then it will swap them, if we wanted."— Presentation transcript:

1 Array & Matrix Selected topics

2 1. It will compare two adjacent elements, if second element is smaller than the first then it will swap them, if we wanted to sort an array in an ascending order. 2. It will continue the above process of comparing pares, from the first pare to the last pair, at its first iteration the greatest element is placed at the last index of an array. 3. Then it will again repeat the above steps for all the elements by excluding the greatest one. 4. It will continue to repeat until the array becomes sorted. Let’s take a look towards the following example which will illustrate and completely describe the use, working and operations of bubble sort.

3 // sort array #include using namespace std; main() { int hold; int array[5]; cout<<"Enter 5 numbers: "<<endl; for(int i=0; i<5; i++) { cin>>array[i]; } cout<<endl; cout<<"Orignally entered array by the user is: "<<endl; for(int j=0; j<5; j++) { cout<<array[j]; cout<<endl; } cout<<endl; for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { if(array[j]>array[j+1]) { hold=array[j]; array[j]=array[j+1]; array[j+1]=hold; } } } cout<<"Sorted Array is: "<<endl; for(int i=0; i<5; i++) { cout<<array[i]<<endl; } getch(); }

4 // getting number in array #include main() { int abb[4]; int r; int initial=0; int final=4; int mid; int location=-5; cout<<"Enter 5 numbers to store in array: "<<endl; for(int i=0; i<5; i++) { cin>>abb[i]; } cout<<endl; cout<<"Enter the number you want to found :"; cin>>r; cout<<endl; while(initial<=final) { mid= (initial+final)/2; if(abb[mid]==r) { location=mid; break; } if(r<abb[mid]) final=mid-1; if(r>abb[mid]) initial=mid+1; } if(location==-5) cout<<" Required number not found "<<endl; else cout<<" Required number is found at index "<<location<<endl; getch(); }

5 // prime number #include using namespace std; bool prime(int); main() { int a; cout<<"Enter an interger greater then 1: "; cin>>a; if(prime(a)) cout<<a<< " is a prime Number "<< endl; else cout<<a<<" is not a prime Number"; getch(); } bool prime(int z) { for(int i=2; i<z; i++) { if(z%i==0) return false; } return true; }

6 //Two-Dimensional Integer Array #include main() { int matrix [2][3]; // For taking integer inputs in a matrix // for (int m1=0 ; m1<2 ; m1++) { for (int m2=0 ; m2<3 ; m2++) { cout<<"Enter Integer :"; cin>>matrix [m1][m2]; } } cout<<endl; // For displaying elements of a matrix on a screen // for (int m1=0 ; m1<2 ; m1++) { for (int m2=0 ; m2<3 ; m2++) { cout<<"Your Entered Integer are :"; cout<<matrix [m1][m2]; cout<<endl; } getch(); }

7 //Removing element from array #include using namespace std; void removeAt(int [], int, int); int main() { const int len = 12; int ylist[len] = {4, 23, 65, 34, 82, 37, 12, 17, 24, 36, 82, 51}; int index = 1; removeAt(ylist, len, index); system("pause"); return 0; } void removeAt(int ylist[], int len, int index) { cout << "Please pick an array index from 0 to " << (len - 1) << ": "; cin >> index; for (int loc = 0; loc < len; loc++) if (ylist[loc] == ylist[index]) { cout << endl << "The array element that you are removing is " << ylist[loc] << endl; }

8 // counted for (int i = index; i < len; i++) { ylist[index] = ylist[index+1]; ylist[len-1] = 0; } cout << "Final Array Elements are: "; for (int x = 0; x < len; x++) { cout << ylist[x] << " "; } cout << endl; }

9 // largest number in array #include const int SIZE = 15; main() { // Puts some numbers in the array. int ara[SIZE]={5,2,7,8,36,4,2,86,11,43,22,12,45,6,585}; int high_val, ctr; high_val = ara[0]; // Initializes with first // array element. for (ctr=1; ctr<SIZE; ctr++) { // Stores current value if it is // the higher than the highest. if (ara[ctr] > high_val) { high_val = ara[ctr]; } } cout << "The highest number in the list is " << high_val << "\n"; system("pause"); return 0 ; }

10 // Finds the highest and the lowest value in the array. #include const int SIZE = 15; main() { int ara[SIZE]; int high_val, low_val, ctr; // Fills array with random numbers from 0 to 99. for (ctr=0; ctr<SIZE; ctr++) { ara[ctr] = rand() % 100; } // Prints the array to the screen. cout << "“Here are the “ << SIZE << “ random numbers:\n"; for (ctr=0; ctr<SIZE; ctr++) { cout << ara[ctr] << "\n"; } cout << "\n\n"; // Prints a blank line. high_val = ara[0]; // Initializes first element to // both high and low. low_val = ara[0]; for (ctr=1; ctr<SIZE; ctr++) { // Stores current value if it is // higher than the highest. if (ara[ctr] > high_val) { high_val = ara[ctr]; } if (ara[ctr] < low_val) { low_val = ara[ctr]; } } cout << "The highest number in the list is " <<high_val << "\n"; cout << "The lowest number in the list is " <<low_val << "\n"; system("pause"); return 0; }

11 // sort number in array #include using namespace std; main() { int hold; int array[5]; cout<<"Enter 5 numbers: "<<endl; for(int i=0; i<5; i++) { cin>>array[i]; } cout<<endl; cout<<"Orignally entered array by the user is: "<<endl; for(int j=0; j<5; j++) { cout<<array[j]; cout<<endl; } cout<<endl; for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { if(array[j]>array[j+1]) { hold=array[j]; array[j]=array[j+1]; array[j+1]=hold; } cout<<"Sorted Array is: "<<endl; for(int i=0; i<5; i++) { cout<<array[i]<<endl; } getch(); }

12 #include // Reverse Contents in Array #include void reverse(int [], int); void printarray(int [], int ); int main () { const int SIZE = 10; int arr [SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cout<<"Before reverse\n"; printarray(arr, SIZE); reverse(arr, SIZE); cout<<"After reverse\n"; printarray(arr, SIZE); getch(); return 0; } void printarray(int arr[], int count) { for(int i = 0; i < count; ++i) cout<<arr[i]<<' '; cout<<'\n'; } void reverse(int arr[], int count) { int temp; for (int i = 0; i < count/2; ++i) { temp = arr[i]; arr[i] = arr[count-i-1]; arr[count-i-1] = temp; }

13


Download ppt "Array & Matrix Selected topics. 1. It will compare two adjacent elements, if second element is smaller than the first then it will swap them, if we wanted."

Similar presentations


Ads by Google