0% found this document useful (0 votes)
13 views12 pages

MUCLecture 2024 4213248

Array in c++

Uploaded by

jameskulwa2002
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)
13 views12 pages

MUCLecture 2024 4213248

Array in c++

Uploaded by

jameskulwa2002
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/ 12

Al- Mustaqbal University

College of Sciences
Department of Cybersecurity

‫كلية العلوم‬
‫قسم األمن السيبراني‬

Lecture: (1)
Arrays

Subject: Structured Programming


First Stage
Lecturer: Asst. Prof. Dr. Ali Kadhum Al-Quraby

Page |1 Study Year: 2023-2024


Arrays:
An array is a variable that can store multiple values of the same type. Suppose a
class has 27 students, and we need to store the grades of all of them. Instead of creating
27 separate variables, we can simply create an array.
An array is a consecutive group of homogeneous memory locations.
Each element (location) can be referred to using the array name along with
an integer that denotes the relative index of that element within the array.
The data items grouped in an array can be simple types like int or float;
or can be user-defined types like structures and objects.

1. Array of one dimension:


It is a single variable that specifies each array element. The general
declaration of one- dimensional arrays is:

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

Initializing array elements:


The initializing of array can be done in several methods such as:
- The first element of array named age: age [0] = 18;
- The last element of array age: age [9] = 19;
- All elements of array age:
age [9] = { 18, 17, 18 ,18 ,19, 20 ,17, 18 ,16,19 };
- int x [ ] = { 12, 3, 5, 0, 11, 7, 30, 100, 22 };
- int y [10] = { 8, 10, 13, 15, 0, 1, 17, 22};

Accessing array elements:


We can access each array element by written the name of array, followed
by brackets delimiting a variable (or constant) in the brackets which are
called the array index.

- Accessing the first element of array num to variable x.


x = num [0];
- Print the second element of the array age:
- cout<<age[1];
- Accessing the last element of array num to variable y:
y = num [9];
- cout << num [0] + num [9];
- num [0] = num [1] + num[2];
- num [7] = num [7] + 3;

Input/print /process array elements:


Herein several C++ codes illustrate how to input, print, and processing of num
array elements:
Input Processing Printing
sum=0; cout << num [4];
for (int i=0;i<10;i++){ cout>>num[9];
cin>>num[10];
sum=sum+num[i]; cout>>num[0];
} cout>>num[2]+4;
cin>>num[0]; for (int i=0; i<10; i++){ for (int i=0; i<10; i++) {
cin>>num[2]; num[i]=num[i]+1; cout << num[ i ];
} }
num[0]=num[2]+num[3]; if ( num [5] > 5 )
for (int i=0<10; i++){
num[1]=num[2]*num[3]; {
cin>>num[i];
num[2]=num[2]*2; cout << “greater”;
}
num[3]=num[0]+num[5]; }

Another examples of C++ code:


int mark[5] = {19, 10, 8, 17, 9};
19 10 8 17 9 Value
marks
0 1 2 3 4 Index

Now, change 4th element to 50


mark[3] = 50;
- The result is:
19 10 8 50 9 Value
marks
0 1 2 3 4 Index
Then, take input from the user and store the input at the third index. Suppose
that the input value is 150.
cin >> mark[2];
- The result is:
19 10 150 50 9 Value
marks
0 1 2 3 4 Index
And, take input from the user and insert it into ith index. Suppose that the value
of i is 2 and the input value is 20.
cin >> mark[i-1];
- The result is:
19 20 150 50 9 Value
marks
0 1 2 3 4 Index

Print first element of the array


cout << mark[0];
- The result is:
19
Print ith element of the array. Suppose that the i is 5.
cout >> mark[i-1];
- The result is:
9
Ex 1:
- Write a C++ program to read 1-D array and print it.
#include <iostream.h>
#include <conio.h> Output
int main()
The numbers are: 7 5 6 12 35
{
int numbers[5] = {7, 5, 6, 12, 35};
cout << "The numbers are: ";
for (int i = 0; i < 5; i++)
{
cout << numbers[i] << " ";
}
return (0);
}
Ex 2:
- Write a C++ program to take five inputs from user and store them in an array
called number. After that, print them.
#include <iostream.h>
#include <conio.h>
void main()
{
int numbers[5]; Output
cout << "Enter 5 numbers: " << endl; Enter 5 numbers:
for (int i = 0; i < 5; i++) 12
{ 13
cin >> numbers[i]; 14
}
15
cout << "The numbers are: ";
16
for (int n = 0; n < 5; n++)
The numbers are: 12 13 14 15 16
{
cout << numbers[n] << " ";
}
}
Ex 3:
- Write a C++ program to add 1 to all elements of array called number. After that,
print the result.
#include <iostream.h>
void main()
{ int numbers[5] = {7, 5, 6, 12, 35}, i ;
cout << "The numbers are: ";
for (i = 0; i < 5; i++)
{
cout << numbers[i] << " ";
}
for (i = 0; i < 5; i++)
{
numbers[i]=numbers[i]+1;
}
cout<<"\n";
cout << "The numbers after adding are: ";
for (i = 0; i < 5; i++)
{
cout << numbers[i] << " "; Output
} The numbers are: 7 5 6 12 35
} The numbers after adding are: 8 6 7 13 36

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.

You might also like