4 Replies - 325 Views - Last Post: 08 August 2017 - 05:42 AM Rate Topic: -----

#1 Juan1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-November 14

Converting C++ to Java

Posted 07 August 2017 - 11:15 PM

Hello everyone, I am just wanting to see if someone can assist me with translating my code below from C++ to Java. I'm attempting to compare the two, but I've come across some difficulties when going through the C++ code. It seems there are a few things that do not easily convert. Any help is greatly appreciated.

#include <cstdio>
#include <cstring>

using namespace std;

#define i(n) scanf("%d",&n)
#define c(n) scanf("%c",&n)
#define s(n) scanf("%s",n)
#define N 1005                   
#define INF 1000000000
#define REP(i,a,B)/>\
		for(int i = a ; i < b ; ++i)

int dist[N][N];             // N < 10**3

void floyd(int v)                  //for small graphs
{
	REP(k,1,v+1)                   //vertices start from 1
	   REP(i,1,v+1)
	      REP(j,1,v+1)
	          if(dist[i][j] > dist[i][k] + dist[k][j])    // && i != j 
	    	       dist[i][j] = dist[i][k] + dist[k][j];
}

int main()
{
	int t,v,e,i,j,k,wt;
	i(t);
	while(t--)
	{
		i(v);i(e);
        REP(i,1,v+1)
	        REP(j,1,v+1)
                if( i != j )                //weight of edge : i --> i or self loop is zero if not given
                    dist[i][j] = INF;
        

		REP(k,0,e)
		{
			i(i);i(j);i(wt);
			dist[i][j] = wt;
            dist[j][i] = wt;          // undirected graph
		}
		
		floyd(v);
        printf("The shortest distance matrix is : \n");
		REP(i,1,v+1)
		{
			REP(j,1,v+1)
                printf(" %2d   ",dist[i][j]);
		    printf("\n");
		}
	}
	return 0;
}



Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Converting C++ to Java

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,412
  • Joined: 20-September 08

Re: Converting C++ to Java

Posted 08 August 2017 - 02:08 AM

Whoever converts that for you is going to need an input file for testing. You should attach one
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6962
  • View blog
  • Posts: 14,550
  • Joined: 16-October 07

Re: Converting C++ to Java

Posted 08 August 2017 - 03:10 AM

As a start, I'd get rid of that REP macro because, wtf?!? Once you have some reasonable procedural code it wouldn't really be that far from Java. And the global...
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,412
  • Joined: 20-September 08

Re: Converting C++ to Java

Posted 08 August 2017 - 03:33 AM

Quote

As a start, I'd get rid of that REP macro because, wtf?!?

Horrible! Example of all-too-common (bad) C posing as C++
Was This Post Helpful? 0
  • +
  • -

#5 Thomas1965  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 206
  • Joined: 09-September 16

Re: Converting C++ to Java

Posted 08 August 2017 - 05:42 AM

Not sure if it helps anybody, but here is the code without the macros.
int dist[1005][1005];             

void floyd(int v)                  
{
  for(int k = 1 ; k < v + 1 ; ++k)                   
    for(int i = 1 ; i < v + 1 ; ++i)
    for(int j = 1 ; j < v + 1 ; ++j)
    if (dist[i][j] > dist[i][k] + dist[k][j])    
      dist[i][j] = dist[i][k] + dist[k][j];
}

int main()
{
  int t, v, e, i, j, k, wt;
  scanf("%d",&t);
  while (t--)
  {
    scanf("%d",&v); scanf("%d",&e);
    for(int i = 1 ; i < v + 1 ; ++i)
      for(int j = 1 ; j < v + 1 ; ++j)
      if (i != j)                
        dist[i][j] = 1000000000;


    for(int k = 0 ; k < e ; ++k)
    {
      scanf("%d",&i); scanf("%d",&j); scanf("%d",&wt);
      dist[i][j] = wt;
      dist[j][i] = wt;          
    }

    floyd(v);
    printf("The shortest distance matrix is : \n");
    for(int i = 1 ; i < v + 1 ; ++i)
    {
      for(int j = 1 ; j < v + 1 ; ++j)
        printf(" %2d   ", dist[i][j]);
      printf("\n");
    }
  }
  return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1