Types of Arrays:
(i) Single dimensional array
(ii) Multi dimensional array
Singledimentional Arrays:
#include<stdio.h>
#include<conio.h>
#define size 5
void main()
{
int array[size],i;
clrscr();
printf(“Enter 5 integers\n\n”);
for(i=0;i<size;i++)
{
printf(“Enter integer %d”,i+1);
scanf(“%d”,&array[i]);
}
for(i=0;i<5;i++)
{
printf(“\n Integer %d:%d\n”,i+1,array[i]);
}
getch();
}
Initializing Arrays:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[4]={4,3,2,1};
clrscr();
clrscr();
for(i=0;i<4;i++)
printf(“a[%d]=%d\n”,i,a[i]);
getch();
}
Multidimensional array:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20], b[20][20], c[20][20],r,c,i,j;
clrscr();
printf(“Enter rows and columns value:”);
scanf(“%d%d”,&r,&c);
printf(“Enter 1st matrix value:\n”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Enter 2nd matrix value:\n”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf(“%d”,&b[i][j]);
}
}
printf(“Addition matrix:\n”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
c[i][j] = a[i][j] + b[i][j];
printf(“%d\t”,c[i][j]);
}
}
printf(“\n”);
}
getch();
}
#include<stdio.h>
#include<conio.h>
#define MAX_ROWS 20
#define MAX_COLS 30
void main()
{
int a[MAX_ROWS][MAX_COLS], b[MAX_ROWS][MAX_COLS], c[MAX_ROWS][MAX_COLS];
int tot_row, tot_col, row, col;
clrscr();
printf(“How many rows?”);
scanf(“%d”,&tot_row);
printf(“How many columns?”);
scanf(“%d”,&tot_col);
printf(“Input for first table\n”);
for(row=0;row<tot_row;row++)
{
printf(“Enter the data row no. %d”,row+1);
for(col=0;col<tot_col;col++)
{
scanf(“%d”,&a[row][col]);
printf(“\t”);
}
}
printf(“Input for second table\n”);
for(row=0;row<tot_row;row++)
{
printf(“Enter the data row no. %d”,row+1);
for(col=0;col<tot_col;col++)
{
scanf(“%d”,&b[row][col]);
printf(“\t”);
}
}
for(row=0;row<tot_row;row++)
{
for(col=0;col<tot_col;col++)
{
c[row][col]=a[row][col]*b[row][col];
}
}
printf(“Sums of elements;\n”);
for(row=0;row<tot_row;row++)
{
for(col=0;col<tot_col;col++)
{
printf(“%d\t”,c[row][col]);
}
}
getch();
}
Initialization of character arrays:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[5]=”hari”;
int len;
clrscr();
printf(“Name = %s\n”,a);
len=strlen(a);
printf(“\n Length=%d”,len);
getch();
}
Two-dimensional character array:
#include<stdio.h>
#include<conio.h>
void main()
{
char month[ ][30] = {“January”, “February”, “March”, ”April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
int mon_num;
clrscr();
printf(“Enter the month number:”);
scanf(“%d”,&mon_num);
if((mon_num>=0) && (mon_num<12))
{
printf(“\n Month corresponding to number %d is %s”,mon_num,month[mon_num-1]);
}
else
printf(“Invalid month”);
getch();
}
Null Terminated Strings:
Functions:
#include<string.h>
Strcpy(s2,s1);
Strcat(s1,s2);
Strcmp(s1,s2);
Strchr(s1,ch);
Strcmpi(s1,s2);
M=strlen(s1);
Strlwr(s1);
Strupr(s1);
Strrev(s1)
Strstr(s1,s2);
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[30],str[30];
int len;
clrscr();
printf(“Enter your name:”);
gets(name);
strcpy(str,name);
printf(“The copied string is:%s”,str);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[30];
int len;
clrscr();
printf(“Enter your name:”);
gets(name);
len=strlen(name);
strrev(name);
printf(“The length of the string is:%d\n”,len);
printf(“The reversed string is:%s”,name);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[30];
clrscr();
printf(“Enter your name:”);
gets(name);
printf(“The uppercase string is\n”,strupr(name));
printf(“The lowercase string is\n”,strlwr(name));
getch();
}
No comments:
Post a Comment