0

Hello Every body

My question is very simple for you all experts, as I am learning how to write C++ program. I need a code for summing up the 4 corner of 2D array in turbo C++.

Thank you all with regards

3
Contributors
3
Replies
18
Views
3 Years
Discussion Span
Last Post by Ancient Dragon
1

In a M by N array, where M is the number of rows and N is the number of columns, the array has these coordinates

Upper left: 0,0
Lower left: (M-1),0
Upper Right: 0,(N-1)
Lower Right: (M-1),(N-1)

The way I understand your assignment is to find the sum of those four cells.

0
#include <iostream>
#include <iomanip>

int main()
    {
    srand(time(NULL));
    const int a=5;
    const int b=5;
    int arr[a][b];
    for(int i=0; i<a; i++){
        for(int j=0; j<b; j++){
            arr[i][j] = rand()%9+1;
            std::cout << std::setw(4) << arr[i][j];
            }
        std::cout << "\n";
        }
    std::cout << "\n";

    std::cout << "corner: " << arr[0][0] << " " << arr[a-1][0] << " " << arr[0][b-1] << " " << arr[a-1][b-1] << "\n\n";
    std::cout << "cornerSum=" << ( arr[0][0] + arr[0][b-1] + arr[a-1][0] + arr[a-1][b-1] ) << "\n\n";

    system("PAUSE");
    return 0;
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.