0

I have a code, which creates an array for at least 4 rows and columns randomly or type from keyboard. Then it calculates a sum of each row an column. The problem is that i don't have any idea how to delete a row and a column which sum is 0.

I wondered maybe a row move to upside and column to the leftside and then create a new array with smaller size and starting with element [1][1] in the previous one.

Here is my code so far:

#include <iostream>

using namespace std;

int i, j;

int main(){
    int m, n, sum;
    bool answer;
    string an;
    string y;
    do{
        cout << "Firstly, could you enter a size of first dimension of an array(at least 4): \n";
        cin >> m;}while(m <= 3);
    do{
        cout << "Firstly, could you enter a size of first dimension of an array(at least 4): \n";
        cin >> n;}while(n <= 3);
    int array[m][n];
    do{
        answer = false;
        cout << "Select what type of filling the array you want\n";
        cout << "If you want to select a filling from keyboard type Y\n";
        cout << "If you want to select a random filling type N\n";
        cin >> y;
        if(y == "Y" || y == "y"){
            cout << "*****You chose manual filling*****\n";
            for(i=0;i<m;i++){
                for(j=0;j<n;j++){
                    cin >> array[i][j]; 
                }
            }
        }
        else if(y == "N" || y == "n"){
            cout << "*****You chose a random filling*****\n";
            for(i=0;i<m;i++){
                for(j=0;j<n;j++)
                array[i][j] = (rand() % (m+n) - ((m+n)/2));
            }
            cout << endl;
        }
        else{
            cout << "You didn't choose a type!\n";
            cout << "Do you want to continue? Type Y, if you want: \n";
            cin >> an;
            if(an == "Y" || an == "y") answer = true;
        }
    }while(answer);
    cout << "Here is your array: \n";
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            cout << array[i][j] << "\t";}
        cout<<endl;               
    }
    for(i=0;i<m;i++){
        sum = 0;
        for(j=0;j<n;j++){
            sum += array[i][j];
        }
        cout << "Sum of elements of Row " << i+1 << ": " << sum << endl;}
    for(i=0;i<n;i++){   
        sum = 0;
        for(j=0;j<m;j++){
            sum += array[j][i];
        }
        cout << "Sum of elements of Column " << i+1 << ": " << sum << endl;
    }

}   
  • [C++ doesn't have variable-length arrays](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Whatever source taught you to use those, or to use global variables, or to not include needed header files (like ``), should be viewed with great suspicion. – Some programmer dude Dec 01 '22 at 14:04
  • 1
    Regarding arrays, even if your compiler allow the non-standard and non-portable extension of VLA's, it's an array. And arrays have a fixed size, you can not change its size once defined. For "arrays" whose size is known only at run-time, and that needs to be dynamic (i.e. elements can be removed) then use `std::vector` instead. – Some programmer dude Dec 01 '22 at 14:05
  • `int array[m][n];` - which C++ textbook are you using, where you read something like that? We need to figure which textbook gives out such bad information, that can't even be compiled by many C++ compilers, as this is not standard C++. – Sam Varshavchik Dec 01 '22 at 14:13

0 Answers0