Unit 4
Unit 4
ARRAYS :
An array is a group of related data items that share a common name.
Ex:- Students
The complete set of students are represented using an array name students. A particular
value is indicated by writing a number called index number or subscript in brackets after array
name. The complete set of value is referred to as an array, the individual values are called elements.
A list of items can be given one variable index is called single subscripted variable or a
one-dimensional array.
The subscript value starts from 0. If we want 5 elements the declaration will be
int number[5];
This array is of type float. Its name is avg. and it can contains 50 elements only. The
range starting from 0 – 49 elements.
Initialization of Arrays :
Initialization of elements of arrays can be done in same way as ordinary variables are
done when they are declared.
If the number of values in the list is less than number of elements then only that elements will be
initialized. The remaining elements will be set to zero automatically.
The size may be omitted. In such cases, Compiler allocates enough space for all initialized
elements.
#include<stdio.h>
main()
int i;
float x[10],value,total;
for(i=0;i<10;i++)
scanf(“%f”,&value)
; x[i]=value;
total=0;
for(i=0;i<10;i+
+)
total=total+x[i]
for(i=0;i<10;i++)
printf(“x*%2d+=%5.2f\n”,I+1,x*I+);
printf(“total=%0.2f”,total);
To store tables we need two dimensional arrays. Each table consists of rows and
columns. Two dimensional arrays are declare as
#include<stdio.h>
#include<math.h>
#define ROWS 5
#define COLS 5
main()
int
row,cols,prod[ROWS][COLS];
int i,j;
printf(“Multiplication table”);
for(j=1;j< =COLS;j++)
printf(“%d”,j)
for(i=0;i<ROWS;i++
row = i+1;
printf(“%2d|”,row);
for(j=1;j < =
COLS;j++)
COLS=j;
printf(“%4d”,prod*i+*j+);
}
}
They can be initialized by following their declaration with a list of initial values
enclosed in braces.
Initializes the elements of first row to zero and second row to one. The initialization is done by
row by row. The above statement can be written as
When all elements are to be initialized to zero, following short-cut method may be used.
Store a single list of the element Store a ‘list of lists’ of the element of a similar
Definition of a similar data type. data type.
STRINGS(CHARACTER ARRAYS) :
A String is an array of characters. Any group of characters (except double quote sign
)defined between double quotes is a constant string.
A string variable is any valid C variable name and is always declared as an array.
char string name [size];
size determines number of characters in the string name. When the compiler assigns a character
string to a character array, it automatically supplies a null character (‘\0’) at end of String.
Therefore, size should be equal to maximum number of character in String plus one.
address[15];
scanf(“%s”,address);
The problem with scanf function is that it terminates its input on first white space it finds. So scanf
works fine as long as there are no spaces in between the text.
If we are required to read a line of text we use getchar(). which reads a single characters.
Repeatedly to read successive single characters from input and place in character array.
#include<stdio.h>
main()
{
char line[80],ano_line[80],character;
int c;
c=0;
scanf(“%s”, line);
do
character = getchar();
ano_line[c] = character;
c++;
- while(character
!=’\n’); c=c-1;
ano_line*c+=’\0’;
strcat() function:
strcat(string1,string2);
string1 = VERY
string2 = FOOLISH
strcat(string1,string2);
string1=VERY
FOOLISH string2 =
FOOLISH
strcmp() function :
This function compares two strings identified by arguments and has a value 0 if they are
equal. If they are not, it has the numeric difference between the first non-matching characters in
the Strings.
strcmp(string1,string2);
Ex:- strcmp(name1,name2);
strcmp(name1,”John”);
strcmp(“ROM”,”Ram”
);
strcpy() function :
strcpy(string1,string2);
strlen() function :
Counts and returns the number of characters in a string.
n= strlen(string);
/* Illustration of string-handling */
#include<stdio.h>
#include<string.h
>main()
char s1[20],s2[20],s3[20];
int X,L1,L2,L3;
scanf(“%s %s”,s1,s2);
X=strcmp(s1,s2)
; if (X!=0)
strcat(s1,s2);
}
else
strcpy(s3,s1);
L1=strlen(s1);
L2=strlen(s2)
;
L3=strlen(s3)
;
\n”,s3,L3);
STRUCTURES :
A Structure is a collection of elements of dissimilar data types. Structures provide the
ability to create user defined data types and also to represent real world data.
Suppose if we want to store the information about a book, we need to store its name
(String), its price (float) and number of pages in it(int). We have to store the above three items as
a group then we can use a structure variable which collectively store the information as a book.
Structures can be used to store the real world data like employee, student, person etc.
Declaration :
The declaration of the Structure starts with a Key Word called Struct and ends with ; .
The Structure elements can be any built in types.
Initialization :
Structure elements are stored in contiguous memory locations as shown below. The
above Structure occupies totally 26 bytes.
main()
{
struct emp
{
int empno;
char ename[20];
float sal;
};
struct emp e;
printf (“ Enter Employee number: \n”);
scanf(“%d”,&e.empno);
printf (“ Enter Employee Name: \n”);
scanf(“%s”,&e.empname);
printf (“ Enter the Salary: \n”);
scanf(“%f”,&e.sal);
printf (“ Employee No = %d”, e.empno);
printf (“\n Emp Name = %s”, e.empname);
printf (“\n Salary = %f”, e.sal);
}
/* Program to read Student Details and Calculate total and average using structures */
#include<stdio.h>
main()
{
struct stud
{
int rno;
char sname[20];
int m1,m2,m3;
};
struct stud s;
int tot;
float avg;
#include<stdio.h>
main()
{
struct item
{
int itemno;
char itemname[20];
float rate;
int qty;
};
struct item i;
float tot_amt;
ARRAY OF STRUCTURES :
To store more number of Structures, we can use array of Structures. In array of
Structures all elements of the array are stored in adjacent memory location.
main()
{
struct book
{
char name[20];
float price;
int pages;
};
struct book b[10];
int i;
for (i=0;i<10;i++)
{
print(“\n Enter Name, Price and Pages”);
scanf(“%s%f%d”, b[i].name,&b[i].price,&b[i].pages);
}
for (i i=0;i<10;i++)
printf(“ \n%s%f%d”, b[i].name,b[i].price,b[i].pages);
}
UNIONS :
Union, similar to Structures, are collection of elements of different data types. However,
the members within a union all share the same storage area within the computer‟s memory,
whereas each member within a Structure is assigned its own unique Storage area.
Structure enables us to treat a member of different variables stored at different places in
memory, a union enables us to treat the same space in memory as a number of differentvariables.
That is, a union offers a way for a section of memory to be treated as a variable of onetype on one
occasion and as a different variable of a different type on another occasion.
Unions are used to conserve memory. Unions are useful for applications involving multiple
members, where values need not be assigned to all of the members at any given time. An attempt
to access the wrong type of information will produce meaningless results.
The union elements are accessed exactly the same way in which the structure elements are
accessed using dot(.) operator.
The difference between the structure and union in the memory representation is as follows.
struct cx
int i;
char ch[2];
};
struct cx s1;
union ex
int i;
char ch[2];
union ex u;
u.i
u.ch[0] u.ch[0]
main()
{
union example
int i;
char ch[2];
};
union exaple u;
u.i = 412;
print(“\n u.ch*0+ =
%d”,u.ch*1+);
print(“\n u.ch*1+ =
%d”,u.ch*1+);
Output :
u.i = 512
u.ch[0] =
0u.ch[1]
=2
u.i = 562
u.ch[0] =
50u.ch[1]
=2
A union may be a member of a structure, and a structure may be a member of a union. And also
structures and unions may be freely mixed with arrays.
/* Program to illustrate union with in a Structure */
main()
{
union id
char color;
int size;
};
struct {
char supplier[20];
float cost;
union id desc;
}pant, shirt;
printf(“%d\n”, sizeof(union
shirt.desc.size = 12;