structured data type arrays in c++...structured data type arrays in c++ objectives declaration of...
Post on 03-Sep-2020
45 Views
Preview:
TRANSCRIPT
ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY
STRUCTURED DATA TYPE ARRAYS IN C++
ObjectivesDeclaration of 1-D and 2-D Arrays
Initialization of arrays
Inputting array elements
Accessing array elements
Manipulation of 1-D array elements (Sum, Product, Average,
Maximum/Minimum, Linear search)
Manipulation of 2- D array elements (Sum of row elements, column
elements, diagonal elements, finding maximum/minimum values)
An Array is a series of elements of the same data type placed
in contiguous memory locations referred by a common name.
The elements of array are referred by their Subscript number
or Index number which starts from zero.
Array
One Dimensional Array
One Dimensional or Single Dimensional Arrays comprises of definite number of homogeneous elementsSyntax to declare 1-D Array is :
datatype NameOfArray [ SizeOfArray] ;
Example:int Num[5]; // Num can store 5 integers char Str[10]; // Str can store a single string
101 103 105 107 109
Num[0] Num[1] Num[2] Num[3] Num[4]
H A N S R A J \0Str[0] Str[1] Str[2] Str[3] Str[4] Str[5] Str[6] Str[7] Str[8] Str[9]
Memory Usage of 1-D Array
Memory Consumed by 1D Array = Size of Data Type X No. of Elements
Example:
int Num[5]; = 2 Byte X 5 Elements = 10 Bytes
char Str[10]; = 1 Byte X 10 Elements = 10 Bytes
float Amt[3]; = 4 Byte X 3 Elements = 12 Bytes
Two Dimensional Array
Two Dimensional comprises of an Array of elements each of which is itself an Array
Syntax to declare 2-D Array is :
datatype NameOfArray [ NoOfRows] [ NoOfColumns] ;
Example:
int arr[4][4];
COL=0 COL=1 COL=2 COL=3
ROW=043
arr[0] [0]
22arr[0] [1]
16arr[0] [2]
34arr[0] [3]
ROW=167
arr[1] [0]
26arr[1] [1]
98arr[1] [2]
12arr[1] [3]
ROW=254
arr[2] [0]
24arr[2] [1]
89arr[2] [2]
64arr[2] [3]
ROW=323
arr[3] [0]
34arr[3] [1]
78arr[3] [2]
29arr[3] [3]
Memory Usage of 2-D Array
Memory consumed by 2D Array = Size of Data Type X No. of Rows X No. of Columns
Example:
int Num[4][3] ; = 2 Byte X 4 Rows X 3 Cols = 24 Bytes
char Str[5][10]; = 1 Byte X 5 Rows X 10 Cols = 50 Bytes
float Amt[[5][4]; = 4 Byte X 5 Rows X 4 Cols = 80 Bytes
Initialization of 1-D Array ElementsAt the time of declaration array elements can be initialized as follows:int Num[4] = { 54,65,72,34 };char Str[4] = { ‘C’, ‘+’ , ’+’ , ’\0’ };char Str[4] = ”C++”;
Note:
int A[5] = {21, 45, 67, 43, 23, 87, 90, 100};// Error because number of values between braces {} is greater than the array sizeint A[5] = {34, 56, 22}; // If less initializers , the remaining elements are set to default values i.e A[3] = 0, A[4] = 0
Unsized Array Initialization of 1-D Arrays :
If you skip the size of the array in the array initialization statement, compiler automatically creates an array big enough to hold all initializers present in the statement.int Arr[] = { 500,600,700 };char Str[] = ”Hello”;
54 65 72 34Num[0] Num[1] Num[2] Num[3]
C + + \0Str[0] Str[1] Str[2] Str[3]
500 600 700Arr[0] Arr[1] Arr[2]
H e l l o \0Str[0] Str[1] Str[2] Str[3] Str[4] Str[5]
Initialization of 2-D Array Elements
Example:int arr[2][3]={43,22,16,67,26,98};// can store a 2x3 Matrix of Integers
char arr[2][5]={ ″AMAN ″, ″RAMA″ };
// can store 2 Strings of maximum 5 chars each
Unsized Array Initialization of 2-D Arrays :2-D Arrays can be initialized in an unsized manner, but you can skip the Row size only and the Column size needs to be mentioned in the declaration.int arr [][2]={500,600,700,800,900};
Advantage of Unsized Array Initialization It allows you to change the array content i.e lengthen or shorten the number of elements without resizing the Array
COL=0 COL=1 COL=2
ROW=043
arr[0] [0]
22arr[0] [1]
16arr[0] [2]
ROW=167
arr[1] [0]
26arr[1] [1]
98arr[1] [2]
COL=0 COL=1
ROW=0500
arr[0] [0]
600arr[0] [1]
ROW=1700
arr[1] [0]
800arr[1] [1]
ROW=2900
arr[2] [0]
0arr[2] [1]
COL=0 COL=1 COL=2 COL=3 COL=4
ROW=0A
arr[0] [0]
Marr[0] [1]
Aarr[0] [2]
Narr[0] [3]
\0arr[0] [4]
ROW=1R
arr[1] [0]
Aarr[1] [1]
Marr[1] [2]
Aarr[1] [3]
\0arr[1] [4]
Inputting Array Elements in 1-D Arrays
#define MAX 5
Accepting input in 1-D integer array:int num[MAX];
//can store MAX integersfor(int i=0;i<MAX;i++)
cin>>num[i];
Accepting input in 1-D character array:char arr[MAX]; //can store only one string containing MAX-1 chars
cin>>arr;or
gets(arr);
Num[0] Num[1] Num[2] Num[3] Num[4]
Str[0] Str[1] Str[2] Str[3] Str[4]
Inputting Array Elements in 2-D Integer Arrays
#define MAX 4
Accepting input in 2-D integer array:
int arr[MAX][MAX];//can store a matrix of MAX * MAX integers
//Row wise input:for(int i=0;i<MAX;ifor(int j=0;j<MAX;j++)
cin>>arr[i][j];
//Column wise input:for(int j=0;j<MAX;j++)for(int i=0;i<MAX;i++)
cin>>arr[i][j];
Col 0 Col 1 Col 2 Col 3
Row 0
Row 1
Row 2
Row 3
Col 0 Col 1 Col 2 Col 3
Row 0
Row 1
Row 2
Row 3
Inputting Array Elements in 2-D Character Arrays
#define MAX 4
Accepting input in 2-D character array:
char arr[MAX][MAX];//can store MAX strings - each containing MAX-1 chars
for(int i=0;i<MAX;i++)gets(arr[i]);
String 0S
arr[0] [0]
Aarr[0] [1]
Tarr[0] [2]
\0arr[0] [3]
String 1M
arr[1] [0]
Aarr[1] [1]
Tarr[1] [2]
\0arr[1] [3]
String 2X
arr[2] [0]
Aarr[2] [1]
Tarr[2] [2]
\0arr[2] [3]
String 3C
arr[3] [0]
Aarr[3] [1]
Tarr[3] [2]
\0arr[3] [3]
Assignment Statement
Array elements can be assigned a new value during the execution of the program. Example:
int A[5];A[3] = 4;A[1] = A[3] + 10;A[4] = A[1] + A[3];A[2] = 7 – A[4];A[0] = A[1] + A[4];
4
A[0] A[1] A[2] A[3] A[4]
14 4
A[0] A[1] A[2] A[3] A[4]
14 4 18
A[0] A[1] A[2] A[3] A[4]
14 -11 4 18
A[0] A[1] A[2] A[3] A[4]
32 14 -11 4 18
A[0] A[1] A[2] A[3] A[4]
Passing 1-D arrays to functions
Arrays are always automatically passed by reference. Thus when you specify the name of a 1-D integer array as an argument to a function,
only the starting address of the array i.e the address of the 0th element is received by the function.
This function has no means to find out how many elements are present in the array, since integer arrays are not terminated by null character ‘\0’
Therefore you need to pass a 2nd separate argument to the called function which is the size of the array.
Function Prototype should be:
returndatatype function_name ( datatype arrayname[], int NoOfElements );
Sum of elements of 1-D array#include <iostream.h>#define MAX 10int SUM (int[], int); //prototypevoid main(){ int A[MAX], N, Ans;
cout << “Enter the number of locations ”; cin >> N;for (int I = 0; I <N; I++)
{ cout << “Enter data in A[“ << I << “] = “;cin >> A[I];
}Ans = SUM (A, N);cout << “Sum of elements = “ << Ans;
}int SUM( int Arr[], int n){ int S = 0;
for (int I = 0; I < n; I++)S = S + Arr[I];
return S; }
Product of elements of 1-D array#include <iostream.h>#define MAX 10int PROD (int[], int);void main(){ int A[MAX], N, Ans;
cout << “Enter the number of locations ”; cin >>N;
for (int I = 0; I < N; I++){ cout << “Enter data in A[“ << I << “] = “;
cin >> A[I];}
Ans = PROD (A, N);cout << “Product of elements = “ << Ans;
}int PROD( int Arr[], int n){ int P = 1;
for (int I = 0; I < n; I++)P = P * Arr[I];
return P; }
Average of elements of 1-D array#include <iostream.h>#define MAX 10float Average (int[], int); //prototypevoid main(){ int A[MAX], n;
float Ans;cout << “Enter the number of locations ”; cin >> n;
for (int I = 0; I < N; I++){ cout << “Enter data in A[“ << I << “] = “;
cin >> A[I];}
Ans = Average (A,N);cout << “Average of elements = “ << Ans;
}float Average ( int Arr[], int n){ int S = 0;
for (int I = 0; I < n; I++)S = S + Arr[I];
return float(S)/n; }
top related