Unit - 3
Unit - 3
1
Array
• Array is defined as the collection of similar type of data items
stored at contiguous memory locations. that are referenced by a
common name.
• Instead of that, we can define an array which can store the marks
in each subject at the contiguous memory locations.
2
Advantage of Array
3
Disadvantage of C Array
Fixed Size:
Whatever size, we define at the time of declaration
of the array, we can't exceed the limit.
4
Types
One-Dimensional array(1-D)
Two-Dimensional array(2-D)
5
How to declare an array
• Syntax:
datatype array_name[size];
• Syntax:
data_type array_name[size]={values};
7
Example
8
#include<stdio.h>
void main()
{
int a[5]={10,20,30,40,50},i;
clrscr();
for(i=0;i<5;i++)
{
printf("\nThe value in a[%d] is %d",i,a[i]);
}
getch();
}
OUTPUT
14
Two-Dimensional array
Array Declaration
• Syntax:
data_type array_name[row_size] [col_size];
Example: int x[3][3];
row 0 1 50 2
row 1 75 8 4
row 2 9 33 77
16
//program to assign values to array and to display it
#include<stdio.h>
#include<conio.h>
void main() OUTPUT
{ Values in array :
int a[3][3]={10,20,30,40,50,60,70,80,90}; a[0][0]=10
int i,j; a[0][1]=20
clrscr(); a[0][2]=30
printf(“Values in array : \n"); a[1][0]=40
for(i=0;i<3;i++) a[1][1]=50
{ a[1][2]=60
for(j=0;j<3;j++) a[2][0]=70
{ a[2][1]=80
printf(“a[%d][%d] = %d\n”,i,j,a[i][j]); a[2][2]=90
}
}
getch();
}
17
//program to assign values to array from user and to display it
#include<stdio.h>
#include<conio.h>
OUTPUT
void main()
Enter 9 values in array :
{
10
int a[3][3],i,j;
20
clrscr();
30
printf(“Enter 9 values in array : \n");
40
for(i=0;i<3;i++)
50
{
60
for(j=0;j<3;j++)
70
{
80
scanf(“%d”,&a[i][j]);
90
}
Values in array :
}
a[0][0]=10
printf(“Values in array : \n”);
a[0][1]=20
for(i=0;i<3;i++)
a[0][2]=30
{
a[1][0]=40
for(j=0;j<3;j++)
a[1][1]=50
{
a[1][2]=60
printf(“a[%d][%d] = %d\n”,i,j,a[i][j]);
a[2][0]=70
}
a[2][1]=80
} getch(); a[2][2]=90 18
}
//program to implement Matrix addition
#include<stdio.h> for(i=0;i<3;i++)
#include<conio.h> {
void main() for(j=0;j<3;j++)
{ {
int a[3][3],b[3][3],c[3][3],i,j;
c[i][j]=a[i][j]+b[i][j];
clrscr();
printf(“Enter values of Matrix A : \n"); }
for(i=0;i<3;i++) }
{ printf(“Added Matrix\n”);
for(j=0;j<3;j++) for(i=0;i<3;i++)
{ {
for(j=0;j<3;j++)
scanf(“%d”,&a[i][j]); {
} printf(“%d\t”,c[i][j]);
} }
printf(“Enter values of Matrix B : \n"); printf(“\n”);
for(i=0;i<3;i++) }
{ getch();
for(j=0;j<3;j++) }
{
scanf(“%d”,&b[i][j]);
}
}
19
//program to implement Matrix addition
OUTPUT
Enter values of Matix A :
1
2
3
4
5
6
7 OUTPUT
8 Addded Matrix
9 2 4 6
Enter values of Matix B : 8 10 12
1 14 16 18
2
3
4
5
6
7
8
9
20
//program to implement Matrix multiplication
#include<stdio.h> for(i=0;i<3;i++)
#include<conio.h> {
void main() for(j=0;j<3;j++)
{ {
int a[3][3],b[3][3],c[3][3],i,j,k;
c[i][j]=0;
clrscr();
printf(“Enter values of Matix A : \n"); for(k=0;k<3;k++)
for(i=0;i<3;i++) {
{ c[i][j]=c[i][j]+a[i][k]*b[k][j];
for(j=0;j<3;j++) }
{ }
scanf(“%d”,&a[i][j]); }
} printf(“Multiplied Matrix\n”);
} for(i=0;i<3;i++)
printf(“Enter values of Matix B : \n"); {
for(i=0;i<3;i++) for(j=0;j<3;j++)
{ {
for(j=0;j<3;j++) printf(“%d\t”,c[i][j]);
{
}
printf(“\n”);
scanf(“%d”,&b[i][j]); }
} getch();
} }
21
//program to implement Matrix
OUTPUT
Enter values of Matrix A :
1
1
1
1
1
1
1 OUTPUT
1 Multiplied Matrix
1 1 1 1
Enter values of Matrix B : 1 1 1
1 1 1 1
1
1
1
1
1
1
1
1
22
//program to find transpose of Matrix
#include<stdio.h> for(i=0;i<3;i++)
#include<conio.h> {
void main() for(j=0;j<3;j++)
{ {
printf(“%d\t”,i,j,a[j][i]);
int a[3][3],i,j; } printf(“\n”);
clrscr(); }
printf(“Enter values in array : \n"); getch();
for(i=0;i<3;i++) } OUTPUT
{ Enter values in array :
for(j=0;j<3;j++) 1
{ 2
scanf(“%d”,&a[i][j]); 3
} 4
} 5
printf(“\nTranspose of given Matrix : \n”); 6
7
8
9
Transpose of given Matrix :
1 4 7
2 5 8
3 6 9 23
string
STRING
String is a sequence of characters enclosed
within double quotes.
Ex:”Hello World“
String Declaration
Method:1
create a string as "Scaler" where the last character must always be a
null character.
Ex: char company[7] = {'S', 'C', 'A', 'L', 'E', 'R' , '\0'};
Method:2
In this method, we do not need to put the null
character at the end of the string constant. The compiler
automatically inserts the null character at the end of the
string.
28
• strcmp()
syntax:
strcmp(string1,string2)
– Returns 0 if two strings are equal.
• strrev()
syntax:
strlwr(string)
strupr(string)
29
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 30
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
31
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
32
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();
} 33
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
34
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
35
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);
36
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
37