#include..

int x,y,x1,x2,y1,y2;

int midpoint (int x, int y)
{
x=2(x1+x2);
y=2(y1+y2);
}

void main()

{
clrscr();
printf("enter 4 points");
scanf("%d %d %d %d",&x1,&x2,&y1,&y2);
midpoint(x,y);
printf("the midpoint is %d %d",x,y);
getch();
}

i cant run my program i think there's a problem in the function midpoint but i cant figure it out

Recommended Answers

All 3 Replies

I think you're highly mistaken about how functions work.
1.Why are you declaring all the variables as global ?
2.The compiler isn't your math teacher:

x=2(x1+x2);
y=2(y1+y2);

you should explicitly tell the compiler what to do(use '*' operator):

x=2*(x1+x2);
y=2*(y1+y2);

The brackets in c are either used to override the precedence of the operators or in function calls.

3.The function "midpoint"is not returning anything at all, it should return an integer according to your function definition.If it isn't returning anything you should declare it like this:

void midpoint(int ,int);//void means returns nothing

What do you think calling your function "midpoint" will result?
4.Only calling the function from main is not enough, you should save the result somewhere to use it later.

int result = midpoint(x,y);

5. main should always return an 'int'

int main(void)

Here's a tutorial about functions.
Furthermore, you should go through your C Programming book(and the exercises) to fully understand how to create and use functions in c.

int midpoint (int x, int y)
{
    x=2*(x1+x2);
    y=2*(y1+y2);
}

i cant run my program i think there's a problem in the function midpoint but i cant figure it out

Also, that's not how you calculate a midpoint. You should divide by two, not multiply.

Member Avatar for MonsieurPointer

There is also an issue with the below function:

int midpoint (int x, int y)
{
x=2(x1+x2);
y=2(y1+y2);
}

Since you are changing the values of x and y, you need to use references (only if you are coding in C++ - it doesn't work in C. If you code in C, you will need to use pointers and de-referencing), otherwise you are only changing the values within the scope of the function.

int midpoint (int& x, int& y)
{
x=2(x1+x2);
y=2(y1+y2);
}

In addition, using global variables is of very poor design. You should consider changing the signature of your midpoint function like:

void midpoint (int x1, int y1, int x2, int y2, int& x, int& y)
{
...
}

Or maybe even:

void midpoint (int point1[2], int point2[2], int midpoint[2])
{
...
}

And as a final note: Why did you choose integers? What happens if your midpoint function returns a double or a float, like 1.2? You lose precision if you cast that as integer.

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.