0% found this document useful (0 votes)
2 views

Aarray_DS

The document provides an overview of arrays in C++, detailing one-dimensional, two-dimensional, and three-dimensional arrays. It includes syntax, examples, and code snippets for declaring and initializing arrays, as well as methods for user input and output. The document serves as a guide for understanding and using arrays in C++ programming.

Uploaded by

Muhammad Sufyan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Aarray_DS

The document provides an overview of arrays in C++, detailing one-dimensional, two-dimensional, and three-dimensional arrays. It includes syntax, examples, and code snippets for declaring and initializing arrays, as well as methods for user input and output. The document serves as a guide for understanding and using arrays in C++ programming.

Uploaded by

Muhammad Sufyan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Array

C++ provides a data structure, the array, which stores a fixed-size


sequential collection of elements of the same type. An array is used to
store a collection of data, but it is often more useful to think of an array as a
collection of variables of the same type.

Types of Array:
1. One-Dimensional Array
2. Multi-Dimensional Array

One-Dimensional Array:
A type of array in which all elements are arranged in the form of a list is
known as one-dimensional array.it is also called single-dimensional array. It
consists of one column or one row.

Syntax
type arrayName [ arraySize];

Example
int foo [5];
int foo [5] = {16, 2, 77, 40, 12071 } ;

This statement declares an array that can be represented


like this:

Example
#include<iostream>
using namespace std;
int main()
{
int arr[4];
cout<<"enter the value for index 0"<<endl;
cin>>arr[0];
cout<<"enter the value for index 1"<<endl;
cin>>arr[1];
cout<<"enter the value for index 2"<<endl;
cin>>arr[2];
cout<<"enter the value for index 3"<<endl;
cin>>arr[3];
cout<<"enter the value for index 4"<<endl;
cin>>arr[4];

cout<<"value are"<<endl;
cout<<arr[0]<<endl;
cout<<arr[1]<<endl;
cout<<arr[2]<<endl;
cout<<arr[3]<<endl;
cout<<arr[4]<<endl;
}

Using Loop to input a One-Dimensional Array from


user:

#include<iostream>
using namespace std;
int main()
{
int foo [5];
cout<<"enter the five values"<<endl;

for(int x=0;x<5;x++)
{
cin>>foo[x];
}

cout<<"orignal values of an Array"<<endl;

for(int y=0;y<5;y++)
{
cout<<foo[y]<<endl;
}
return 0; }
Two-Dimensional Array:
Two-dimensional array can be considered as a table that consists of rows
and column. Each element in 2-D array is referred with the help of two
indexes. one index is used to indicate the row and second index indicates
the column of the element.

Syntax
type arrayName [ size][size];

Example
Int jimmy [3][5];

Initialization of Two-Dimensional Array:

int jimmy [3][5] = {2, 4, 5, 9, 0, 19, 23, 47, 5, 91, 3, 11, 55, 93, 6};
#include<iostream>
using namespace std;
int main()
{
int arr[2][3];
cout<<"enter the value"<<endl;
cin>>arr[0][0];
cout<<"enter the value"<<endl;
cin>>arr[0][1];
cout<<"enter the value"<<endl;
cin>>arr[0][2];
cout<<"enter the value"<<endl;
cin>>arr[1][0];
cout<<"enter the value"<<endl;
cin>>arr[1][1];
cout<<"enter the value"<<endl;
cin>>arr[1][2];

cout<<"value are"<<endl;
cout<<arr[0][0]<<endl;
cout<<arr[0][1]<<endl;
cout<<arr[0][2]<<endl;
cout<<arr[1][0]<<endl;
cout<<arr[1][1]<<endl;
cout<<arr[1][2]<<endl;
}

Using Loop to input a Two-Dimensional Array from


user:

#include<iostream>

using namespace std;

int main()

int arr [3][5];

for(int x=0;x<3;x++)

for(int a=0;a<5;a++)

cin>>arr[x][a];

}
for(int y=0;y<3;y++)

for(int b=0;b<5;b++)

cout<<arr[y][b];

return 0; }

Three-Dimensional Array:
Syntax
type arrayName [ size][size][size];

Initialization of three-dimensional array:

int test[2][3][4] = {3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23,


2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};
Using Loop to input a three-Dimensional Array from
user:

#include<iostream>

using namespace std;

int main()

int number [3][2][3];

cout<<"Enters the values"<<endl;

for(int x=0;x<3;x++)

for(int a=0;a<2;a++)

for(int d=0;d<3;d++)

cin>>number[x][a][d];

}
cout<<"values are"<<endl;

for(int y=0;y<3;y++)

for(int b=0;b<2;b++)

for(int e=0;e<3;e++)

cout<<number[y][b][e]<<endl;

return 0; }

You might also like