0
//program to calculate horizontal Distance and Reduced Level by tacheometry
#include <stdio.h>
#include <math.h>

void main()
{
  float m,s,cosine,t,c,d,pi,v,h,hi,bm,rl,r;
  printf("enter value of stadia intercept,theta,bench mark,central reading on bm,central reading on staff station");
  printf("\n");
  scanf("%f %f %f %f %f", &s, &t, &bm, &r, &h);
  m=100; c=0;
  pi=22.0/7.0;
  cosine=cos(t*pi/180.0);
  d=((m*s*pow(cosine,2))+(c*cosine));
  v=d*tan(t*pi/180.0); hi=bm+r-v; rl=hi+v-h;
  printf("distance=%f meters, vertival distance=%f meters,height of instrument=%f meters,reduced level=%f meters",d,v,hi,rl);
}

Above I have provided the program to calculate Horizontal distance and Reduced Level.

I have a question that after printing the answer that is 12th line in the image. Can I repeat the steps 8 to 12 again instead of restarting the program?

I am a beginner in C programming. So I am sorry if I didn't specified something important.

mch
  • 9,424
  • 2
  • 28
  • 42

1 Answers1

0

You need to learn loops (see comments) but to help, your code could look like this:

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

int main(void)
{
    do {
        float m,s,cosine,t,c,d,v,h,hi,bm,rl,r;
        printf("enter value of stadia intercept,theta,bench mark,central reading on bm,central reading on staff station");
        printf("\n");
        if (scanf("%f %f %f %f %f", &s, &t, &bm, &r, &h)!=5)
            break;
        m=100; c=0;
        cosine=cos(t*M_PI/180.0);
        d=((m*s*pow(cosine,2))+(c*cosine));
        v=d*tan(t*M_PI/180.0); hi=bm+r-v; rl=hi+v-h;
        printf("distance=%f meters, vertival distance=%f meters,height of instrument=%f meters,reduced level=%f meters",d,v,hi,rl);
    } while (1);
    return(0);
}

the do {...} while(1); is an infinite loop but the loop breaks when the user does not enter the 5 parameters. After the loop, the main function returns zero, which will be forwarded to the operating system, which interprets zero as success.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • you could put the variable declarations inside the `do { ` block. that's be a little nicer. – Tom Tanner Jan 02 '17 at 17:00
  • It might be helpful to suggest use of `M_PI` instead of the rough approximation `22.0 / 7.0`. – ad absurdum Jan 02 '17 at 17:03
  • 1
    @DavidBowling Note that [conforming standard library file math.h is not only not required to, but actually must not define M_PI by default](http://stackoverflow.com/a/5008376/2410359). Yet `pi=22.0/7.0;` is crude. – chux - Reinstate Monica Jan 02 '17 at 18:57
  • @chux-- Thanks for pointing this out, and for the link. If I had remembered that a feature test macro was needed to enable `M_PI`, I would have just left this alone, since OP's problem was not related to the definition of `pi`. – ad absurdum Jan 02 '17 at 23:34