C programming. Help ASAP

Distance between 2 points using arrays

Page 1 of 1

4 Replies - 1805 Views - Last Post: 24 May 2007 - 01:26 PM Rate Topic: -----

#1 bballer69   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-May 07

C programming. Help ASAP

Posted 24 May 2007 - 06:09 AM

Ey guys,
I have an assignment whcih is on distance between 2 points, its due tomorrow. i keep getting these errors can some one please help me out ASAP?
thanks bballer 69

Attached File(s)


Is This A Good Question/Topic? 0
  • +

Replies To: C programming. Help ASAP

#2 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: C programming. Help ASAP

Posted 24 May 2007 - 07:15 AM

Can you post the errors?
Was This Post Helpful? 0
  • +
  • -

#3 PennyBoki   User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Re: C programming. Help ASAP

Posted 24 May 2007 - 07:30 AM

I fixed some things for you, not all of it but just enough so that you can see the errors you've made.
The program I guess won't work the way you want it to, but if I help you do that, then I would be doing your homework instead of you, and we don't want that to happen right :) , so here you go:

#include <stdio.h>
#include <math.h>
#define BART 10

void distance(float coord[BART]);

int main ( void )
{
	
		float coordx[100], coordy[100];
		int i,j,k,l,m,n;
	
		printf("Please input your set of co-ordinates, you may have up to 6 sets.\n\n");   
	
		printf("Please enter the first set of co-ordinates.\n");
		for (i = 0; i < 3; i++) {
			printf("x = :");
			scanf("%f\n", &coordx[i]);
			printf("Y:");
			scanf("%f\n", &coordy[i]);
		}
		printf("Please enter the second set of co-ordinates, \n");
		for (j = 0; j < 3; j++) {
			printf("x = ");
			scanf("%f\n", &coordx[j]);
			printf("y = ");
		scanf("%f\n", &coordy[j]);
		}
		printf("Please enter the thrid set of co-ordinates.\n");
		for (k = 0; k < 3; k++) {
			printf("x = :");
		scanf("%f\n", &coordx[k]);
		printf("Y:");
		scanf("%f\n", &coordy[k]);
		}
		printf("Please enter the fourth set of co-ordinates, \n");
		for (l = 0; l < 3; l++) {
			printf("x = ");
			scanf("%f\n", &coordx[l]);
			printf("y = ");
		scanf("%f\n", &coordy[l]);
		}
		printf("Please enter the fifth set of co-ordinates.\n");
		for (m = 0; m < 3; m++) {
			printf("x = :");
			scanf("%f\n", &coordx[m]);
			printf("Y:");
			scanf("%f\n", &coordy[m]);
		}
		printf("Please enter the sixt set of co-ordinates, \n");
		for (n = 0; n < 3; n++) {
			printf("x = ");
			scanf("%f\n", &coordx[n]);
			printf("y = ");
		scanf("%f\n", &coordy[n]);
		}
	
	return 0;
		
}


void distance( float coord[BART] ){
		double dist1, dist2, dist3, coordx[100], coordy[100];
		int i,j,k,l,m,n;  
		dist1 = sqrt((coordx[i]-coordx[j])*(coordx[i]-coordx[j]) + (coordy[i]-coordy[j])*(coordy[i]-coordy[j]));
		printf(" The distance between the first set and second set of co-ordinates is: ");
		printf("%f\n", dist1);
		
		dist2 = sqrt((coordx[k]-coordx[l])*(coordx[k]-coordx[l])  + (coordy[k]-coordy[l])*(coordy[k]-coordy[l]));
		printf(" The distance between the thrid set and fourth set of co-ordinates is: ");
		printf("%f\n", dist2);
		
		dist3 = sqrt((coordx[m]-coordx[n])*(coordx[m]-coordx[n])  + (coordy[m]-coordy[n])*(coordy[m]-coordy[n]));
		printf(" The distance between the fifth set and sixth set of co-ordinates is: ");
		printf("%f\n", dist3);
		
		}

Was This Post Helpful? 0
  • +
  • -

#4 KenJackson   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 11-May 07

Re: C programming. Help ASAP

Posted 24 May 2007 - 11:31 AM

(coordx[i]-coordx[j])^2
I see that PennyBoki fixed this already. The ^ character is a bit-wise exclusive-OR operator, not exponentiation.

As for the overall thing though, it's not clear if distance() is supposed to be an external function that is being called from main() and is followed by additional code, or if the code below it is intended to be the distance() function itself, which is incorrectly included in the body of main().

The coordx[] and coordy[]arrays are declared twice--once in an extra set of braces and once below distance(). These are two separate sets of arrays. One is written to, the other is read from. The second set is never initialized so the answers will be garbage.

Similarly, the integer variables are never initialized in the code below (or in) function distance().

When ferreting out confusing C errors, it's often helpful to focus only on the very first error the compiler generates and fix that. Then recompile and focus on the next very first error.
Was This Post Helpful? 0
  • +
  • -

#5 skyhawk133   User is offline

  • Head DIC Head
  • member icon

Reputation: 1981
  • View blog
  • Posts: 20,434
  • Joined: 17-March 01

Re: C programming. Help ASAP

Posted 24 May 2007 - 01:26 PM

Hey Ken, thanks for helping out! Great to see a new face around the forums helping new members.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1