Upload
tharindutpd
View
272
Download
0
Embed Size (px)
Citation preview
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 1/26
LAB -01- Student Answer
Solution 01.
1. #include <stdio.h>
2. int main(){
3. int A[10]; /* Array declaration for the input */
4. int i,j,m,key;
5. for (m=0;m<10;m++) /* To read elements to the array */
6. {
7. printf("A[%d] element : ",m);
8. scanf("%d",&A[m]);
9. }10. for (j=1;j<10;j++)
11. {
12. key=A[j];
13. i = j-1;
14. while((i>0)&&(A[i]>key))
15. {
16. A[i+1]=A[i];
17. i-=1;
18. }
19. A[i+1]=key;
20. }
21. for (m=0;m<10;m++) /* To read elements to the array */
22. {
23. printf("A[%d] = %d\n ",m,A[m]);
24. }
25. }
Exercise 02
Solution 01
1. #include <stdio.h>
2. int main(){
3. int A[10]; /* Array declaration for the input */
4. int i,j,k,ThisSum,MaxSum;
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 2/26
5. MaxSum=0;
6. for (i=0;i<10;i++) /* To read elements to the array */
7. {
8. printf("A[%d] element : ",i);
9. scanf("%d",&A[i]);
10. }
11. for (j=0;j<10;j++)
12. {
13. ThisSum=0;
14. for (k=j;k<10;k++)
15. {
16. ThisSum += A[k];
17. if (ThisSum>MaxSum)
18. MaxSum=ThisSum;
19. }
20. }
21. printf("Maximun Subsequence Sum : %d\n",MaxSum);
22. }
Solution 02
1. #include <stdio.h>
2. int main(){
3. int A[10]; /* Array declaration for the input */
4. int i,j,k,ThisSum,MaxSum;
5. MaxSum=ThisSum=0;
6. for (i=0;i<10;i++) /* To read elements to the array */
7. {
8. printf("A[%d] element : ",i);
9. scanf("%d",&A[i]);
10. }
11. for (j=0;j<10;j++)
12. {
13. ThisSum += A[j];
14. if (ThisSum>MaxSum)
15. MaxSum=ThisSum;
16. else if (ThisSum < 0)
17. ThisSum = 0;18. }
19. printf("Maximun Subsequence Sum : %d\n",MaxSum);
20. }
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 3/26
Exercise 03
Solution 03
1. #include <stdio.h>
2. int main(){
3. int A[10]; /* Array declaration for the input */
4. int i,j,m,smallest,temp;
5. for (m=0;m<10;m++) /* To read elements to the array */
6. {
7. printf("A[%d] element : ",m);
8. scanf("%d",&A[m]);
9. }
10. for (j=0;j<9;j++)
11. {
12. smallest=j;
13. for (i=j+1;i<10;i++)
14. {
15. temp=0;
16. if (A[i]<A[smallest])
17. { // To swap the elements.
18. smallest=i;
19. temp=A[j];
20. A[j]=A[smallest];21. A[smallest]=temp;
22. smallest=j;
23. }
24. }
25. }
26. for (m=0;m<10;m++) /* To read elements to the array */
27. {
28. printf("A[%d]=%d\n",m,A[m]);
29. }
30. }
Exercise 04
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 4/26
Solution 04.
1. #include <stdio.h>2. int main(int argc,char *argv[]){
1. int i,A[10];
2. 3. if (argc==1||argc==2){
4. printf("Less arguments \n");
5. }
6. else if (argc>11){
7. printf("More arguments \n");
8.
}9. else {
1. for(i=0;i<argc-1;i++){
2. A[i]=atoi(argv[i+1]);
3. printf("A[%d]=%d\n",i,A[i]);
4. }
5. }
6. 7. }
Exercise 05.
Solution 05
1. #include<stdio.h>
2. long factorial(int n){
3. if (n==0)
4. {
5. return 1;
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 5/26
6. }
7. else
8. {
9. return (n*factorial(n-1));
10. }
11. }
12. int main(){
13. int n;
14. long result;
15. printf("Enter the number for factorial :");
16. scanf("%i",&n);
17. result=factorial(n);
18. printf("%i!= %i\n",n,result);
19. }
lab 2 student answersExercise 01. Write a program to read a set of numbers (between 10
to 20) from stdin and arrange them on an array.
Sort the numbers in ascending order with the Insertion
sorting algorithm. Calculate how many times does it execute the while of
the algorithm.
Solution 01.
#include <stdio.h>
int main(){
int A[10]; /* Array declaration for theinput */
int i,j,m,key;
for (m=0;m<10;m++) /* To read elements
to the array */
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 6/26
{
printf("A[%d] element : ",m);
scanf("%d",&A[m]);
} for (j=1;j<10;j++)
{
key=A[j];
i = j-1;
while((i>0)&&(A[i]>key))
{
A[i+1]=A[i];
i-=1;
}
A[i+1]=key;
}
for (m=0;m<10;m++) /* To read
elements to the array */ {
printf("A[%d] = %d\n
",m,A[m]);
}
}
MAXIMUM SUBSEQUENCE SUM PROBLEM: Given (possibly negative) integers a1 , a2 , . . . , an , find
the maximum value of .
(For convenience, the maximum subsequence sum is 0
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 7/26
if all the integers are negative.)
Example:
For input -2, 11, -4, 13, -5, -2, the answer is 20 (a2
through a4).Exercise 02.
Write an algorithm in psedocode to give the solution
for MAXIMUM SUBSEQUENCE SUM
PROBLEM. Then write a program to read set of
integers from stdin ,store them on an array and give
the maximum subsequence sum for the given input.
Solution 01
#include <stdio.h>
int main(){
int A[10]; /* Array declaration for the input */
int i,j,k,ThisSum,MaxSum; MaxSum=0;
for (i=0;i<10;i++) /* To read elements to the array
*/
{
printf("A[%d] element : ",i);
scanf("%d",&A[i]);
} for (j=0;j<10;j++)
{
ThisSum=0;
for (k=j;k<10;k++)
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 8/26
{
ThisSum += A[k];
if (ThisSum>MaxSum)
MaxSum=ThisSum; }
}
printf("Maximun Subsequence Sum :
%d\n",MaxSum);
}Solution 02
#include <stdio.h>
int main(){
int A[10]; /* Array declaration for the input */
int i,j,k,ThisSum,MaxSum;
MaxSum=ThisSum=0; for (i=0;i<10;i++) /* To read elements to the array */
{ printf("A[%d] element : ",i);
scanf("%d",&A[i]);
}
for (j=0;j<10;j++)
{
ThisSum += A[j];
if (ThisSum>MaxSum) MaxSum=ThisSum;
else if (ThisSum < 0) ThisSum = 0;
}
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 9/26
printf("Maximun Subsequence Sum :
%d\n",MaxSum);
}
Exercise 03
Consider sorting n numbers stored in array A by first
finding the smallest element of A and exchanging it
with the element in A[1]. Then find the second
smallest element of A, and exchange it with A[2].
Continue in this manner for the first n - 1 elements of
A. Write pseudocode for this algorithm, which is
known as selection sort.
Then write a program for the above algorithm to sort
set of numbers.
Solution 03 Psedocode:
SELECTION-SORT(A) n← length[ A]
for j← 1 to n − 1
do smallest ← j
for i← j + 1 to n
do if A[i ] < A[smallest ]
then smallest ← i
exchange A[ j ]↔ A[smallest ]Solution 03
#include <stdio.h>
int main(){
int A[10]; /* Array declaration for the input */
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 10/26
int i,j,m,smallest,temp;
for (m=0;m<10;m++) /* To read elements to the
array */
{ printf("A[%d] element : ",m);
scanf("%d",&A[m]);
}
for (j=0;j<9;j++)
{
smallest=j;
for (i=j+1;i<10;i++)
{
temp=0;
if (A[i]<A[smallest])
{ // To swap
the elements.
smallest=i; temp=A[j];
A[j]=A[smallest];
A[smallest]=temp;
smallest=j;
}
}
} for (m=0;m<10;m++) /* To read elements
to the array */
{
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 11/26
printf("A[%d]=%d\n",m,A[m]);
}
}
Exercise 04
Write a program to read a set of numbers as command
line arguments and then store them on an array.
Sort the above elements with selection sort algorithm.
Solution 04. #include <stdio.h>
int main(int argc,char *argv[]){
int i,A[10];
if (argc==1||argc==2){ printf("Less arguments \n");
} else if (argc>11){
printf("More arguments \n");
}
else {
for(i=0;i<argc-1;i++){
A[i]=atoi(argv[i+1]); printf("A[%d]=%d\n",i,A[i]);
} }
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 12/26
}
Exercise 05.
Write recursive and iterative algorithms to find thefactorial value for an integer.
Implement the above algorithm in c.
Solution 05
#include<stdio.h>
long factorial(int n){ if (n==0)
{
return 1;
}
else
{ return (n*factorial(n-1));
} }
int main(){
int n;
long result;
printf("Enter the number for factorial :");
scanf("%i",&n);
result=factorial(n); printf("%i!= %i\n",n,result);
}
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 13/26
Lab 4 Student Answers
Lab 04
Exercise 04
Write a program to read a set of numbers as command line arguments and then store them on
an array.
Sort the above elements with selection sort algorithm.
Solution 04.
1. #include <stdio.h>
2. int main(int argc,char *argv[]){
1. int i,A[10];
2.
3. if (argc==1||argc==2){4. printf("Less arguments \n");
5. }
6. else if (argc>11){
7. printf("More arguments \n");
8. }
9. else {
1. for(i=0;i<argc-1;i++){
2. A[i]=atoi(argv[i+1]);
3. printf("A[%d]=%d\n",i,A[i]);
4. }
5. }
6.
7. }
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 14/26
Exercise 05
Write recursive and iterative algorithms to find the factorial value for an integer.
Implement the above algorithm in c.
Solution 05
1. #include<stdio.h>
2. long factorial(int n){
3. if (n==0)
4. {
5. return 1;
6. }
7. else
8. {
9. return (n*factorial(n-1));
10. }
11. }
12. int main(){
13. int n;
14. long result;15. printf("Enter the number for factorial :");
16. scanf("%i",&n);
17. result=factorial(n);
18. printf("%i!= %i\n",n,result);
19. }
Exercise 05.
Write a program to read a set of numbers and store them on an array. Then using the
following psedocode for partition procedure for quick sort algorithm, divide the array into two
parts giving the partition point.
PARTITION( A, p, r )
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 15/26
1 x ← A[r ]
2 i ← p - 1
3 for j ← p to r - 1
4 do if A[ j ] ≤ x
5 then i ← i + 1
6 exchange A[i ] ↔ A[ j ]
7 exchange A[i + 1] ↔ A[r ]
8 return i + 1
Solution 05.
1. #include <stdio.h>
2. int main()
3. {4. int A[8],k,l;
5. void partition(int A[],int a,int b);
6. for(k=0;k<8;k++){
7. printf("A[%d =",k);
8. scanf("%d",&A[k]);}
9. partition(A,0,7);
10. for(l=0;l<8;l++){
11. printf("A[%d]= %d\n",l,A[l]); }
12. }
13. void partition(int A[],int a,int b)
14. { int x,i,j,temp1,temp2;15. 16. x=A[b];
17. i=a-1;
18. for(j=a;j<7;j++)
19. {
20. if (A[j]<=x)
21. {
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 16/26
22. i+=1;
23. temp1=A[i];
24. A[i]=A[j];
25. A[j]=temp1;
26. }
27. }
28. temp2=A[7];
29. A[7]=A[i+1];
30. A[i+1]=temp2;
31. printf("Partition point is %d\n",i+1);
32. }
Lab 5 Student Answers
Lab 05
DYNAMIC MEMORY ALLOCATION, MERGER SORT ALGORITHM
Exercise 06.
Write a program to read a set of numbers and store them on an array. Then using the
following psedocode for partition procedure for quick sort algorithm, divide the array into two
parts giving the partition point.
PARTITION( A, p, r )
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 17/26
1 x ← A[r ]
2 i ← p - 1
3 for j ← p to r - 1
4 do if A[ j ] ≤ x
5 then i ← i + 1
6 exchange A[i ] ↔ A[ j ]
7 exchange A[i + 1] ↔ A[r ]
8 return i + 1
Solution 06.
1. #include <stdio.h>
2. int main()
3. {
4. int A[8],k,l;
5. void partition(int A[],int a,int b);
6. for(k=0;k<8;k++){
7. printf("A[%d =",k);
8. scanf("%d",&A[k]);}
9. partition(A,0,7);
10. for(l=0;l<8;l++){
11. printf("A[%d]= %d\n",l,A[l]); }
12. }
13. void partition(int A[],int a,int b)
14. { int x,i,j,temp1,temp2;
15. 16. x=A[b];17. i=a-1;
18. for(j=a;j<7;j++)
19. {
20. if (A[j]<=x)
21. {
22. i+=1;
23. temp1=A[i];
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 18/26
24. A[i]=A[j];
25. A[j]=temp1;
26. }
27. }
28. temp2=A[7];
29. A[7]=A[i+1];
30. A[i+1]=temp2;
31. printf("Partition point is %d\n",i+1);
32. }
Exercise 07.
Modify the previous program (Exercise 06) with quick sort algorithm to sort the elements of
the array.
Procedure QUICKSORT (A, p,r )
1 if p < r
2 then q ← PARTITION(A, p,r )
3 QUICKSORT (A, p,q-1)
4 QUICKSORT (A,q+1,r )
Solution 07.
1. #include <stdio.h>
1. int main()
2. {
3. int A[8],k,l,q;
4. int partition(int A[],int a,int b);
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 19/26
5. void qsort(int A[],int p,int r);
1. for(k=0;k<8;k++)
2. {
3. printf("A[%d =",k);
4. scanf("%d",&A[k]);
5. }
6. 7. qsort(A,0,7);
8. for(l=0;l<8;l++)
9. {
10. printf("A[%d]= %d\n",l,A[l]);
11. }
1. }
1. void qsort(int A[],int p,int r)
2. {
3. int q;
1. if (p>=r)
2. return;
3. else
4. q=partition(A,p,r);5. qsort(A,p,q-1);
6. qsort(A,q+1,r);
7. 8. }
1. int partition(int A[],int a,int b)
2. {
3. int x,i,j,temp1,temp2;
4. x=A[b];
5.
i=a-1;6. for(j=a;j<b;j++)
7. {
8. if (A[j]<=x)
9. {
10. i+=1;
11. if (i>b||i>j)
12. break;
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 20/26
13. temp1=A[i];
14. A[i]=A[j];
15. A[j]=temp1;
16. }
17. }
18. temp2=A[b];
19. A[b]=A[i+1];
20. A[i+1]=temp2;
21. printf("Partition point is %d\n",i+1);
22. return i+1;
23. }
Exercise 08.
Write a program to read a set of numbers and store them on an array. Then using the
following psedocode for merge procedure for merge sort algorithm, divide the array into two
parts giving the partition point.
MERGE( A, p, q, r )
1 n1 ← q - p + 1
2 n2 ← r - q
3 create arrays L[1.. n1 + 1] and R[1.. n2 + 1]
4 for i ← 1 to n1
5 do L[i ] ← A[ p + i - 1]
6 for j ← 1 to n2
7 do R[ j ] ← A[q + j ]
8 L[n1 + 1] ← ∞
9 R[n2 + 1] ← ∞
10 i ← 1
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 21/26
11 j ← 1
12 for k ← p to r
13 do if L[i ] ≤ R[ j ]
14 then A[k ] ← L[i ]
15 i ← i + 1
16 else A[k ] ← R[ j ]
17 j ← j + 1
Lab 6 – Student Answers
#include <stdio.h>
#include <stdlib.h>
/**
* merge()
* Merge two sorted arrays, A with a integers and
* B with b integers, into a sorted array C.
**/
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 22/26
void
merge (int *A, int a, int *B, int b, int *C)
{
int i,j,k;
i = 0;
j = 0;
k = 0;
while (i < a && j < b) {
if (A[i] <= B[j]) {
/* copy A[i] to C[k] and move the pointer i and k forward */
C[k] = A[i];
i++;
k++;
}
else {
/* copy B[j] to C[k] and move the pointer j and k forward */
C[k] = B[j];
j++;
k++;
}
}
/* move the remaining elements in A into C */
while (i < a) {
C[k]= A[i];
i++;
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 23/26
k++;
}
/* move the remaining elements in B into C */
while (j < b) {
C[k]= B[j];
j++;
k++;
}
}
/**
* merge_sort()
* Sort array A with n integers, using merge-sort algorithm.
**/
void
merge_sort(int *A, int n)
{
int i;
int *A1, *A2;
int n1, n2;
if (n < 2)
return; /* the array is sorted when n=1.*/
/* divide A into two array A1 and A2 */
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 24/26
n1 = n / 2; /* the number of elements in A1 */
n2 = n - n1; /* the number of elements in A2 */
A1 = (int*)malloc(sizeof(int) * n1);
A2 = (int*)malloc(sizeof(int) * n2);
/* move the first n/2 elements to A1 */
for (i =0; i < n1; i++) {
A1[i] = A[i];
}
/* move the rest to A2 */
for (i = 0; i < n2; i++) {
A2[i] = A[i+n1];
}
/* recursive call */
merge_sort(A1, n1);
merge_sort(A2, n2);
/* conquer */
merge(A1, n1, A2, n2, A);
free(A1);
free(A2);
}
/**
* main()
8/2/2019 DAA Lab Answers
http://slidepdf.com/reader/full/daa-lab-answers 25/26
* The main function for input and output
**/
void
main(int argv, char** args)
{
int i, n;
int *A;
char c;
printf("Please input the number of elements: " );
scanf("%d", &n);
A = (int *)malloc(sizeof(int) * n);
printf("Please input %d integers: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &(A[i]));
}
/* print the original array */
printf("************** Result **************\n");
printf("The input array is: ");
for (i = 0; i < n; i++) {
printf("%d ", A[i]);
}
printf("\n");