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

Recommended Answers

All 3 Replies

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.

#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;
    }

Thanks for doing his homework for him.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.