s yllabus Arrays and strings Arrays Declaration and Initialization, 1-Dimensional Array, 2- Dimensional Array String processing: In built String handling functions ( strlen , strcpy , strcat and strcmp , puts, gets) Linear search program, bubble sort program, simple programs covering arrays and strings 2 Programming in C
Introducing Arrays Array is a data structure that represents a collection of the same types of data. JAIN STOBLE B Programming in C 3
The arrays are generally in the form of continuous memory locations one-dimensional array two-dimensional array Programming in C 4
One dimensional Array An array which is having either a single row or single column is termed as a one-dimensional array. Programming in C 5 a a[0] contain first element a[1] contain second element …….
Declaring Array Variables Data type array name [array size]; Example: int list[10]; char num[15]; float hat[20]; JAIN STOBLE B 13 Programming in C 6
Initializing array It's possible to initialize an array during declaration. int mark[5] = {19, 10, 8, 17, 9}; Another method to initialize array during declaration: int mark[ ] = {19, 10, 8, 17, 9}; Here C automatically figures out how many elements are in the array.(implicit declaration) Programming in C 7
JAIN STOBLE B Initializing array Next method is declaring the array first and then assigning values to its elements. int mark[3]; mark[0] = 2; mark[1] = 4; mark[2] = 8; Programming in C 8
Partial Initialization C allows you to initialize a part of the array. Int a [4] = {1, 2}; assigns values 1, 2 to the first two elements of the array. The other two elements will be set to zero. Programming in C 9
Accessing Array Elements You can access elements of an array by indices. Array indices start from to (arraySize-1.) int mark[5] JAIN STOBLE B Programming in C 10
Entering Data into an Array #include<stdio.h> void main() { int a[10],i,n; p ri n t f ( " E nt er si z e o f ar ra y " ); scanf("%d",&n); //inputting elements to array printf("enter elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); } Programming in C 11
Entering Data into an Array and displaying the e l eme n ts # include<stdio.h> void main() { int a[10],i,n; printf("Enter size of First array"); scanf("%d",&n); //inputting elements to array printf("enter elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); //d i s pl a y in g ele m e n ts of a r r a y for(i=0;i<n;i++) printf("%d\n",a[i]); } Programming in C 12
Sum of elements of an array #include<stdio.h> void main() { int a[10],i,n,sum=0; printf("enter limit"); scanf("%d",&n); printf("enter elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { sum=sum+a[i]; } printf("sum of array elements is %d",sum); } Programming in C 13
Biggest element in an array # include<stdio.h> void main() { int a[10],i,n,big; printf("enter limit"); scanf("%d",&n); printf("enter elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); big=a[0]; for(i=0;i<n;i++) { if(a[i]>big) big=a[i]; } printf("Biggest element of array is %d",big); } Programming in C 14
Biggest and smallest element in an array #i n cl ud e < s t d i o . h > void main() { int a[10],i,n,big,small; printf("enter limit"); scanf("%d",&n); printf("enter elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); big=a[0]; small=a[0]; Programming in C 15
Biggest and smallest element in an array for(i=0;i<n;i++) { if(a[i]>big) big=a[i]; } for(i=0;i<n;i++) { if(a[i]<small) small=a[i]; } printf("Biggest element of array is %d\n",big); printf("Smallest element of array is %d\n",small); } Programming in C 16
Copy one array to another # i n c l u d e < s t d io. h > void main() { int a[10],copy[10],i,n; printf("enter limit"); scanf("%d",&n); printf("enter elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); Programming in C 17
Copy one array to another (continued) for(i=0;i<n;i++) { copy[i]=a[i]; } printf("elements of copied array is"); for(i=0;i<n;i++) printf("%d ",copy[i]); } Programming in C 18
Reverse an array #include<stdio.h> void main() { int a[10],i,n,j,rev[100]; printf("Enter size of First array"); scanf("%d",&n); //inputting elements to array printf("enter elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); Programming in C 19
Reverse an array(continued) j=0; for(i=n-1;i>=0;i--) { rev[j]=a[i]; j++; } printf("reversed array is"); for(i=0;i<n;i++) printf(" %d",rev[i]); } Programming in C 20
Reverse elements in the same array # include<stdio.h> void main() { int a[10]={1,2,3,4,5}; int i,start=0,end=4,temp; while (start < end) { temp = a[start]; a[start] = a[end]; a[end] = temp; start++; end--; } printf("reversed array is"); for(i=0;i<5;i++) p r i n tf ( " % d ", a [ i ]); Programming in C 21
//print the array in reverse using for #include<stdio.h> void main() { int a[50],b[50], i,t,n,j ; printf ("Enter the size of the list "); //5 scanf ("% d",&n ); //read the list of n elem for( i =0;i< n;i ++) { printf ("\ nenter the next value "); scanf ("% d",&a [ i ]); } for( i =0,j=n-1;i< n;i ++,j--) { b[ i ]=a[j]; } //array printing after sorting printf ("\n after reverse\n"); for( i =0;i< n;i ++) printf ("%d ",b[ i ]); } Programming in C 22
Programming in C 23 //print the array in reverse withour second array #include<stdio.h> void main() { int a[50], i,t,n,j ; printf ("Enter the size of the list "); //5 scanf ("% d",&n ); //read the list of n elem for( i =0;i< n;i ++) { printf ("\ nenter the next value "); scanf ("% d",&a [ i ]); } // printg from last index for( i =n-1;i>=0;i--) { printf ("%d ",a[ i ]); } //array printing the printf ("\n from front\n"); for( i =0;i< n;i ++) printf ("%d ",a[ i ]); }
Operation on 1D array Searching Inserting Deleting Sorting Merging 2 arrays JAIN STOBLE B Programming in C 24
Search an element in an array(linear search) # include<stdio.h> void main() { int a[10],i,n,k,flag=0; printf("Enter size of First array"); scanf("%d",&n); //inputting elements to array printf("enter elements"); for(i=0;i<n;i++) {scanf("%d",&a[i]); } Programming in C 25
Search an element in an array (continued) printf("Enter element to be searched"); scanf("%d",&k); for(i=0;i<n;i++) { if (a[i]==k) { printf("element found at position %d\n",i); flag=1; } } if(flag==0) printf("element is not present in the array"); Programming in C 26
Pgm to insert element to an array #include<stdio.h> void main() { int a[10],i,n,insrt,pos,j; printf("enter limit"); scanf("%d",&n); printf("enter elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("enter element to be inserted"); scanf("%d",&insrt); printf("enter position to be inserted"); scanf("%d",&pos); Programming in C 27
Pgm to insert element to an array(continued) for(i=n;i>=pos;i--) { a[i]=a[i-1]; } a[pos-1]=insrt; for(i=0;i<n+1;i++) p ri n t f ( "a[ % d ] = % d \ n ",i,a[ i ]); } Programming in C 28
Program to delete element from an ar r a y #include<stdio.h> void main() { int a[10],i,n,d,j; printf("enter limit"); scanf("%d",&n); printf("enter elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("enter element to be deleted"); scanf("%d",&d); Programming in C 29
for(i=0;i<n;i++) { if(a[i]==d) {pos=i; b r ea k ;} } for(j=pos;j<n-1;j++) { a[j]=a[j+1]; } printf(“the new array is”) for(i=0;i<n-1;i++) printf("%d\n",a[i]); } Programming in C 30
Pgm to combine two arrays #include<stdio.h> void main() { int a[10],b[10],i,j,n,m; printf("Enter size of First array"); scanf("%d",&n); //inputting elements to first array printf("enter elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter size of Second array"); scanf("%d",&m); //inputtingelements to second array printf("enter elements"); for(i=0;i<m;i++) scanf("%d",&b[i]); Programming in C 31
Pgm to combine two arrays //combining two arrays j=0; for(i=n;i<n+m;i++) { a[i]=b[j]; j++; } //printing final array for(i=0;i<n+m;i++) printf("%d\n",a[i]); } Programming in C 32
//delete an element in an array from a given elemnt #include<stdio.h> void main() { int a[50], i,s,n,pos ; printf ("Enter the size of the list "); scanf ("% d",&n ); //read the list of n elem for( i =0;i< n;i ++) { printf ("\ nenter the next value "); scanf ("% d",&a [ i ]); } //array printing printf ("\ nbefore \n"); for( i =0;i< n;i ++) printf ("%d ",a[ i ]); printf ("\ nEnter the elemnt to be deleted "); scanf ("% d",&s ); for( i =0;i< n;i ++) { if(a[ i ]==s) { pos = i ; break; } } //shifting for( i = pos;i < n;i ++) a[ i ]=a[i+1]; //array printing after shifting printf ("\n after\n"); for( i =0;i<n-1;i++) printf ("%d ",a[ i ]); } Programming in C 33
//linear search with flag #include<stdio.h> void main() { int a[50], i,s,n,y =10; printf ("Enter the size of the list"); scanf ("% d",&n ); //read the list of n elem for( i =0;i< n;i ++) { printf ("\ nenter the next value "); scanf ("% d",&a [ i ]); } printf ("Enter the searching element"); scanf ("% d",&s ); //comparing //traversing for( i =0;i< n;i ++) { if(a[ i ]==s) { y=21; } } if(y==21) { printf ("its inside"); } else { printf ("not inside"); } } Programming in C 34
// find the largest and smallest of n elements in array #include<stdio.h> void main() { int i,marks [50], n,l,s ; //static allocation printf ("enter the number of elements "); //5 marks[0] [1] [2] [3] [4] [9] scanf ("% d",&n ); //reading for( i =0;i< n;i ++) { printf ("Enter the next value "); scanf ("% d",&marks [ i ]); } //largest l s // 2 6 7 9 1 4 6 12 1 50 -1 l=marks[0]; s=marks[0]; for( i =1;i< n;i ++) { if(l<marks[ i ]) { l=marks[ i ]; } if(s>marks[ i ]) { s=marks[ i ]; } } printf ("largest and smallest value in the list is %d and %d ", l,s ); } Programming in C 35
//sorts elements in an array #include<stdio.h> void main() { int a[50], i,t,n,j ; printf ("Enter the size of the list "); scanf ("% d",&n ); //read the list of n elem for( i =0;i< n;i ++) { printf ("\ nenter the next value "); scanf ("% d",&a [ i ]); } //array printing before printf ("\ nbefore sorting\n"); for( i =0;i< n;i ++) printf ("%d ",a[ i ]); //sorting Programming in C 36 //sorting for(j=0;j<n-1;j++) //x n-1 time repeating the inner for loop { for( i =0;i<n-1;i++) //n-1 doing comparison { if(a[ i ]>a[i+1]) { t=a[ i ]; // swaping a[ i ]=a[i+1]; a[i+1]=t; } } } //array printing after sorting printf ("\n after sorting\n"); for( i =0;i< n;i ++) printf ("%d ",a[ i ]); }
//delete an element in an array given the element #include<stdio.h> v oid main() { int a[50], i,s,n,pos ; printf ("Enter the size of the list "); scanf ("% d",&n ); //read the list of n elem for( i =0;i< n;i ++) { printf ("\ nenter the next value "); scanf ("% d",&a [ i ]); } //array printing printf ("\ nbefore \n"); for( i =0;i< n;i ++) printf ("%d ",a[ i ]); printf ("\ nEnter the elemnt to be deleted "); scanf ("% d",&s ); Programming in C 37 for( i =0;i< n;i ++) { if(a[ i ]==s) { pos = i ; break; } } //shifting for( i = pos;i < n;i ++) a[ i ]=a[i+1]; //array printing after shifting printf ("\n after\n"); for( i =0;i<n-1;i++) printf ("%d ",a[ i ]); }
/print the array in reverse #include<stdio.h> void main() { int a[50],b[50], i,t,n,j ; printf ("Enter the size of the list "); //5 scanf ("% d",&n ); //read the list of n elem for( i =0;i< n;i ++) { printf ("\ nenter the next value "); scanf ("% d",&a [ i ]); } for( i =0,j=n-1;i< n;i ++,j--) { b[ i ]=a[j]; } //array printing after sorting printf ("\n after reverse\n"); for( i =0;i< n;i ++) printf ("%d ",b[ i ]); } Programming in C 38
//print the array in reverse withour second array #include<stdio.h> void main() { int a[50], i,t,n,j ; printf ("Enter the size of the list "); //5 scanf ("% d",&n ); //read the list of n elem for( i =0;i< n;i ++) { printf ("\ nenter the next value "); scanf ("% d",&a [ i ]); } // printg from last index for( i =n-1;i>=0;i--) { printf ("%d ",a[ i ]); } //array printing the printf ("\n from front\n"); for( i =0;i< n;i ++) printf ("%d ",a[ i ]); } Programming in C 39
Strings Strings - array of characters ended with null character (‘\0’). ● always enclosed by double quotes. Programming in C 40
Strings The way a group of integers can be stored in an integer array, similarly a group of characters can be stored in a character array. A string constant is a one-dimensional array of characters terminated by a null ( ‘\0’ ) Each character in the array occupies one byte of memory and the last character is always ‘\0’. ‘\0’ and ‘0’ are not same. ASCII value of ‘\0’ is 0, whereas ASCII value of ‘0’ is 48. The elements of the character array are stored in contiguous memory locations. Programming in C 41
a[0] a[1] a[2] a[3] a[4] Strings Declaring an array : ● char a[10]; // character array i.e. string a Programming in C 42
Initializing Character Arrays char a[6] = {'H', 'e', 'l', 'l', 'o'}; char a[ ] = {' H', 'e','l','l','o '}; char a[6] = “Hello”; C adds the character '\0', called the null terminator , to indicate the end of the string, as shown in Figure. Programming in C 43
JAIN STOBLE B Reading string s canf () %s getchar () with loop gets() Programming in C 44
JAIN STOBLE B S c an f() #include <stdio.h> void main() { char name[20]; printf("Enter name: "); scanf("%s", name); printf("Your name is %s.", name); } Programming in C 45
JAIN STOBLE B Using gets() and puts() #include <stdio.h> void main() { char name[30]; printf("Enter name: "); gets(name); // read string printf("Name: "); puts(name); // display string } Programming in C 46
JAIN STOBLE B #i n clu d e < s t dio . h> void main() { int i;char a[10]; printf("enter text"); for(i=0;i<8;i++) a[i]=getchar(); for(i=0;i<8;i++) putchar(a[i]); } Programming in C 47
Programming in C 48 ///length of string without function #include<stdio.h> void main() { char c[60]; //string \0 int i,len =0; printf ("enter the string "); scanf ("% s",c ); for( i =0;c[ i ]!='\0';i++) { len ++; } printf ("%d", len ); }
Printing Character Array For a character array, it can be printed using one print statement. For example, the following code displays Dallas: char city[]="Dallas"; printf (“% s”,city ); Programming in C 49
JAIN STOBLE B 50 #inc l ude < s t d i o . h> void main() {int i; char a[] = “ Kottayam "; for(i=0;a[i]!='\0';i++) printf("%c",a[i]); } Programming in C 50
JAIN STOBLE B 51 String handling functions strcpy(s1, s2); Copies string s2 into string s1. strcat(s1, s2); Concatenates string s2 onto the end of string s1. strlen(s1); Returns the length of string s1. strcmp(s1, s2); Returns if s1 and s2 are the same; less than 0 if s1<s2; greater than if s1>s2. Programming in C 51
Programming in C 52
Strings– string.h # include <string.h> int main() { char test[100]; char test2[]= "World"; strcpy (test,"Hello"); strcat (test,test2); if ( strcmp (test,"dave") == 0) printf ("Test is same as Dave\n"); printf ("Length of test is %d\n", strlen (test)); Programming in C 53
} To read a string including white spaces using gets and puts # i n c l u d e < s t d i o.h> void main() { char name[20]; printf("Enter name"); gets(name); p u t s ( n ame ) ; Programming in C 54
} To print a string character by character #include<stdio.h> void main() { char text[30]; int i; printf("enter string"); gets(text); for(i=0;text[i]!='\0';i++) printf(" %c",text[i]); Programming in C 55
printf(" Length of the string is %d",i); } Pgm to find the length of string with out using string handling function #include<stdio.h> void main() {char text[30]; int i; printf("enter string"); gets(text); f o r ( i= ; t e x t [i ] ! = ' \ ' ; i + +) printf(" %c",text[i]); Programming in C 56
Pgm to count vowels in a string() #include<stdio.h> void main() { char text[30]; int vcount=0; int i; printf("enter string"); gets(text); for(i=0;text[i]!='\0';i++) { if (text[i]=='a' || text[i]=='e' ||text[i]=='i' ||text[i]=='o' ||text[i]=='u' ) vcount++; } printf(" vowel count is %d",vcount); } Programming in C 57
Pgm to reverse a string #include<stdio.h> void main() { char text[30],rev[30]; int i,len=0,j=0,n=0; printf("enter string"); gets(text); f o r ( i= ; t e x t [i ] ! = ' \ ' ; i + +) n++; for( i =n-1,i>=0;i--) { r ev [ j ] =t e x t [ i ]; j++ ; } rev[j]='\0'; printf (" reversed string is % s",rev ); } Programming in C 58
String palindrome with out reverse #include<stdio.h> void main() {char text[30],f=0; int i,len=0,j=0; printf("enter string"); gets(text); for(i=0;text[i]!='\0';i++) len++; for(i=len-1,j=0;j<len/2;i--,j++) { if (text[i]!=text[j]) { f= 1; break; } } if (f==1) printf("not palindrome"); else printf(" palindrome"); } Programming in C 59
Pgm to check two strings are equal with out using string handling functions #include<stdio.h> void main() { char str1[1000], str2[1000]; int i, flag=0; printf("Enter the First string :\n"); gets(str1); printf("Enter the Second string :\n"); gets(str2); for(i=0;str1[i]!='\0';i++) { if(str1[i]==str2[i]) continue; else { fla g= 1; break; } } if(flag==0) printf("Strings are equal \n"); else printf("Strings are not equal \n"); } Programming in C 60
String handling functions Programming in C 61
Standard Library String Functions Programming in C 62
Programming in C 63
strlen( ) Function Example #include<stdio.h> #i nc l u d e < s tri n g.h> void main() { char text[30]="hello world"; int n; n=strlen(text); p ri n tf ( "le n g th of s tri n g is %d" ,n) ; } Programming in C 64
strcpy( ) #include<stdio.h> #include<string.h> void main() { char string1[30]="C"; char string2[30]="PROGRAMMING"; strcpy(string1,string2); printf("string after copying is %s",string1); } //output is PROGRAMMING Programming in C 65
strcat( ) #include<stdio.h> #i n c l u d e < s t ri n g. h > void main() { char string1[30]="C"; char string2[30]=" PROGRAMMING "; strcat(string1,string2); printf("string after concatenation is %s",string1); } //output is CPROGRAMMING Programming in C 66
strcmp( ) #include<stdio.h> #include<string.h> void main() { char string1[30]="HELLO"; char string2[30]=“WORLD"; if(strcmp(string1,string2)==0) printf("string is equal"); else p ri n tf ( " s t ri n g is n o t e q u al"); } //output is string is not equal Programming in C 67
strcmp( ) #include<stdio.h> #include<string.h> void main() { char string1[30]="HELLO"; char string2[30]="HELLO"; if(strcmp(string1,string2)==0) printf("string is palindrome"); else printf("string is not palindrome"); } //output is string is palindrome Programming in C 68
s tr r e v () #include<stdio.h> #include<string.h> void main() { char text[30]="hello world"; strrev(text); printf(“reversed string is %s",text); } Programming in C 69
strcmp( ) main( ) { char string1[ ] = "Jerry" ; char string2[ ] = "Ferry" ; int i, j, k ; i = strcmp ( string1, "Jerry" ) ; j = strcmp ( string1, string2 ) ; k = strcmp ( string1, "Jerry boy" ) ; printf ( "\n%d %d %d", i, j, k ) ; } ou t pu t ... 0 4 -32 1st call to strcmp( ), the two strings are identical—“Jerry” and “Jerry”—and the value returned by strcmp( ) is zero. 2nd call, the first character of “Jerry” doesn't match with the first character of “Ferry” and the result is 4, which is the numeric difference between ASCII value of ‘J’ and ‘F’. 3rd call to strcmp( ) “Jerry” doesn’t match with “Jerry boy”, because the null character at the end of “Jerry” doesn’t match the blank in “Jerry boy”. The value returned is -32, which is the value of null character minus the ASCII value of space, i.e., ‘\0’ minus ‘ ’, which is equal to -32. Programming in C 70
program to copy a string to another without using string handling function #i nc lu d e< s t dio . h> main() { char a[50], b[50]; int i=0; printf(“enter string1”); gets(a); for(i=0;a[i]!=‘\0’;i++) { b[i]=a[i]; } b[i]=‘\0’; } Programming in C 71
Program to compare 2strings(string matching) without using string handling function #include<stdio.h> #i nc l u d e < s tri n g.h> main() { char a[50], b[50]; int i=0, flag; printf(“enter string1”); gets(a); printf(“enter string2”); gets(b); for(i=0;a[i]!=‘\0’;i++) { if(a[i]!=b[i]) { fl a g = 1; break; } } if(flag==1) { printf(“strings are not equal”); } else printf(“strings are equal”); } Programming in C 72
2Dimensional array Programming in C 73
2D array declaration Datatype arrayname [rowcount][columncount]; Eg: int name[10][10]; float x[3][4]; Here, x is a two-dimensional (2d) array. The array can hold 12 elements.
2D array declaration and initialization int d [2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} }; Or int d [2][4] = { 10, 11, 12, 13, 14, 15, 16, 17}; Gives the matrix 10 11 12 13 14 15 16 17
Internal Memory representation The computer memory is an one-dimensional sequence of bytes. C c o m p il e r s t o r e s t h e t w o - d i m e n s i o n a l o bj ec t in row-major order in the memory JAIN STOBLE B 96
Things which you must consider while initializing 2D array Can omit first dimension But must specify the second dimension
e x amples int abc[2][2] = {1, 2, 3 ,4 } /* Valid declaration*/ int abc[][2] = {1, 2, 3 ,4 } /* Valid declaration*/ int abc[][] = {1, 2, 3 ,4 } /* Invalid declaration – you must specify second dimension*/ (stored as row major) int abc[2][] = {1, 2, 3 ,4 } /* Invalid because */ (stored as row major)
Reading elements into the matrix #include<stdio.h> void main() { int a[10][10],i,j,m,n; printf("enter nO: of rows"); scanf("%d",&m); printf("enter nO: of columns"); scanf("%d",&n); printf("enter elements"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } }}
Displaying elements in an array for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(" %d",a[i][j]); } printf("\n"); }
Sum of elem e nts of matrix #include<stdio.h> void main() { int a[10][10],i,j,m,n,sum=0; printf("enter nO: of rows"); scanf("%d",&m); printf("enter nO: of columns"); scanf("%d",&n); printf("enter elements"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } for( i =0;i< m;i ++) { for(j=0;j< n;j ++) { sum= sum+a [ i ][j]; } } printf ("\ nsum is % d",sum ); }
} Trace of a matrix(sum of main diagonal elements) for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(i==j) sum=sum+a[i][j]; }
} Transpose of a matrix for(i=0;i<m;i++) { for(j=0;j<n;j++) { trans[i][j]=a[j][i]; }
#include <stdio.h> int main() { int a[10][10], transpose[10][10], r, c, i, j; printf("Enter rows and columns of matrix: "); scanf("%d %d", &r, &c); // Storing elements of the matrix printf("\nEnter elements of matrix:\n"); for(i=0; i<r; ++i) for(j=0; j<c; ++j) { scanf("%d", &a[i][j]); } // Displaying the matrix a[][] */ printf("\nEntered Matrix: \n"); for(i=0; i<r; ++i){ for(j=0; j<c; ++j) { printf("%d ", a[i][j]); } printf("\n\n"); JAIN STOBLE B
// Finding the transpose of matrix a for(i=0; i<r; ++i) for(j=0; j<c; ++j) { transpose[j][i] = a[i][j]; } // Displaying the transpose of matrix a printf("\nTranspose of Matrix:\n"); for(i=0; i<c; ++i) { for(j=0; j<r; ++j) { printf("%d ",transpose[i][j]); } printf("\n\n"); } return 0; } JAIN STOBLE B
Row sum of a matrix //row sum calculation for(i=0;i<m;i++) { for(j=0;j<n;j++) { sum=sum+a[i][j]; } printf("\nsum of %d row is %d ",i,sum); sum=0; }
column sum of a matrix //column sum calculation for(i=0;i<m;i++) { for(j=0;j<n;j++) { sum=sum+a[j][i]; } printf("\nsum of %d column is %d ",i,sum); sum=0; }
Sum of two matrix(1) # i n clu d e< s t d io .h > void main() { int a[10][10],i,j,m,n,p,q,b[10][10],c[10][10]; printf("enter nO: of rows of 1st matrix"); scanf("%d",&m); printf("enter nO: of columns of 1st matrix"); scanf("%d",&n); printf("enter nO: of rows of 2nd st matrix"); scanf("%d",&p); printf("enter nO: of columns of 2nd matrix"); scanf("%d",&q);
Sum of two matrix(2) if (m==p && n==q) { printf("enter elements of first matrix"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } printf("enter elements of second matrix"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&b[i][j]); } }
Sum of two matrix(3) for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=a[i][j]+b[i][j]; } }
Sum of two matrix(4) printf("Added matrix is \n"); for(i=0;i<m;i++) } { for(j=0;j<n;j++) { printf(" %d",c[i][j]); } printf("\n"); } //closing of if else printf("matrix addition not possible"); }
Matrix multiplication Programming in C 92
} Matrix multiplication #include<stdio.h> void main() { int a[10][10], b[10][10], result[10][10], m,n,p,q, i, j, k; printf("Enter rows and column for first matrix: "); scanf("%d %d", &m, &n); printf("Enter rows and column for second matrix: "); scanf("%d %d",&p, &q); if(n != p) { printf("matrix multiplication not possible");
} Matrix multiplication(1) else { // Storing elements of first matrix. printf("\nEnter elements of matrix 1:\n"); for(i=0; i<m; ++i) for(j=0; j<n; ++j) { scanf("%d", &a[i][j]); } // Storing elements of second matrix. printf("\nEnter elements of matrix 2:\n"); for(i=0; i<p; ++i) for(j=0; j<q; ++j) { scanf("%d",&b[i][j]);
Matrix multiplication(2) // Multiplying matrices a and b and // storing result in result matrix for(i=0; i<m; ++i) for(j=0; j<q; ++j) for(k=0; k<n; ++k) { result[i][j]=result[i][j]+a[i][k]*b[k][j]; } // Displaying the result printf("\nOutput Matrix:\n"); for(i=0; i<m; ++i) for(j=0; j<q; ++j) { printf("%d ", result[i][j]); } printf("\n"); }}
JAIN STOBLE B 2 D Character array Declaration c har name [3][10]
JAIN STOBLE B Initialization of the character array char name[5][10]= { "tree", "bowl", "hat", "mice", "toon" }; The first subscript [5] represents the number of Strings in the array the second subscript [10] represents the length of each String . This is static memory allocation. We are giving 5*10=50 memory locations for the array elements to be stored in the array.
how the elements are stored in the memory location: JAIN STOBLE B
JAIN STOBLE B TAKING DATA INPUT FROM USER for(i=0 ;i<5 ;i++ ) scanf("% s",name [i]);
JAIN STOBLE B PRINTING THE ARRAY ELEMENTS for(i=0 ;i<5 ;i++) printf("%s\n",name[i]); The way a 2D character array is printed in not the same as a 2D integer array. If we display it in the same way as a 2D integer array we will get unnecessary garbage values in unoccupied spaces. This format will print only the string contained in the index numbers specified and eliminate any garbage values after ‘\0’.
Reverse string #include<stdio.h> # i n c l ud e < s tr in g . h > int main() { char str[100], temp; int i, j = 0; printf("\nEnter the string :"); scanf("%s",str); i = 0; j = strlen(str)-1; while (i < j) { temp = str[i]; str[i] = str[j]; str[j] = temp; i++; j--; } printf("\nReverse string is :%s", str); return (0); } Enter the string :hello Reverse string is :olleh JAIN STOBLE B 102
JAIN STOBLE B Convert string to uppercase # include <stdio.h> void main() { char s[100]; int c = 0; printf("Enter a string to convert it into upper case\n"); scanf("%s",s); while (s[c] != '\0') { if (s[c] >= 'a' && s[c] <= 'z') { s[c] = s[c] - 32; c++; } } printf("The string in upper case: %s\n", s); }
JAIN STOBLE B Convert string to lowercase #include <stdio.h> void main() { char s[100]; int c = 0; printf("Enter a string to convert it into upper case\n"); scanf("%s",s); while (s[c] != '\0') { if (s[c] >= 'A' && s[c] <= 'Z') { s[c] = s[c] + 32; c++; } } printf("The string in lower case: %s\n", s); }
Multidimensional arrays Multidimensional arrays are defined in much the same manner as one-dimensional arrays, except that a separate pair of square brackets is required for each subscript. Thus, a two-dimensional array will require two pairs of square brackets, a three- dimensional array will require three pairs of square brackets, and so on.
Several typical multidimensional array definitions float table[50][50]; char page[24][80]; Int records[100][66][255];
Initialization of a three dimensional array. You can initialize a three dimensional array in a similar way like a two dimensional array. Here's an example, int test[2][3][4] = { { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} }, { {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} } }; JAIN STOBLE B 125