PF Week 8
PF Week 8
PF Week 8
OBJECT: To Understand Array Fundamentals, Defining Arrays, Array Elements, Accessing Array
Elements, Initializing Arrays, Operations on Arrays Multidimensional Arrays, Two
Dimensional Array
ARRAY INTRODUCTION
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. Instead of declaring individual variables, such as number0, number1, ..., and
number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ...,
numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All
arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the
highest address to the last element.
DECLARING ARRAYS
To declare an array in C++, the programmer specifies the type of the elements and the number of elements
required by an array as follows:
type arrayName [ arraySize ];
This is called a single-dimension array. The arraySize must be an integer constant greater than zero
and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type
double, use this statement
double balance[10];
INITIALIZING ARRAYS
You can initialize C++ array elements either one by one or using a single statement as follows −
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
The number of values between braces { } can not be larger than the number of elements that we declare for
the array between square brackets [ ]. Following is an example to assign a single element of the array −
If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you
write
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
You will create exactly the same array as you did in the previous example.
balance[4] = 50.0;
The above statement assigns element number 5th in the array a value of 50.0. Array with 4th index will be
5th, i.e., last element because all arrays have 0 as the index of their first element which is also called base
index. Following is the pictorial representation of the same array we discussed above
#include<iostream>
using namespace std;
int main() {
int arr[5];
int k;
return 0;
}
Exercise 1: Write a program that takes input of 5 elements for array int a[5] and the calculate the
sum and average of the elements of array.
#include<iostream>
using namespace std;
int main() {
int arr[10];
int loc=0, large, k;
for (k=0 ; k<10 ; k++){
cout<<"Enter element No." << k+1 << ": ";
cin>>arr[k];
}
large=arr[0];
for (k=0 ; k<10 ; k++)
if (large < arr[k]){
loc = k;
large=arr[k];
}
cout<<"The largest element is:" << arr[loc];
return 0;
}
PROGRAM 3: Demonstrates array initialization and shows days from start of year to date specified
#include<iostream>
using namespace std;
int main() {
int month, day, total_days;
int days_per_month[12] = { 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
cout << "\nEnter month (1 to 12): "; //get date
cin >> month;
cout << "Enter day (1 to 31): ";
cin >> day;
total_days = day; //separate days
for(int j=0; j<month-1; j++) //add days each month
total_days += days_per_month[j];
cout << "Total days from start of year is: " << total_days
<< endl;
return 0;
}
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
int main() {
const int size=5;
int roll_no[size];
int marks[size];
float per[size];
char grade[size];
if(per[i]>=80) grade[i]='A';
else if(per[i]>=70 && per[i]<80) grade[i] = 'B';
else if(per[i]>=60 && per[i]<70) grade[i] = 'C';
else if(per[i]>=50 && per[i]<60) grade[i] = 'D';
else if(per[i]>=40 && per[i]<50) grade[i] = 'P';
else grade[i] = 'F';
}
return 0;
}
Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is the name of
the array, and i and j are the subscripts that uniquely identify each element in a.
The above statement will take 4th element from the 3rd row of the array. You can verify it in the above
diagram.
int main() {
int arr[5][3];
cout<<"An Integer Double Dimensional Array..."<<endl;
cout<<"Enter elements"<<endl;
for (int r=0 ; r<5 ; r++)
for (int c=0; c<3; c++) {
cout<<"Enter arr[" << r << "]["<<c<<"]:";
cin>>arr[r][c];
}
cout<<"The elements in array are:"<<endl;
for (int r=0 ; r<5 ; r++){
for (int c=0; c<3; c++)
cout<<arr[r][c]<<" ";
cout<<endl;
}
return 0;
Exercise 3: Write a program in C++ for addition of two Matrices of same size (3 x 3).
Input the element of the square matrix (3 x 3 )
Input elements in the first matrix:
[0][0] : 9
[0][1] : 8
[0][2] : 7
[1][0] : 6
[1][1] : 5
[1][2] : 4
[2][0] : 3
[2][1] : 2
[2][2] : 1