BCA (2nd Year) - C Programming - (Topic - The Array)
BCA (2nd Year) - C Programming - (Topic - The Array)
1. Creation of Array:
int x[10];
Data
45 60 55 68 98 49 36 28 35 56
0 1 2 3 4 5 6 7 8 9
Index
2. Types of Array :
We can use arrays to represent not only simple lists of values but also tables
of data in two, three or more dimensions. So, there are three types of arrays.
These are:
1. One-dimensional Array.
2. Two-dimensional Array.
3. Multi-dimensional Array.
1
Created By: Er. Raj Kumar Patel
1. One-Dimensional Array:
A list of items can be given in one variable name using only one subscript,
such variable is called a single-subscripted variable or a One-dimensional
Array.
Data_type Variable_name[Size];
The data type specifies the type of element that will be contained in the array,
such as char, int, float etc. the variable_name indicates the array name and the
size specifies the maximum number of elements that can be stored inside the
array.
Example:
int x[5];
45 60 50 75 80
0 1 2 3 4
Index
2
Created By: Er. Raj Kumar Patel
1. At Compile Time.
2. At Run Time.
3
Created By: Er. Raj Kumar Patel
Example:
…………………………….
…………………………….
for (i=0;i<100;i++)
{
Add[i]=0.0; /* Assignment Statement*/
}
…………………………….
…………………………….
Example:
int x[5];
for(i=0;i<5;i++)
{
scanf{“%d”,&x[i]);
}
4
Created By: Er. Raj Kumar Patel
/* Q1: Write a C program which will read 10 numbers of elements and store
into an array and also display that numbers. */
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int x[10], i;
clrscr();
printf("Enter any 10 numbers");
for(i=0;i<10;i++)
{
scanf("%d",&x[i]);
}
for(i=0;i<10;i++)
{
printf("%d",x[i]);
}
getch();
}
5
Created By: Er. Raj Kumar Patel
/* Q2: Write a C program to find out the largest element in the array. */
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int x[10];
int i, large;
clrscr();
printf("Enter any 10 numbers");
for(i=0;i<10;i++)
{
scanf("%d",&x[i]);
}
large=x[0];
for(i=1;i<10;i++)
{
If(x[i]>large)
large=x[i];
}
printf("%d",large);
getch();
}