Professional Documents
Culture Documents
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;
5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. }
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++) { ThisSum += A[k]; if (ThisSum>MaxSum) MaxSum=ThisSum; } } printf("Maximun Subsequence Sum : %d\n",MaxSum);
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. }
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
Solution 04.
1. 2. 3. 4. 5. 6. 7. 8. 9.
int i,A[10]; if (argc==1||argc==2){ printf("Less arguments \n"); } else if (argc>11){ printf("More arguments \n"); } else {
1. 2. 3. 4. 5. 6. 7. }
Exercise 05.
Solution 05
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. }
{ 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 .
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++)
} 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; }
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 */
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 */ {
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]); } }
Exercise 05. Write recursive and iterative algorithms to find the factorial 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); }
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. 2. 3. 4. 5. 6. 7. 8. 9.
int i,A[10]; if (argc==1||argc==2){ printf("Less arguments \n"); } else if (argc>11){ printf("More arguments \n"); } else {
1. 2. 3. 4. 5. 6. 7. }
Exercise 05
Write recursive and iterative algorithms to find the factorial value for an integer. Implement the above algorithm in c.
Solution 05
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19.
#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); }
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)
1 x A[r] 2ip-1 3 4 5 6 7 8 for j p to r - 1 do if A[j] x then i i + 1 exchange A[i] A[j] exchange A[i + 1] A[r] 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. {
22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. }
i+=1; temp1=A[i]; A[i]=A[j]; A[j]=temp1; } } temp2=A[7]; A[7]=A[i+1]; A[i+1]=temp2; printf("Partition point is %d\n",i+1);
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)
1 x A[r] 2ip-1 3 4 5 6 7 8 for j p to r - 1 do if A[j] x then i i + 1 exchange A[i] A[j] exchange A[i + 1] A[r] 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];
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 3 4 then q PARTITION(A,p,r) QUICKSORT (A,p,q-1) QUICKSORT (A,q+1,r)
Solution 07.
1. #include <stdio.h>
5.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
1. }
1. void 2. 3.
1. 2. 3. 4. 5. 6. 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;
13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. }
temp1=A[i]; A[i]=A[j]; A[j]=temp1; } } temp2=A[b]; A[b]=A[i+1]; A[i+1]=temp2; printf("Partition point is %d\n",i+1); return i+1;
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 2 3 4 5 6 7 8 9 10 n1 q - p + 1 n2 r - q create arrays L[1.. n1 + 1] and R[1.. n2 + 1] for i 1 to n1 do L[i] A[p + i - 1] for j 1 to n2 do R[j] A[q + j] L[n1 + 1] R[n2 + 1] i1
11 12 13 14 15 16 17
j1 for k p to r do if L[i] R[j] then A[k] L[i] ii+1 else A[k] R[j] jj+1
/** * merge() * Merge two sorted arrays, A with a integers and * B with b integers, into a sorted array C. **/
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++;
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;
n1 = n / 2; n2 = n - n1;
/* 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);
/** * main()
* **/ void
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");
/* print the sorted array */ printf("The sorted array is: "); for (i = 0; i < n; i++) { printf("%d ", A[i]); } printf("\n"); free(A);