CS 201- Assignment 1

CS 201- Assignment 1
CS 201- Assignment 1


Solution:

#include <iostream>


using namespace std;


// Function to calculate the falling distance

double calculateFallingDistance(double time)

{

    const double gravity = 9.8;

    double distance = 0.5 * gravity * time * time;

    return distance;

}


int main()

{

    const int numTimes = 5; // Number of times to calculate the falling distance

    double sumDistances = 0; // Accumulator variable to store the sum of distances


    int currentTime = 1; // Current time in seconds

    int maxTime = 9; // Maximum time to calculate the falling distance


    while (currentTime <= maxTime)

    {

        double distance = calculateFallingDistance(currentTime);

        sumDistances += distance;

        cout << "Distance for time " << currentTime << " seconds: " << distance << " meters\n";

        currentTime += 2; // Increment the current time by 2 seconds

    }


    double averageDistance = sumDistances / numTimes; // Calculate the average distance

    cout << "Average distance covered: " << averageDistance << " meters\n";


    return 0;

}

Some Imp Links