0% found this document useful (0 votes)
41 views

Tribhuvan University Himalaya College of Engineering C-Programming (BCE/BEX/BCT (I/I) ) Lab-7: Arrays

- Arrays allow storing and manipulating multiple values of the same type under one name. - A one-dimensional array stores elements in contiguous memory locations. Elements are accessed using an index between 0 and size-1. - Multi-dimensional arrays store arrays of arrays, accessed using multiple indices. - Arrays can be initialized at declaration with curly braces. Missing elements are initialized to 0.

Uploaded by

Safal Panthy
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)
41 views

Tribhuvan University Himalaya College of Engineering C-Programming (BCE/BEX/BCT (I/I) ) Lab-7: Arrays

- Arrays allow storing and manipulating multiple values of the same type under one name. - A one-dimensional array stores elements in contiguous memory locations. Elements are accessed using an index between 0 and size-1. - Multi-dimensional arrays store arrays of arrays, accessed using multiple indices. - Arrays can be initialized at declaration with curly braces. Missing elements are initialized to 0.

Uploaded by

Safal Panthy
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/ 20

TRIBHUVAN UNIVERSITY

Himalaya College Of Engineering


C-Programming (BCE/BEX/BCT( I/I))
Lab-7: Arrays
Objectives:
➢ To deal with array.
➢ To be familiar array structures, accessing and manipulating
multidimensional data.

Background Theory:
ARRAYS
Introduction
Array is defined as a homogeneous collection of similar data elements.
Syntax :
datatype arrayname[size];
eg.
int A[4];

datatype arrayname[size][size];
eg.
float B[5][5];
An array is a data structure used to process multiple elements with the same data
type when a number of such elements are known. You would use an array when,
for example, you want to find out the average grades of a class based on the
grades of 50 students in the class. Here you cannot define 50 variables and add
their grades. This is not practical. Using an array, you can store grades of 50
students in one entity, say grades, and you can access each entity by using
subscript as grades[1], grades[2]. Thus you have to define the array of grades of
the float data type and a size of 50. An array is a composite data structure; that
means it had to be constructed from basic data types such as array integers.
One or single dimensional array:

There are several forms of an array in C-one dimensional and multi-dimensional


array. In one dimensional array, there is a single subscript or index whose value
refers to the individual array element which ranges form 0 to n-1 where n is the
size of the array. For e.g. int a[5] ; is a declaration of one dimensional array of
type int. Its elements can be illustrated as
1st element 2nd 3rd 4th 5th element
a[0] a[1] a[2] a[3] a[4]
2000 2002 2004 2006 2008
The elements of an integer array a[5] are stored continuous memory locations. It
is assumed that the starting memory location is 2000. As each integer element
requires 2 bytes, subsequent element appears after gap of 2 locations.
The general syntax of array is [i.e.declaration of an array :]

data_type array_name[size];
• data_type is the data type of array. It may be int, float, char, etc.
• array_name is name of the array. It is user defined name for array. The name of
array may be any valid identifier.
• size of the array is the number of elements in the array. The size is mentioned
within[ ]. The size must be an int constant like 10, 15, 100, etc or a constant
expression like symbolic constant. For example
#define size 80
a [size] ;
The size of the array must be specified [i.e. should not be blank) except in array
initialization.
int num[5] ; i.e. num is an integer array of size 5 and store 5 integer values char
name[10] ; i.e. name is a char array of size 10 and it can store 10 characters.

float salary[50] ; i.e. salary is a float type array of size 50 and it can store 50
fractional numbers.

Initialization of array:

The array is initialized like follow if we need time of declaration

data_type array_name[size] = {value1, value2, ........, valuen} ;

For eg.
1. int subject[5] = {85, 96, 40, 80, 75} ;
2. char sex[2] = {‘M’, ‘F’} ;
3. float marks[3] = {80.5, 7.0, 50.8} ;
4. int element[5] = {4, 5, 6}
In example(4), elements are five but we are assigning only three values. In this
case the value to each element is assigned like following
element[0] = 4
element[1] = 5
element[2] = 6
element[3] = 0
element[4] = 0

i.e. missing element will be set to zero

Accessing elements of array:


If we have created an array, the next thing is how we can access (read or write)
the individual elements of an array. The accessing function for array is

array_name[index or subscript]

For e.g.
int a[5], b[5] ;
a[0] = 30 ; /* acceptable */
b = a ; /* not acceptable */

if (a<b)
{_ _ _ _ } /* not acceptable */

we can assign integer values to these individual element directly:


a[0] = 30 ;
a[1] = 31 ;
a[2] = 32 ;
a[3] = 33 ;
a[4] = 34 ;

A loop can be used to input and output of the elements of array.

Examples of single dimensional array:


/* Program that reads 10 integers from keyboard and displays entered numbers
in the screen*/
#include<stdio.h>
void main( )
{
int a[10], i ;
clrscr( ) ;
printf (“Enter 10 numbers : \t”) ;
for (i=0 ; i<10 ; i++)
scanf(“%d”, &a[i]) ; /*array input */
printf (“\n we have entered these 10 numbers : \n”) ;
for (i=0 ; i<10 ; i++)
printf (“\ta[%d]=%d”, i, a[i] ) ; /* array output */
getch( ) ;
}

Output:
Enter 10 numbers : 10 30 45 23 45 68 90 78 34 32
We have entered these 10 numbers:

a[0] = 10 a[1] = 30 - - - - - - - - - - - - - - - - -a[9] = 32

/* Program to sort n numbers in ascending order */


void main( )
{
int num[50], i, j, n, temp,
clrscr( ) ;
printf(“How many numbers are there? \t”) ;
scanf(“%d”, &n) ;
printf(“\n Enter %d numbers: \n”, n) ;
for (i=0; i<n; i++)
{
for (j=i+1 ; j<n ; j++)
{
if(num[i]>num[j]) ;
{
temp = num[i] ;
num[i] = num[j] ;
num[j] = temp ;
} /* end of if */
} /*end of inner loop */
} /* end of outer loop */
printf(“\n The numbers in ascending order : \n”) ;
for (i=0 ; i<n ; i++)
printf(“\t%d, num[i] ) ;
getch( );
} /* end of main */

Output:
How many numbers are there? 5
Enter 5 numbers : 12 56 3 9 17
The numbers in ascending order: 3 9 12 17 56

Multi-dimensional arrays:
An array of arrays is called multi-dimensional array. For example a one
dimensional array of one dimensional array is called two dimensional array. A one
dimensional array of two dimensional arrays is called three dimensional arrays,
etc. The two dimensional array are very useful for matrix operations.

Declaration of two dimensional array:


Multidimensional arrays are declared in the same manner as one dimensional
arrayexcept that a separate pair of square brackets are required for each
subscript i.e. two dimensional array requires two pair of square bracket ; three
dimensional array requires three pairs of square brackets, four dimensional array
require four pair of square brackets, etc.

The general format for declaring multidimensional array is


data_type array_name [size1] [size2] ...... [sizen] ;

For example:
1. int n[5] [6] ;
2. float a[6] [7] [8] ;
3. char line [5] [6] ;
4. add [6] [7] [8] [9] ;

Initialization of multidimensional array:


Similar to one dimensional array, multidimensional array can also be initialized.
For e.g.
1.int [3] [2] = {1, 2, 3, 4, 5, 6) ;

The value is assigned like follow:


a[0] [0] = 1 ;
a[0] [1] = 2 ;
a[1] [0] = 3 ;
a[1] [1] = 4 ;
a[2] [0] = 5 ;
a[2] [1] = 6 ;

1. int a[3] [2] = { {1,2}, {3,4}, {5,6} } ;


In above example, the two value in the first inner pair of braces are assigned to
the array element in the first row, the values in the second pair of braces are
assigned to the array element in the second row and so on. i.e.
a[0] [0] = 1 ; a[0] [1] = 2 ;
a[1] [0] = 3 ; a[1] [1] = 4 ;
a[2] [0] = 5 ; a[2] [1] = 6 ;

2. int a[3] [3] = { {1,2}, {4,5}, {7, 8, 9} } ;

In above example, the values are less than the elements of array. The value assign
like
follow:
a[0] [0] = 1 a[0] [1] = 2 a[0] [2] = 0
a[1] [0] = 4 a[1] [1] = 5 a[1] [2] = 0
a[2] [0] = 7 a[2] [1] = 8 a[2] [] = 9
In above example the value zero is assigned to a[0] [2] and a[1] [2] because no
value assigned to these.

3. int a[ ] [3] = {12, 34, 23, 45, 56, 45} ; is perfectly acceptable.

It is important to remember that while initializing a 2-dimensional array, it is


necessary to mention the second (column) size where as first size (row) is
optional.
In above example, value assign like follow
a[0] [0] = 12 a[0] [1] = 34 a[0] [2] = 23
a[1] [0] = 45 a[1] [1] = 56 a[1] [2] = 45
int a[2] [ ] = {12, 34, 23, 45, 56, 45} ;
int a[ ] [ ] = {12, 34, 23, 45, 56, 45} ;
would never work.

Accessing elements of multidimensional array:


We can access the multidimensional array with the help of following accessing
function:

array_name [index1] [index2]. . . . . . . [indexn]

For example:
int a[5] [6] ;

We can write the following statement


1. a[0] [2] = 5 ;
2. x = a [3] [2] ;
3. printf(“%d”, a[0] [0] ) ;
4. printf(“%d”, a[2] [2] ) ;
If we want to read all the elements of above array, we can make a loop like
for (i=0; i<=4; i++)
for (j=0; j<=5; j++)
scanf(“%d”, &a[i] [j] ) ;
If we want to print all the elements of above array a then the loop is
for (i=0;j<=4;i++)
for (j=0; j<=5; j++)
printf(“%d”, a[i] [j] ) ;

Processing an array:

/* Program to read & display 2×3 matrix */


#include<stdio.h>
#include<conio.h>
void main( )
{
int matrix [2] [3], i, j ;
clrscr( ) ;
for (i=0 ; i<2 ; i++)
for (j = 0 ; j<3 ; j++)
{
printf(“Enter matrix [%d] [%d] : \t”, i, j) ;
scanf(“%d”, &matrix [i] [j] ) ;
}
printf (“\n Entered matrix is : \n”) ;
for (i=0 ; i<2 ; i++)
{
printf(“%d\t”, matrix [i] [j]) ;
printf(“\n”) ;
}
getch( )
}

Output:
Enter matrix [0] [0] : 1
Enter matrix [0] [1] : 2
Enter matrix [0] [2] : 3
Enter matrix [1] [0] : 4
Enter matrix [1] [1] : 5
Enter matrix [1] [2] : 6
Enter matrix is
123
456

/* Program to read two matrices and display their sum */


#include<stdio.h>
#include<conio.h>
void main( )
{
int a[3] [3], b[3] [3], s[3] [3], i, j ;
clrscr( ) ;
printf(“Enter first matrix : \n”) ;
for (i=0 ; i<3 ; i++)
{
for (j=0 ; j<3 ; j++)
{
scanf(“%d”, &a[i] [j]) ;
}
}
printf(“Enter second matrix : \n”) ;
for (i=0 ; i<3 ; i++)
{
for (j=0 ; j<3 ; j++)
{
scanf(“%d”, &b[i] [j]) ;
}
}
for (i=0 ; i<3 ; i++)
{
//adding the matrix
for (j=0 ; j<3 ; j++)
{
s[i] [j] = a[i] [j] + b[i] [j] ;
}
}
printf(“\n The sum matrix is : \n”) ;
for (i=0 ; i<3, i++)
{
for(j=0 ; j<3 ; j++)
{
printf(“\t%d”, s[i] [j]) ;
}
printf(“\n”) ;
}
getch( ) ;
}
Output:
Enter first matrix:
123
456
789
Enter second matrix:
987
654
321
The sum matrix is
10 10 10
10 10 10
10 10 10

/* Program to read two matrices and multiply them if possible */


#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10] [10], b[10] [10], s[10] [10] ;
int m, n, l, p, i, j, k ;
clrscr( );
printf(“Enter row of first matrix(<=10) : \t”) ;
scanf(“%d”, &m) ;
printf(“Enter column of first matrix(<=10) : \t”) ;
scanf(“%d”, &n) ;
printf(“Enter row of second matrix(<=10) : \t”) ;
scanf(“%d”, &l) ;
printf(“Enter column of second matrix(<=10) : \t”) ;
scanf(“%d”, &p) ;
if (n!=l)
printf(“Multiplication is not possible :”) ;
else
{
printf(“Enter the first matrix : \n”) ;
for (i=0 ; i<m ; i++)
{
for (j=0 ; j<n ; j++)
{
printf(“Enter a[%d] [%d] : \t”, i, j) ;
scanf (“%d”, &a [i] [j] ;
}
}
printf(“Enter the second matrix : \n”) ;
for (i=0 ; i<l ; i++)
{
for (j=0 ; j<p ; j++)
{
printf (“Enter b[%d] [%d] :\t”, i, j) ;
scanf(“%d”, &b[i] [j] );
}
}
for (i=0 ; i<m ; i++)
{
for (j=0 ; j<p ; j++)
{
s[i] [j] = 0 ;
for (k=0 ; k<n ; k++)
{

s[i] [j] = s[i][j] + a[i] [k] * b[k] [j] ;


}
}
}
printf (“The matrix multiplication is : \n”) ;
for (i=0 ; i<m; i++)
{
for (j=0 ; j<p ; j++)
{
printf(“%d\t”, s[i] [j],
}
printf(“\n”) ;
}
} /* end of else */
getch( ) ;
} /* end of main( ) */
Output:
Enter row of the first matrix (<=10) : 2
Enter column of the first matix (<=10) : 1
Enter row of the second matrix (<=10) : 1
Enter column of the second matrix (<=10) : 2
Enter the first matrix :
Enter a[0] [0] : 2
Enter a[1] [0] : 2
Enter the second matrix :
Enter b[0] [0] : 3
Enter b[0] [1] : 3
The matrix multiplication is :
66
66

Write algorithm, flowchart, source code and output of followings.


1. Declare an integer array naming box1 of size 5 and initialize the array box1
with numbers 0, 11, 22, 33, 44 and declare another float array box2, which
will take user input. Display the contents of both arrays simultaneously.
2. WAP to output the character array in following format.
array[0]= A
array[1]= R
array[2]= R
array[3]= A
array[4]= Y
3. WAP to find the sum of 10 elements of an array. Read all elements from the
user.
4. WAP to find the largest element and smallest element of an array.
5. WAP to sort the contents of an array (ascending and descending).
6. WAP to add two matrices A and B. Display the result in matrix format. Use
function.
7. Write a program to multiply two matrices of orders m x n and p x q. Read
orders from the user. Before multiplying, check that the matrices can
actually be multiplied. If they cannot be multiplied, then display the error
message “Order Mismatch ! Matrices cannot be multiplied !” ///
8. Write a program to find transpose of a m*n matrix. ///
9. Write a C program to read two strings and concatenate them using user
defined functions(without using library functions).
10.WAP in C to read a string . create a user defined function to reverse it and
display in main() function. ///

Assignments:
Write algorithm, flowchart, source code and output of followings.
1. WAP that will enter a line of text, store it in a array and then display it
backwards.
2. WAP that will examine how many characters are letters, how many are
digits, how many are white space characters in a line.
3. WAP that will examine each character in character type array and
determine how many vowels and consonants are there.
4. WAP that will examine each character in character type array called text
and print out the result by converting the lowercase letter to uppercase
and vice versa.
5. WAP that will examine each character in character type array called text
and print result by replacing all the vowels by “*” character.
6. WAP to count the frequency of character present in a line of text and print
the result in screen.
7. WAP to read “n” number of person’s age in an array and print minimum,
maximum and average age.
8. WAP to take co-ordinate of ten different points and find the distance
between first point and last point where distance is the sum of the distance
between consecutive points not a displacement. ///
9. WAP that will input name of ten persons and display the result by printing
in ascending order. ///
10.WAP that accepts a sentence of words and counts number of words that a
sentence has then display each words of the sentence in different lines.///
11.WAP to take input 3*3 matrixes and create a new matrix by replacing all
the elements of previous matrix by 15 if the element of previous matrix is
less than 5.
12.Write two 3*4 matrix A and B in your program and print them. And obtain
matrix C=2*(A+B) and print.
13.WAP to take two 1- dimensional array of size n and m and merge them into
a single array with size n + m. And display them. ///

You might also like