Unit4 C Program
Unit4 C Program
Arrays:
Array is a collection of homogeneous elements stored in a common name. We can store and retrieve
elements in an array using index or subscript. In C array index starts from zero (0). Array data structure
allocates continuous memory spaces in memory.
int a[5];
char name[30];
Types of arrays:
a. Single-dimensional arrays
b. Tow-dimensional arrays
c. Multi-dimensional arrays
If only one index or subscript is required to store and retrieve elements in an array is called single
dimensional or one dimensional array. Like any other variable, array variable must be declared before they
are going to use in C program.
Syntax:
datatype arrayname[size];
Ex:
int a[5];
char name[30];
Static Array:
If the size of the array is specified in the declaration then the array is called static array.
Ex:
int a[10];
float avgs[35];
MS Page 1
UNIT – IV Programming in C
Dynamic Array:
If the size of the array is not specified in the declaration then the array is called dynamic array.
Ex:
int a[];
char str[];
After an array is declared, its elements must be initialized. Otherwise, they will contain “garbage”
value. We can initialize the elements of arrays in the same way as the ordinary variables are initialized.
Syntax:
datatype arrayname[size]={list of values};
Ex:
int vals[5]={12,45,67,85,6};
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int vals[5]={12,-56,89,25,69};
int i;
clrscr();
printf("\nYour array contains :\n");
for(i=0;i<5;i++)
printf("vals[%d] = %d\n",i,vals[i]);
getch();
}
MS Page 2
UNIT – IV Programming in C
Output:
vals[0] = 12
vals[1] = -56
vals[2] = 89
vals[3] = 25
vals[4] = 69
Write a C program to get n values using array and display its sum value.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,sum=0;
clrscr();
MS Page 3
UNIT – IV Programming in C
getch();
}
Output:
24 78 45 15 90
Your array values are :
24
78
45
15
90
Your sum value is : 252
Two dimensional Arrays:
If two indices or subscripts are required to store and retrieve elements in an array is called two
dimensional array. Two-dimensional array is generally used to represent matrix or table of data.
Syntax:
datatype arrayname[rowsize][columnsize];
Ex:
int mat[5][5];
Just like a single-dimensional arrays, two-dimensional arrays also can be initialized. They general
format of initialization of two-dimensional array is any one of the following form.
(or)
datatype arrayname[rowsize][colsize]={(row1),{row2},….{rown)};
MS Page 4
UNIT – IV Programming in C
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int x[3][3]={{0,0,0},{1,1,1},{2,2,2}};
int i,j;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",x[i][j]);
}
printf("\n");
}
getch();
}
Output:
0 0 0
1 1 1
2 2 2
MS Page 5
UNIT – IV Programming in C
#include<stdio.h>
#include<conio.h>
void main()
{
int mat1[5][5],mat2[5][5],add[5][5],i,j;
clrscr();
printf("Enter your first 3X3 matrix :\n");
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
scanf("%d",&mat1[i][j]);
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
add[i][j]=mat1[i][j]+mat2[i][j];
for(j=1;j<=3;j++)
{
printf(" %d",add[i][j]);
}
MS Page 6
UNIT – IV Programming in C
printf("\n");
}
getch();
Output:
Enter your first 3X3 matrix :
2 2 2 3 3 3 4 4 4
Enter your second 3X3 matrix :
4 1 1 1 2 2 2 3 3
Your addtion matrix is :
6 3 3
4 5 5
6 7 7
Multi-dimensional Arrays:
C allows arrays of three or more dimensions. The exact limit is determined by the compiler. The
general form of a multi-dimensional array is
Datatype arrayname[s1][s2][s3]…[sn];
Ex:
int cbook[200][20][30];
In the above example cbook is a three dimensional array. First index specify the page number of the
book, second index specify the line number in that particular page and the third index specify the character
in that line.
Structures:
We know that array can be used to group similar data items together. We can’t use array to represents
group of different data items.
C is having a constructed (user defined) data type known as structures. Structure can support the
mechanism of packing data of different types.
MS Page 7
UNIT – IV Programming in C
Defining structure:
struct struct_name
{
members declarations;
};
Example:
struct employee
{
int eno; /* member declaration */
char ename[30];
float sal ;
};
We can declare the structure using the keyword struct.
Body of the structure is enclosed with a pair of braces and terminated by a semicolon.
Variables declared inside of a structure is called members of the structure.
Syntax:
Ex:
We can access the structure members using the structure variable. The syntax of the accessing
members is given below.
MS Page 8
UNIT – IV Programming in C
structvar.membername=value;
Ex:
e1.eno=101;
e2.esal=5500;
Example:
#include<stdio.h>
#include<conio.h>
struct date
{
int dd;
int mm;
int yy; /* member declaration */
};
void main()
{
getch();
}
MS Page 9
UNIT – IV Programming in C
Output:
Structure initialization:
Like any other data type variable, structure type variable can be initialized at compile time. The
following example clearly the explain the concept of structure variable initialization. Note that the compile-
time initialization of a structure variable must have the following elements:
The order of values enclosed within braces must match the order of members inside the structure.
It is permitted to have a partial initialization. We can initialize only the first few members.
Example:
#include<stdio.h>
#include<conio.h>
struct studper
{
int rno;
char name[30];
float height,weight;
};
MS Page 10
UNIT – IV Programming in C
void main()
{
Output:
Arrays of structure:
Like any other built-in type variable, structure variable can be declared as an array type variable.
This process is called arrays of structure.
Syntax:
struct structname[size];
Ex:
struct e[3];
Array within a structure:
Structure member can be an array type of variable. This process is known as array within a structure.
Example:
#include<stdio.h>
#include<conio.h>
struct emp
{
MS Page 11
UNIT – IV Programming in C
int eno;
char names[30]; /* Array within structure */
};
void main()
{
struct emp e[5]; /* Arrays of structure */
int i;
clrscr();
printf("Enter 3 employees eno and name :\n");
for(i=1;i<=3;i++)
scanf("%d %s",&e[i].eno,e[i].names);
{
printf("\nEmp Code : %d",e[i].eno);
printf("\nEmp Name : %s\n",e[i].names);
}
getch();
}
Output:
MS Page 12
UNIT – IV Programming in C
Kumar
109
Sathish
Example:
#include<stdio.h>
#include<conio.h>
struct day
{
int dd;
int mm;
int yy;
};
MS Page 13
UNIT – IV Programming in C
struct empper
{
int eno;
char name[30];
struct day dob; /* structure within structure */
};
void main()
{
struct empper e1;
clrscr();
printf("Enter your eno,name :\n");
scanf("%d%s",&e1.eno,e1.name);
printf("Enter your dob (date, month and year ) :\n");
scanf("%d%d%d",&e1.dob.dd,&e1.dob.mm,&e1.dob.yy);
printf("\n Your detail is : \n");
Prabakar
Enter your dob (date, month and year ) :
7
3
MS Page 14
UNIT – IV Programming in C
1992
Your detail is :
Example:
#include<stdio.h>
#include<conio.h>
struct store
{
char item[30];
float price;
int itemid;
};
clrscr();
printf("\nEnter your item name,price and itemid :\n");
scanf("%s%f%d",s.item,&s.price,&s.itemid);
MS Page 15
UNIT – IV Programming in C
}
Output:
Before increment:
Item ID : 153
Item Name : Sugar
Price : 26.70
MS Page 16
UNIT – IV Programming in C
After increment :
Item ID : 153
Item Name : Sugar
Price : 36.70
Unions:
In structure each member has its own storage, whereas all the members of a union use the same location.
Unions are concept borrowed from structure. Like a structure a union can be declared using the keyword
union.
Syntax:
union unionname
{
Member declarations;
};
Ex:
union item
{
int m;
float x;
char c;
}code;
MS Page 17