UNIT - 3 Noornilo Nafees
Array It is a collection of elements of same data type, that are referenced by a common name Types One-Dimensional array Two-Dimensional array Multi-Dimensional array Noornilo Nafees
One-Dimensional array Array Declaration Syntax: datatype array_name[size]; Example: int x[3]; x X[0] X[1] X[2] Noornilo Nafees
x X[0] X[1] X[2] Array initialization Syntax: data_type array_name[size]={variables}; Example: int x[3]={5,3,7}; x int a[5]; a[0]=10; a[1]=20; a[2]=30; a[3]=40; a[4]=50; X[0] X[1] X[2] 5 3 7 Int a[5]={10,20,30,40,50}; char a[6]=“Nafees”; Noornilo Nafees
//program to set values of array and display it #include<stdio.h> #include<conio.h> void main() { int a[5],i; clrscr(); a[0]=10; a[1]=20; a[2]=30; a[3]=40; a[4]=50; for(i=0;i<5;i++) printf("\nThe value in a[%d] is %d",i,a[i]); } getch(); OUTPUT The value in a[0] is 10 The value in a[1] is 20 The value in a[2] is 30 The value in a[3] is 40 The value in a[4] is 50 Noornilo Nafees
//program to get values of array from users and display it #include<stdio.h> #include<conio.h> void main() { int a[5],i; clrscr(); printf("Enter five values : \n"); for(i=0;i<5;i++) scanf("%d",&a[i]); } printf("\nThe value in a[%d] is %d",i,a[i]); getch(); OUTPUT Enter five values : 10 20 30 40 50 The value in a[0] is 10 The value in a[1] is 20 The value in a[2] is 30 The value in a[3] is 40 The value in a[4] is 50 Noornilo Nafees
//program to find maximum no in an array #include<stdio.h> #include<conio.h> void main() { int a[5],i,max; clrscr(); printf("Enter five values : \n"); for(i=0;i<5;i++) scanf("%d",&a[i]); } max=a[0]; for(i=1;i<5;i++) if(max<a[i]) max=a[i]; printf(“Maximum no is %d“,max); getch(); } OUTPUT Enter five values : 10 20 30 40 50 Maximum no is 50 Noornilo Nafees
//program to find sum of elements in an array #include<stdio.h> #include<conio.h> void main() { int a[5],i,sum=0; clrscr(); printf("Enter five values : \n"); for(i=0;i<5;i++) scanf("%d",&a[i]); } sum=sum+a[i]; printf(“The sum of elements in array is : %d”,sum); getch(); OUTPUT Enter five values : 1 2 3 4 5 The sum of elements in array is : 15 Noornilo Nafees
//program to sort number in ascending order #include<stdio.h> #include<conio.h> void main() { int a[5],i,j,t; clrscr(); printf("Enter five values : \n"); for(i=0;i<5;i++) scanf("%d",&a[i]); } for(j=i+1;j<5;j++) if(a[i]>a[j]) t=a[i]; a[i]=a[j]; a[j]=t; } printf(“Ascending order : \n”); for(i=0;i<5;i++) { printf(“%d\t”,a[i]); getch(); OUTPUT Enter five values : 40 10 30 50 20 Ascending order : 10 20 30 40 50 Noornilo Nafees
//program to search element in array(Linear Search) #include<stdio.h> #include<conio.h> void main() { int a[5]={10,20,30,40,50}; int key; clrscr(); printf("Enter search value:\n"); scanf("%d",&key); for(i=0;i<5;i++) if(key==a[i]) printf(“Value found”); break; } else { printf(“Value not found”); } getch(); OUTPUT Enter search value : 40 Value found Noornilo Nafees
Two-Dimensional array Array Declaration Syntax: data_type array_name[row_size] [col_size]; Example: int x[3][3]; Col 0 Col 1 Col 2 row 0 row 1 row 2 X[0][0] X[1][0] X[2][0] X[0][1] X[1][1] X[2][1] X[0][2] X[1][2] X[2][2] Noornilo Nafees
Array Initialization Syntax: data_type array_name[row_size] [col_size];={variables}; Example: int x[3][3]={1,50,2,75,8,4,9,33,77}; Col 0 Col 1 Col 2 row 0 row 1 row 2 1 50 2 75 8 4 9 33 77 Noornilo Nafees
//program to assign values to array and to display it #include<stdio.h> #include<conio.h> void main() { int a[3][3]={10,20,30,40,50,60,70,80,90}; int i,j; clrscr(); printf(“Values in array : \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) printf(“a[%d][%d] = %d\n”,i,j,a[i][j]); } getch(); OUTPUT Values in array : a[0][0]=10 a[0][1]=20 a[0][2]=30 a[1][0]=40 a[1][1]=50 a[1][2]=60 a[2][0]=70 a[2][1]=80 a[2][2]=90 Noornilo Nafees
//program to assign values to array from user and to display it #include<stdio.h> #include<conio.h> void main() { int a[3][3],i,j; clrscr(); printf(“Enter 9 values in array : \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf(“%d”,&a[i][j]); } printf(“Values in array : \n”); printf(“a[%d][%d] = %d\n”,i,j,a[i][j]); getch(); } OUTPUT Enter 9 values in array : 10 20 30 40 50 60 70 80 90 Values in array : a[0][0]=10 a[0][1]=20 a[0][2]=30 a[1][0]=40 a[1][1]=50 a[1][2]=60 a[2][0]=70 a[2][1]=80 a[2][2]=90 Noornilo Nafees
//program to implement Matrix addition #include<stdio.h> #include<conio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j; clrscr(); printf(“Enter values of Matrix A : \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf(“%d”,&a[i][j]); } printf(“Enter values of Matrix B : \n"); scanf(“%d”,&b[i][j]); for(i=0;i<3;i++) { for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; } printf(“Added Matrix\n”); printf(“%d\t”,c[i][j]); printf(“\n”); getch(); Noornilo Nafees
//program to implement Matrix addition OUTPUT Enter values of Matix A : 1 2 3 4 5 6 7 8 9 Enter values of Matix B : OUTPUT Addded Matrix 4 6 10 12 14 16 18 Noornilo Nafees
//program to implement Matrix multiplication #include<stdio.h> #include<conio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j,k; clrscr(); printf(“Enter values of Matix A : \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf(“%d”,&a[i][j]); } printf(“Enter values of Matix B : \n"); scanf(“%d”,&b[i][j]); for(i=0;i<3;i++) { for(j=0;j<3;j++) c[i][j]=0; for(k=0;k<3;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } printf(“Multiplied Matrix\n”); printf(“%d\t”,c[i][j]); printf(“\n”); getch(); Noornilo Nafees
//program to implement Matrix OUTPUT Enter values of Matrix A : 1 Enter values of Matrix B : OUTPUT Multiplied Matrix 1 1 1 1 1 1 Noornilo Nafees
//program to find transpose of Matrix #include<stdio.h> #include<conio.h> void main() { int a[3][3],i,j; clrscr(); printf(“Enter values in array : \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf(“%d”,&a[i][j]); } printf(“\nTranspose of given Matrix : \n”); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf(“%d\t”,i,j,a[j][i]); } printf(“\n”); } getch(); OUTPUT Enter values in array : 1 2 3 4 5 6 7 8 9 Transpose of given Matrix : 1 4 7 2 5 8 3 6 9 Noornilo Nafees
Strings Handling of Character Strings Syntax: char var_name[size]=“String”; Example char a[6]=“Cse”; #include<stdio.h> #include<conio.h> void main() { char a[6]; clrscr(); printf(“Enter a string : “); scanf(“%s”,a); printf(“Your String is : %s”,a); getch(); } OUTPUT Enter a string: cse Your String is : cse Noornilo Nafees
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. strcpy(string1,string2) strcat() It is used to combine two strings. strcat(string1,string2) Noornilo Nafees
It is used to compare two strings. syntax: strcmp(string1,string2) Returns 0 if two strings are equal. strrev() It used to reverse a string. strrev(string) strlwr(), strupr() It used to change the case of a string. strlwr(string) strupr(string) Noornilo Nafees
#include<stdio.h> #include<conio.h> strlen() It is used to find the length of the string. syntax: strlen(string) #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 Noornilo Nafees
#include<stdio.h> #include<conio.h> strcpy() It is used to copy one string to another. syntax: strcpy(string1,string2) #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]=“Cse"; char b[]="Dept"; clrscr(); strcpy(a,b); printf("\nThe string is %s",a); getch(); } Output: The string is Dept Noornilo Nafees
#include<stdio.h> #include<conio.h> strcat() It is used to combine two strings. syntax: strcat(string1,string2); #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]=“Cse"; char b[]="Dept"; clrscr(); strcat(a,b); printf("\nThe string is %s",a); getch(); } Output: The string is CseDept Noornilo Nafees
#include<stdio.h> #include<conio.h> strcmp() It is used to compare two strings. syntax: strcmp(string1,string2) Returns 0 if two strings are equal. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[10]=“csedept"; Output: char b[10]=“cse"; Strings are not equal int i; clrscr(); i=strcmp(a,b); if(i==0) printf(“Strings are equal”); else printf("Strings are not equal”); getch(); } Noornilo Nafees
strrev() It used to reverse a string. syntax: strrev(string); #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[]="Dept"; clrscr(); printf("\nThe reversed string is %s",strrev(a)); getch(); } Output: The string is tpeD Noornilo Nafees
#include<stdio.h> #include<conio.h> strlwr(), strupr() It used to change the case of a string. syntax: strlwr(string); strupr(string); #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[20]=“csedept"; 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 :csedept The string after conversion to uppercase :CSEDEPT The string after conversion to lowercase :csedept Noornilo Nafees
String Palindrome #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s1[15],s2[15]; printf("\nenter the string:"); scanf("%s",s1); strcpy(s2,s1); strrev(s1); Noornilo Nafees
The string is palindrome if(strcmp(s1,s2)==0) printf("\n The string is palindrome"); else printf("\n The string is not a palindrome"); getch(); } Output: enter the string: aba The string is palindrome Noornilo Nafees
Managing I/O Operations Noornilo Nafees
Input/Output Function Formatted Unformatted Input scanf() Output printf() Input getc() gets() getchar() getch() Output putc() puts() putchar() Noornilo Nafees
Formatted Input/Output C uses two functions for formatted input and output. Formatted input : reads formatted data from the keyboard. Formatted output : writes formatted data to the monitor. Noornilo Nafees
Formatted Input and Output Noornilo Nafees
Standard Output The standard output file is the monitor. Like the keyboard, it is a text file. When you need to display data that is not text, it must be converted into to the text before it is written to the screen. Noornilo Nafees
Format of printf Statement Noornilo Nafees
Formatted Input (scanf) The standard formatted input function in C is scanf (scan formatted). scanf consists of : a format string . an address list that identifies where data are to be placed in memory. scanf ( format string, address list ); (“%c….%d…..%f…..”, &a,….&i,…..,&x…..) Noornilo Nafees
Format of scanf Statement Noornilo Nafees
getchar() Example #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char x; printf("enter the character:"); x=getchar(); Noornilo Nafees
if(islower(x)) putchar(toupper(x)); else putchar(tolower(x)); getch(); } Output: enter the character : A a Noornilo Nafees
getc Example #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char x; printf("enter the character:"); x=getc(stdin); Noornilo Nafees
if(islower(x)) putc(toupper(x),stdout); else putc(tolower(x),stdout); getch(); } Output: enter the character:a A Noornilo Nafees
gets() Example #include <stdio.h> #include<conio.h> void main() { char c[80]; clrscr(); printf("Input a string:"); gets(c); Noornilo Nafees
printf("The string is:"); puts(c); getch(); } Output: Input a string:qwerty The string is:qwerty Noornilo Nafees
getch() Example #include <stdio.h> #include <conio.h> void main() { char c; clrscr(); printf("\nInput a character:"); c = getch(); Noornilo Nafees
printf("\nCharacter is:"); putch(c); getch(); } Output: Input a character : N Character : N Noornilo Nafees