UNIT Arrays and Strings Part A (mark questions). What is an array? (or) Define array. An array is a collection of same data type elements All elements are stored in continuous locations Array index always start from 0 It is represented using [ ] square brackets. Write a program to find sum of array elements. int i, n,sum, a[0]; printf( enter how many numbers ); printf( enter numbers one by one: ); for( i=0; i<n; i++) sum = sum + a[i]; printf( answer = %d, sum); Enter how many numbers Enter numbers one by one 0 0 0 0 0 0. List out any six string handling functions. Strcat() to concatenate a string (join) Strupr() to change to Upper case (Capital letters) Strlwr() to change to Lower case ( Small letters) Strlen() to find length of string Strrev() to reverse a string Strcmp() to compare two strings Strcpy() to copy one string to another string. What is a two-dimensional array? It is an array in which rows and columns are the two dimensions Two square brackets are used for rows and columns More number of elements can be stored Ex:- Int a[][] = 0,0, 0,0 ;- Rows / A[0] A[] Columns A[0] A[0][0] = 0 A[0][] = 0 A[] A[][0] = 0 A[][] = 0. What is a string? A string is a one dimensional array to store characters It is represented inside double quotes It is also called as sequence of characters Array is used to store the string. Ex:- char a[0] = Mechanical ; M e c h a n i c a l A[0] A[] A[] A[] A[] A[] A[] A[] A[] A[]
Part B ( and mark questions). Write a C program to arrange numbers in ascending order and descending order. Ascending order:- int i, j, temp; int a[0] =,,,,,,,,, 0 ; for( i=0; i<0; i++) for(j=i+; j<0; j++) if ( a[i] > a[j] ) temp = a[i]; a[i] = a[j]; a[j] = temp; printf( answer= ); for( i=0; i<0; i++) printf( %d, a[i]); Descending order:- int i, j, temp; int a[0] =,,,,,,,,, 0 ; for( i=0; i<0; i++) for(j=i+; j<0; j++) if ( a[i] < a[j] ) temp = a[i]; a[i] = a[j]; a[j] = temp; printf( answer= ); for( i=0; i<0; i++) printf( %d, a[i]); 0 0. Write a C program for addition of two matrices int i, j; int a[][], b[][], c[][]; printf( enter matrix a in x ); for( i=0; i<; i++) for(j=0; j<; j++) printf( enter matrix b in x ); for( i=0; i<; i++) for(j=0; j<; j++) scanf( %d,&b[i][j]); for( i=0; i<; i++) for(j=0; j<; j++) c[i][j] = a[i][j] + b[i][j]; printf( answer = ); for( i=0; i<; i++) for(j=0; j<; j++) printf( %d,c[i][j]); A in X B in X 0
. Write a C program for subtraction of two matrices int i, j; int a[][], b[][], c[][]; printf( enter matrix a in x ); for( i=0; i<; i++) for(j=0; j<; j++) printf( enter matrix b in x ); for( i=0; i<; i++) for(j=0; j<; j++) scanf( %d,&b[i][j]); for( i=0; i<; i++) for(j=0; j<; j++) c[i][j] = a[i][j] - b[i][j]; printf( answer = ); for( i=0; i<; i++) for(j=0; j<; j++) printf( %d,c[i][j]); A in X 0 B in X.Write a program for multiplication of two matrices int i, j, k; int a[][], b[][], c[][]; printf( enter matrix a in x ); for( i=0; i<; i++) for(j=0; j<; j++) printf( enter matrix b in x ); for( i=0; i<; i++) for(j=0; j<; j++) for(k=0; k<=; k++) c[i][j] = c[i][j] + a[i][j] * b[i][j]; for( i=0; i<; i++) for(j=0; j<; j++) c[i][j] = a[i][j] - b[i][j]; printf( answer = ); for( i=0; i<; i++) for(j=0; j<; j++) printf( %d,c[i][j]); A in X B in X 0 0 0
. Write a C program for transpose of a matrix int i, j; int a[][]; printf( enter matrix A in x ); for( i=0; i<; i++) for(j=0; j<; j++) printf( answer = ); for( j=0; j<; i++) for(i=0; i<; j++) printf( %d,a[i][j]); in X. Write a program to find sum of diagonal elements of a matrix int i, j, sum; int a[][]; printf( enter matrix in x ); for( i=0; i<; i++) for(j=0; j<; j++) for( i=0; i<; i++) for(j=0; j<; j++) if(i==j) sum = sum + a[i][j]; printf( answer = %d, sum); in X. Write a C program to find determinant of a matrix int i, j, det; int a[][]; printf( enter matrix in x ); for( i=0; i<; i++) for(j=0; j<; j++) det = a[0][0] * a[][] - a[][0] * a[0][]; printf( answer = %d, det); Enter x matrix
.Explain some string handling functions with example C programs. Strcat() to concatenate two strings (join) mechengg char s[0] = mech ; char s[0] = engg ; strcat(s,s); printf( %s,s); Strlen() to find length of a string 0 int a; char s[0]= mechanical ; a=strlen(a); printf( %d,a); Strcpy() to copy one string to another string mechanical char s[0]= mechanical ; char s[0]= ; strcpy(s,s); printf( %s,s); strrev() to reverse a string lacinahcem char s[0]= mechanical ; printf( %s,strrev(s) ); strupr() to change letters to upper case (Captials) MECHANICAL char s[0]= mechanical ; printf( %s,strupr(s) ); strlwr() to change letters to lower case (small letter) mechanical char s[0]= MECHANICAL ; printf( %s,strlwr(s) ); strset() to change letters to some other letters zzzzzzzzzz char s[0]= mechanical ; strset(s,z); printf( %s,s );. Explain binary search with an example. It follows divide and conquer technique. Key means, search element. Key is compared with middle element. If key > middle, ignore first half of elements If key < middle, ignore second half of elements This process is repeated recursively. Until the element is found. Input: sorted array of elements Output: element found (or) not found. Sorted array Key Steps:- Find middle element of array Compare it with key Binary search Element Found Element not found If middle < key, repeat steps and for st half of array If middle > key, repeat steps and for nd half of array if middle == key, element is found.
Sorted array: 0, 0, 0, 0, 0, 0 Key : 0 Here, left = 0, right = 0, mid = (0 + 0) / = Therefore, mid = 0 or 0. We take mid = 0 0 = 0? (false). 0 < 0? (true) So, take second half of elements: 0, 0, 0 Sorted array: 0, 0, 0 Key : 0 Here, left = 0, right = 0, mid = (0 + 0) / = 0 0 = 0? (true) Therefore, element found at th position. Program:- int a[0], i, n, key, c, left, right, mid; printf( enter the sorted array elements ); printf( enter the key ); scanf( %d,&key); right = n-; while(left<=right) mid = (left+right)/; if(key==a[mid] c=; else if(key < a[mid]) right = mid -; else left = mid +; if ( c ==0 ) printf( element not found ); else printf( element found ); Enter number of elements Enter sorted array 0 0 0 0 0 0 Enter key 0 Element found Advantages Efficient and faster Uses divide and conquer Suitable for large elements Disadvantages Not suitable for unsorted array Not suitable for dynamically changing data 0. Explain linear search with an example. The process of finding the key from the collection. Compare all the elements one by one. No technique is used Input: unsorted array of elements Output: element found (or) not found unsorted array key linear search element found element not found Steps:- Read the first element Compare with key If true, element is found. If not true, compare next element. Continue step,, and till the last element. Unsorted array:,,,,,, 0 Key : 0? (false). Go to next 0? (false). Go to next 0? (false). Go to next 0? (false). Go to next 0? (true) element found Program:- int a[0], key, c, i; printf( enter the sorted array elements ); printf( enter the key ); scanf( %d,&key);
if(a[i] = = key) c = ; break; if( c ==0) printf( element not found ); else printf( element found ); Enter number of elements Enter sorted array 0 Enter key Element found Advantages Easy and faster Array can be unsorted Suitable for small number of elements Disadvantages Not suitable for large number of elements Slower method Very basic technique. Explain bubble sort with an example It the oldest and easiest sorting. In this sorting, the elements are arranged in some technique. Take first element, compare it with next element. If first > second, swap the elements. Then, again compare st element with next element Continue this process until, each element is compared with every other element. Unsorted array Bubble sort Sorted array Program:- int n, temp, i, j, a[0]; printf( enter the elements ); for(i=0; i<n, i++) for(i=n-; i>0; i--) for(j=0; j<=i; j++) if(a[j]>a[j+]) temp = a[j]; a[j] = a[j+]; a[j+]= temp; printf( answer= ); printf( %d,a[i]); Enter number of elements Enter elements Advantages Easy technique simple Suitable for small number of elements Disadvantages Not suitable for large number of elements Not efficient Very basic technique
. Explain insertion sort with example Insertion sort is the process of taking elements from the list one by one and inserting them at correct position in the array. It is the simplest sorting. Unsorted array Sorted array Steps:- Take first element in the list Then take nd element in the list Check if it is less than st Insert at correct position Then take next element, insert at correct position Repeat this process till all elements are sorted. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Program:- int n, temp, i, j, a[0]; printf( enter the elements ); for(i=0; i<n, i++) For(i=0; i<s; i++) Temp = a[i]; J = i=; While( ( temp < a[j] ) && (j>=0) ) A[j+] = a[j]; J= j-; A[j+]=temp; printf( answer= ); printf( %d,a[i]); Insertion sort Enter number of elements Enter elements 0 0 0 0 0 Answer=0 0 0 0 0 Advantages Disadvantages Easy technique Not suitable for large number of elements Simple and stable method expensive Suitable for small number of elements. Explain selection sort with example It is one of the basic sorting technique. It is used to sort the elements in ascending order It is based on comparison and swapping. Take the first element and compare it with all the other elements. The elements are swapped if first one is greater than the other element. Unsorted array Steps:- selection sort sorted array Take first element Compare it with next element If st < nd, compare st element with rd element If st > nd element, swap them. Continue this process till all the elements are in ascending order.
Program:- int n, temp, i, j, a[0]; printf( enter the elements ); for(i=0; i<n, i++) for(j=i+; j<n; j++) if(a[i] > a[j]) temp = a[i]; a[i]=a[j]; a[j]=temp; printf( answer= ); printf( %d,a[i]); Advantages Easy technique Simple method Suitable for small number of elements Disadvantages Not suitable for large number of elements Slow process. Explain merge sort with example It uses divide and conquer technique Any number of elements can be sorted The unsorted array is divided into smaller pieces. Divided until single element. Then it is merged (joined) in the increasing order. Both dividing and merging is done recursively. Unsorted array Enter number of elements Enter elements Merge sort Answer= Sorted array Program:- Refer book page number:.. Differentiate entry checked and exit checked conditional constructs with an example. (or) Differentiale WHILE loop and Do..WHILE loop constructs with an example. WHILE loop Entry checked loop Top-tested loop Condition is checked at the entry Loop will not execute if condition is FALSE Execute loop until condition is satisfied While(condition) Body of the loop int a=0; while(a<) printf( %d,a); a++; 0
Do..While loop:- Exit checked loop Bottom tested loop Condition is checked at the end If condition is false, loop will execute one time Execute the loop until the condition is true. Do Body of the loop; while(condition); Example program:- int a=0; do printf( %d,a); a++; while(a<);. Write a C program to generate Fibbonacci series int n, fib, f, f, i; for(i=; i<=n;i++) fib = f + f; printf( %d,fib); f = f; f = fib; 0 Enter number of elements 0