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

3-Arrays 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views21 pages

3-Arrays 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Arrays

If we want to take from user 10 integers ?


In normal, you will think to create 10 integer
variables.
But
If we want to take from user 1000 integers
that's look so boring.
So
in C++ there is something will
help you in that called Arrays
Arrays
• Array : is a series of elements of
the same type placed in contiguous
memory locations that can be
individually referenced by adding an
index to a unique identifier
Arrays
Declaring Arrays :
• Like a regular variable, an array must be
declared before it is used. A typical declaration
for an array in C++ is:

DataType Name [Number of elements];

int x[5];
Arrays
Initializing Arrays :

int x[5] = {1, 2, 3, 4, 5} ;

OR

int x[] = {1, 2, 3, 4, 5} ; size of array will be 5


Arrays
Arrays
Accessing the values of an array:
#include <iostream>
using namespace std;
int main() {
int x[1000];

x[0] = 5;
x[1] = 20 + x[0];

x[2] = x[0] * x[1];

int y = x[0];

return 0;
}
Arrays
Accessing the values of an array using loops:
#include <iostream>
using namespace std;
int main() {
int x[10];
for(int i = 0; i < 10; i++){
cin >> x[i];
}

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


cout << x[i] << endl;
}
return 0;
}
Problems
1. Write a program to take from user N numbers and
print their Summation.

2. Write a program to take array of size N from user and


number X then check if X is exist in array or not.

3. Write a program to take a 10 numbers from user and


print largest and smallest numbers.

4. Write a program to take array of size N from user


then if number even change its value to 0 otherwise
to 1 then print the array.
Arrays
Array of Char Declaring and Initializing
#include <iostream>
using namespace std;
int main() {

char name1[4];
name1[0] = 'A';
name1[1] = 'l';
name1[2] = 'i';

char name5[4];
cin >> name5;

char name2[4] = {'A', 'l', 'i'};


//char name2[4] = {'A', 'l', 'i', '\0'};
char name3[4] = "Ali";

char name4[4];

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


cin >> name4[i];
Arrays
Array of Char print to user
#include <iostream>
using namespace std;
int main() {
char name1[4];
cin >> name1;

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


cout << name1[i];
}
cout << endl;

cout << name1 << endl;


return 0;
}
Problems
● Write a program to take array of char of
size N from user then convert every char
that are upper to lower and every char
that are lower to upper.

● Write a program to take array of char of


size N form the user then convert every
char in an even index into upper char
and every char in the odd index into
lower char.
Bubble Sort
- It is a simple sort algorithm that make a sequence of elements sorted
in increasing order.

- It works by repeatedly swapping the adjacent elements if they are in


wrong order.
Bubble Sort
#include<iostream>
using namespace std;
int main ()
{
int i, j, n = 10;
int a[10] = {10,2,0,14,43,25,18,1,5,45};

for(i=0; i < (n-1); i++)


{
for(j=0; j < (n-i-1); j++)
{
if(a[j] > a[j+1])
{
swap(a[j], a[j + 1]);
}
}
}

cout << "Sorted Element List ...\n";


for(i = 0; i < 10; i++) {
cout << a[i] << " ";
}
return 0;
}
Problems
● Write a program to take array of char of size N
from user, reverse it and print it.

● Write a program to take array of integer of size N


from user, and print the Maximum number and
the 2nd Maximum number in the array.
Arrays 2D
● If you have to take something like a Matrix as input like this :

1 2 3
4 5 6
7 8 9

and do some operations in each cell,


how could you take it ?

So, here we introducing the 2D array (Multidimensional array)


Arrays 2D
It is Multidimensional arrays can be described as
“Arrays of Arrays” , and it's like matrix.
Arrays 2D
Declaring and assign values to array 2D

#include <iostream>
using namespace std;
int main() {
int a[2][3];

int a[2][3] = {
{1, 2, 3},
{4, 5, 6}
};

for(int i = 0; i < 2; i++){


for(int j = 0; j < 3; j++){
cin >> a[i][j];
}
}
return 0;
}
Arrays 2D
Declaring and assign values to array 2D

#include <iostream>
using namespace std;
int main() {
int a[2][3];

for(int i = 0; i < 2; i++){


for(int j = 0; j < 3; j++){
cin >> a[i][j];
}
}

for(int i = 0; i < 2; i++){


for(int j = 0; j < 3; j++){
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
Problems
● Write a program to take an array 2D from user and
print largest and smallest number in each row.

● Write a program that takes 2D array of integers of size (N x N)


from the user and print the summation of even numbers in
each row and the summation of odd numbers in each row.

● Write a program that takes 2D array of integers of size (N x N)


from the user and print the summation of prime numbers in
each row.
Problems
● Write a program to print this 2D array of char of size N * N
if N = 7, so the 2D array is :

*******
* *
* *
* *
* *
* *
*******
● Write a program that takes 2D array of integer of size (N x N) and replace each
even number with (0) and each odd number with (1) and print the 2D array.
Problems
● You and your friend are lost in 2D array and you want to know
the minimum distance between yours, you know your index
(x1, y1) and your friend`s index (x2, y2), what is distance
between yours ?

You might also like