Lecture Six
Lecture Six
An array is a collection of data elements of the same data type. It is described by a single name
and each element of an array is referenced by using the array name and its index number.
Declaration of Array
Type arrayName[numberOfElements];
For example,
int Age[5] ;
float cost[30];
An array can be initialized along with the declaration. For array initialization, it is required to
place the elements separated by commas enclosed within braces.
int A[5] = {11,2,23,4,15};
It is possible to leave the array size open. The compiler will count the array size.
int B[] = {6,7,8,9,15,12};
In any point of a program in which an array is visible, we can access the value of any of its
elements individually as if it was a normal variable, thus being able to both read and modify its
value. The format is as simple as:
name[index]
Examples:
printf("%d", age[4]); /* print an array element */
age[4] = 55; /* assign value to an array element */
scanf("%d", &age[4]); /* input element */
Arrays as Parameters
#include <stdio.h>
Two-Dimensional Array
It is a collection of data elements of the same data type arranged in rows and columns (that is, in
two dimensions).
Type arrayName[numberOfRows][numberOfColumns];
For example,
int Sales[3][5];
A two-dimensional array can be initialized along with the declaration. For two-dimensional array
initialization, elements of each row are enclosed within curly braces and separated by commas.
All rows are enclosed within curly braces.
int A[4][3] = {{22, 23, 10},
{15, 25, 13},
{20, 74, 67},
{11, 18, 14}};
To access the elements of a two-dimensional array, we need a pair of indices: one for the row
position and one for the column position. The format is as simple as:
name[rowIndex][columnIndex]
Examples:
printf("%d", A[1][2]); /* print an array element */
A[1][2]=13; /* assign value to an array element */
scanf("%d", &A[1][2]); /* input element */
int A[3][5], i, j ;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 5; j++)
{
scanf("%d", &A[i][j];
}
}
Arrays as Parameters
Two-dimensional arrays can be passed as parameters to a function, and they are passed by
reference. When declaring a two-dimensional array as a formal parameter, we can omit the size
of the first dimension, but not the second; that is, we must specify the number of columns. For
example:
void print(int A[][3],int n, int m)
In order to pass to this function an array declared as:
int arr[4][3];
we need to write a call like this:
print(arr);
#include <stdio.h>
int A[10][10];
input(A, r, c);
printf("\n");
display(A, r, c);
return 0;
}
10 11 12 13
18 19 40 35
33 25 16 56
Initializing a string
A string can be initialized to a constant value when it is declared.
char str[ ] = "Good";
Or
char str[]={'G','o','o','d','\0'};
Here. 'G' will be stored in str[0], 'o' in str[1], and so on.
Note: When the value is assigned to the complete string at once, the computer automatically
inserts the NULL character at the end of the string. But, if it is done character by character, then
we have to insert it at the end of the string.
Reading strings with/without embedded blanks
To read a string without blanks scanf can be used
scanf("%s", str);
To read a string with blanks gets() can be used.
gets(str);
Printing strings
printf() and puts() can be used to print a string.
printf("%s", str);
Or
puts(str);
compares its first string argument to its second string argument, character by
character. It returns 0 if the strings are equal, returns a negative value if the first
strcmp(s1,s2)
string is less than the second, and returns a positive value if the first string is
greater than the second.
Appends string s2 to array s1. The first character of s2 overwrites the terminating
strcat()
null character of s1. The value of s1 is returned.
Locates the first occurrence in string s1 of string s2. If the string is found, a pointer
strstr()
to the string in s1 is returned. Otherwise, a NULL pointer is returned.
Structure
A structure is a collection of variable which can be the same or different types. You can refer to a
structure as a single variable, and its parts as members of that variable by using the dot (.)
operator. The power of structures lies in the fact that once defined, the structure name becomes
a user-defined data type and may be used the same way as other built-in data types, such as int,
double, char.
struct Student
{
int rollno, age;
char name[80];
float marks;
};
int main()
{
return 0;
}
Defining a structure
When dealing with the students in a school, many variables of different types are needed. It may
be necessary to keep track of name, age, Rollno, and marks the point for example.
struct Student
{
int rollno, age;
char name[80];
float marks;
};
The student is called the structure tag and is your brand new data type, like int, double, or char.
struct Student
{
int rollno, age;
char name[80];
float marks;
} s1, s3;
struct Student
{
int rollno, age;
char name[80];
float marks;
};
int main()
{
/* declare two variables of the new type */
struct Student s1, s3;
………
………
return 0;
}
s3 = s2;
The statement assigns the value of each member of s2 to the corresponding member of s3. Note
that one structure variable can be assigned to another only when they are of the same structure
type, otherwise the compiler will give an error.
struct Day
{
int month, date, year;
};
struct Student
{
int rollno, age;
char name[80];
struct Day date_of_birth;
float marks;
};
s.date_of_birth.month = 11;
s.date_of_birth.date = 5;
s.date_of_birth.year = 1999;
Arrays of Structure
To store data of 20 students we would be required to use 20 different structure variables from s1
to s20, which is not very convenient. A better approach would be to use an array of structures.
The following program shows how to use an array of structures.
#include <stdio.h>
struct student
{
int rollno;
char name[80];
int marks;
};
int main()
{
struct student data[20];
int n, choice, rollno;
return 0;
}
printf("\n\nRollno\tName\tMarks\n");
for (i = 0; i < s; i++)
{
printf("%d\t%s\t%d\n", list[i].rollno, list[i].name, list[i].marks);
}
}
Output:
Number of records you want to enter? : 3
Related links
DJGPP a complete 32-bit C/C++ development system for Intel 80386 and higher PCs.
https://www.sanfoundry.com/c-programming-examples/