Arrays
-One Dimensional array
-Sorting using one Dimensional array
-Concept of two Dimensional array
-String-Array of Characters
-String Manipulation
Array-Definition
Array is the collection of similar type of elements.
Array is also known as Derived Data Type
Type of Array
-One Dimension
-Two Dimension
-Multi Dimension
One Dimension Array
Syntax:
<data_type> <array_name> [array_size];
Example:
int number[5];
Here name of the array is number and Size of array is of 5 elements
Data type is integer so all the elements of array are integers.
Data type specifies type of elements contained by the array.
The declaration int number[5] will reserve space of 2 byte per element. Refer the picture below to understand it.
The first element of array is referred as number[0] , second element is number[1] so each element of an array can be accessed and used according to use.
The size of the array is int number[5] so space for five elements 0 to 4 is given, if we access number[5] then it will return the error as it does not exist (Out of bound exception)
Initialization of Array
int number[5]={1,2,3,4,5};
int number[ ]={1,2,3,4,5};
Suppose starting address of array element number[0] is 2340 and type of array is integer (int=4 byte)
then the next element number[1] will be 2344 and so on.
Loops can be useful to manage the elements of the array.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf("Insert Arrar Elements\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]); //insert array element
}
for(i=0;i<5;i++)
{
scanf("element[%d]:%d\n",i,a[i]); //print array element with position
}
getch();
}
Sorting an Array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,temp;
clrscr();
printf("Insert the numbers\n");
for(i=0;i<9;i++)
{
scanf("%d",&a[i]);
}
printf("Array inserted by user\n");
for(i=0;i<9;i++)
{
printf("%d",a[i]);
}
//Sorting
for(i=0;i<8;i++)
{
for(j=i+1;j<9;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
//Print Sorted Array
printf("Array after Sorting\n");
for(i=0;i<9;i++)
{
printf("%d",a[i]);
}
getch();
}
Two Dimension Array
Syntax:
<data_type> <array_name> [Row][Column];
Example:
int number[3][3];
when we are declaring the two dimension array first dimension is row and second dimension is column.
Using two dimension array we can store the values in row and column format.
Initialization of Two Dimension Array
int ar[3][3]={1,1,1,2,2,2,3,3,3};
or
int ar[3][3]={{1,1,1},{2,2,2},{3,3,3}};
would be stored like
-One Dimensional array
-Sorting using one Dimensional array
-Concept of two Dimensional array
-String-Array of Characters
-String Manipulation
Array-Definition
Array is the collection of similar type of elements.
Array is also known as Derived Data Type
Type of Array
-One Dimension
-Two Dimension
-Multi Dimension
One Dimension Array
Syntax:
<data_type> <array_name> [array_size];
Example:
int number[5];
Here name of the array is number and Size of array is of 5 elements
Data type is integer so all the elements of array are integers.
Data type specifies type of elements contained by the array.
The declaration int number[5] will reserve space of 2 byte per element. Refer the picture below to understand it.
The first element of array is referred as number[0] , second element is number[1] so each element of an array can be accessed and used according to use.
number[0]
|
First element
|
number[1]
|
Second element
|
number[2]
|
:
|
number[3]
|
:
|
number[4]
| Last element |
The size of the array is int number[5] so space for five elements 0 to 4 is given, if we access number[5] then it will return the error as it does not exist (Out of bound exception)
Initialization of Array
int number[5]={1,2,3,4,5};
int number[ ]={1,2,3,4,5};
Value
|
Address
| |
number[0]
|
1
|
2340
|
number[1]
|
2
|
2344
|
number[2]
|
3
|
2348
|
number[3]
|
4
|
2352
|
number[4]
|
5
|
2356
|
Suppose starting address of array element number[0] is 2340 and type of array is integer (int=4 byte)
then the next element number[1] will be 2344 and so on.
Loops can be useful to manage the elements of the array.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf("Insert Arrar Elements\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]); //insert array element
}
for(i=0;i<5;i++)
{
scanf("element[%d]:%d\n",i,a[i]); //print array element with position
}
getch();
}
Sorting an Array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,temp;
clrscr();
printf("Insert the numbers\n");
for(i=0;i<9;i++)
{
scanf("%d",&a[i]);
}
printf("Array inserted by user\n");
for(i=0;i<9;i++)
{
printf("%d",a[i]);
}
//Sorting
for(i=0;i<8;i++)
{
for(j=i+1;j<9;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
//Print Sorted Array
printf("Array after Sorting\n");
for(i=0;i<9;i++)
{
printf("%d",a[i]);
}
getch();
}
Two Dimension Array
Syntax:
<data_type> <array_name> [Row][Column];
Example:
int number[3][3];
when we are declaring the two dimension array first dimension is row and second dimension is column.
Using two dimension array we can store the values in row and column format.
Initialization of Two Dimension Array
int ar[3][3]={1,1,1,2,2,2,3,3,3};
or
int ar[3][3]={{1,1,1},{2,2,2},{3,3,3}};
would be stored like
Col 1
|
Col 2
|
Col 3
| |
Row 0
|
1
|
1
|
1
|
Row 1
|
2
|
2
|
2
|
Row 2
|
3
|
3
|
3
|
Multi Dimension Array
Syntax:
<data_type> <array_name> [a1][a2][a3]...[an];
Example:
int number[2][3][4];
String Array of Characters-Strings
The collection of character can be stored in a character array.
Character array is called string
Syntax:
char String_name[size];
Example:
char str[50];
char name[30];
Initialization
char str[ ]={'H','e','l','l','o','\0'};
In above example string is ended with the null character '\0'
String is always terminated with the null character '\0'
similarly char str[ ]="Hello";
is same as above initialization but here we dont require to put null character '\0' because C inserts the null character automatically.
So character array is created like
H
|
E
|
l
|
l
|
o
|
\0
|
char name[11]={'C','','L','A','N','G','U','A','G','E','\0'};
char name[11]="C Language";
Here array name has size of 11 character including space for the null character which is working as a terminator.
C
|
L
|
A
|
N
|
G
|
U
|
A
|
G
|
E
|
\0
|
And here space is also considered as a character .
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
char name[30];
int i=0;
clrscr();
printf("Insert name:");
gets(name); //insert the string to array
printf("Name inserted by user is:");
for(i=0;str[i]!='\0';i++)//Loop will executed till null character is found
{
printf("%c",name[i]); //character will be printed as loop is executed till it found null character
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char name[30];
int i=0;
clrscr();
printf("Insert name:");
gets(name); //insert the string to array
printf("Name inserted by user is:");
printf("%s",name); //It will print the whole string
getch();
}
Above both examplesa are same with different method to display the string
String Manipulation
Some common string manipulation operations are
1.Finding the Length of String
2.Copying One sting into another
3.Concatenate Two String
4. Comparing Two Strings
5. Search a String in another String
Jumping statement is goto statement??
ReplyDelete