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

Unit V

The document provides an overview of arrays in computer science, detailing their definition, types (one-dimensional and two-dimensional), and how to declare and initialize them in C programming. It includes examples of accessing array elements, initializing arrays using loops, and sample programs for operations like summing, multiplying, and finding the maximum or minimum elements in arrays. Additionally, it covers matrix operations such as addition and transposition for two-dimensional arrays.

Uploaded by

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

Unit V

The document provides an overview of arrays in computer science, detailing their definition, types (one-dimensional and two-dimensional), and how to declare and initialize them in C programming. It includes examples of accessing array elements, initializing arrays using loops, and sample programs for operations like summing, multiplying, and finding the maximum or minimum elements in arrays. Additionally, it covers matrix operations such as addition and transposition for two-dimensional arrays.

Uploaded by

harrisonkalenga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

800CS120 – COMPUTER SCIENCE I

Array
Array is a kind of data structure that can store a fixed-size sequential collection of elements of the
same type.
An array is used to store a collection of data, but it is often more useful to think of an array as a
collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare
one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to
represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.

Definition:
An array is defined as a set of homogeneous data items of the same type that share a common name.

TYPES OF ARRAYS
1. One dimensional array
2. Two dimensional array

ONE DIMENSIONAL ARRAY


It is a dimensional array that has only one subscript.
General Format/syntax Of Declaring an Array
data type array-name [subscript];

Note : Datatype specifies the type of element that will be stored in the array.
array-name is a valid C variable of type datatype,
Subscript is an integer constant indicating the maximum number of elements that can be
stored in the array.

For Example :
int a[5];

a [0] a [1] a [2] a [3] a [4]

char name[10];
float avg[8];

[Link] Page 1
800CS120 – COMPUTER SCIENCE I
How to access element of an array
You can use array subscript (or index) to access any element stored in array. Subscript starts with
0, which means array_name[0] would be used to access first element in an array.

In general array_name[n-1] can be used to access nth element of an array. where n is any integer
number.
For e.g.
int data[20];
data[0] /* first element of array data*/
data[19] /* last (20th) element of array data*/

Initializing one-dimensional arrays:


Array can be initialized by two ways:
1. Initializing an array during declaration.
2. Initializing an array using loops.

Initializing an array during declaration:


For each element in the array, we provide a value. The only difference is that the values must be
enclosed in braces and, if there is more than one, separated by commas.
General formart/syntax:
data type array-name[size] = {list of values};

Examples 1:
int age [5] = { 3 , 7, 12, 24, 45};

Array elements: age [0] age [1] age [2] age [3] age [4]
3 7 12 24 45

Example 2:
float height[ ]={1.3 ,2.1 .1.8 ,3.6};

Example 3:
int age [5] = { 3, 7 };

Memory representation of an Array

Array elements: age [0] age [1] age [2] age [3] age [4]

Values: 3 7 ? ? ?
(The rest are filled
with 0’s)
Char name[6]={“Lydia”);
Char name[10]={‘l’,’y’,’d’,’I’,’a’};
name[0] name[1] name[2] name[3] name[4] name[5]
L y d i a \0

[Link] Page 2
800CS120 – COMPUTER SCIENCE I

Initializing array using Loops


An array can be initialized by using a for loop, while loop or do-while loop together with the scanf
function.

Example on how to give array elements input using “for loop and scanf()” statements:
for (i=0; i<=n;i++)
{
printf("enter the integer number”);
scanf("%d", &x[i]);
}

Example on how to display array elements using “for loop and printf()” statements:
for (int i=0; i<n; i++)
{
printf("%d\n", x[i]);
}

Example Programs:
Example 1:
Write a Program in C to find the sum of elements in one dimensional array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10], i, n, sum=0;
clrscr();
printf(“Enter the Array size: \n”);
scanf(“%d”,&n);
printf(“Enter the values of an array:\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
sum=sum+a[i];
}
printf(“The sum of elements is :%d”,sum);
getch();
}

OUTPUT:
Enter the Array size: 4
Enter the values of an array: 2 5 4 2

[Link] Page 3
800CS120 – COMPUTER SCIENCE I
The Sum of elements is: 13

Example 2:
Write a Program in C to find the product of elements in one dimensional array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,mul=1;
clrscr();
printf(“Enter the Array size:\n”);
scanf(“%d”,&n);
printf(“Enter the values of an array :\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
mul=mul*a[i];
}
printf(“The product of elements is:%d”,mul);
getch();
}

OUTPUT:
Enter the Array size: 4
Enter the values of an array: 2 2 2 2
The product of elements is: 16

Example :3
Write a Program in C to find the average of elements in one dimensional array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,sum=0;
float avg; OUTPUT:
clrscr(); Enter the Array size: 4
printf(“Enter the Array size :\n”); Enter the Values: 2 2 2 2
scanf(“%d”,&n); The Sum of elements is: 16
printf(“Enter the values of an array:\n”); The Average of array element is: 4
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
sum=sum+a[i];
}
avg=sum/n;
printf(“The Sum of elements is: %d”,sum);
printf(“The Average of array elements is: %d”,avg);

[Link] Page 4
800CS120 – COMPUTER SCIENCE I
getch();
}

Example 4:
Program to find the maximum element in a one dimensional array
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i,n,a[10], max=0; OUTPUT:
printf("\n Enter the size of the array : "); Enter the size of the array: 3
scanf("%d",&n); Enter the array elements: 7 2 5
printf("\n Enter the array elements"); Maximum is = 7
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
printf("\n Maximum is=%d",max);
getch();
}

Example 5:
Program to find the minimum element in a single dimensional array
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i,n,a[10],min=0; OUTPUT:
printf("Enter the size of the array: \n "); Enter the size of the array: 3
scanf("%d",&n); Enter the array elements: 7 2 5
printf("Enter the array elements: \n"); Minimum is = 2
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]<min)
min=a[i];
}
printf("\n Minimum is=%d",min);

[Link] Page 5
800CS120 – COMPUTER SCIENCE I
}
getch();
}
TWO-DIMENSIONAL ARRAY
A two-dimensional array is defined in the same manner as a single dimensional array except that it
requires two pairs of square brackets for two subscripts.
A two dimensional array is also referred as a matrix. Each subscript must be expressed as a non-
negative integer.
The general form/syantax:
data type array-name [row-size] [column-size];

For Example :
float marks[2][2];
int x[3][4];
An array element starts with an index zero and so the individual elements of the array will be

[0][0] [0][1]
float marks
[1][0] [1][1]

Note: The first element is stored in the memory location [0][0], followed by the second element in
[0][1] and so on, and the last element will be stored in the memory location [1][1].

But the memory in a computer stores values such as:


Marks[0][0] marks[0][1] marks[1][0] marks[1][1]

Initializing two-dimensional arrays


Array can be initialized by two ways :
1. Initializing an array during declaration.
2. Initializing an array using loops and the scanf () statement.

Initializing an array during declaration:


For each element in the array, we provide a value. The only difference is that the values must be
enclosed in braces and, if there is more than one, separated by commas.
General form/synatax :
data-type array-name[row-size][column-size] = {list of values};

For Example:
int age [4] [2] = {
{85, 56},
{92, 67},
{78, 75},
{69, 89}
};
The results of the initial assignments are,
mark[0][0]=85 mark[0][1]=56

[Link] Page 6
800CS120 – COMPUTER SCIENCE I
mark[1][0]=92 mark[1][1]=67
mark[2][0]=78 mark[2][1]=75
mark[3][0]=69 mark[3][1]=89
or
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

Initializing array using for Loop and scanf() function


An array can be initialized by using a for loop, while loop or do-while loop. The following example
illustrates the use of initializing an array using the for loop and scanf statements:
Example:
int x[10][10];
int i,j,m,n;
printf("Enter the elements for the matrix”);
for(i=0; i<m; i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”, &x[i][j]);
}
}
Example 1
Program to access array elements in a two dimensional array
#include <stdio.h>
#include<conio.h>
void main()
{
int a[10][10], i,j,n,m;
printf("Enter the array size\n");
scanf("%d%d",&m,&n);
printf("Enter the array elements\n");
for (i = 0; i < m;++i)
{ Enter the array size
for(j = 0; j < n; ++j) 22
{ Enter the array elements
scanf("%d", &a[i][j]); 1
} 2
} 3
printf("\nDisplaying values:\n"); 4
for (i = 0; i <m; ++i) Displaying values
{ 1
for(j = 0; j < n; ++j) 2
{ 3
printf("%d",a[i][j]); 4
printf("\t");
}
}
getch();
}

[Link] Page 7
800CS120 – COMPUTER SCIENCE I

Example 2:
Write a Program in C to find the Transpose matrix using two dimensional arrays.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],i,j,m.n;
clrscr();
printf(“Enter the Row size :\n”);
scanf(“%d”,&m);
printf(“Enter the column size :\n”);
scanf(“%d”,&n);
printf(“Enter the values :\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
}
printf(“The Transpose matrix is :\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d \t”,a[j][i]);
}
printf(“\n”);
}
getch();
}
OUTPUT:
Enter the Row size: 2
Enter the Column Size: 2
Enter the Values: 1 5 7 9
The Transpose matrix is:
1 7
5 9

[Link] Page 8
800CS120 – COMPUTER SCIENCE I
Example 2:
PROGRAM TO ADD TWO MATRICES
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
clrscr();
int A[10][10],B[10][10],C[10][10],i,j,m,n,p,q; //Declaration of variables
printf("Enter the row and column size of A matrix :\n"); //INPUT for A matrix
scanf("%d%d",&m,&n);
printf("Enter the elements of A matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("Enter the row and column size of B:\\n"); //INPUT for B matrix
scanf("%d%d",&p,&q);
printf("Enter the elements of B matrix \n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<m;i++) //ADDING Matrix
for(j=0;j<n;j++)
C[i][j]=A[i][j]+B[i][j];

printf("\n The sum of A and B matrices is:"); //OUTPUT Matrix


for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf("\t %d",C[i][j]);
printf(“\n”);
getch();
}
Output :
Enter the row and column size of A matrix : 2 2
1 2
2 3
Enter the row and column size of A matrix : 2 2
4 2
2 2
The sum of A and B matrices is:
5 4
4 5

[Link] Page 9
800CS120 – COMPUTER SCIENCE I
Example 4:
Program to find the difference of two matrices
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
clrscr();
int A[10][10],B[10][10],C[10][10],i,j,m,n,p,q; //Declaration of variables
printf("Enter the row and column size of A matrix :\n"); //INPUT for A matrix
scanf("%d%d",&m,&n);
printf("Enter the elements of A matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("Enter the row and column size of B:\\n"); //INPUT for B matrix
scanf("%d%d",&p,&q);
printf("Enter the elements of B matrix \n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<m;i++) //ADDING Matrix
for(j=0;j<n;j++)
C[i][j]=A[i][j]-B[i][j];

printf("\n The difference of A and B matrices is:"); //OUTPUT Matrix


for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf("\t %d",C[i][j]);
printf(“\n”);
getch();
}
Output :
Enter the row and column size of A matrix : 2 2
7 2
8 3
Enter the row and column size of A matrix : 2 2
4 2
2 2
The sum of A and B matrices is:
3 0
6 1

[Link] Page 10
800CS120 – COMPUTER SCIENCE I
PROGRAM TO PASS AN ARRAY BY USING CALL BY VALUE:
#include<stdio.h>
#include<conio.h>
void display(int m);
void main()
{
clrscr();
int i;
int marks[]={55,65,75,56,78,78,90};
{
for(i=0;i<=6;i++)
display(marks[i]);
}
void display (int m)
{
printf("\n%d",m);
}

output:
55
65
75
56
78
78
90

[Link] Page 11

You might also like