Lecture 6 Arrays
Lecture 6 Arrays
By
0 31
1 34
WeeklyTemp[0] = 31;
WeeklyTemp[2]= 38; 2 38 4 bytes
WeeklyTemp[4] =30 ; 3 36
4 30
5 29
6 26
cout<< WeeklyTemp[0];
cout<< WeeklyTemp[2];
cout<< WeeklyTemp[4];
Declaring Array Variables
datatype arrayName[arraySize];
Example:
double myList[10];
int size = 4;
double myList[size]; // Wrong
const int size = 4;
double myList[size]; // Correct
double myList[20]; // Correct
Input/Output of Array elements
int marks[3];
marks[0] = 76;
marks[1] = 65;
marks[2] = 27;
cout<<marks[2]<<marks[0]<<marks[1];
Input/Output of Array elements –
Using Loops
int marks[5];
for(int i=0;i<5;i++)
cin>>marks[i];
for(int j=0;j<5;j++)
cout<<marks[j];
Indexed Variables
• Array elements are accessed through the index.
• Array indices are 0-based; that is, they start from 0
to arraySize-1.
For example:
int marks[5];
Five int values:
marks[0], marks[1], marks[2], marks[3],
marks[4]
Using array values:
marks[2] = marks[1] + marks[0]
Using Indexed Variables
• An indexed variable can be used in the same
way as a regular variable.
• For Example:
• Example: …
Arbitrary Initial Values
• When an array is created, its elements are
assigned with arbitrary values.
int marks[5];
for(int i=0;i<5;i++)
cout<<marks[i];
1D Array Values Example
int n[10]; //n is an array of 10 integers
// initialize elements of array n to 0
for (int i = 0; i<10; i++) {
n[i] = i + 100;
}
cout<<"Element"<<setw(13)<<"Value"<<endl;
• Example:
int myList[4] = {32, 11, -6, 65};
What about:
int List2[4];
List2= {32, 11, -6, 65};
Declaring, creating, initializing Using the
Shorthand Notation
int myList[4];
myList[0] = 32;
myList[1] = 11;
myList[2] = -6;
myList[3] = 65;
Initializing Wrong way
int myList[4];
int myList[10];
• Row B
Example output:
Enter a number to search: 44
44 is at location 6
C-Strings or Character Arrays
• The elements of an array can be just about
anything (any-datatype)
For example:
Bubble Sort
• Repeatedly stepping through the array to be sorted,
comparing each pair of adjacent items and swapping
them if they are in the wrong order. The pass through
the array is repeated until no swaps are needed (which
indicates that the list is sorted)
Sorting An Array(Ascending)
(Using bubble sort)
Sorting An Array(Ascending)
int a[4]={4,19,1,3};
int i,j,temp;
for (i=3; i>=1; i--)
{
for (j=0; j<i; j++)
if (a[j] > a[j+1])
{ temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
cout<<“Displaying sorted array”;
for (i=0; i<4; i++)
{ cout<<array[i];
}
Sorting An Array(Ascending)
int a[10]={33,10,1,87,6,44,23,3,11,82};
int i,j,temp;
int N=10;
datatype arrayName[rowSize][coulmnSize];
Example:
double myList[2][4];
Coulmn Indexes
In memory:
0 1
0 22 33
Row Indexes 1 44 55
2 66 77
Initialization Examples
int temp[4][3] = {{50, 70, 60}, {48, 75, 62},
{51, 69, 60}, {52, 78, 63}};
//Declaration
double table[10][10];
V 0 1
1 2
2 3
Example: Computations
• Compute the average value of an matrix with n
rows and m columns.
double sum=0;
double average;
double sum=0;
double rowAverage;
average = sum / c;
Outputting 2D Arrays
• Two dimensional arrays are often printed in a
row by row format, using nested for statements.
double Coord[100][100][100];
Single 1D Array
value 2D Array 3D Array
Larger-Dimension Arrays
• Arrays with more than two dimensions allowed in
C++ but not commonly used
Example:
1, 2, 3, 4,
1 2 3 4
5, 6, 7, 8,
5 6 7 8
Output
9, 10, 11, 12,
9 10 11 12
13, 14, 15, 16,
13 14 15 16
(Nested Loops) – Example Program-2
- Write a program to that creates a matrix of size 5 by 5 (5
Columns, and 5 Rows). The program should ask the user
to enter values in each matrix element. Then the program
should display the matrix Coulmn-wise.
Example:
1, 5, 9, 13,
1 2 3 4
2, 6, 10, 14,
5 6 7 8
Output
3, 7, 11, 15,
9 10 11 12
4, 8, 12, 16,
13 14 15 16
(Nested Loops) – Example Program-3
- Write a program to that creates a matrix of size 10 by 10
(10 Columns, and 10 Rows). The program should ask the
user to enter values in each matrix element. Then the
program should display the left-diagonal elements of the
matrix.
Example (5 by 5 matrix):
1 2 3 4
5 6 7 8
Output 1, 6, 11, 16,
9 10 11 12
13 14 15 16
Output ?
Example-4: Zero Matrix
• Write a program that creates a matrix of 3 by 3 (3
rows, and 3 coulmns). Get input values from the user
for the complete matrix. Then, the program should
determine whether the matrix is a “Zero” matrix (all
elements are zero) or not.
Example-5: Coulmn sum
• Write a program that creates a matrix of 4x4 (4 rows,
and 4 coulmns). Get input values from the user for
the complete matrix. The program should calculate
and print the sums of each individual coulmn.
Example-6: Matrix Vector
• Write a program that creates a matrix of 3 by 3 (3
coulmns, 3 rows). Create a vector/array having 3
elements. The program should multiply the matrix
with the given vector and print the resultant matrix in
proper format.
Example-6: Matrix Vector-code
int A[4][4];
Int B[4]; int C[4]; int r;
// Get input in the “A” matrix and “B”
array
for(int i=0; i<4; i++)
{
r = 0;
for(int j=0;j<4; j++)
r = r + A[i][j] * B[j];
C[i] = r;
}