0% found this document useful (0 votes)
6 views6 pages

Lab 5

Uploaded by

mahmudereza43024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Lab 5

Uploaded by

mahmudereza43024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

“Heaven’s Light is Our Guide”

Department of Computer Science & Engineering

RAJSHAHI UNIVERSITY OF ENGINEERING & TECHNOLOGY

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,

 Searching and sorting 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) :

 storageclass datatype Arrayname[ Size],


E.g: int x[100];
char text[80];
static char message[25];

 Storageclass datatype Arrayname[] = {value1, value2,…..,valueN}


Example-
int x[5]={1,2,3,4,5};
For two dimensional-
Arrays are declared a follows:

type array_name [row_size][column_size];


Example of initialization-
int table[2][3]= {0,0,0,1,2,3}; OR
int table[2][3]= { {0,0,0}, {1,2,3}};

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;
}

2. Write a program that inserts any number in an array.


Program Code:
#include<stdio.h>
int main()
{
int i, n, a[100],p;
printf("How many numbers: ");
scanf("%d", &n);
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("Enter any position to inserts: ");
scanf("%d", &p);

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;
}

3. Write a program that display first n Fibonacci numbers.


Program Code:
#include<stdio.h>
int main()
{
int i, n;
long a[100];
printf("How many numbers:");
scanf("%d", &n);
a[0]=0;
a[1]=1;
for(i=2; i<n; i++)
a[i] = a[i-1] + a[i-2];
for(i=0; i<n; i++)
printf("%d ", a[i]);
return 0;
}

4. Write a program that ads two matrics.


Program code:
#include<stdio.h>
int main()
{
int i,j,r1,c1,r2,c2,a[10][10],b[10][10],c[10][10];
printf("How many row in A: ");
scanf("%d", &r1);
printf("How many column in A: ");
scanf("%d", &c1);
for(i=0; i<r1; i++)
for(j=0; j<c1; j++)
scanf("%d", &a[i][j]);
printf("How many row in B: ");
scanf("%d", &r2);
printf("How many column in B: ");
scanf("%d", &c2);
for(i=0; i<r2; i++)
for(j=0; j<c2; j++)
scanf("%d", &b[i][j]);
if (r1==r2 && c1==c2)
{
for(i=0; i<r1; i++)
for(j=0; j<c1; j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nThe sum of A and B is: \n");
for(i=0; i<r1; i++)
{
for(j=0; j<c1; j++)
printf("%d ", c[i][j]);
printf("\n");
}
}
else
printf("\nThe sum of A and B is imposible.");
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.

You might also like