3-Arrays 1
3-Arrays 1
Community
Newcomers
Training
Arrays
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:
int x[5];
Arrays
Initializing Arrays :
OR
x[0] = 5;
x[1] = 20 + x[0];
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];
}
char name1[4];
name1[0] = 'A';
name1[1] = 'l';
name1[2] = 'i';
char name5[4];
cin >> name5;
char name4[4];
1 2 3
4 5 6
7 8 9
#include <iostream>
using namespace std;
int main() {
int a[2][3];
int a[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
#include <iostream>
using namespace std;
int main() {
int a[2][3];
*******
* *
* *
* *
* *
* *
*******
● 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 ?
For more information about 2D Arrays visit this Link
Arrays Sheet
Good luck <3