BCSL-045 Algorithm Design Lab BCA (IV)/045/Assignment/14-15
Transcription
BCSL-045 Algorithm Design Lab BCA (IV)/045/Assignment/14-15
1 www.ignouhelp.com BCSL-045 Algorithm Design Lab BCA (IV)/045/Assignment/14-15 Q.1 (i) A Palindrome is a string that reads the same forward and backward. Write an algorithm for determining whether a string of n characters is palindrome. Solution:C program to check whether is a palindrome and also find the length of string # include <stdio.h> #include <string.h> Void main () { Char string [25] , reverse_string [25] = { ‘\0’ }; Int I, length = 0, flag = 0; Printf(“Enter a string \n”); Gets(string); For( i = 0; string [i] ! = ‘\0’ ; i++) { Length++; } Printf (“the length of the string ‘%s’ = %d\n” , string, length); for ( I = Length – 1; I >=0 ; I - - ) { Reverse _ string [ length – i – I ] = string [i]; } AIS COMPUTER EDUCATION C-1075, IIND FLOOR, JAHANGIR PURI, DELHI-110033 M: 9899329565 2 www.ignouhelp.com For (flag = 1, i= 0; i< length; i++) { If (reverse _ string [i] != string [i] ) Flag = 0; } If (flag==1) Printf (“%s is a palindrome \n”, string) ; else Printf (“%s is a not palindrome \n”, string) ; } Output: Enter a string How are you The length of the string ‘how are you’ = 12 How are you is not a palindrome -------------------------------------------------------------------------------------------Enter a string Madam The length of string ‘madam’ = 5 Madam is a palindrome Algorithm:Step 1 : Start Step 2 : Declare variables char_string [25], reverse_string [25] Int i , length = 0 , flag = 0 ; Step 3 : read values string. Step 4 : for (I = 0 ; string [i] != ‘\0’ ; i++) length++ Step 5 : display ‘the length of string =’ , string , length AIS COMPUTER EDUCATION C-1075, IIND FLOOR, JAHANGIR PURI, DELHI-110033 M: 9899329565 3 www.ignouhelp.com Step 6 : for (I = length – 1; i >=0 ; I -- ) do reverse _ string [length – I - 1] = string [i] Step 7 : for (flag = 1, i= 0 ; i< length ; i++) if reverse _ string [i] ! = string [i] do flag = 0 If flag ==1 display string is palindrome. else display string is not palindrome. Step 8 : Stop. (ii) Find the largest number in an array and count a number of comparison operations as well as running time complexity of each statement and total complexity of the problem. Solution:#include <stdio.h> int main() { Int I, n, count = 0; Float arr [100] ; Printf(“Enter total number of elements (1 to 100) : ”) ; Scanf (“%d”, &n); Printf ( i=0 ; i<n ; ++i ) { Printf (“Enter Number %d:” , i+1); scanf (“%f”, &arr [i] ) ; } For ( i=1; i<n;++i) { If (arr [0] < arr [i] ) { Count++; } Arr [0] = arr [i] ; } Printf(“Largest element = %. 2f \n” , arr [0]); printf (“the number of comparison %d”, count); AIS COMPUTER EDUCATION C-1075, IIND FLOOR, JAHANGIR PURI, DELHI-110033 M: 9899329565 4 www.ignouhelp.com Return 0 ; Getch(); } Q 1(iii) Write a programme which interchanges the values the variable using only assignment operations. What is the minimum number of assignments operations required? Ans. #include <stdio.h> int main() { int x, y, temp; printf("Enter the value of x and y\n"); scanf("%d%d", &x, &y); printf("Before Swapping\nx = %d\ny = %d\n",x,y); temp = x; x = y; y = temp; printf("After Swapping\nx = %d\ny = %d\n",x,y); return 0; } Minimum number of assignments are three in this case of swapping the values. Q. (iv) Write a programme to do bubblesort to sort an array X={10,5,7,8,3,6,4,14} showing the list obtained at each step. Also count no. of comparison operations in the programme. Ans. #include <stdio.h> int main() { int array[100], n, c, d, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); AIS COMPUTER EDUCATION C-1075, IIND FLOOR, JAHANGIR PURI, DELHI-110033 M: 9899329565 5 www.ignouhelp.com for (c = 0 ; c < { for (d = 0 ; d { if (array[d] { swap array[d] array[d+1] } } } ( n - 1 ); c++) < n - c - 1; d++) > array[d+1]) /* For decreasing order use < */ = array[d]; = array[d+1]; = swap; printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); return 0; } Q 1(v) Write a programme to find multiplication of matrices of order 4*4 and find the total number of comparison, total no. of assignments, total number of multiplications and total number of addition operations required in the programme. Ans. #include <stdio.h> int main() { int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], multiply[10][10]; printf("Enter the number of rows and columns of first matrix\n"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &first[c][d]); printf("Enter the number of rows and columns of second matrix\n"); scanf("%d%d", &p, &q); if ( n != p ) printf("Matrices with entered orders can't be multiplied with each other.\n"); AIS COMPUTER EDUCATION C-1075, IIND FLOOR, JAHANGIR PURI, DELHI-110033 M: 9899329565 6 www.ignouhelp.com else { printf("Enter the elements of second matrix\n"); for ( c = 0 ; c < p ; c++ ) for ( d = 0 ; d < q ; d++ ) scanf("%d", &second[c][d]); for ( c = { for ( d { for ( { sum } 0 ; c < m ; c++ ) = 0 ; d < q ; d++ ) k = 0 ; k < p ; k++ ) = sum + first[c][k]*second[k][d]; multiply[c][d] = sum; sum = 0; } } printf("Product of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) printf("%d\t", multiply[c][d]); printf("\n"); } } return 0; } AIS COMPUTER EDUCATION C-1075, IIND FLOOR, JAHANGIR PURI, DELHI-110033 M: 9899329565