Arrays in C
Arrays in C
Data types
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf("Enter Array Elements: ");
for (i=0;i<5;i++)
{
scanf("%d ",&a[i]);
}
printf("\n Array Elements: ");
for (i=0;i<5;i++)
{
printf("%d ",a[i]);
}
getch();
}
Can you imagine how long we have to write the
declaration part by using normal variable
declaration?
– int main(void)
– {
• int studMark1, studMark2, studMark3, studMark4, …, …,
studMark998, stuMark999, studMark1000;
• …
• …
• getch();
– }
#include<stdio.h>
#include<conio.h>
void main()
{
int roll_no[5]={10,20,30,40,50};
printf("%d",roll_no[0]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char alpha[5]={'X','N','T','U','Z'};
clrscr();
printf("%c",alpha[2]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
float perc[5]={37.85,45.67,31.29,51.67,78.23};
clrscr();
printf("%f\t""%f",perc[2],perc[3]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char alpha[5],i;
clrscr();
printf("Enter Array Elements: ");
for (i=0;i<5;i++)
{
scanf ("%c",&alpha[i]);
}
printf ("\n Array Elements: ");
for (i=0;i<5;i++)
{
printf("%c\t",alpha[i]);
}
getch();
}
Q. Program to Display Five Decimal values using Array datatypes (run time).
#include<stdio.h>
#include<conio.h>
void main()
{
float alpha[5],i;
clrscr();
printf("Enter Array Elements: ");
for (i=0;i<5;i++)
{
scanf ("%f",&alpha[i]);
}
printf ("\n Array Elements: ");
for (i=0;i<5;i++)
{
printf("%f\t",alpha[i]);
}
getch();
}
A variable which represents the list of items using two index (subscript) is
called two-dimensional array.
Two-dimensional array is known as matrix.
In Two dimensional arrays, the data is stored in rows and columns format.
2-d array is a collection of 1-D array placed one below the other.
Its syntax is:
Data-type array name[row][column];
type array-name[row_size][column_size];
Here the type specifies the data type of elements contained in the array, such as int, float, or
char.
The size indicates the size of number of rows and number of columns.
If the values are missing in an initializer, they are automatically set to zero.
Example:
int table[2][3] = {1,1,2};
Here first row initializes to 1,1 and 2 and second row initialize to 0,0 and 0 automatically.
In Two dimensional arrays, the data is stored in rows and columns format.
For example:
int table[2][3] = {1,2,3,4,5,6};
The memory layout of above example:
table[0][0] = 1;
table[0][1] = 2;
table[0][2] = 3;
table[1][0] = 4;
table[1][1] = 5;
table[1][2] = 6;
-Columns
#include<stdio.h>
#include<conio.h>
void main()
{
int s[2][3]={
{7,8,9},
{6,5,4}
};
int i,j;
clrscr();
for (i=0;i<=1;i++)
{
printf ("\n");
for(j=0;j<=2;j++)
printf("%d\t",s[1][2]);
}
}
#include<stdio.h>
#include<conio.h>
void main()
{
char s[2][3]={
{'A','W','T'},
{'R','V','L'}
};
char i,j;
clrscr();
for (i=0;i<=1;i++)
{
printf ("\n");
for(j=0;j<=2;j++)
printf("%c\t",s[1][1]);
}
}
#include<stdio.h>
#include<conio.h>
void main()
{
float s[2][3]={
{1.1,2.5,6.7},
{2.1,6.3,4.1}
};
float i,j;
clrscr();
for (i=0;i<=1;i++)
{
printf ("\n");
for(j=0;j<=2;j++)
printf("%f\t",s[0][1]);
}
}
#include<stdio.h>
#include<conio.h> printf("Two D array elements are\n");
int main() for (i=0;i<2;i++)
{ {
int a[2][3],i,j; for (j=0;j<3;j++)
printf("Enter array element"); {
for(i=0;i<2;i++) printf("%d",a[i][j]);
{ }
for (j=0;j<3;j++) printf("\n");
{ }
printf("\nelement a[%d][%d]= ",i,j); return 0;
scanf("%d",&a[i][j]); }
}
}
#include<stdio.h>
#include<conio.h> printf("Two D array elements are\n");
void main() for (i=0;i<2;i++)
{ {
float a[2][3],i,j; for (j=0;j<3;j++)
printf("Enter array element"); {
for(i=0;i<2;i++) printf("%f",a[i][j]);
{ }
for (j=0;j<3;j++) printf("\n");
{ }
printf("\nelement a[%f][%f]= ",i,j); getch();
scanf("%f",&a[i][j]); }
}
}
#include<stdio.h> fflush(stdin);
#include<conio.h> scanf("%c",&a[i][j]);
int main() }
{ }
char a[2][3]; printf("Two D array elements are\n");
int i,j; for (i=0;i<2;i++)
clrscr(); {
printf("Enter array element"); for (j=0;j<3;j++)
for(i=0;i<2;i++) {
{ printf("%c\t",a[i][j]);
for (j=0;j<3;j++) }
{ printf("\n");
printf("\nelement a[%d][%d]= ",i,j); }
getch();
return 0;
}
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
clrscr();
int a[2][2][2]={{{1,2},{3,4}},{{5,6},{7,8}}};
x=a[1][1][0];
y=(*(*(*(a+1)+1)+0));
printf("%d %d",x,y);
return 0;
}
c s t r i n g \0
• char string_name[size];
G O O D \0 \0 \0 \0 \0 \0
#include<stdio.h>
int main ()
{
char name[30]="Rohit";
printf("%s",name);
}
#include<stdio.h>
#include<conio.h>
int main ()
{
char name[30];
clrscr();
printf("enter name:");
scanf("%s",name);
printf("%s",name);
}
Q. Example of String displaying the name. (Run-Time)
#include<stdio.h>
#include<conio.h>
int main ()
{
char name[30];
clrscr();
printf("enter name:");
gets(name);
printf("%s",name);
}
• char a[10];
• scan(“%5s”,a);
If input is RAM, then string stored is as follows:
R A M \0 \0 \0 \0 \0 \0 \0
• If input is KRISHNA,then string stored will be
K R I S H \0 \0 \0 \0 \0
#include<stdio.h>
#include<conio.h>
int main()
{
char name[30];
clrscr();
printf("enter name:");
scanf("%3s",name);
printf("%s",name);
return 0;
}
%20.10s
N E W D E L H I
• %-20.10s
N E W D E L H I
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
Q. Example based on %ws in printf
#include<stdio.h>
#include<conio.h>
int main()
{
char name[30];
clrscr();
printf("enter name:");
scanf("%s",name);
printf("%-10.5s",name);
return 0;
}
#include<stdio.h>
#include<conio.h>
int main()
{
char name[30];
clrscr();
printf("enter name:");
scanf("%s",name);
printf("%10.5s",name);
return 0;
}
#include<stdio.h>
#include<conio.h>
int main ()
{
char name[30];
clrscr();
printf("enter name:");
scanf("%s",name);
printf("%s",name);
}
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
• The scanf() function reads the sequence of characters
until it encounters space .
• Syntax:
gets(stringname);
#include<stdio.h>
#include<conio.h>
int main ()
{
char name[30];
clrscr();
printf("enter name:");
gets(name);
printf("%s",name);
}
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
puts() function
#include<stdio.h>
#include<conio.h>
int main ()
{
char name[30];
clrscr();
printf("enter name:");
gets(name);
puts(name);
puts(name);
}
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
String Handling Functions in C:
• C library supports a large number of string handling
functions that can be used to manipulate(perform
different operations) strings.
• string handling functions are defined under "string.h"
header file.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int count=0;
char name[30];
clrscr();
printf("Enter name: ");
gets(name);
count=strlen(name);
puts(name);
printf("string length is: %d",count);
return 0;
} Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
strlen()
It counts and returns No.of Characters in a string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int count=0,i=0;
char name[30];
clrscr();
printf("Enter name: "); puts(name);
gets(name); printf(“Length of string is:%d",count);
}
while (name[i]!='\0')
{
count++;
i++;
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
}
strcpy()
• strcpy(string1,string2);
• The strcat( ) function joins two strings and result is returned to first_string.
• strcat(string1,string2);
#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[30]="Krishna";
char s2[6]=" Patil";
clrscr();
strcat (s1,s2);
printf (“String after cancatenation is: %s\n",s1);
puts(s2); Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
}
strcmp()