In: Computer Science
Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function that, given an arbitrary 2-d array as input, returns its perimeter sum, which is defined as the sum of all the elements along its perimeter. You must name your function perimeter_sum
Solution :-
In the question, it was asked to write the perimeter_sum function in either c++ or python.
So here I have written the program in C++.
In the question, it is being asked to write a function named perimeter_sum(), that takes input a 2D array and returns the sum of the elements present in the perimeter of the 2D array.
How do we get the sum of all the elements that are present in the edge of the 2D array :-
Step 1 :-
The rows and columns that are present in the array is known from the beginning. We can use this information to calculate the total sum of all the elements present in the perimeter of the 2D matrix.
Step 2 :-
As we know that arrays in C++ are 0 based indexed, that means the index of the first element starts from the 0th index and goes till n-1 index where n is the total number of elements present in the array. So we have 4 cases for the perimeters of the 2D array.
Case 1 :-
Row = 0 and the value of Column is varied form 0 to Column - 1.
Case 2 :-
Column = Column - 1 and the Row value is varied from 0 to Row - 1.
Case 3 :-
Row = Row - 1 and the value of Column is varied form Column - 1 to 0.
Case 4 :-
Column = 0 and the Row value is varied from Row - 1 to 0.
Step 3 :-
Subtracting the value of every corner element from the total sum that is being calculated, as the corner elements have been considered twice.
Step 4 :-
If the number of row or column is 1, then the elements present in the inside and not on the edges are counted twice. Hence, they also need to be subtracted.
Step 5 :-
Returning back the value of Total perimeter sum as an integer type.
Following the above steps, the program is written to calculate the sum of all the numbers present in the perimeter,
The source code is written below :-
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
using namespace std;
int perimeter_sum(int A[][500], int R, int C) // the function calculates the total sum of all the elements that are present in the perimeter of the matrix.
{
int sum = 0; // the variable in which the total sum is stored.
for(int i = 0; i<C; i++) // sum of all the elements when the Row is 0th and the column keeps changing.
{
sum += A[0][i];
}
for(int i = 0; i<R; i++) // sum of all the elements when the Column is (C-1)th and the Row keeps changing.
{
sum += A[i][C-1];
}
for(int i = 0; i<C; i++) // sum of all the elements when the Row is (R-1)th and the column keeps changing.
{
sum += A[R-1][i];
}
for(int i = 0; i<R; i++) // sum of all the elements when the Column is 0th and the Row keeps changing.
{
sum += A[i][0];
}
sum = sum - (A[0][0] + A[0][C-1] + A[R-1][C-1] + A[R-1][0]); // we have considered all the corner elements 2 times each, hence subtracting each corner elements.
if(R == 1) // if the number of row is just one the the elements prenent on the inside except the edge has also been calculated twice, hence we need to subtract them.
{
for(int i = 1; i<C-1; i++)
{
sum -= A[0][i];
}
}
if(C == 1) // if the number of column is just one the the elements prenent on the inside except the edge has also been calculated twice, hence we need to subtract them.
{
for(int i = 1; i<R-1; i++)
{
sum -= A[i][0];
}
}
return sum; // finally returning the total sum.
}
int main() {
int Row, Column;
cout<<"Enter the rows and columns respectively\n"; // Entering the values of Rows and Columns.
cin>>Row>>Column;
int M[500][500]; // declaring the 2D matrix.
cout<<"Enter the elements of the 2D array.\n";
for(int i = 0; i<Row; i++) // Entering every element if the matrix.
{
for(int j = 0; j<Column; j++)
cin>>M[i][j];
}
int totalsum = perimeter_sum(M, Row, Column); // calling the perimeter_sum function to calculate the total sum of the perimeter.
cout<< "The total perimeter sum of the matrix is "<<totalsum<<endl; // finally displaying the perimeter sum value.
return 0;
}
Some screenshots of the output of the program is shown below :-
In this case, the number of rows are 4 and the number of columns are 5.
The sum of all the elements present in the perimeter are 3 + 4 + 2 + 1 + 8 + 2 + 0 + 7 + 2 + 3 + 5 + 1 + 2 + 3 = 43
In this case, the number of rows are 5 and the number of columns are 7.
The sum of all the numbers in the perimeter is = 5 + 3 + 2 + 5 + 2 + 1 + 1 + 6 + 2 + -8 + 22 + 5 + 2 + -6 + 3 + 6 + 4 + 0 + 7 + 0 = 62.
In this case, the number of row is 1 and the number of columns are 7.
The sum of all the numbers in the row is = 4 + 2 + 5 + 2 + 5 + 6 + 7 = 31.
In this case, the number of rows are 6 and the number of column is 1.
The sum of all the numbers of the column is 4 + 9 + 0 + 4 + 2 + 1 = 20.