CP Unit Iii QB
CP Unit Iii QB
CP Unit Iii QB
15. Write a C program to find sum of n integers that are stored in an array.
main() { int array[10]; int i; int sum = 0; for ( i = 0; i < 11; i++){ scanf("%d", &array[i]); } for (i = 0; i < 11; i++) { sum += array[i]; } printf("%d", sum); return 0;
#include <stdio.h> #define MAXSIZE 10 void main() { int array[MAXSIZE]; int i, j, num, temp; printf("Enter the value of num \n"); scanf("%d", &num); printf("Enter the elements one by one \n"); for (i = 0; i < num; i++) { scanf("%d", &array[i]); } printf("Input array is \n"); for (i = 0; i < num; i++) { printf("%d\n", array[i]); } /* Bubble sorting begins */ for (i = 0; i < num; i++) { for (j = 0; j < (num - i - 1); j++) { if (array[j] > array[j + 1]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } printf("Sorted array is...\n"); for (i = 0; i < num; i++) { printf("%d\n", array[i]); } } OUTPUT: Enter the value of num 6 Enter the elements one by one 23 45 67 89 12 34 Input array is 23 45 67 89
2.
#include <stdio.h> #include <string.h> void main() { char name[10][8], tname[10][8], temp[8]; int i, j, n; printf("Enter the value of n \n"); scanf("%d", &n); printf("Enter %d names n", \n); for (i = 0; i < n; i++) { scanf("%s", name[i]); strcpy(tname[i], name[i]); } for (i = 0; i < n - 1 ; i++) { for (j = i + 1; j < n; j++) { if (strcmp(name[i], name[j]) > 0) { strcpy(temp, name[i]); strcpy(name[i], name[j]); strcpy(name[j], temp); } } } printf("\n----------------------------------------\n"); printf("Input NamestSorted names\n"); printf("------------------------------------------\n"); for (i = 0; i < n; i++) { printf("%s\t\t%s\n", tname[i], name[i]); } printf("------------------------------------------\n"); } OUTPUT Enter the value of n 7 Enter 7 names heap stack queue
3.
What is a two-dimensional array and how to declare and initialize a Two-dimensional array in C?
Two Dimensional Array It is a collection of data elements of same data type arranged in rows and columns (that is, in two dimensions). Declaration of Two-Dimensional Array Type arrayName[numberOfRows][numberOfColumn]; For example, int Sales[3][5]; Initialization of Two-Dimensional Array An two-dimensional array can be initialized along with declaration. For two-dimensional array initialization, elements of each row are enclosed within curly braces and separated by commas. All rows are enclosed within curly braces. int A[4][3] = {{22, 23, 10}, {15, 25, 13}, {20, 74, 67}, {11, 18, 14}}; Referring to Array Elements To access the elements of a two-dimensional array, we need a pair of indices: one for the row position and one for the column position. The format is as simple as: name[rowIndex][columnIndex] EXAMPLE #include<stdio.h> int main(){ int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p; printf("\nEnter the row and column of first matrix"); scanf("%d %d",&m,&n);
4.
Strings reverse using strrev in c programming language #include<stdio.h> #include<string.h> int main(){ char str[50]; char *rev; printf("Enter any string : "); scanf("%s",str); rev = strrev(str); printf("Reverse string is : %s",rev); return 0; } C PROGRAM TO REVERSE A STRING WITHOUT USING STRING FUNCTIONS. #include<stdio.h> int main(){ char str[50]; char rev[50]; int i=-1,j=0; printf("Enter any string : "); scanf("%s",str); while(str[++i]!='\0'); while(i>=0) rev[j++] = str[--i]; rev[j]='\0';
printf("Reverse of string is : %s",rev); return 0; } Sample output: Enter any string : cquestionbank.blogspot.com Reverse of string is : moc.topsgolb.knabnoitseuqc
C program to reverse a string using pointers #include<stdio.h> int main(){ char str[50]; char rev[50]; char *sptr = str; char *rptr = rev; int i=-1; printf("Enter any string : "); scanf("%s",str); while(*sptr){ sptr++; i++; } while(i>=0){ sptr--; *rptr = *sptr; rptr++; --i; } *rptr='\0'; printf("Reverse of string is : %s",rev); return 0; }
5.
Write a C program to find the largest and smallest number in the array.
printf("\nEnter the size of the array: "); scanf("%d",&size); printf("\nEnter %d elements in to the array: ", size); for(i=0;i<size;i++) scanf("%d",&a[i]); big=a[0]; for(i=1;i<size;i++){ if(big<a[i]) big=a[i]; } printf("Largest element: %d",big); small=a[0]; for(i=1;i<size;i++){ if(small>a[i]) small=a[i]; } printf("Smallest element: %d",small); return 0; } Sample Output: Enter the size of the array: 4 Enter 4 elements in to the array: 2 7 8 1 Largest element: 8 Smallest element: 1 6. Briefly explain the various string handling functions in C.
A string is a sequence of characters. Any sequence or set of characters defined within double quotation symbols is a constant string Following are some of the useful string handling functions supported by C. 1) strlen() 2) strcpy() 3) strncpy() 4) strcat() 5) strncat() 6) strcmp() 7) strncmp() 8) strcmpi() 9) strncmpi() Function & Purpose S.N. strcpy(s1, s2); 1 Copies string s2 into string s1. strcat(s1, s2); 2 Concatenates string s2 onto the end of string s1. strlen(s1); 3 Returns the length of string s1. strcmp(s1, s2); 4 Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. strchr(s1, ch); 5 Returns a pointer to the first occurrence of character ch in string s1.
#include <stdio.h> #include <string.h> int main () { char str1[12] = "Hello"; char str2[12] = "World"; char str3[12]; int len ; /* copy str1 into str3 */ strcpy(str3, str1); printf("strcpy( str3, str1) : %s\n", str3 ); /* concatenates str1 and str2 */ strcat( str1, str2); printf("strcat( str1, str2): %s\n", str1 ); /* total lenghth of str1 after concatenation */ len = strlen(str1); printf("strlen(str1) : %d\n", len ); return 0; } OUTPUT: strcpy( str3, str1) : Hello strcat( str1, str2): HelloWorld strlen(str1) : 10
#include <stdio.h> #include <string.h> int main(){ char str1[]="LOWer Case"; puts(strlwr(str1)); //converts to lowercase and displays it. return 0; } Output lower case
#include <stdio.h>