2. Write a program to insert an array element in a particular
position.
3. Write a program for bubble sort and explain it with a suitable
example.
4. Write a program for binary search and explain it with a
suitable example.
5. write a program to get five students roll number, name and
three subject marks and calculate total , average and grade of
each students.
6. write a program to check whether a given string is palindrome
or not
7.write a program that implements the following string
functions:
Strlen()
Strcpy()
Strcmp()
Strupr()
Strlwr()
Strcat()
8. write a program for matrix addition.
ARRAYS: Definition: An array is a derived data type. It is a collection of similar data type elements. The data elements are stored in contiguous memory location. The elements of an array can be accessed by using pointers The syntax for array is : Data_type array_variable[size or subscript of the array]; Example: int a[10]; where, int is the data type of the elements. a is the array name. [10] is the index which denotes the array contains 10 data elements. ARRAY INITIALIZATION: The value can be initialized to an array , when they are declared like ordinary variables, otherwise they hold garbage values. The array can be initialized in two ways i) At compile time ii) At run time i) At compile time: Syntax: data_type array_name[size]={list of values}; Example: int marks[3]={70,80,90); Example: int a[5]={1,2,3,4,5} Here, 5 data elements are stored in the array called "a". The array elements are stored sequentially in separate locations. The array elements are called as below: A[0] refers to 1 st element 1 A[1] refers to 1 st element 2 A[2] refers to 1 st element 3 A[3] refers to 1 st element 4 A[4] refers to 1 st element 5 ii) At run time: Example: while(i<=10) { if(i<5) sum[i]=0; else sum[i[=sum[i]+I; } CHARACTERISTICS OF AN ARRAY: 1. All the data elements share the same name, and they are distinguished from one another with the help of an element number. 2. Any particular element of an array can be modified separately without disturbing other elements. 3. The element number in an array plays major role for calling each element. CLASSIFICATION OF ARRAY: Arrays can be classified into 2 major types. 1. One-dimensional Array. 2. Two or more dimensional Array. 1. One-Dimensional Array: The array elements are arranged in rows or columns. The data are stored in continuous memory location. Example: Element a[0] a[1] a[2] a[3] a[4] Address 2000 2002 2004 2006 2008 Example Program: #include<stdio.h> void main() { int a[10],n,i; clrscr(); printf("\nEnter the size of the array"); scanf("%d",&n); printf("\nEnter the elements into the array one by one"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nThe array elements are\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); getch(); } OUTPUT: Enter the size of the array 5 Enter the elements into the array one by one 1 2 3 4 5 The array elements are 1 2 3 4 5 2. Two-Dimensional Array: Two dimensional arrays are used in situation where a table of values need to be stored in an array. Two pair of aquare brackets are required for each subscript. The array elements are thought to be stored in the form of rows and columns. Syntax: Data_type array_name[row size][column size]; Example: int a[3][3]; Diagrammatic representation: Column 1 Column 2 Column 3 Row 1 a[0][0] a[0][1] a[0][2] Row 2 a[1][0] a[1][1] a[1][2] Row 3 a[2][0] a[2][1] a[2][2] The above arrangement of array elements is only for understanding. Physically array elements are stored in continuous memory locations. The two dimensional array is a collection of one- dimensional array, which are placed one after another. Example Program: #include<stdio.h> void main() { int a[10][10],n,i,j; clrscr(); printf("\nEnter the size of the array"); scanf("%d",&n); printf("\nEnter the elements into the array one by one"); for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nThe array elements are\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("%d\t",a[i][j]); } printf("\n"); } getch(); } OUTPUT: Enter the size of the array2 Enter the elements into the array one by one 1 2 3 4 The array elements are 1 2 3 4 Initialising a 2-D array: Syntax: Data_type array_name[row size][column size]={List of values}; Example: int stud[4][2]={{60,80},{68,95},{82,82},{83,85}}; Int stud[4][2]={80,76,55,33,55,33,67,31}; PROGRAMS IN INTEGER ARRAYS SORTING OF AN ARRAY #include <stdio.h> #include <conio.h> void main() { int a[25],i,j,n,temp; clrscr(); printf("\nEnter the no. of elements in the array"); scanf("%d",&n); printf("\nEnter the elements in the array\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("\nThe sorted array is \n"); for(i=0;i<n;i++) { printf("%d\t",a[i]); } getch(); } OUTPUT: Enter the no. of elements in the array6 Enter the elements in the array 4 1 7 4 7 2 The sorted array is 1 2 4 4 7 7 SUM OF THE ELEMENTS IN AN ARRAY #include <stdio.h> #include <conio.h> void main() { int a[25],i,j,n,sum=0; clrscr(); printf("\nEnter the no. of elements in the array"); scanf("%d",&n); printf("\nEnter the elements in the array\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { sum=sum+a[i]; } printf("\nThe sum of the elements in the array is %d",sum); getch(); } OUTPUT: Enter the no. of elements in the array4 Enter the elements in the array 4 3 2 1 The sum of the elements in the array is 10 INSERTING AN ELEMENT INTO AN ARRAY #include <stdio.h> void main() { int a[30],i,n,x,p; clrscr(); printf("\nEnter the size of the array"); scanf("%d",&n); printf("\nenter the elements into the array"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nEnter the element to be inserted"); scanf("%d",&x); printf("\nEnter the position in which the element to be inserted"); scanf("%d",&p); for(i=n-1;i>=p;i--) a[i+1]=a[i]; a[p]=x; printf("\nThe elements are \n"); for(i=0;i<=n;i++) printf("%d\t",a[i]); getch(); } OUTPUT: Enter the size of the array 5 enter the elements into the array 3 4 5 2 1 Enter the element to be inserted 5 Enter the position in which the element to be inserted 3 The elements are 3 4 5 5 2 1 LARGEST ELEMENT IN AN ARRAY #include <stdio.h> #include <conio.h> void main() { int a[25],i,n,max; clrscr(); printf("\nEnter the no. of elements in the array"); scanf("%d",&n); printf("\nEnter the elements in the array\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } max=a[0]; for(i=1;i<n;i++) { if(a[i]>max) max=a[i]; } printf("\nThe largest element is %d",max); getch(); } output: Enter the no. of elements in the array4 Enter the elements in the array 6 7 5 45 The largest element is 45 SEARCHING AN ELEMENT IN AN ARRAY #include <stdio.h> #include <conio.h> void main() { int a[25],i,j,n,x; clrscr(); printf("\nEnter the no. of elements in the array"); scanf("%d",&n); printf("\nEnter the elements in the array\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\nEnter the element to be searched in the array"); scanf("%d",&x); for(i=0;i<n;i++) { if(a[i]==x) { printf("\nThe element is found"); break; } } if(i==n) { printf("\nThe element is not found"); } getch(); } OUTPUT: Enter the no. of elements in the array 4 Enter the elements in the array 2 1 4 5 Enter the element to be searched in the array 2 The element is found PROGRAMS IN TWO DIMENSIONAL ARRAY MATRIX SUBTRACTION #include <stdio.h> #include <conio.h> void main() { int a[10][10],b[10][10],c[10][10],i,j,k,m,n; clrscr(); printf("\nEnter the no. of rows in the matrix A"); scanf("%d",&m); printf("\nEnter the no. of columns in the matrix A"); scanf("%d",&n); printf("\nEnter the elements in the matrix A"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } printf("\nEnter the elements in the matrix B"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=a[i][j]-b[i][j]; } } printf("\nThe Resultant matrix is\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d\t",c[i][j]); } printf("\n"); } getch(); } OUTPUT: Enter the no. of rows in the matrix2 Enter the no. of columns in the matrix2 Enter the elements in the matrix A 1 1 1 1 Enter the elements in the matrix B 1 1 1 1 The Resultant matrix is 0 0 0 0 MATRIX MULTIPLICATION #include <stdio.h> #include <conio.h> void main() { int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p; clrscr(); printf("\nEnter the no. of rows in the matrix A"); scanf("%d",&m); printf("\nEnter the no. of columns in the matrix A"); scanf("%d",&n); printf("\nEnter the no. of columns in the matrix B"); scanf("%d",&p); printf("\nEnter the elements in the matrix A"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } printf("\nEnter the elements in the matrix B"); for(i=0;i<n;i++) { for(j=0;j<p;j++) { scanf("%d",&b[i][j]); } } printf("\nThe Resultant matrix is\n"); for(i=0;i<m;i++) { for(j=0;j<p;j++) { c[i][j]=0; for(k=0;k<n;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } printf("%d\t",c[i][j]); } printf("\n"); } getch(); } OUTPUT: Enter the no. of rows in the matrix A2 Enter the no. of columns in the matrix A2 Enter the no. of columns in the matrix B2 Enter the elements in the matrix A 1 1 1 1 Enter the elements in the matrix B 1 1 1 1 The Resultant matrix is 2 2 2 2 SUM OF DIAGONAL ELEMENTS IN A MATRIX #include <stdio.h> #include <conio.h> void main() { int a[10][10],i,j,m,n,sum=0; clrscr(); printf("\nEnter the no. of rows in the matrix A"); scanf("%d",&m); printf("Enter the no. of columns in the matrix A"); scanf("%d",&n); printf("\nEnter the elements in the matrix A"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } printf("\nThe Addition of diagonal elements in the matrix is\n"); for(i=0;i<m;i++) { sum=sum+a[i][i]; } printf("%d",sum); getch(); } OUTPUT: Enter the no. of rows in the matrix A 2 Enter the no. of columns in the matrix A 2 Enter the elements in the matrix A 1 1 1 1 The Addition of diagonal elements in the matrix is 2 MULTI DIMENSIONAL ARRAY: The dimension with 3 or more called multi dimensional arrays. Syntax: data_type array_name[size1][size2][size n]; Example: int a[3][3][3]; ARRAYS OF STRINGS(CHARACTERS) A string is a collection of characters. A string constant is a one dimensional array of characters terminated by a null ('\0) character. Example: char name[]={'L','A','K','\0'}; where , '\0' is a null character and specifies end of the string If a string is stored in the character array, a null character ('\0') is automatically added to the array at the end, which denotes the end of the string. Example: Char name[]="LAK"; Example program: #include<stdio.h> main() { char name[]="LAK"; printf("%s",name); } OUTPUT: LAK CHARACTER ARRAY: The data elements stored in the array belong to character data type. Hence the name, character array. The character array can be used to stored individual characters or a collection of characters called strings. If a string is stored in the character array, a null character ('\0') is automatically added to the array at the end, which denotes the end of the string. Example program: Program to display character array & their addresses: #include <stdio.h> void main() { char a[]={'A','B','C','D'}; int i; clrscr(); for(i=0;a[i]!='\0';i++) { printf("\nThe array elements are : %c",a[i]); printf("\nThe memory location is %u",&a[i]); } getch(); } OUTPUT: The array elements are : A The memory location is 65522 The array elements are : B The memory location is 65523 The array elements are : C The memory location is 65524 The array elements are : D The memory location is 65525 Strings Character Strings A sequence of characters is often referred to as a character "string". A string is stored in an array of type char ending with the null character '\0 '. A string containing a single character takes up 2 bytes of storage. Character vs. String A string constant is a sequence of characters enclosed in double quotes. n For example, the character string: char s1[2]="a"; //Takes two bytes of storage. s1: a \0 On the other hand, the character, in single quotes: char s2= `a`; //Takes only one byte of storage. s2: a String Functions strlen() It is used to find the length of the string. syntax: strlen(string) strcpy() It is used to copy one string to another. syntax: strcpy(string1,string2) strcat() It is used to combine two strings. syntax: strcat(string1,string2) strcmp() It is used to compare two strings. syntax: strcmp(string1,string2) Returns 0 if two strings are equal. Return value <0 if s1 is less than s2. Return value >0 if s1 is greater than s2. strrev() It used to reverse a string. syntax: strrev(string) strlwr(), strupr() It used to change the case of a string. syntax: strlwr(string) strupr(string) strncpy() It used to copy 'n' characters of one string to another. strstr() n It is used to determine the first occurrence of a given string in another string. strncat() It Appends source string to destination string up to specified length. strspn() It is used t find up to what length two strings are identical. strncmp() It is used to compare 'n' character of two strings. strcmpi() It is used to compare two strings without regarding the case. strnicmp() It is used to compare first 'n' characters of two strings without regarding the case. strchr() It is used to determine the first occurrence of a given character in a string. strrchr() It is used to determine the last occurrence of a given character in a string. Example program for strlen(): #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="college"; int b; clrscr(); b=strlen(a); printf("\nThe length of the string is %d",b); getch(); } Output: The length of the string is 7 Example Program for strcpy(); #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="IT"; char b[]="Dept"; clrscr(); strcpy(a,b); printf("\nThe string is %s",a); getch(); } Output: The string is Dept Example Program for strcat(): #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="IT"; char b[]="Dept"; clrscr(); strcat(a,b); printf("\nThe string is %s",a); getch(); } Output: The string is ITDept Example program for strlcmp(): #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="itdept"; char b[]="it"; int i; clrscr(); i=strcmp(a,b); if(i==0) printf("\nstrings are equal:%d",i); else if(i<0) printf("\nstring1 is less than string2:%d",i); else printf("\nstring1 is greater than string2:%d",i); getch(); } Output: string1 is greater than string2:100 Example Program for strupr() and strlwr(): #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="itdept"; clrscr(); printf("\nThe string is :%s",a); strupr(a); printf("\nThe string after conversion to uppercase :%s",a); strlwr(a); printf("\nThe string after conversion to lowercase :%s",a); getch(); } Output The string is :itdept The string after conversion to uppercase :ITDEPT The string after conversion to lowercase :itdept Example Program for strrev(): #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="Dept"; clrscr(); printf("\nThe string is %s",strrev(a)); getch(); } Output: The string is tpeD Example Program for strncpy(): #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="itdept"; char b[15]; int i=0; clrscr(); strncpy(b,a,2); b[2]='\0'; printf("\nThe string is :%s",b); getch(); } Output: The string is :it Example Program for String Palindrome #include<stdio.h> #include<conio.h> #include<string.h> void main() { int len,i,j; char str[15]; clrscr(); printf("\n Enter the string:"); scanf("%s",str); len=strlen(str); for(i=0,j=len-1;i<len/2;i++,j--) { if(str[i]!=str[j]) { printf("\nThe String is not a palindrome"); getch(); exit(0); } } printf("\nThe String is a palindrome"); getch(); } Output: Enter the string:abcba The String is a palindrome
Binary Search #include <stdio.h> void main() { int c, first, last, middle, n, search, array[100]; 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]); printf("Enter value to find\n"); scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2;//first and last position of array element while( first <= last ) { if ( array[middle] < search )// array[middle] is the middle element in array first = middle + 1; else if ( array[middle] == search ) { printf("%d found at location %d\n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if ( first > last ) printf("Not found! %d is not present in the list.\n", search); return 0; } Linear search #include <stdio.h> int main() { int array[100], search, c, number; printf("Enter the number of elements in array\n"); scanf("%d",&number); printf("Enter %d numbers\n", number); for ( c = 0 ; c < number ; c++ ) scanf("%d",&array[c]); printf("Enter the number to search\n"); scanf("%d",&search); for ( c = 0 ; c < number ; c++ ) { if ( array[c] == search ) /* if required element found */ { printf("%d is present at location %d.\n", search, c+1); break; } } if ( c == number ) printf("%d is not present in array.\n", search); return 0; } Selection Sort #include<stdio.h> void main() { int i,j,min,minat,temp,length,array[20]; printf("Enter the number of elements in array); scanf("%d",&length); for(i=0;i<length;i++) scanf("%d",&array[i]); for(i=0;i<(length-1);i++) { minat=i; //the position of the min element min=array[i]; for(j=i+1;j<(length);j++) //select the min of the rest of array { if(min>array[j]) //ascending order { minat=j; //the position of the min element min=array[j]; } } temp=array[i] ; array[i]=array[minat]; //swap array[minat]=temp; } for(i=0;i<10;i++) printf("%d",array[i]); } BUBBLE SORT #include <stdio.h> #include<conio.h> void 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]); for (c = 0 ; c < ( n - 1 ); c++) { for (d = 0 ; d < n - c - 1; d++) { if (array[d] > array[d+1]) /* For decreasing order use < */ { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; } } } printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); getch(); } /* insertion sort ascending order */ #include <stdio.h> #include<conio.h> Void main() { int n, array[1000], c, d, t; 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]); } for (c = 1 ; c <= n - 1; c++) { d = c; while ( d > 0 && array[d] < array[d-1]) { t = array[d]; array[d] = array[d-1]; array[d-1] = t; d--; } } printf("Sorted list in ascending order:\n"); for (c = 0; c <= n - 1; c++) { printf("%d\n", array[c]); } getch(); }