MUCLecture 2024 4213248
MUCLecture 2024 4213248
College of Sciences
Department of Cybersecurity
كلية العلوم
قسم األمن السيبراني
Lecture: (1)
Arrays
dataType arrayName[arraySize];
For example,
int age [10];
int num
[30]; float
degree[5];
char a [15];
The item in an array are called elements (in contrast to the items in a
structure which are called members). The elements in an array are of the
.same type only the values vary
Ex 4:
- Write a C++ program to read 4 numbers and print it in reverse order.
#include <iostream> Output
void main() Enter 4 numbers:
{
0: 10
int a [4],i;
cout << "Enter 5 numbers: \n"; 1: 20
for ( i =0; i <4; i++ ) 2: 30
{ 3: 40
cout << i << ": "; The reverse order is:
cin >> a [ i ];
cout << "\n"; 3: 40
} 2: 30
cout << "The reverse order is: \n"; 1: 20
for ( i =3; i >=0; i-- ) 0: 10
{
cout << i << ": " << a [ i ] << endl;
}
}
Ex 5:
- Write a C++ program, to find the summation of array elements. Suppose
that the size of array is 3.
#include <iostream.h>
void main() Output
{ Enter 3 numbers
int a [3], sum = 0; Enter the number:7
cout << "Enter 3 numbers\n"; Enter the number:5
for ( int i =0; i <3; i++ )
Enter the number: -1
{ cout << "Enter the number:";
The sum is: 11
cin >> a [ i ];
sum = sum+a [ i ];
}
cout << "The sum is: " << sum << endl;
}
Ex 6:
- Write a C++ program, to split the odd numbers and even numbers of one
array into two arrays.
void main()
{ int a [ 10 ]= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, odd[10], even [10], i , o=0, e=0;
cout<<"The number of array are:"<<endl;
for ( i=0 ; i<10; i++ )
{ cout<<a[i]<<" ";
}
for ( i=0 ; i<10; i++ ) Output
{ if (a[i] % 2 !=0)
The number of array are:
{ odd[o]=a[i];
o=o+1; 1 2 3 4 5 6 7 8 9 10
} The odd numbers are:
else 1 3 5 7 9
{ even[e]=a[i]; The even numbers are:
e=e+1; 2 4 6 8 10
}
}
cout<<endl;
cout<<"The odd numbers are:"<<endl;
for ( i=0 ; i<o; i++ )
{
cout<<odd[i]<<" ";
}
cout<<endl;
cout<<"The even numbers are:"<<endl;
for ( i=0 ; i<e; i++ )
{
cout<<even[i]<<" ";
}
}
Ex 7:
- Write a C++ program, to add two arrays. Then, print the result.
#include<iostream.h>
int main()
{ Output
int first[20], second[20], sum[20], c, n;
Enter the number of elements: 3
cout << "Enter the number of elements:"; Enter elements of first array
cin >> n; 1
cout << "Enter elements of first array" << endl; 2
for (c = 0; c < n; c++) 3
Enter elements of second array
{ cin >> first[c];
4
}
5
cout << "Enter elements of second array" << endl; 6
for (c = 0; c < n; c++) Sum of elements of the arrays:
{ cin >> second[c]; 5
} 7
9
cout << "Sum of elements of the arrays:" << endl;
for (c = 0; c < n; c++)
{ sum[c] = first[c] + second[c];
cout << sum[c] << endl;
}
}
Homework:
1. Which command shall we write in order to print the second element of
array distance [] = { 23, 7, 1, 4, 6} ?
2. What is the output of the following C++ Program?
#include<iostream.h>
void main ( )
{
int number[3]=[10,2,33]
number [0]= number[0]+3;
cout << number [1];
}
3. What are the first and last Indexes of the below array?
int num [9];
4. What is the output of the following C++ Program?
#include<iostream.h>
int main()
{
int ary[4];
ary[4] = {1,2,3,4};
cout << ary[2];
return 0;
}
5. What is the output of the following C++ Program?
#include<iostream.h>
int main()
{ int ary[] = {10,20,30},
for (int i=0;i<3;i++)
{ cout<< ary[i]+1<<” “; }
}
6. What is the output of the following C++ Program?
int main ()
{ int array[] = {0, 2, 4, 6, 7, 5, 3};
int n, result = 0;
for (n = 0 ;n < 8 ;n++) {
result += array[n];
}
cout << result;
}
7. Write a c++ code to print the third element of the array age:
8. Suppose that you have the following array:
int mark [5] = {19, 10, 8, 17, 9};
change 2nd element to 17.
9. What is the output of the following C++ Program?
int main ()
{ int mark[] = {4, 2, 4, 6, 7};
mark[0]=mark[0]+mark[0];
mark[1]=mark[1]+mark[4];
mark[3]=1+mark[4];
cout<< mark[0]+11<<endl;
cout<< mark[1] -1<<endl;
cout<< mark[3]+2 <<endl;
cout<< mark[4]*2;
}
10.Write a C++ program to subtract 1 from all elements of array called degree.
After that, print the result. Suppose that the array has 6 elements.
11.Write a C++ program, to sum the positive numbers of array called degree.
Suppose that the size of array is 5.
12.Write a C++ program, to sum the negative numbers of array called degree.
Suppose that the size of array is 5.
13.Write a C++ program, to sum the odd numbers of array called degree. Suppose
that the size of array is 5.
14.Write a C++ program, to sum the even numbers of array called degree.
Suppose that the size of array is 5.
15.Write a C++ program, to split the positive numbers and negative numbers of
one array into two arrays (one for positive number and the other for the
negative number)
16.Write a C++ program, to find the average of the student. Suppose that the
student has 7 degrees.
17. Write a C++ program, to subtract one array from the other array. Then, print
the result.
18. Check whether each of the following statements is true or false and correct
the false statement.
The dimension of array num [2] is 2.
In C++ programming language, in order to print the second element
of array named BOOK we must write the command cout<<BOOK
[2];
In C++ programming language, we can write the command int age
[3] = {1,2,3];
Suppose that we have array called num [4] = {1,2,3,4}. The result of
num [0] +num [2] is 4.
Suppose that we have array age [3] = {20,19,12}, we can write a
C++ command:
cin >> age [ 2];
Suppose that we have array salary [3] = {10,20,30} and sum=1. The
result of sum=sum+a[1] is 21.
Suppose that we have array called money [4] = {1,2,4}. The result of
money [0] +money [2] +1 is 5.