C++ Doubt-Basic Solution Northwest Corner

Asked

Viewed 99 times

0

I’m kind of forgotten about programming and I have a math algorithm to do, it’s the transportation problem, I’m in the northwest corner phase.

My doubt is in a build error.

Code:

#include<iostream>
#include<conio.h>
using namespace std;

int minimo(int valor1,int valor2)
{
    if(valor1<valor2)return valor1;
    else return valor2;
}


int main()
{
    int n,m,i,j;
    cin>>n>>m;
    int matriz[n][m];
    for(i=0;i<n-1;i++)for(j=0;j<m-1;j++)cin>>matriz[i][j];

    i=0;
    j=0;

    int x,y;
    x=n-1;
    y=m-1;

    while( ((i<=m-2)&&(i<=n-2)) && ((j<=m-2)&&(j<=n-2)) )
    do
{ matriz[i][j]=minimo(matriz[x,j],matriz[i,y]);
if(matriz[i][j]==matriz[x,j]){matriz[x,j]=0;matriz[i,y]-=matriz[i][j];}
if(matriz[i][j]==matriz[i,y]){matriz[i,y]=0;matriz[x,j]-=matriz[i][j];}

if(matriz[x,j]==0)i++;
 if(matriz[i,y]==0)j++;}

    return 0;
}

That is the mistake:

C:\Users\acer\Desktop\transporte.cpp||In function 'int main()':|
C:\Users\acer\Desktop\transporte.cpp|28|error: invalid conversion from 'int (*)[(((sizetype)(((ssizetype)m) + -1)) + 1)]' to 'int' [-fpermissive]|
C:\Users\acer\Desktop\transporte.cpp|5|error:   initializing argument 1 of 'int minimo(int, int)' [-fpermissive]|
C:\Users\acer\Desktop\transporte.cpp|28|error: invalid conversion from 'int (*)[(((sizetype)(((ssizetype)m) + -1)) + 1)]' to 'int' [-fpermissive]|
C:\Users\acer\Desktop\transporte.cpp|5|error:   initializing argument 2 of 'int minimo(int, int)' [-fpermissive]|
C:\Users\acer\Desktop\transporte.cpp|29|error: ISO C++ forbids comparison between pointer and integer [-fpermissive]|
C:\Users\acer\Desktop\transporte.cpp|29|error: incompatible types in assignment of 'int' to 'int [(((sizetype)(((ssizetype)m) + -1)) + 1)]'|
C:\Users\acer\Desktop\transporte.cpp|29|error: incompatible types in assignment of 'int' to 'int [(((sizetype)(((ssizetype)m) + -1)) + 1)]'|
C:\Users\acer\Desktop\transporte.cpp|30|error: ISO C++ forbids comparison between pointer and integer [-fpermissive]|
C:\Users\acer\Desktop\transporte.cpp|30|error: incompatible types in assignment of 'int' to 'int [(((sizetype)(((ssizetype)m) + -1)) + 1)]'|
C:\Users\acer\Desktop\transporte.cpp|30|error: incompatible types in assignment of 'int' to 'int [(((sizetype)(((ssizetype)m) + -1)) + 1)]'|
C:\Users\acer\Desktop\transporte.cpp|39|error: expected 'while' before numeric constant|
C:\Users\acer\Desktop\transporte.cpp|39|error: expected '(' before numeric constant|
C:\Users\acer\Desktop\transporte.cpp|39|error: expected ')' before ';' token|
||=== Build failed: 13 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

1 answer

0


You are incorrectly referencing matrix indexes in some parts of the code. matriz[x,j] for example. The correct is matriz[x][j]

Another thing. There is no while (condição) do {}. The correct is while (condição) { } or do {} while (condição). Taking into account this error, and the previous error, I venture to say that you are migrating from pascal to C.

I made the repairs on the syntax of your code, but I didn’t check the functionality, because I don’t even know what it should do.

#include<iostream>
#include<conio.h>
using namespace std;

int minimo(int valor1,int valor2)
{
    if(valor1<valor2)return valor1;
    else return valor2;
}


int main()
{
    int n,m,i,j;
    cin>>n>>m;
    int matriz[n][m];
    for(i=0;i<n-1;i++)for(j=0;j<m-1;j++)cin>>matriz[i][j];

    i=0;
    j=0;

    int x,y;
    x=n-1;
    y=m-1;

    while( ((i<=m-2)&&(i<=n-2)) && ((j<=m-2)&&(j<=n-2)) ) 
    {
       matriz[i][j]=minimo(matriz[x][j],matriz[i][y]);

        if(matriz[i][j]==matriz[x][j]){
            matriz[x][j]=0;matriz[i][y]-=matriz[i][j];
        }

        if(matriz[i][j]==matriz[i][y]){
            matriz[i][y]=0;matriz[x][j]-=matriz[i][j];
        }

        if(matriz[x][j]==0)
        i++;
        if(matriz[i][y]==0)
        j++;

    }
   return 0;
}

I also recommend that you identify the code, as this will facilitate the work of those who help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.