Click here to Skip to main content
15,889,429 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i am writing a series of if statements that determine which set of coordinates is the has the max value.

I have a formula that calculates the max value of z and by using coordinates of (x,y). Ie z= (2*y)+x
that works well. I then need to be able to get it so that the program tells me which set of coordinates it used to get that z value. (there are 3 pairs of coordinates).

The coordinates are given to us and i just want the max coordinates to be called a new variable.

I haven't included the whole code as its very long as I'm doing other stuff there as well but i think i gave all the info needed...

The program has already calculated z0, z1,z2, max successfully
Coordinates are defined like this at the start:
double P0 [] = {7,8};
double P1 [] = {2,4};
double P2 [] = {3,2};

I just need max to contain the array of the coordinates that give me the value for zmax.

Thanks!!

What I have tried:

C++
if (zmax==z0){
            max=P0[];
        } else if (zmax==z1){
            max=P1[];
        } else {
            max=P2[];
        }

        printf("ph is %f/n",max[0],max[1]);


NEW VERSION:
C++
float maxformula (float z0, float z1, float z2, float zmax,float P0[], float P1[], float P2[]);
float maxformula (float z0, float z1, float z2, float zmax,float P0[], float P1[], float P2[]){


    if (zmax==z0){
        printf("zmax is zo");
        double max[]={P0[0],P0[1]};
printf("max is %f %f\n", max[0],max[1]);
    } else if (zmax==z1){
        printf("zmax is z1\n");
        double max[]={P1[0],P1[1]};
        printf("max is %f %f\n", max[0],max[1]);
    } else if (zmax==z2){
        printf("zmax is z2");
        double max[]={P2[0],P2[1]};
printf("max is %f %f\n", max[0],max[1]);
   }

}


In the main:
C++
maxformula(z0,z1,z2,zmax,P0,P1,P2);


For some reason gives me the correct values but the x and y are flipped (so the max[0] is the y max[1] is the x
Posted
Updated 27-Apr-20 8:04am
v3

1 solution

Why are you using double types for integer values? You can simplify the code to something like:
C++
int coords[][2] = { { 7,8}, {2,4}, {3,2} };
int maxi = -1;
int maxz = 0;
for (int i = 0; i < 3; i++)
{
    int z = coords[i][0] + coords[i][1] * 2;  // assuming numbers are x, y
    if (z > maxz)
    {
        // save the maximum z and the index to its co-ordinates
        maxz = z;
        maxi = i;
    }
}
printf("max z: %d, cordinates %d, %d\n", maxz, coords[maxi][0], coords[maxi][1]);
 
Share this answer
 
v3
Comments
Member 14814978 27-Apr-20 13:44pm    
Thanks for the answer!

I was using doubles as this process will occur a few times and i wasn't sure that the points will remain as integers (there is another formula after this that could change the points into decimals).

I was looking for this to be a formula that i could call and use to get an array.

I've updates the question with my current version but for some reason when it prints max in the main it flips the coordinate points...
Richard MacCutchan 27-Apr-20 14:06pm    
I am not sure I understand what you mean. The code I provided could quite easily use double types if necessary. You need to clarify exactly what inputs are to be passed to the function, and what outputs are expected. The original question was just to find the max pair of co-ordinates.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900