Question

In: Computer Science

Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function...

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

Solutions

Expert Solution

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.


Related Solutions

Write a user defined function named “findTwosComplement” in C++, which will find 2’s complement of a...
Write a user defined function named “findTwosComplement” in C++, which will find 2’s complement of a signed number. Input must be passed by reference to the function and it will return the answer. There is no need to call this function anywhere. You can use following operators only: <<>> ~ & ^ | Decimal
Write a C++ function that lets the user enter alphabet letters into a static char array...
Write a C++ function that lets the user enter alphabet letters into a static char array until either the user enters a non-alphabet letter or, it has reached the MAXSIZE. You can use the isalpha([Char]) function to check if the input is an alphabet letter or not. void fillArray (char ar[], size_t& size){ // this is the function prototype }
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write Matrix Addition 2 D (dimensional) Array program in c++.
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the travellingpointer(1stversion) notation to traverse the array. The function has the following prototype. int maximum ( int *p [ ], int n); 2. Implement the function psum( )that is passed an array of n floats and returns a pointer to the sum of such an array. Print the...
Please solve the following problem for MATLAB Write a user-defined function (name it F to C)...
Please solve the following problem for MATLAB Write a user-defined function (name it F to C) that converts temperature in degrees F to temperature in degrees C. Use the function to solve the following problem. The change in the length of an object, ∆L, due to a change in the temperature, ∆T, is given by: ∆L = αL∆T, where a is the coefficient of thermal expansion. Determine the change in the area of a rectangular(4.5 m by 2.25m) aluminum (α=23·10-61/˚C)...
Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
note: USE C++ WITH USER DEFINED FUNCTIONS AND RECURSIVE FUNCTION ONLY. No C++ library function is...
note: USE C++ WITH USER DEFINED FUNCTIONS AND RECURSIVE FUNCTION ONLY. No C++ library function is allowed. The game of “Jump It” consists of a board with n positive integers in a row, except for the first column, which always contains zero. These numbers represent the cost to enter each column. Here is a sample game board where n is 6: 0 3 80 6 57 10 The object of the game is to move from the first column to...
how to write a unit test for a write function in python? is there a better...
how to write a unit test for a write function in python? is there a better way than running main once and checking whether the new file path exists?
C++ [2] Write a program that prompts the user to enter a non-negative decimal number and...
C++ [2] Write a program that prompts the user to enter a non-negative decimal number and a base in the range 2 <= base <= 16. Write a function multibaseOutput() that displays the number in the specified base. The program terminates when the user enters a number of 0 and a base 0. Run: Enter a non-negative decimal number and base (2 <= B <= 16) or 0 0 to terminate: 155 16     155 base 16 is 9B Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT