Академический Документы
Профессиональный Документы
Культура Документы
Arrays
Introducing Arrays
Array are collection of elements of same
data type.
All elements
memory.
are
consecutively in
Uses:
Arrays are useful when we store related
data items.
Introducing Arrays
Examples:
List of marks of students
List of grades of students
List of employees in an organization
List of names of students
Declaration of an Array
type Array_name[size];
Here size indicates the maximum number
of elements.
The subscript value start from 0 to size-1.
Example:
int number[10];
float number[10];
char grade[10];
Initialising an Array
An
Initialising an Array
Second approach:
elements of array can be initilised one at a time.
Ex. mark[0] = 55;
mark[1] = 60;
mark[2] = 40;
mark[3] = 80;
mark[4] = 90;
Note: In an array index of first element is considered
as zero.
Therefore in an array of n elements first index is 0
and last index is n-1.
Initialising an Array
#include<stdio.h>
void main()
{
int A[10],i,j,temp;
printf(Enter 10 numbers);
for(i=0; i<10; i++)
scanf(%d,&A[i]);
Size of an Array
Once an array is declared, its size is
fixed.
After that it can not changed.
Ex.
int A[10];
Initialising an Array
Declaring,
int
Multidimensional Array
The
Two
Row 0
Row 1
Row 2
Row 3
Column 2 Column 3
Declaration: Multidimensional
Array
type
array_name[row_size][column_size];
row_size >Number of rows in matrix
column_size > Number of columns in matrix
int number[4][3];
float number[3][2];
char name[10][20];
/* 12 elements */
/* 6 elements */
/* 200 chars */
int a[2][3]={1,2,3,4,5,6};
int a[2][3]={0}
int a[3][]={2,4,6,8,10,12};
int a[][]={2,4,6,8,10,12};
Note:
Example
#include <stdio.h>
#define row 4
#define col 3
void main()
{
int M[row][col];
int i,j,k;
printf(Enter data for Matrix M1\n);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf(%d,&M1[i][j]);
}
printf(\n);
}
a[1][0], a[1][1],
a[2][2]
a[0][1], a[1][1],
a[2][2]
Strings
Strings
Null Character
The
Declaring strings
Char stringname[size];
The size determinesthe number ofcharacters
in thestringname.
Example:
char monthname[12];
char address[100];
The size of the array should be one byte
more than the actual space occupied by
the stringsince the complier appends a
nullcharacterat the end of thestring.
Initializing Strings
char month[ ]={j,a,n,u,a,r,y ,\0};
Or
char month[ ]={january};
Then thestringmonth is initializing to
January. This is perfectly valid but C offers a
special way to initialize strings. The
abovestringcan
be
initializedchar
month1[]=January. The characters of
thestringare enclosed within a part of
double quotes.
String Initialization:
char str[9] = I like C;
same as
char str[9]={I, ,l,i,k,e, ,C,\0};
Q. Is there any difference between following
Initialization?
char str[]=ITMU;
char str[4]= ITMU;
Ans: Yes, in second declaration there is no null
character
Arrays of Strings
Declaration:
char
name[5][30];
Five strings each contains maximum thirty characters.
Initialization:
char[5]
[10]={One,Two,Three,Four,Five};
Other
valid declarations
char[][]={One,Two,Three,Four,Five};
char[5][]={One,Two,Three,Four,Five};
22
Copying String
String1=xyz;
String2=string1;
C
library
supports
a
large
number
ofstringhandling functions that can be used
forstring manipulations such as:
strlen() function:
strcat() function:
strcat(string1,string2);
strcmp function:
Strcmp(string1,string2)
Example:
strcpy() function:
syntax
strcpy(string1,string2);