Home > OS >  Update 2-dimentional array in a loop (c )
Update 2-dimentional array in a loop (c )

Time:05-19

The first 2 if statements (i==0 and i==1) work fine but not the others. I don't want to store a value in a variable for each addition, but to use an array that it's going to change with each loop.

#include <iostream>
using namespace std;

int main(){
    
    int G[3][3];
    int SL[3];
    int SC[3];
    int i,j;
    
    cout<<"fill the array: "<<endl;
    for(i=0;i<3;i  ){
        for(j=0;j<3;j  ){
            cin>>G[i][j];   
        }
    }
    
    for(i=0;i<3;i  ){
        for(j=0;j<3;j  ){
            
            if(i==0){
                SL[i]=SL[i] G[i][j];
            }
            
            if(i==1){
                SL[i]=SL[i] G[i][j];
            }
            
            if(i==2){
                SL[i]=SL[i] G[i][j];
            }
        
            if(j==0){
                SC[j]=SC[j] G[i][j];
            }
            
            if(j==1){
                SC[j]=SC[j] G[i][j];
            }
            
            if(j==2){
                SC[j]=SC[j] G[i][j];
            }
            
        }
    }
    
    for(i=0;i<3;i  ){
        for(j=0;j<3;j  ){
        cout<<G[i][j];
        }
        cout<<"\n";
    }
    
    cout<<"\n";
    
    for(i=0;i<3;i  ){
        cout<<SL[i];
        cout<<"\n";
    }
    
    cout<<"\n";
    
    for(i=0;i<3;i  ){
        cout<<SC[i];
        cout<<"\n";
    }
    
}

CodePudding user response:

For starters these arrays if they are declared in a function (for example in main)

int SL[3];
int SC[3];

are not initialized. So statements like this

SL[i]=SL[i] G[i][j];

invoke undefined behavior.

There is no great sense to use numerous if statements.

What you need is the following

const size_t M = 3, N = 3;
int G[M][N] = {};
int SL[M] = {};
int SC[N] = {};

std::cout << "fill the array: " << std::endl;

for ( size_t i = 0; i < M; i    )
{
    for ( size_t j = 0; j < N; j   )
    {
        std::cin >> G[i][j];   
    }
}

for ( size_t i = 0; i < M; i   )
{
    for ( size_t j = 0; j < N; j   )
    {
        SL[i]  = G[i][j];
        SC[j]  = G[i][j];
    }
}

//...

Declare the variables i and j where they are used that is in for loops. Also it is a bad idea to use upper case letters for identifier names.

Here is a demonstration progran.

#include <iostream>

int main()
{
    const size_t M = 3, N = 3;

    int a[M][N] =
    {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 }
    };

    int b[M] = {};
    int c[N] = {};

    for ( size_t i = 0; i < M; i   )
    {
        for ( size_t j = 0; j < N; j   )
        {
            b[i]  = a[i][j];
            c[j]  = a[i][j];
        }
    }

    for ( const auto &item : b )
    {
        std::cout << item << ' ';
    }

    std::cout << '\n';

    for ( const auto &item : c )
    {
        std::cout << item << ' ';
    }

    std::cout << '\n';
}

The program output is

6 15 24 
12 15 18 

CodePudding user response:

For the kind of vectorized math that's in linear algebra, which your problem resembles, you might consider the standard library's valarray class. The loops for vectorized operations are already present in its overloaded operators, and its constructor will zero-fill (or fill with a provided value).

While valarray doesn't have native multi-dimensional indexing, it does support "slices", allowing you to work with individual columns and rows of an array that is nominally 1-D.

const size_t R = 3, C = 3;
valarray<int> G(R * C), SR(R), SC(C);

cout << "Fill the grid: " << endl;
for (auto e = begin(G); e != end(G);   e) {
  cin >> *e;
}

for (size_t c = 0; c < C; c  ) {
  // sum of rows requires adding a column at a time
  SR  = valarray<int>(G[slice(c,     R, C)]);
}
for (size_t r = 0; r < R; r  ) {
  // sum of columns requires adding a row at a time
  SC  = valarray<int>(G[slice(r * C, C, 1)]);
}

cout << "SR: "; copy(begin(SR), end(SR), ostream_iterator<int>(cout, " ")); cout << endl;
cout << "SC: "; copy(begin(SC), end(SC), ostream_iterator<int>(cout, " ")); cout << endl;

For this example, it may not look simpler, but if you are translating algorithms from, say, NumPy or MATLAB, it may be a better match.

Note that you can also ask valarray to do the sums for you, if you prefer. It may be easier to think about as follows:

for (size_t r = 0; r < R; r  ) {
  SR[r] = valarray<int>(G[slice(r * C, C, 1)]).sum();
}
for (size_t c = 0; c < C; c  ) {
  SC[c] = valarray<int>(G[slice(c,     R, C)]).sum();
}
  • Related