Tribhuvan University Himalaya College of Engineering C-Programming (BCE/BEX/BCT (I/I) ) Lab-7: Arrays
Tribhuvan University Himalaya College of Engineering C-Programming (BCE/BEX/BCT (I/I) ) Lab-7: Arrays
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:
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:
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
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 */
Output:
Enter 10 numbers : 10 30 45 23 45 68 90 78 34 32
We have entered these 10 numbers:
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.
For example:
1. int n[5] [6] ;
2. float a[6] [7] [8] ;
3. char line [5] [6] ;
4. add [6] [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.
For example:
int a[5] [6] ;
Processing an array:
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
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. ///