Lecture - 6
Lecture - 6
Lecture 6
Arrays
an array is a collection of variables of the same type that are referred to
through a common name. A specific element in an array is accessed by an index.
In C/C++, all arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element. Arrays
may have from one to several dimensions.
#include <iostream.h>
main()
{
int x[10];
int t;
for(t=0; t<10; t++)
x[t] = t;
for(t=0; t<10; t++)
cout<< x[t]<<" ";
}
In this example if we need input the elements of array by using cin function we
write the following code :
Array Initialization:
Arrays can be initialized by a comma-separated list of expressions enclosed
in braces:
int [4] = { 9, 8, 7, 2 }; // This means a[0]=9, a[1]=8, a[2]=7, a[3]=2
When the list of initializers is shorter than the size of the array, the remaining
elements are initialized to 0. If uninitialized, external and static arrays are
automatically initialized to 0.
This is not so for automatic arrays, which start with undefined values.
An array declared with an explicit initializer list and no size expression is given
the size of the number of initializers. The following two arrays are equivalent:
int a[] = { 3,5,7 };
int a[3] = { 5,7,8};
Example1 : Input the elements of array x[10] then find out the minimum and
maximum element .
#include<iostream.h>
main)(
{
int x[10],i,max,min;
for(i=0;i<10;i++)
cin>>x[i];
max=x[0];
for(i=1;i<10;i++)
if(x[i]>max)
max=x[i];
min=x[0];
for(i=1;i<10;i++)
if(x[i]<min)
min=x[i];
cout<<"max= "<<max<<endl;
cout<<"min= "<<min<<endl;
}
Two-Dimensional Arrays:
C++ support multidimensional arrays. The simplest form of the
multidimensional array is the two-dimensional array. A two-dimensional array
(Matrix) is, essentially, an array of one-dimensional arrays. The general form for
declaring of a Two dimensional array is :
type var_name[number of rows][number of columns];
To declare a two-dimensional integer array d of size 10, 20 you would
write int d[10][20];
Pay careful attention to the declaration. Some other computer languages use
commas to separate the array dimensions; C++, in contrast, places each dimension
in its own set of brackets. Similarly, to access point 1,2 of array d, you would use
d[1][2]
#include <iostream.h>
main()
{
int j, i, num[3][4];
In this example, num[0][0] has the value 1, num[0][1] the value 2, num[0][2] the
value3, and so on. The value of num[2][3] will be 12 . You can visualize the num
array as shown here :
0 1 2 3
0 1 2 3 4
1 5 6 7 8
2 9 10 11 12
Example7 : Inputs the elements of matrix a[4][6] and find out the average
of elements of it .
#include <iostream.h>
main()
{
int t, i, a[4][6],sum=0;
float av;
for(i=0; i<4; ++i)
for(j=0;j<6; ++j)
cin>>a[i][j];
cout<<av;
Work Sheet
Q1: Write a C++ program to input elements of array and compute the number of zeros in
the array.
Q2: Write a C++ program to print whether the array’s elements are in order or not.
Q3: Write a C++ program to input temperatures over the 30 days into array and
calculate the average of them .
Q4: Write C++ program, to read 3*4 2D-array, then find the summation of each
column.
Q5: Write C++ program, to replace each element in the second diameter (diagonal) with
zero.
Q6: Write C++ program, to input the elements of 2D array and replace the elements of
the main diameter with the elements of the second diameter.
Q7: Write C++ program, to input the elements of 2D array then find the summation of
odd numbers in 2D-array.
Q8: Write C++ program, to find (search) X value in 2D-array, and return the index of its
location.
Q9: Write C++ program, to convert 1D-array that size [16] to 2D-array that size of
[4] [4].
Q10: Write C++ program, to read A[n][n] of character, then find array B and array C,
such that B contain only capital letters and C contain only small letters.
Q11: Write C++ program, to read A[n][n] of numbers, then put 99 instead each even
positive number.
Q12: Write C++ program, to read A[n][n] of numbers, then put 10 instead each even
positive number in the first diagonal.
Q13: Write C++ program, to read A[n][n] of numbers, then find the minimum number
in array.
Q14: Write C++ program, to find the greatest number in the second diagonal, in 3*3
array.
Q15. Write a C++ program to input elements of 1D array A[6] and create array fact
each element in fact represent the factorial of one element from array A , then print
the array fact.
Q16. Write a C++ program to input elements of two arrays A[5] , B[5] and create array
C where C=power ( A , B ), then print the array C.
Q17. Write a C++ program to input elements of array A[10] and swap between
maximum and minimum elements in array.
Q18. Write a C++ program to input elements of array A[10] and find the number of
occurrence of the value X in array A .
Q19. Write a C++ program to input elements of two arrays A[10], B[5] and merge
them in one array C[15], then print array C.
Q20. Write a C++ program to input elements of array A[10] and find the average
of even and odd numbers.
Q21. Write a C++ program to input elements of two arrays L[10] represents the lengths
of 10 rectangles and W[10] represents the widths of same rectangles and find the
two arrays A and M that represent the area and circumference of these rectangles.
Q22. Write a C++ program to input elements of two arrays A[5] , B[5] and print
whether these arrays are equal or not.
Q23. Write a C++ program to input elements of two arrays A[10] , B[10] and find and
print the arrays C = A union B and D =A intersection B.
Q24. Write a C++ program to input elements of array A[10] and print the array A after
sorting it in ascending order.
Q25. Write a C++ program to input elements of array A[10] , B[5] and print whether the
B is subset of A or not.