0% found this document useful (0 votes)
41 views

Faculty: Department: Course: Course Code: Assignment Number: Year: (Part 2) Semester 1

This document contains solutions to programming questions on arrays and sorting algorithms. It includes code for reversing elements in a 2D array, finding the biggest and smallest elements in an array, and implementations of bubble sort, selection sort, quick sort, insertion sort, shell sort, and linear search. Sample code is provided to sort an array using bubble sort and shell sort.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Faculty: Department: Course: Course Code: Assignment Number: Year: (Part 2) Semester 1

This document contains solutions to programming questions on arrays and sorting algorithms. It includes code for reversing elements in a 2D array, finding the biggest and smallest elements in an array, and implementations of bubble sort, selection sort, quick sort, insertion sort, shell sort, and linear search. Sample code is provided to sort an array using bubble sort and shell sort.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

FACULTY: DEPARTMENT: COURSE: COURSE CODE: ASSIGNMENT NUMBER: YEAR: [PART 2] SEMESTER 1

APPLIED SCIENCES COMPUTER SCIENCE computer architecture SCS2102 1

Question: Write a program to enter numbers on a double dimension array, reverse the elements and display them Write a program to display the biggest an the smallest element in an array

Sorting programs: Bubble sort Selection sort Quick sort Insertion sort Shell sort Linear search

Solution Program to display elements in reverse from an array #include <conio.h> #include <iostream>

using namespace std;

int main(){ int i,j, arr[2][4]; cout<<"please enter integers"<<endl ; for( i=0;i<2;i++){ for( j=0;j<4;j++){ cin>>arr[i][j] ;}} cout<<"the reverse is\n\n"<<endl;

for( i=1;i>=0;i--){ for( j=3;j>=0;j--){

cout<<arr[i][j] <<endl; }} getch(); return 0; } Program to display the biggest and the smallest of the two elements in an array

#include <conio.h> #include <iostream.h> voidshellsort(int a[],int n){ inti,j,k,m,mid ; for(m=n/2;m>0;m/=2){ for(j=m;j<n;j++){ for(i=j-m; i>=0;i-=m){ if (a[i+m]> a[i]) break; else { mid = a[i]; a[i]= a[i+m]; a[i+m]= mid; } } }

} }

int main(){ int a[10],i,n; clrscr(); cout<< "enter the number of elements" <<endl; cin>> n; for (i=0; i<n;i++) { cout<<endl<<"element "<< i+1<< ":\t"; cin>> a[i] ; } shellsort(a,n); cout<< "after sorting the array is: " ; for (i=0; i<n; i++) cout<< a[i] << " ";

cout<<endl<<endl<< "the smallest element is:" << a[0]; cout<<endl<< "the biggest element is :" << a[n-1]; getch(); return 0; }

a) bubble sort #include <iostream.h>

#include <conio.h>

int main (){

intarr[5],i,j,k,temp; clrscr();

cout<< "please enter the values for the array" <<endl; for (k=0; k<5;k++) cin>>arr[k];

for (i=0; i<=3; i++){ for (j=0; j<=3-i;j++){ if(arr[j]>arr[j+1]){ temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } cout<<endl<< "after sorting the array is"<<endl; for (k=0;k<5;k++) cout<<arr[k]<<"\t"; getch(); return 0; }

b) Shell sort #include <conio.h> #include <iostream.h> voidshellsort(int a[],int n){ inti,j,k,m,mid ; for(m=n/2;m>0;m/=2){ for(j=m;j<n;j++){ for(i=j-m; i>=0;i-=m){ if (a[i+m]> a[i]) break; else { mid = a[i]; a[i]= a[i+m]; a[i+m]= mid; } } } } }

int main(){ int a[10],i,n;

clrscr(); cout<< "enter the number of elements" <<endl; cin>> n; for (i=0; i<n;i++) { cout<<endl<<"element "<< i+1<< ":\t"; cin>> a[i] ; } shellsort(a,n); cout<< "after sorting the array is: " ; for (i=0; i<n; i++) cout<< a[i] << " "; getch(); return 0; }

You might also like