Lab Manual 4. Array
Lab Manual 4. Array
Programming in C
Lab Manual
Lab 4
Array
INDEX
Lab Objectives
Background
Some Examples
Exercises
Lab Objectives:
• To know about array that provides convenient structure for representing data
• To study about the array definition and types
• To apply array in C program
Background:
An array (or one dimensional array) is a fixed-size collection of consecutive memory locations.
Each memory location in an array is accessed by a relative address called an index.
Basically there are two ways to define one dimensional array:
Ø datatype Arrayname[Size];
Example:
v int x[100];
v char text[80];
Ø datatype Arrayname[] = { value1, value2,….., valueN };
Example:
v int x[]={1,2,3,4,5};
Values assigned to the individual array elements:
x[0] = 1; x[1] = 2; x[2] = 3; x[3] = 4; x[4] = 5;
v char dept[ ] = “C.S.E.”;
Values assigned to the individual array elements:
dept [0]=‘C’; dept [1]=‘.’; dept [2]=‘S’; dept [3]=‘.’; dept [4]=‘E’; dept[5]=‘.’;
dept[6]=‘\0’;
1. Write a program that will put N numbers in an array and display each element of that
array in a new line.
Source code:
#include<stdio.h>
int main()
{
int count, n, number[100];
printf("How many numbers? : ");
scanf("%d",&n);
printf("\n Enter %d numbers:\n\n",n);
for(count = 0; count < n; count++)
{
scanf("%d",&number[count]);
}
2. Write a program that will put N numbers in an array and display the sum.
Source code:
#include<stdio.h>
int main()
{
int count, n, number[100], sum=0;
printf("How many numbers? : ");
scanf("%d",&n);
for(count = 1; count <= n; count++)
{
printf("Enter number[%d]: ",count);
scanf("%d",&number[count]);
sum = sum + number[count];
}
printf("\n Sum = %d\n",sum);
return 0;
}
3. Write a program to find the smallest element of an array.
Source code:
#include<stdio.h>
int main()
{
int count, n, number[100], min;
printf("How many numbers? : ");
scanf("%d",&n);
// Take Input:
for(count = 0; count < n; count++)
{
printf("Enter number[%d]: ",count);
scanf("%d",&number[count]);
}
4. Write a program that will put the even elements and the odd elements of an array into
two separate arrays.
Source code:
#include<stdio.h>
int main()
{
int count, n, number[100], even[100], odd[100], countEVEN = 0, countODD = 0;
/* even[] : Array to store even numbers,
odd[] : Array to store odd numbers,
countEVEN = Variable used to count even numbers,
countODD = Variable used to count odd numbers */
printf("Enter number of elements: ");
scanf("%d",&n);
return 0;
}
Exercise:
1. Write a program that will store N numbers in an array and display the elements of
that array in reverse order of index.
Sample Input: Sample Output:
Enter the number of elements: 5 The elements in reverse order:
Enter 5 elements: Element[5] = 5 ,
10 Element[4] = 7 ,
9 Element[3] = 8 ,
8 Element[2] = 9 ,
7 Element[1] = 10 ,
5
2. Write a program that will store the class test marks of N students in an array and
find the average class test mark. [Consider that class test marks can only be integer and
display the average class test mark in floating point format]
3. Write a program to find the largest element of an array.
4. Write a program that will count the number of odd elements stored in an array.
5. Write a program that will search a particular element from an array.
Sample Input: Sample Input:
Enter the number of elements: 5 Enter the number of elements: 5
Enter 5 elements: Enter 5 elements:
10 19
9 39
8 18
7 27
5 15
Element going to be searched = 7 Element going to be searched = 5