Unit IV Array and Structure
Unit IV Array and Structure
1 of 16
This specification table provides general guidelines to assist students for their learning
and to teachers to teach and assess students with respect to attainment of Learning
Outcomes (LOs). The actual distribution of marks at different taxonomy levels (R, U and
A) in the question paper may vary from above table.
2 of 16
- Highest Memory Location
-
a[9]
a[8]
a[7]
a[6]
a[5]
a[4]
a[3]
a[2]
a[1]
a[0]
-
- 0 Memory Location
Figure 4.1: Memory Allocation for integer array a[10]
interest_rate[4]
interest_rate[3]
interest_rate[2]
interest_rate[1]
interest_rate[0]
-
- 0 Memory Location
Figure 4.2: Memory Allocation for float array interest_rate[5]
In C, index always starts with 0. So the last element of the array has
index one less than Size-of-array.
3 of 16
4.2.2 Initialization of One-Dimensional Array
Arrays can also be explicitly initialized at run-time. This can be done for
initializing large arrays. One example is shown here.
4 of 16
for(i=0;i<150;i++)
a[i]=i;
Using one-dimensional array we can store list of values. But many times
there is need of storing table of values. A table contains rows and columns (i.e.
two dimensions). So, a two-dimensional array can be used for storing such
values.
Syntax for declaring a two-dimensional array is given below
data-type array-name[no-of-rows][no-of-columns];
One example of declaration of two-dimensional array is shown below.
int m[3][3];
/* Here m is two-dimensional array (of type int) with 3 rows (0, 1
and 2) and 3 columns (0, 1 and 2) */
Generally, two-dimensional arrays are stored in memory with a row-major
technique. i.e. They are stored row-wise. Initially, all the elements of 0th row are
stored then all the elements of 1st row are stored and so on. Figure 3.3 shows the
array m[3][3] in a matrix form and figure 3.4 shows how memory gets allocated
to that array.
Column
0 1 2
0 a[0][0] a[0][1] a[0][2]
Row 1 a[1][0] a[1][1] a[1][2]
2 a[2][0] a[2][1] a[2][2]
Figure 4.3: Matrix representation of integer array m[3][3]
- Highest Memory Location
-
m[2][2]
m[2][1]
m[2][0]
m[1][2]
m[1][1]
m[1][0]
m[0][2]
m[0][1]
m[0][0]
-
- 0 Memory Location
Figure 4.4: Memory Allocation for integer array m[3][3]
5 of 16
4.2.4 Initialization of Two-Dimensional Array
Once an array has been declared, its individual element can be accessed
by using array-name and index value that specifies the position of the element in
the array. In C, index values start from 0.
Values can be read from user through keyboard and stored in individual
array element using scanf( ) function as shown in following examples.
scanf(“%d”,&a[3]);
6 of 16
/* A integer value will be read from user and stored at 3rd location in
the array a. */
scanf(“%f”,&b[i]);
/* A float value will be read from user and stored at i th location in
the array a. Here i should be integer variable. */
scanf(“%d”,&a[2][1]);
/* A integer value will be read from user and stored at 1 st location in
the 2nd row of array a. */
Values of array elements can be displayed using printf( ) function as
shown in following example.
printf(“%d %f %d”,a[3],b[i],c[2][1]);
/* This statement will display following values
Value of 3rd element of integer array a
Value of ith element of float array b (i should be integer variable)
Value of 1st location in 2nd row of integer array c */
We can assign values to array elements as shown in following examples.
b[6]=34.7;
/* Value 34.7 is assigned to 6th element of float array b. */
a[i]=7;
/* Value 7 will be assigned to ith element of array a. Here i should be
integer variable. */
m[0][2]=62;
/* Value 62 will be assigned to 2nd location in the 0th row of array m.
*/
We can create character arrays just like any other data types. One
example is shown below.
char name[10];
Character arrays can be initialized as shown in following examples.
char name[10]={„V‟,„e‟,„n‟,„k‟,„a‟,„t‟,„e‟,„s‟,„h‟,„\0‟};
/* Here name is declared as array of 10 characters and is initialized
to a string “Venkatesh” ending with a null character („\0‟). */
char first_name[ ]={„S‟,„a‟,„c‟,„h‟,„i‟,„n‟,„\0‟};
/* Here first-name is declared as array of 7 characters and is
initialized to a string “Sachin” ending with a null character („\0‟). */
char first_name[ ]=“Sachin”;
/* Effect is same as that of previous example. */
char last_name[10]={„T‟,„e‟};
7 of 16
/* The values „T‟ and „e‟ are initialized to array elements with index
0 and 1. Remaining elements are initialized to null values („\0‟). */
Just like numeric arrays, string variables (i.e. character arrays) can also
be initialized at the time of declaration.
char last_name[10]={„P‟,„a‟};
/* The values „P‟ and „a‟ are initialized to array elements with index
0 and 1. Remaining elements are initialized to null values („\0‟). */
8 of 16
Figure 3.5 shows how the memory is allocated to the string variable name
and how the characters are stored in it.
A string can be read by using different ways. A string can be read by using
following functions.
- scanf( )
- getchar( )
- gets( )
9 of 16
The above code executes differently according to input as shown below,
i) If user enters a string “Deo”, it will get stored followed by null
character.
ii) If user enters a string “Tendulkar”, the string “Tendulk” will get stored
followed by null character (as width is mentioned as 7).
iii) If user enters a string “Kolhe Patil”, the string “Kolhe” will get stored
followed by null character and the string “Patil will be carried forward
for next input. (As after characters „K‟, „o‟, „l‟, „h‟ and „e‟ there is a white
space.)
Even after specifying width of input field, scanf( ) function terminates its
input when white space is received. We can read a whole line containing any
characters using scanf( ) function as shown in following example (4th example
shown in table 1.16 is also valid).
char text[80]; /*As single line may contain 80 characters. */
/* we may have less or more than 80 characters also. */
scanf(“%[^\n]”,text);
Examples:
1. Student: roll, name, branch
2. Employee: ID, name, department, salary
3. Date: day, month, year
4. Time: seconds, minutes, hours
5. Bank_Customer: account_number, name, mobile, balance
11 of 16
4.5.1 Defining structure
struct struct-name
{
data-type membername;
---
---
};
Example 1:
struct student
{
int roll;
char name[15];
float percent;
};
Example 2:
struct date
{
short day;
short month;
int year;
};
Examples:
struct student s1;
struct date b_date;
12 of 16
4.5.3 Accessing members of structure
Examples:
b_date.day=26;
scanf(“%d”,&admission_date.year);
Just like any other variable, structure can also be initialized while
creating a structure variable. Some examples are shown below.
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[20];
float percent;
};
main( )
{
struct student s1,s2;
clrscr( );
13 of 16
printf(“Enter data of second student:\n”);
printf(“Enter roll number: ”);
scanf(“%d”,&s2.roll);
printf(“Enter name: ”);
scanf(“%s”,s2.name);
printf(“Enter percent: ”);
scanf(“%f”,&s2.percent);
getch( );
}
One may use a structure within another structure. The concept is called
nested structure.
Example:
struct date
{
short d;
short m;
int y;
};
struct student
{
int roll;
char name[20];
struct date admission_date; //variable of another structure
};
Example:
struct student s[75];
14 of 16
4.6.1 typedef
C supports a type-definition feature that allows user to define his/ her own
data type identifier. This user-defined identifier can then be further used for
declaring variables. This can be achieved by using following syntax.
typedef type identifier;
e.g.
typedef int my_int;
Then the variables can be declared by using the user-defined identifier (in
above example it is my_int) as,
my_int x,y,z;
It helps to create our own data type and define possible values that a
variable of that data type can take.
Example:
enum gender
{
male,female,other
};
When we create enum variable, it can accept any one value from the
listing only.
Sample Questions
15 of 16
11. Write a program to sort elements of an array in ascending order. [6M]
12. Define array. Write a program to accept ten numbers in array. Sort array
elements and display them. [6M]
13. With suitable example, explain how two-dimensional arrays can be
created. [4M]
14. Write a program for addition of two 3X3 matrices. [6M]
15. Declare structure student with elements roll-no and name. [2M]
16. Write a program to declare structure employee having data member name,
age, street and city. Accept data for two employees and display it. [6M]
17. Write a program to declare structure student having rollno, name and
marks. Accept and display data for three students. [4M]
18. Develop a program using structure to print data of three students having
data members name, class and percentage. [4M]
19. Give a method to create, declare and initialize structure. Develop a
program to demonstrate nested structure. [6M]
16 of 16