EST102 Programming in C
MODULE III
Arrays and strings-Arrays Declaration and Initialization, 1-Dimensional Array, 2-
Dimensional Array, String processing: In built String handling functions (strlen, strcpy,
strcat and strcmp, puts, gets), Linear search program, bubble sort program, simple
programs covering arrays and strings
ARRAY
An array in C is a collection of items stored at contiguous memory locations and
elements can be accessed randomly using indices of an array. They are used to store
similar type (homogeneous) of elements as the data type must be the same for all
elements. They can be used to store collection of primitive data types such as int, float,
double, char, etc of any particular type.
Indices 0 1 2 3
A
Array elements 10 20 30 40
Why do we need arrays?
We can use normal variables (v1, v2, v3) when we have a small number of
objects, but if we want to store a large number of instances, it becomes difficult to
manage them with normal variables. The idea of an array is to represent many instances
in one variable.
Different ways of declaring an Array
1. Array declaration by specifying size
Eg: int A[10];
2. Array declaration by initializing elements
Eg: int A[] = { 10, 20, 30, 40 };
3. Array declaration by specifying size and initializing elements
Eg: int A[6] = { 10, 20, 30, 40 };
Advantages of an Array
1. Random access of elements using array index.
2. Use of less line of code as it creates a single array of multiple elements.
3. Easy access to all the elements.
4. Traversal through the array becomes easy using a single loop.
5. Sorting becomes easy as it can be accomplished by writing less line of code.
IIPE 1
, EST102 Programming in C
Disadvantages of an Array
• It allows us to enter only fixed number of elements into it. We cannot alter the size of
the array once array is declared. Hence if we need to insert more number of records
than declared then it is not possible. We should know array size at the compile time
itself.
• Inserting and deleting the records from the array would be costly since we add /
delete the elements from the array, we need to manage memory space too.
• It does not verify the indexes while compiling the array. In case there is any indexes
pointed which is more than the dimension specified, then we will get run time errors
rather than identifying them at compile time.
Example:
int a[3]={1,2,3}
printf("%d",A[3]);
Output: 3245431 (Garbage Value)
1. Write a program to read and display an array of size n.
#include<stdio.h>
void main()
{
int n,A[20],i;
printf("Enter the size of Array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the number:");
scanf("%d",&A[i]);
}
for(i=0;i<n;i++)
{
printf("%d\t",A[i]);
}
}
OUTPUT
Enter the size of Array: 5
Enter the number:10
Enter the number:20
Enter the number:30
Enter the number:40
Enter the number:50
IIPE 2
, EST102 Programming in C
10 20 30 40 50
2. Write a program to read and display an array of size n in reverse order.
#include<stdio.h>
void main()
{
int n, A[20],i;
printf("Enter the size of Array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the number");
scanf("%d",&A[i]);
}
for(i=n-1;i>=0;i--)
{
printf("%d\t",A[i]);
}
}
OUTPUT
Enter the size of Array: 5
Enter the number:10
Enter the number:20
Enter the number:30
Enter the number:40
Enter the number:50
50 40 30 20 10
3. Write a program to find sum and average of an array of size n.
#include<stdio.h>
void main()
{
int n,A[20],i,sum; float avg;
printf("Enter the size of Array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the number");
scanf("%d",&A[i]);
}
sum=0;
for(i=0;i<n;i++)
IIPE 3
MODULE III
Arrays and strings-Arrays Declaration and Initialization, 1-Dimensional Array, 2-
Dimensional Array, String processing: In built String handling functions (strlen, strcpy,
strcat and strcmp, puts, gets), Linear search program, bubble sort program, simple
programs covering arrays and strings
ARRAY
An array in C is a collection of items stored at contiguous memory locations and
elements can be accessed randomly using indices of an array. They are used to store
similar type (homogeneous) of elements as the data type must be the same for all
elements. They can be used to store collection of primitive data types such as int, float,
double, char, etc of any particular type.
Indices 0 1 2 3
A
Array elements 10 20 30 40
Why do we need arrays?
We can use normal variables (v1, v2, v3) when we have a small number of
objects, but if we want to store a large number of instances, it becomes difficult to
manage them with normal variables. The idea of an array is to represent many instances
in one variable.
Different ways of declaring an Array
1. Array declaration by specifying size
Eg: int A[10];
2. Array declaration by initializing elements
Eg: int A[] = { 10, 20, 30, 40 };
3. Array declaration by specifying size and initializing elements
Eg: int A[6] = { 10, 20, 30, 40 };
Advantages of an Array
1. Random access of elements using array index.
2. Use of less line of code as it creates a single array of multiple elements.
3. Easy access to all the elements.
4. Traversal through the array becomes easy using a single loop.
5. Sorting becomes easy as it can be accomplished by writing less line of code.
IIPE 1
, EST102 Programming in C
Disadvantages of an Array
• It allows us to enter only fixed number of elements into it. We cannot alter the size of
the array once array is declared. Hence if we need to insert more number of records
than declared then it is not possible. We should know array size at the compile time
itself.
• Inserting and deleting the records from the array would be costly since we add /
delete the elements from the array, we need to manage memory space too.
• It does not verify the indexes while compiling the array. In case there is any indexes
pointed which is more than the dimension specified, then we will get run time errors
rather than identifying them at compile time.
Example:
int a[3]={1,2,3}
printf("%d",A[3]);
Output: 3245431 (Garbage Value)
1. Write a program to read and display an array of size n.
#include<stdio.h>
void main()
{
int n,A[20],i;
printf("Enter the size of Array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the number:");
scanf("%d",&A[i]);
}
for(i=0;i<n;i++)
{
printf("%d\t",A[i]);
}
}
OUTPUT
Enter the size of Array: 5
Enter the number:10
Enter the number:20
Enter the number:30
Enter the number:40
Enter the number:50
IIPE 2
, EST102 Programming in C
10 20 30 40 50
2. Write a program to read and display an array of size n in reverse order.
#include<stdio.h>
void main()
{
int n, A[20],i;
printf("Enter the size of Array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the number");
scanf("%d",&A[i]);
}
for(i=n-1;i>=0;i--)
{
printf("%d\t",A[i]);
}
}
OUTPUT
Enter the size of Array: 5
Enter the number:10
Enter the number:20
Enter the number:30
Enter the number:40
Enter the number:50
50 40 30 20 10
3. Write a program to find sum and average of an array of size n.
#include<stdio.h>
void main()
{
int n,A[20],i,sum; float avg;
printf("Enter the size of Array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the number");
scanf("%d",&A[i]);
}
sum=0;
for(i=0;i<n;i++)
IIPE 3