0% found this document useful (0 votes)
5 views17 pages

Unit4 C Program

C program notes for college students

Uploaded by

deepaamalan21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views17 pages

Unit4 C Program

C program notes for college students

Uploaded by

deepaamalan21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 17

UNIT – IV Programming in C

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:

There are three types of arrays. They are

a. Single-dimensional arrays
b. Tow-dimensional arrays
c. Multi-dimensional arrays

Single-dimensional or one dimensional array:

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[];

Initializing one dimensional array:

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:

Your array contains :

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();

printf("\nEnter your n vlaue:");


scanf("%d",&n);
printf("\nEnter your array values:\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);

printf("\nYour array values are :\n");


for(i=1;i<=n;i++)
{
printf("%d\n",a[i]);
sum+=a[i];
}
printf("\nYour sum value is : %d",sum);

MS Page 3
UNIT – IV Programming in C

getch();
}
Output:

Enter your n vlaue: 5


Enter your array values:

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];

Initializing two-dimensional array:

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.

datatype arrayname[rowsize][colsize]={list of values of values};

(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();

printf("\nYour two-dimensional array is:\n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",x[i][j]);
}

printf("\n");
}
getch();
}
Output:

Your two-dimensional array is:

0 0 0

1 1 1

2 2 2

MS Page 5
UNIT – IV Programming in C

Write a C program to add two matrices

#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]);

printf("\nEnter your second 3X3 matrix :\n");


for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
scanf("%d",&mat2[i][j]);

for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
add[i][j]=mat1[i][j]+mat2[i][j];

printf("\nYour addtion matrix is :\n");


for(i=1;i<=3;i++)
{

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.

Declaring structure variable:


After defining a structure we can declare variables of that type. A structure variable declaration is
same as the declaration of ordinary variable of any other data type. Without this variable we can’t access
the member of the structure.

Syntax:

struct structname var1,var2,…,varn;

Ex:

struct emp e1,e2,e3;

Accessing structure members:

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()
{

struct date d; /* structure variable */


clrscr();
d.dd=23;
d.mm=07; /* accessing structure members */
d.yy=1993;
printf("\nYour data of birth is :\n");
printf("\n%d / %d / %d",d.dd,d.mm,d.yy);

getch();
}

MS Page 9
UNIT – IV Programming in C

Output:

Your data of birth is :


23 / 7 / 1993

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:

1. The keyword struct.


2. The structure name.
3. The name of the structure type variable.
4. The assignment operator.
5. A set of values for the structure type variable separated by commas (,) and enclosed in braces.
6. A terminating semicolon.

Rules for initializing structures:

 We cannot initialize individual members inside the structure.

 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()
{

struct studper s1={100,"sathish",5.4,56}; /* structure initialization */


struct studper s2={104,"ranjith",5.8,60};
clrscr();
printf("\n %d %s %2.1f %3.1f",s1.rno,s1.name,s1.height,s1.weight);
printf("\n %d %s %2.1f %3.1f",s2.rno,s2.name,s2.height,s2.weight);
getch();
}

Output:

100 sathish 5.4 56.0


104 ranjith 5.8 60.0

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("\nYour employees are :\n");


for(i=1;i<=3;i++)

{
printf("\nEmp Code : %d",e[i].eno);
printf("\nEmp Name : %s\n",e[i].names);
}

getch();

}
Output:

Enter 3 employees eno and name :


107
Ramesh
108

MS Page 12
UNIT – IV Programming in C

Kumar
109
Sathish

Your employees are :

Emp Code : 107


Emp Name : Ramesh

Emp Code : 108


Emp Name :Kumar

Emp Code : 109


Emp Name : Sathish

Structure within structure:


Structure within structure means nesting of structures. Nesting of structure is allowed in C. It means
that a structure member can be another one structure type variable. The following example clearly explains
the concept of structure within structure.

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");

printf("\n Emp Number : %d",e1.eno);


printf("\n Emp Name : %s",e1.name);
printf("\n DOB : %d / %d / %d",e1.dob.dd,e1.dob.mm,e1.dob.yy);
getch();
}
Output:

Enter your eno,name :


101

Prabakar
Enter your dob (date, month and year ) :
7
3

MS Page 14
UNIT – IV Programming in C

1992

Your detail is :

Emp Number : 107


Emp Name : Prabakar
DOB : 7 / 3 / 1992

Passing structure to a function:


Like any other built-in data we can pass structure type data to a function. Similarly a function can return
structure type of value after its execution.

Example:

#include<stdio.h>
#include<conio.h>

struct store
{
char item[30];
float price;
int itemid;

};

struct store inc(struct store);


void main()
{
struct store s;

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

printf("\n Before increment :\n");


printf("\n Item ID : %d",s.itemid);
printf("\n Item Name : %s", s.item);

printf("\n Price : %5.2f",s.price);


s=inc(s);
printf("\n\n After increment :");
printf("\n Item ID : %d",s.itemid);
printf("\n Item Name : %s",s.item);
printf("\n Price : %5.2f",s.price);
getch();

struct store inc(struct store k)


{
k.price=k.price+10;
return k;

}
Output:

Enter your item name,price and itemid :


Sugar
26.70
153

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

You might also like