The total distance travelled by vehicle in ‘t’ seconds is given by distance = ut+1/2at2 where ‘u’ and ‘a’ are the initial velocity (m/sec.) and acceleration (m/sec2). Write C program to find the distance travelled at regular intervals of time given the values of ‘u’ and ‘a’. The program should provide the flexibility to the user to select his own time intervals and repeat the calculations for different values of ‘u’ and ‘a’.

Description:

The total distance travelled by vehicle in 't' seconds is given by distance = ut+1/2at2 where 'u' and 'a' are the initial velocity (m/sec.) and acceleration  (m/sec2)

Algorithm:

Step 1: Start
Step 2: Read interval as integer
Step 3: for counter: 1 to interval increment counter by 1
        begin
            Read time, velocity, acceleration
            Distance += (velocity * time + (accelerations * pow(time, 2)) / 2);
        end
Step 4: Print Distance
Step 5: Stop

Flowchart:

Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()                      e
{
   int i, n, sec;
   float d, u, a;
   clrscr();
   printf("Enter the no. of intervals\n");
   scanf("%d", &n);
   for(i = 1; i <= n; i++)
   {
      printf("interval: %d \n", i);
      printf("Enter the time in seconds \n");
      scanf("%d",&sec);
      printf("Enter the velocity \n");
      scanf("%f", &u);
      printf("Enter the acceleration \n");
      scanf("%f", &a);
      d= d + (u * sec + (a * (pow(sec, 2))) / 2);
   }
   printf("Total distance travelled is  %.2f", d);
   getch();
}

Input & Output:

Enter the number of intervals: 2
Interval: 1
Enter the time in seconds 
30
Enter the velocity 
35
Enter the acceleration
20
Interval: 2
Enter the time in seconds 
40
Enter the velocity 
45
Enter the acceleration
30
Total distance travelled is 35850.00

Viva Questions:

Q: How many types of arrays are there ?

Ans: Three types. They are one dimensional ,two dimensional and multi dimensional arrys