






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Type array Name [array Size];
Double balance [10];
Double balance [5] = {1000.0, 2.0, 3.4, 17.0, and 50.0};
Double balance [ ] = {1000.0, 2.0, 3.4, 17.0, 50.0};
#include
cout<<"\n------------------------------------------------------\n"; // Loop to store largest number to arr[0] for(i = 1;i < n; ++i) { // Change < to > if you want to find the smallest element if(arr[0][0] < arr[i][j]) arr[0][0] = arr[i][j];} cout << "\n\n\t\tLargest element = " << arr[0][0]<<"\n\n"; cout<<"\n------------------------------------------------------\n"; return 0;}
#include
cout<<"\n\tEnter total number of elements : "; cin>>n; cout<<"-----------------------------------------------\n"; cout<<"\nEnter elements of Array\n\n"; for(i=0;i<n;++i) cin>>a[i]; cout<<"\n-----------------------------------------------\n"; cout<<"\n\tEnter element to delete: "; cin>>x; cout<<"\n-----------------------------------------------\n"; for(i=0,j=0;i<n;++i) {if(a[i]!=x) b[j++]=a[i];} if(j==n) {cout<<"\n\tSoory !!! Element is not in the Array"; exit(0);}else { cout<<"\n\tNew Array is : "; for(i=0;i<j;i++) cout<<b[i]<<" ";} return 0;}
cin>>insert; cout<<"------------------------------------------------------\n"; cout<<"At which position (Enter index number) : "; cin>>pos; cout<<"------------------------------------------------------"; // now create a space at the required position for(i=size; i>pos; i--) {arr[i]=arr[i-1];} arr[pos]=insert; cout<<"\n\t Element inserted successfully...!!\n"; cout<<"------------------------------------------------------\n"; cout<<"Now the new array is : "; for(i=0; i<size+1; i++) {cout<<arr[i]<<" "; } cout<<"\n------------------------------------------------------\n";} else {cout<<"\n------------------------------------------------------\n"; cout<<"\n\n\t\tNo Space for new element max limit 10\n"; } }
Step 1 - Read the search element from the user. Step 2 - Compare the search element with the first element in the list. Step 3 - If both are matched, then display "Given element is found!!!" and terminate the function Step 4 - If both are not matched, then compare search element with the next element in the list. Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list. Step 6 - If last element in the list also doesn't match, then display "Element is not found!!!" and terminate the function
#include
21.87 second required to search an element in linear search
Step 1 - Read the search element from the user. Step 2 - Find the middle element in the sorted list. Step 3 - Compare the search element with the middle element in the sorted list. Step 4 - If both are matched, then display "Given element is found!!!" and terminate the function. Step 5 - If both are not matched, then check whether the search element is smaller or larger than the middle element. Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the left sublist of the middle element. Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the right sublist of the middle element. Step 8 - Repeat the same process until we find the search element in the list or until sublist contains only one element. Step 9 - If that element also doesn't match with the search element, then display "Element is not found in the list!!!" and terminate the function.
#include
int count, i, arr[30], num, first, last, middle; cout<<"\n\tHow many elements would you like to enter :"; cin>>count; cout<<"\n------------------------------------------------------\n"; for (i=0; i<count; i++) { cout<<"\tEnter number "<<(i+1)<<": "; cin>>arr[i];} cout<<"\n------------------------------------------------------\n"; cout<<"\tEnter the number that you want to search: "; cin>>num; cout<<"\n------------------------------------------------------\n"; first = 0; last = count-1; middle = (first+last)/2; while (first <= last) { if(arr[middle] < num) {first = middle + 1; } else if(arr[middle] == num) {cout<<"\n\t"<<num<<" Found in the array at the location "<<middle+1<<"\n"; break; } else { last = middle - 1; } middle = (first + last)/2; } if(first > last) { cout<<"\n\t"<<num<<" Not found in the array "; } }