Array:
Array is a collection of similar data items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They can be used to store collection of primitive data types such as int, float, double, char etc.
Declaration of Array:
There are different ways to declare Arrays in C language. To declare an array, we must provide following information.
1) Data Type of an array
2) The name of the array
3) The number of subscripts in an array
4) The maximum value of each subscript
Like other variables in c, an array has to be declared before it is used. Following is the syntax for declaring an array
<data_type> <array_name> [<array_size>];
Eg. int student[50];
The above example declares an array student of integer data type that can store 50 elements. That means, It is equivalent to 50 different variables of integer type. Each element in the array has a index number which starts from 0 and goes upto n-1. Each member can be addressed as student[0], student[1], ….. student[49]. The index number of array always starts from 0, not from 1.
Initializing Array.
We can initialize and array in C by the followings ways.
a. Single statement
double balance[5] ={100.2,2.0,3.4,7.2,4.1};
The number of values between braces cannot be larger than the number of elements that we declare for the array between square brackets[]
If you omit the size of array, the array is just enough to hold initialization is created.
int arr[]={2,5,7}
We can assign elements of array in the following way too.
int a[4];
a[0]=1;
a[1]=3;
a[2]=4;
a[3]=7;
Accessing Array:
A single dimensional or linear array is accessed by using a loop by which a variable is used as the index number of array. The index variable initialized to 0 at the beginning of the loop and it is incremented to a number one lesser than the size (n-1) .
FOr example array student[5] can store and assessed elements student[0],student[1],student[2],student[3], student[4];
We can access these elements through a variable ‘i’ whose value starts from 0 and goes up to 4. So a single statement student[5] replaces the individual element names of array. In each iteration of loop, the value of i is increased and next element of array is accessed.
Program:
WAP to enter marks of 5 students in Computer science and display them.
#include<stdio.h> #include<volio.h> void main() { int student[5], i; for(i=0;i<5;i++) { printf("Enter Mark:"); scanf("%d",&student[i]); } printf("\n The entered Marks are:") for(i=0;i<5;i++) { printf("%d",student[i]); } getch(); }
Types of Array
There are two types of array declaration
1. One Dimensional array
2. Two-Dimensional Array
One Dimensional Array:
The values on an array variable assigned in one row are called one dimensional. It si a simplest array. The individual values are not given different names in the array. Instead, they are named as a Gorup of related values. When we want to access a particular value, we hate to use an index value. For example: if we want to access first student age in array int age[50] like age[0] for the first age, age[1] for the second age and so on.
The first element in an array is given an index value of 0 and then the index is increased for each new element.
The syntax of one-dimensional array is as follow.
datya_type array_name[size];
data_type array_name={number_of_values};
One-Dimensional Array Question Collection:
- Write a program to enter 10 integer numbers into array and display. [5] (2075 Set B Q.no. 2b)
Output:
2. Write a program to enter 10 numbers in array and find the sum and average of all the numbers.
3. Define array. Write a program to input any 10 numbers in array and display it. Find the greatest number among the input numbers.
Output
4. WAP to input any 10 numbers in array and display it. Find the smallest number among the input numbers.
Output
5. Define array. Write a program to sort the ten integers number in ascending order and display it. [2+2.5] ( 2077, Set D Q.No. 1)
6. Write a program to enter 10 integer numbers into array and display in descending order. [5] (2062 )
Output
7. Write an algorithm and C program to read salaries of 200 employees and count the number of eployees getting salaries between 500 – 1000 [10] (2062 Q No. 3a)
OTHER OLD QUESTIONS
- WAP to input n numbers and find out the greatest [5] (2062)
- Write a program to input any 10-integer number in an array and find the total. [10] (2074)
- WAP using C language to read the age of 100 persons and count the number of persons in the age broup between 50 and 60. Use for and continue statement. [10] (2061 Q No. 2)