Member Avatar for Jenniferting

Hi guys,
so im required to write a programme that will make a table, along with the values and all and im also required to mark "X" at the minimum value of total time A+B (exactly beside the minimum value in the table).
I tried using if statement but it doesnt work at all. Im quite new in programming so please help me :(

#include <stdio.h>
#include <math.h>
#define v1 3 //Velocity at Beach
#define v2 1.5 //Velocity in Water

int main()
{

float Xo=0, a=0, b=0;
float c=0, distanceA=0, distanceB=0, timeA=0, timeB=0, totaldistance=0, totaltime=0, totaltimeold=0,     totaltimenew=0;
printf("Give the value of Xo min: ");
scanf("%f", &a);
printf("Give the value of Xo max: ");
scanf("%f", &b);
printf("Give the range between the values: ");
scanf("%f", &c);
printf("Xo\tDistance A\tDistance B\tTime A\tTime B\t Total distance A+B\t Total time A+B\n");
printf("________________________________________________________________________________\n\n");


for(Xo=a; Xo<=b; Xo=Xo+c)
{

distanceA = sqrt((20*20) + (Xo*Xo));
timeA = distanceA/v1;
distanceB = sqrt(30*30 + (50-Xo)*(50-Xo));
timeB = distanceB/v2;
totaldistance = distanceA + distanceB;

totaltime = totaltimeold;
totaltimeold = totaltimenew;
totaltimenew = timeA + timeB;

if((totaltime==0 && totaltimeold!=0 && totaltimenew>totaltimeold) || (totaltimeold<totaltimenew && totaltimeold<totaltime )) printf("x"), Xo=Xo+c;

printf("%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t\t%.2f\t\t%.2f\n", Xo, distanceA, distanceB, timeA, timeB, totaldistance, totaltimenew);

}

getchar();
getchar();

return 0;
}

Show your current output, and also an example of what you want. Looking at your code, I can't see what your description is describing. I'm in a mash up here. ;)

BE SURE TO USE CODE TAGS around these examples, so the forum software won't mangle the spacing of them!

Member Avatar for Jenniferting

This is a photo of the output. It should be marked x beside the minimum total time 14

Add a new float variables, minTTnew. On line 19, set minTTnew to a ridiculously high value, beyond anything your data would reach - maybe 9999.999, (or include limits.h file and use FLOAT_MAX for your system).

Then on line 35, put
if(totaltimenew < minTTnew)
minTTnew = totaltimenew;

Also, add an int counter, set to zero, on line 19. Inside the for loop, on line 34, add this:

counter++;

Now you have to count out your column on the screen, for totaltimenew. Say it's 62, for example. and say the first row with a totaltimenew printed on it, is row 10.

So now you need to add this, after the entire for loop, say line 39:

gotoxy(61,10+counter);
and print the 'x' char.

If you have the include file conio.h, include it for gotoxy(). If not, and you're on Windows, you can use setConsoleCursorPosition(), after including windows.h. If you're on Linux, you can use ncurses (which is like conio.h).

It's a bit difficult, only because your data is not arranged in an array of structs - then it would be easy - but that will come later in learning C.

Since I probably confused you, with the above, this is an example:

Note that my compiler is Pelles C, and gotoxy() is referred to as _gotoxy(), since it is a non-standard function.

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

    #define v1 3 //Velocity at Beach
    #define v2 1.5 //Velocity in Water

    int main()
    {
    int count=0,minTTRow=0; 
    float Xo=0, a=0, b=0,minTT=0.0;
    float c=0, distanceA=0, distanceB=0, timeA=0, timeB=0, totaldistance=0, totaltime=0, totaltimeold=0, totaltimenew=0;
    printf("Give the value of Xo min: ");
    scanf("%f", &a);
    printf("Give the value of Xo max: ");
    scanf("%f", &b);
    printf("Give the range between the values: ");
    scanf("%f", &c);
    printf("Xo\tDistance A\tDistance B\tTime A\tTime B\t Total distance A+B\t Total time A+B\n");
    printf("________________________________________________________________________________\n\n");

    minTT=99999.99;     //impossibly high value
    for(Xo=a; Xo<=b; Xo=Xo+c)
    {
      distanceA = sqrt((20*20) + (Xo*Xo));
      timeA = distanceA/v1;
      distanceB = sqrt(30*30 + (50-Xo)*(50-Xo));
      timeB = distanceB/v2;
      totaldistance = distanceA + distanceB;
      totaltime = totaltimeold;
      totaltimeold = totaltimenew;
      totaltimenew = timeA + timeB;
      count++;
      if(totaltimenew < minTT) {
         minTT=totaltimenew;
         minTTRow=count;
      }

     if((totaltime==0 && totaltimeold!=0 && totaltimenew>totaltimeold) || (totaltimeold<totaltimenew && totaltimeold<totaltime )) printf("x"), Xo=Xo+c;
        printf("%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t\t%.2f\t\t%.2f\n", Xo, distanceA, distanceB, timeA, timeB, totaldistance, totaltimenew);
    }
    _gotoxy(64,minTTRow+6);
    printf("X");
    _gotoxy(1,count+6+2);   

    getchar();
    getchar();
    return 0;
}

hi dude can i know wht is the use of this function or wht is work of this function u wrote here _gotoxy(64,minTTRow+6);

gotoxy(int x, int y), moves the console cursor to the designated column and row, on your monitor. It is one of the functions defined in the header file "conio.h".

My compiler prepends an underscore to all non-standard functions, so _gotoxy(), insted of gotoxy(). (conio.h is popular, but not part of the C standard).

Member Avatar for Jenniferting

thanks ADAK!

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.