Lab 5
Lab 5
Lab 5
Lab Manual
Programming in C
Lab 5
Arrays
Index
Lab Objectives
Background
Some Examples
Exercises
Lab Objectives:
To study about the array definition and types,
To apply an array,
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 an array (for one dimensional) :
Some Examples:
1. Write a program that read and display sum.
Program code:
#include<stdio.h>
int main()
{
int i, n, a[100], sum=0;
printf("How many numbers: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
sum=sum+a[i];
}
printf("Sum= %d", sum);
return 0;
}
if(p<0 || p>n)
printf("Insert is imposible.");
else
{
for(i=n-1;i>=p;i--)
a[i+1]=a[i];
printf("Enter any number to Insert: ");
scanf("%d",&a[p]);
n++;
}
printf("\n After insert array contains: \n");
for(i=0;i<n;i++)
printf("%4d", a[i]);
return 0;
}
Exercise:
1. Write a program that deletes any number from an array.
2. Write a program that read and sort an array using bubble sort in ascending or
descending order.
3. Write a program that reads a decimal number and display equivalent binary
number.
4. Write a program that multiplies two matrices.