0% found this document useful (0 votes)
21 views10 pages

Lecture - 6

This document is a lecture on arrays in C/C++, covering their definition, declaration, initialization, and manipulation. It includes examples of single-dimensional and two-dimensional arrays, demonstrating how to input, process, and output array elements. Additionally, it provides a worksheet with programming exercises related to arrays.

Uploaded by

umarhadi10109
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)
21 views10 pages

Lecture - 6

This document is a lecture on arrays in C/C++, covering their definition, declaration, initialization, and manipulation. It includes examples of single-dimensional and two-dimensional arrays, demonstrating how to input, process, and output array elements. Additionally, it provides a worksheet with programming exercises related to arrays.

Uploaded by

umarhadi10109
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/ 10

Programming Fundamentals Arrays

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.

Single Dimensional Arrays:


The general form for declaring a single-dimension array is
type var_name[size];
Like other variables, arrays must be explicitly declared so that the compiler may
allocate space for them in memory. Here, type declares the base type of the array,
which is the type of each element in the array, and size defines how many
elements the array will hold. For example, to declare a 100 element array called
balance of type double, use this statement:
double balance[100];
An element is accessed by indexing the array name. This is done by placing the
index of the element within square brackets after the name of the array. For
example,
balance[3] = 12.23;
assigns element number 3 in balance the value 12.23. In C/C++, all arrays have 0
as the index of their first element. Therefore, when you write int p[10];
you are declaring a integer array that has ten elements, p[0] through p[9]. For
example, the following program loads an integer array with the numbers 0 through
9:

Lecturer : Mohsin Hasan Huseein 1


Programming Fundamentals Arrays
Lecture 6

#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 :

for(i=0; i<10; i++)


cin>>x[i];

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

Lecturer : Mohsin Hasan Huseein 2


Programming Fundamentals Arrays
Lecture 6

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

Lecturer : Mohsin Hasan Huseein 3


Programming Fundamentals Arrays
Lecture 6

Example2 : Write a C++ program to input elements of two arrays a[10],b[10]


then add them in c[10] and print array c .
#include<iostream.h>
#include<conio.h>
main)(
{
int a[10],b[10],c[10],i;
cout<<"Enter the elements of a"<<endl;
for(i=0;i<10;i++)
cin>>a[i];
cout<<"Enter the elements of b"<<endl;
for(i=0;i<10;i++)
cin>>b[i];
for(i=0;i<10;i++)
{ c[i]=a[i]+b[i];
cout<<c[i];
} }

Example3: Write a C+ program to input the elements of array a[10] and


multiply it by 3 (a=a*3) then print the resulted array
#include<iostream.h>
main()
{
int a[10],i;
for(i=0;i<10;i++)
cin>>a[i];
for(i=0;i<10;i++)
a[i]=a[i]*3;
for(i=0;i<10;i++)
cout<<a[i]<<" ";
}

Lecturer : Mohsin Hasan Huseein 4


Programming Fundamentals Arrays
Lecture 6

Example4: Write a C++ program to Input the elements of array arr[10]


and reverse it and print the result.
#include<iostream.h>
main()
{
int arr[10],i,temp;
for(i=0;i<10;i++)
cin>>arr[i];
for(i=0;i<(10/2);i++)
{
temp=arr[i];
arr[i]=arr[10-1-i];
arr[10-1-i]=temp;
}
for(i=0;i<10;i++)
cout<<arr[i]<<" ";
}

Example5: Write a C++ program to input the elements of array a[10]


and split it into two arrays the first one for even elements and second for
odd .
#include<iostream.h>
main()
{
int a[10],even[10],odd[10],i,j=0,k=0;
for(i=0;i<10;i++)
cin>>a[i];
for(i=0;i<10;i++)
if(a[i]%2==0)
even[j++]=a[i];
else
odd[k++]=a[i];
for(i=0;i<j;i++)
cout<<even[i]<<" ";
cout<<endl;
for(i=0;i<k;i++)
cout<<odd[i]<<" ";
}

Lecturer : Mohsin Hasan Huseein 5


Programming Fundamentals Arrays
Lecture 6

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]

Example6: load a two-dimensional array with the numbers 1 through 12


prints them row by row.

#include <iostream.h>
main()
{
int j, i, num[3][4];

for(i=0; i<3; ++i)


for(j=0;j<4; ++j)
num[i][j] = (i*4)+j+1;

for(i=0; i<3; ++i)


{
for(j=0; j<4; ++j)
cout<< num[i][j] ;
cout<<endl;
}
}

Lecturer : Mohsin Hasan Huseein 6


Programming Fundamentals Arrays
Lecture 6

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

for(i=0; i<4; ++i)


for(j=0;j<6; ++j)
sum=sum + a[i][j];
av=sum/24.0;

cout<<av;

Lecturer : Mohsin Hasan Huseein 7


Programming Fundamentals Arrays
Lecture 6

Example8: Write a C program to input the elements of 2D-array m[4][3]


and find the following :
1- print the elements of first column.
2- Summation of all elements of m.
3- Summation of each row of m.
4- Exchange the elements of first row with last row.
#include<iostream.h>
main()
{
int m[4][3],i,j,temp,sum_all=0,sumrow;
for(i=0;i<4;i++)
for(j=0;j<3;j++)
cin>> m[i][j];
: ‫جواب النقطة االولى‬
for(i=0;i<4;i++)
cout<<m[i][0]<<endl;

: ‫جواب النقطة الثانية‬


for(i=0;i<4;i++)
for(j=0;j<3;j++)
sum_all= sum_all+m[i][j];
cout<<" The sum_all= "<<sum_all<<endl;
: ‫جواب النقطة الثالثة‬
for(i=0;i<4;i++)
{ sumrow=0;
for(j=0;j<3;j++)
sumrow = sumrow +m[i][j];
cout<<"The sum of row "<<i<<" is"<<sumrow<<endl;
}
: ‫جواب النقطة الرابعة‬
for(j=0;j<3;j++)
{
temp= m[0][j];
m[0][j]= m[3][j];
m[3][j]=temp;
}
‫طباعة المصفوفة بعد التبديل بين عناصر الصف االول مع الصف االخير‬
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
cout<<m[i][j]<<" ";
cout<<endl;
}
}

Lecturer : Mohsin Hasan Huseein 8


Programming Fundamentals Arrays
Lecture 6

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.

Lecturer : Mohsin Hasan Huseein 9


Programming Fundamentals Arrays
Lecture 6

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.

Lecturer : Mohsin Hasan Huseein 10

You might also like