Course Hero Logo

20BCE0340_DA1.pdf - CSE2012- Design and Analysis of...

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 1 - 8 out of 24 pages.

CSE2012- Design and Analysis of AlgorithmsSlot: L49+L50Assignment 1NameSoumodeep ChakrabortyReg. No20BCE0340
Q1)Design and implement an algorithm that will find the top andthe least scores of students from an online Quiz. Note: Thescores are stored in an array.Aim:To Find Min-Max Marks Using Divide and Conquerj]:
//Solve the subproblems and combine the solutionsD&CMaxMin(A : i; mid; gmax; gmin : integer)D&CMaxMin(A : mid+1; j; hmax; hmin : integer)fmax = max(gmax; hmax);fmin = min(gmin; hmin);}}
Code:#include<iostream>#include<climits>#include<string.h>using namespacestd;// Divide and conquer solution to find the minimum and maximum value in an arrayvoidfindMinAndMax(intarr[],intlow,inthigh,int &min,int &max){if(low == high){if(max < arr[low]){max = arr[low];}if(min > arr[high]){min = arr[high];}return;}if(high - low ==1){if(arr[low] < arr[high]){if(min > arr[low]){min = arr[low];}if(max < arr[high]){max = arr[high];}}else{if(min > arr[high]){min = arr[high];}if(max < arr[low])
{max = arr[low];}}return;}intmid = (low + high) /2;findMinAndMax(arr, low, mid, min, max);findMinAndMax(arr, mid +1, high, min, max);}intmain(){inti, n, a, b;cout <<"Enter number of students who are attempting the online quiz\n";cin >> n;intarr[n];string name[n];string regno[n];cout <<"Enter the quiz marks along with name and registration number now!(They are being stored in an array)\n";for(i =0; i < n; i++){cout <<"Enter quiz marks,name and registration number for student "<< i+1<<" :\t ";cin >> arr[i];cin >> name[i];cin >> regno[i];}intmax =INT_MIN, min =INT_MAX;findMinAndMax(arr,0, n -1, min, max);for(i =0; i < n; i++){if(arr[i] == min)a = i;if(arr[i] == max)b = i;}cout <<"\n";
cout <<"The minimum marks obtained by a student in the online quiz is "<<min <<" and details are\n "<< name[a] <<" \n "<< regno[a] << endl;cout <<"The maximum marks obtained by a student in the online quiz is "<<max <<" and details are\n "<< name[b] <<" \n "<< regno[b];return0;}I/O:Result:Ifwe sort the array: 55 66 77 88 99The Maximum marks is 99 Alex(20BCE0340) andthe Minimum marks is 55Tyson(20BCE0169)Hence the Algorithm is working correctly.
Q2)Design and implement an algorithm that will find the top andthe least scores of students from an online Quiz. Note: Thescores are stored in an array.Aim:2ndMin and 2ndMax Using Divide and ConquerAlgorithm:Algorithm Partition(A : list; first; last : integer){v = A[first]; i = first; j = last;do{do i = i+1 while(A[i] < v);do j = j1 while(A[j] > v);if(i < j) Swap(A[i];A[j]);}while(i < j)A[first] = A[j];A[j] = v;return j;}Algorithm Selection(A : list; n; k : integer){first = 1; last = n+1;while true{j = partition(A; first; last);// return kth smallestif(k == j) break;// search left sublistif(k < j) last = j1;// search right sublistelse first = j +1;}}
Code:#include<climits>#include<iostream>#include<string.h>using namespacestd;intpartition_small(intarr[],intl,intr);intpartition_large(intarr[],intl,intr);intkthSmallest(intarr[],int

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 24 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Spring
Professor
NoProfessor
Tags
Max, int arr, Divide and conquer algorithm, Karatsuba algorithm

Newly uploaded documents

Show More

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture