Arrays_Programs

Printfenter the number to search n scanfdsearch for i

• Homework Help
• 6

This preview shows page 2 - 6 out of 6 pages.

printf("Enter the number to search \n "); scanf("%d",&search); for ( i = 0 ; i < number ; i++ ) { if ( array[i] == search ) { printf("%d is present at location %d. \n ", search, i+1); break ; } } if ( i== number ) printf("%d is not present in array. \n ", search); return 0; }

Subscribe to view the full document.

Prof. Nancy Victor Selection sort (To sort the elements in an array) #include<stdio.h> int main() { int n,i,j,temp,a[20]; printf("Enter total elements: "); scanf("%d",&n); printf("Enter the elements: "); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("The sorted array is: "); for(i=0;i<n;i++) printf(" %d",a[i]); return 0; }
Prof. Nancy Victor Binary search #include <stdio.h> int main() { int i, first, last, mid, n, search, array[100]; printf("Enter number of elements \n "); scanf("%d",&n); printf("Enter the numbers” ); for ( i = 0 ; i < n ; i++ ) scanf("%d",&array[i]); printf("Enter value to find \n "); scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2; while( first <= last ) { if ( array[mid] < search ) first = mid + 1; else if ( array[mid] == search ) { printf("%d found at location %d. \n ", search, mid+1); break ; } else last = mid - 1; mid = (first + last)/2; } if ( first > last ) printf("Not found! %d is not present in the list. \n ", search); return 0; }

Subscribe to view the full document.

Prof. Nancy Victor Reverse an array #include <stdio.h> int main() { int n, i, d, a[100], b[100]; printf("Enter the number of elements in array \n "); scanf("%d", &n); printf("Enter the array elements \n "); for (i= 0; i < n ; i++) scanf("%d", &a[i]) for (i = n - 1, d = 0; i >= 0; i--, d++) b[d] = a[i]; printf("Reversed array is \n "); for (i= 0; i < n; i++) printf("%d \n ", a[i]); return 0; } Reverse an array by swapping (without using additional memory) #include <stdio.h> main() { int array[10], n, c, t, end; printf(“Enter the number of elements”); scanf("%d", &n); end = n - 1; printf(“Enter the elements”); for (c = 0; c < n; c++) { scanf("%d", &array[c]); } for (c = 0; c < n/2; c++) { t = array[c]; array[c]= array[end]; array[end] = t; end--; } printf("Reversed array elements are: \n "); for (c = 0; c < n; c++) { printf("%d \n ", array[c]); }
Prof. Nancy Victor Removing duplicates in an array #include<stdio.h> int main() { int n,i,j,k,temp,a[20],flag=0; printf("Enter total elements: "); scanf("%d",&n); printf("Enter the elements: "); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(a[i]==a[j]) { for(k=j;k<n-1;k++) a[k]=a[k+1]; flag=1; n--; } } } if(flag==0) printf(“No duplicates found”); else { printf("The new array is: "); for(i=0;i<n;i++) printf(" %d",a[i]); } return 0; }
• Spring '16
• dncj ncnd
• scanf, printf, The Elements, Prof. Nancy Victor

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern