Array in c language
Arrays are group of similar data types.It's used to store a large amount of data. The array elements are stored in contiguous memory locations.
In other words, if an array is declared to be of an int type,It can't contain elements that are not of int type.
How to declare an Array:
dataType variablename[size]
Example:- int ary[5];
10 20 30 40 50 ary[0] ary[1] ary[2] ary[3] ary[4]
10 | 20 | 30 | 40 | 50 |
---|---|---|---|---|
ary[0] | ary[1] | ary[2] | ary[3] | ary[4] |
In the above program ary is variable name which hold the 5 elements which have a same name, same data type but with different memory locations. As the memory is allocated sequentially so that data can be easily accessed by increment or decrement by single variable.Arrays always start with index value zero.
Advantages of arrays in c
1. Array is the simplest data structure.
2. It is easy to create.
3. we can use one name for similar identifier.
4. Arrays use reference type so we need not to use return statement.
Limitations
1. Array may require large amount of contiguous memory, which is not available sometimes.
2.Bound checking is not done by 'C' compiler. Means in ary[5] the index value should index value should be between 0 to 4 but if we write ary[7],ary[34] etc compiler does not show any error and display garbage value.
3. Overflow condition may occur in array if we want to store more values then its size.
ARRAY TYPES IN C
Array is two types
1) One dimensional array
2) Multidimensional array
1) One dimension array in c:-
A list of items have only one subscript and such a list is called one dimensional array.
Declaration of an array in c.
int a[5];
In the above example we defined an array a [5]. Here a is a variable name. Data type of a is integer and 5 is the size of an array. Size is always written in subscript []. This means that it can store only five elements. Index of a is start from 0 to 4.
Initialization of an array:-
we can also assign values to an array when we declare it.
int a[]={1,2,3,4,5};
compiler automatically sets the size of the array like in this case size of a is 5 and it requires 10bytes of memory[2.for each int]
int a[10]={1,2,3,4,5,6,7,8,9,10};
In this we assign values to the ten elements of a. We can access array elements with its index, for example, if we want to print 5th element from the list we use a[4].
Write a program to print five elements of an array.
#include< stdio.h >
#include< conio.h >
void main()
{
int a[]={1,4,13,18,11};
clrscr();
printf("\n%d",a[0]);
printf("\n%d",a[1));
printf("\n%d",a[2]);
printf("\n%d",a[3]);
printf("\n%d",a[4]);
getch();
}
In this program 1 is store at location 0,4 at location 1 similary others.
When we print a [0] it will display 1.
Output:-
1
4
13
18
11
Example simple program Array with for loop
One dimension array program
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[5];
clrscr();
for(i=0;i<5;i++)
{
printf("\n enter value of array");
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
printf("\na[%d]=%d",i,a[i]);
}
getch();
}
Output:-
enter value of array 1
enter value of array 2
enter value of array 3
enter value of array 4
enter value of array 5
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5
Two dimension or Multi dimension array program in c
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\nenter the value of array");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\na[%d][%d]=%d",i,j,a[i][j]);
}
}
getch();
}
output:-
enter the value of array 1
enter the value of array 2
enter the value of array 3
enter the value of array 4
enter the value of array 5
enter the value of array 6
enter the value of array 7
enter the value of array 8
enter the value of array 9
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
a[2][0]=7
a[2][1]=8
a[2][2]=9
Add Matrices array Simple Program in c
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],i,j;
printf("enter the value of a array");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the value of b array");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nadd matrix");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("\na[%d][%d]=%d+b[%d][%d]=%d=c[%d][%d]=%d",i,j,a[i][j],i,j,b[i][j],i,j,c[i][j]);
}
}
getch();
}
Multiply Two Matrices array Program in c
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],i,j,k;
clrscr();
printf ("enter 1st matrix");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter 2nd matrix");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d",c[i][j]);
}
printf("\n");
}
getch();
}
C Program to Print Number of Days in a Month
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int i;
char c;
char comp[20];
char name[12][10]={"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
clrscr();
do
{ fflush(stdin);
printf("plz enter month name only in 3 characters");
printf("plz enter month name only in 3 characters");
scanf("%s",&comp);
for(i=0;i<12;i++)
{
if(strcmp(comp,name[i])==0)
{
{
printf("\ndays%d",days[i]);
break;
}
}
}
if(i==12)
{printf("\nPlz enter correct month name");
}
printf("\ndo you want continue press y ");
fflush(stdin);
c=getche();
}while(c=='y');
getch();
}
String library function are the functions, which are used regularly and stored in library file and whenever these functions are need, you need to include header file in like as
#include<string.h>
These 7 inbuilt functions to perform different operation on string.
11) Strlen:- This function is used to count the characters in a string. It calculates the length of string.
Syntax: strlen(array variable);
Example of program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20];
int I;
clrscr();
printf(“enter string”);
scanf(“%s”,&s1);
i=strlen(s1);
printf(“length=%d”,i);
getch();
}
22) Strcpy:- This function copies the content of one string into another string.
Syntax:- strcpy(target,source)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20],s2[20];
int i;
clrscr();
printf(“enter string”);
scanf(“%s”,&s1);
strcpy(s2,s1);
printf(“string=%s”,s2);
getch();
}
33) Strcat : This function is used to concatenate the source string at end of the target string.
Syntax (target,sourcetoadd),
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20],s2[20];
printf(“enter string”);
scanf(“%s”,&s1);
printf(“enter string”);
scanf(“%s”,&s2);
strcat(s2,s1);
printf(“concatenate string=%s”,s2);
getch();
}
44) Strcmp: This function compares two strings to find out whether they are same or different. They two string are compared character by character until there is mismatch or end of the string. This function return 0 if both the strings are identical
Syntax
Strcmp(string1,string2);
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<string.h>
void main()
{
char s1[20],s2[20];
int i;
int i;
printf(“enter string”);
scanf(“%s”,&s1);
printf(“enter string”);
scanf(“%s”,&s2);
i=strcmp(s1,s2);
if(i==0)
printf(“both string are same”);
else
printf(“string are not same”);
getch();
}
55) Strrev() :- String reverse function is used to reverse the string. This function also contain a single argument
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[20];
printf(“enter the string”);
scanf(“%s”,&s);
strrev(s);
printf(“reverse string=%s”,s);
getch();
}
66) Strupr():- String upper case is used to convert the string in to upper case or in capital letter. This function contains single argument
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[20];
printf(“enter the string”);
scanf(“%s”,&s);
strupr(s);
printf(“upper string=%s”,s);
getch();
}
77) Strlwr():- String lower case is used to convert the string in to lower case or in small letter. This function contains single argument
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[20];
printf(“enter the string”);
scanf(“%s”,&s);
strlwr(s);
printf(“lower string=%s”,s);
getch();
}
88) atoi()[define in stdlib header file]:- This function come under <stdlib.h> header file. This function convert numeric string into integer.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
char s1[20];
int i;
printf(“enter numeric string”);
scanf(“%s”,&s1);
i=atoi(s1);
printf(“integer number i=%d”,i);
getch();
}
WAP to print length of string without using inbuilt function.
#include<stdio.h>
#include<conio.h>
void main()
{
char s[20];
int i=0;
printf("Enter the String");
WAP to print length of string without using inbuilt function.
#include<stdio.h>
#include<conio.h>
void main()
{
char s[20];
int i=0;
printf("Enter the String");
scanf("%s",&s);
while(s[i]!=NULL)
{
i++;
}
printf("length of string =%d",i);
getch();
}
while(s[i]!=NULL)
{
i++;
}
printf("length of string =%d",i);
getch();
}
0 comments:
Post a Comment