Hello,

I need to convert C codes to Java codes.this promram calculates surface areas of a 3D geometry which is read from datas from text file.thanks for help.

text file:

12
0 0 0
0 0 1
4 0 1
4 0 3
5 0 3
5 0 0
0 3 1
4 3 1
4 3 3
5 3 3
5 3 0
0 3 0
18
1 2
2 3
3 4
4 5
5 6
6 1
2 7
7 8
8 3
6 11
11 12
12 1
11 10
10 9
9 4
9 8
5 10
7 12
8
6
-6 -5 -4 -3 -2 -1
4
13 -17 5 10
6
11 -18 8 -16 -14 -13
4
1 7 18 12
4
-12 -11 -10 6
4
-7 -8 -9 2
4
-15 16 9 3
4
14 15 4 17

C syntax:

#include <stdio.h>   
#include <conio.h>  
#include <dos.h>  
#include <math.h>



FILE *fr;            

main()

{
   
   int x[50], y[50], z[50];
   int v=0, i, j, e=0, ex[50], ey[50], l=0, le[20], lcomp[20][20]; // v: vertice xyz: coordinates

   char line[100];
   clrscr();

   printf("File name should be 1.txt"),
   getchar();
   clrscr();
   fr = fopen ("1.txt", "rt");
   fgets(line, 80 , fr);
   sscanf (line, "%d", &v);
   printf ("Number of vertices: %d\n", v);


   for (i=1; i<v+1; i++){

	fgets(line, 80 , fr);
	sscanf (line, "%d %d %d", &x[i], &y[i], &z[i]);
	printf ("V%d - %d %d %d\n", i, x[i], y[i], z[i]);

	}

   fgets(line, 80 , fr);
   sscanf (line, "%d", &e);
   getchar();
   clrscr();
   printf ("Number of egdes: %d\n", e);

   for (i=1; i<e+1; i++){

	fgets(line, 80 , fr);
	sscanf (line, "%d %d", &ex[i], &ey[i]);
	printf ("E%d - %d %d\n", i, ex[i], ey[i]);

	}

   fgets(line, 80 , fr);
   sscanf (line, "%d", &l);
   getchar();
   clrscr();
   printf ("Number of loops: %d\n", l);

   for(i=1; i<l+1; i++){

   	fgets(line, 80, fr);
   	sscanf (line, "%d", &le[i]);
   	fgets(line, 80, fr);

	for(j=1; j<le[i]+1; j++){
		sscanf(line, "%d %d %d %d %d %d %d %d %d %d", &lcomp[i][1], &lcomp[i][2], &lcomp[i][3], &lcomp[i][4], &lcomp[i][5], &lcomp[i][6], &lcomp[i][7], &lcomp[i][8], &lcomp[i][9], &lcomp[i][10]);
        	
		}
   	
	}

   fclose(fr);

      for(i=1; i<l+1; i++){

	printf("L%d: %d\n", i, le[i]);

   	for(j=1; j<le[i]+1; j++){

	printf("%d ", lcomp[i][j]);

	}

   	printf("\n");

	}
getchar();
clrscr();


//Area Calculation
int a[20], nx[20][20], ny[20][20], nz[20][20], ni, nj, nk, ta=0;

   for (i = 1 ; i < l+1; i++) {

   ni=0;
   nj=0;
   nk=0;


   	 for (j = 1 ; j < le[i]+1; j++) {

		if (lcomp[i][j]>0) {

		nx[i][j] = (y[ex[lcomp[i][j]]] - y[ey[lcomp[i][j]]]) * (z[ex[lcomp[i][j]]] + z[ey[lcomp[i][j]]]);
		nz[i][j] = (x[ex[lcomp[i][j]]] - x[ey[lcomp[i][j]]]) * (y[ex[lcomp[i][j]]] + y[ey[lcomp[i][j]]]);
		ny[i][j] = (z[ex[lcomp[i][j]]] - z[ey[lcomp[i][j]]]) * (x[ex[lcomp[i][j]]] + x[ey[lcomp[i][j]]]);

        	}

		else {

		nx[i][j] = (y[ey[(-1)*lcomp[i][j]]] - y[ex[(-1)*lcomp[i][j]]]) * (z[ex[(-1)*lcomp[i][j]]] + z[ey[(-1)*lcomp[i][j]]]);
		nz[i][j] = (x[ey[(-1)*lcomp[i][j]]] - x[ex[(-1)*lcomp[i][j]]]) * (y[ex[(-1)*lcomp[i][j]]] + y[ey[(-1)*lcomp[i][j]]]);
		ny[i][j] = (z[ey[(-1)*lcomp[i][j]]] - z[ex[(-1)*lcomp[i][j]]]) * (x[ex[(-1)*lcomp[i][j]]] + x[ey[(-1)*lcomp[i][j]]]);

		}

		ni = ni + nx[i][j];
		nj = nj + ny[i][j];
		nk = nk + nz[i][j];

	 }

	 a[i] = sqrt((ni*ni) + (nj*nj) + (nk*nk)) / 2;

	 printf ("Area of L%d: %d\n", i, a[i]);
         ta = ta + a[i];

   }

  printf ("Total area is: %d", ta); 
           
  getchar();
	 
  return 0;

   
   
}

So get started. Post specific questions if you run into trouble.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)

struct time_details
{
    int strt_time[5] ;
    int end_time[5];
};

int main()
{
   struct time_details user1;
   struct time_details user2;
   int found=0;

   /*taking time inputs from user | we can make the array dynammic also
   here if number of iterationa re not known */
   for( int i=0;i<5; i++)
   {
       printf("enter 1st user start and end time ");
       scanf("%d %d",&user1.strt_time[i],&user1.end_time[i]);

   }

   for( int i=0;i<5; i++)
   {
       printf("enter 2nt user start and end time ");
       scanf("%d %d",&user2.strt_time[i],&user2.end_time[i]);

   }

  for (int k = 0;k<5 ;k++)
  {
      for (int l=0; l<5; l++)
      {
          if (
                (user1.strt_time[k]<=user2.strt_time[l] && user1.end_time[k]>=user2.strt_time[l])
                ||
                (user1.strt_time[k]>=user2.strt_time[l] && user1.end_time[k]>user2.strt_time[l])
              )

          {
              int start_lap_time=0;
              int end_lap_time=0;
              start_lap_time=max(user2.strt_time[l],user1.strt_time[l]);
              end_lap_time=min(user1.end_time[k],user2.end_time[l]);
              found ++;
              printf("\n found overlapping interval start from %d end at %d",start_lap_time,end_lap_time);
          }

      }
  }
  if (found==0)
  {
      printf("\n No overlapping periond found between two users");
  }

    return 0;
}
  1. Do not hijack other people's topics
  2. Do not revive years-old topics
  3. Never ask others to do your work for you

If you would like help in doing the conversion yourself then start a new topic, and explain what you have done/tried so far.

array result[2];

int img_x, img_y, pattern_x, pattern_y;
bool pattern_not_found, pattern_is_matching;

result = [-1, -1];
pattern_not_found = true;

for(img_x=0; (pattern_not_found  && (img_x <= (imageWidth - patternWidth))); ++img_x) { // <- check if img_x == (imageWidth - patternWidth) or not
    for(img_y=0; (pattern_not_found  && (img_y <= (imageHeight - patternHeight))); ++img_y) { // <- check if img_y == (imageHeight - patternHeight) or not
        pattern_is_matching = true

        for(pattern_x=0; (pattern_is_matching && (pattern_x < patternWidth)); ++pattern_x) {
            for(pattern_y=0; (pattern_is_matching && (pattern_y < patternHeight)); ++pattern_y) {
                if (img[(img_x + pattern_x), (img_y + pattern_y)] != pattern[pattern_x, pattern_y]) {
                    pattern_is_matching = false
                }
            }
        }

        if (pattern_is_matching) {
            pattern_not_found = false;
            result = [img_x, img_y];
        }
    }
}

return result;

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.