Uploaded by asraed4

'ENSF' - 337 'Lab Report 1' - 'final', - draft

advertisement
Exercise A with errors
//bug include <stdio.h>
#include <stdio.h>
int main(void)
{
int a = 0, b = 0;
printf("Please enter a value for variable a:\n");
scanf("%d", &a);
printf("Please enter a value for variable b:\n");
scanf("%d", &b);
printf("The value of a and b are %d for a and %d for b. \n", a,b);
printf("The value of a%%b is %d \n", a%b);
return;
}cd
without errors
//bug include <stdio.h>
#include <stdio.h>
;int main(void);cd
;int main(void) {
;
int a = 0, b = 0;
printf("Please enter a value for variable a:\n");
scanf("%d", &a);
printf("Please enter a value for variable b:\n");
scanf("%d", &b);
printf("The value of a and b are %d for a and %d for b. \n", a,b);
printf("The value of a%%b is %d \n", a%b);
return;
}
__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Exercise D
1st time with 7-8 errors.
#include <stdio.h> #include <conio.h>
#include <math.h>
int main(void){ int n = };
{
intx = angle;
printf("Enter an angle x in units of radians");
scanf("%d",&x);
computing sine x;
{
int i, sum = 0,n;
float x;
printf("please enter the desired values for x & n(n>0); ");
scanf ("%f %d", &x, &n);
for (i=1; i<=n; i++)
{
sum = sum + ((pow(-1,i+1)*pow(x,2*i-1)) / (factorial(2*i-1)));
}
printf("%f", sum);
}
int factorial (intn)
{
int c;
int n;
int result = 1;
for (c = 1; c <= n; c++) {
result = result + c;
}
return(result);
}
Taylor = Series Approximation;
sin(x) = x - ((x^3)/3!) + ((x^5)/5!) - ((x^7)/7!) + ((x^9)/9factorial)
printf("Compute and output the sine of the input angle using built in trigonometric functions");
printf("sin(x) = _ ");
scanf("%d",&x);
printf("Compute and output the Taylor series approximation of sin(x) for upto 7 terms");
printf("sin(x) = ");
scanf("%d",&x);
}
________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
2nd time with only one error and factorial declaration
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main(void);;
(){
__attribute__((unused)) intx = angle;
printf("Enter an angle x in units of radians");
scanf("%d",&x);
sine x;
{
int i, sum = 0,n;
float x;
printf("please enter the desired values for x & n(n>0); ");
scanf ("%f %d", &x, &n);
for (i=1; i<=n; i++)
{
sum = sum + ((pow(-1,i+1)*pow(x,2*i-1)) / (factorial(2*i-1)));
}
printf("%f", sum);
}
int factorial (intn)
{
int c;
int n;
int result = 1;
for (c = 1; c <= n; c++) {
result = result + c;
}
return(result);
}
Taylor Series ;Approximation;
sin(x) = x - ((x^3)/3!
int main(void) { int n = 4;}
) + ((x^5)/5!) - ((x^7)/7!) + ((x^9)/9!)
printf("Compute and output the sine of the input angle using built in trigonometric functions");
printf("sin(x) = _ ");
scanf("%d",&x);
printf("Compute and output the Taylor series approximation of sin(x) for upto 7 terms");
printf("sin(x) = ");
scanf("%d",&x);
}
Finally with zero errors
#include <stdio.h>
#include <math.h>
int main()
{
double angle, sine_builtin, taylor_series_approximation = 0.0;
int n, sign = 1;
// Prompt for entering the input angle in radians
printf("Enter the angle in radians: ");
scanf("%lf", &angle);
// Calculate and output the sine of the input angle using the built-in sine function
sine_builtin = sin(angle);
printf("Sine using the built-in function: %lf\n", sine_builtin);
// Compute and output the Taylor series approximation of sin(x) including terms up to order seven
taylor_series_approximation = angle; // Initialize with the first term (x)
for (n = 1; n <= 7; n++) { // We want terms up to 7 terms exactly
double term = pow(angle, 2 * n + 1) / tgamma(2 * n + 2); // tgamma's function is to find the factorial
taylor_series_approximation += sign * term;
sign *= -1; // Change the sign for the next term
}
printf("Taylor series approximation: %lf\n", taylor_series_approximation);
return 0;
}
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
for Exercise D: simple program could be include <stdio.h>
include <math.h>
int main() {
sinx
taylors approximation: sin(x) = x - ((x^3)/3!) + ((x^5)/5!) - ((x^7)/7!) + ((x^9)/9factorial)
} but this is only a brief outline, doesn't work fully.
__________________________________________________________________________________________________________________________________________________________________________________________________________________-
Exercise F
Function create_table: which is called by the main function, receives the projectile initial velocity and displays a
table of the projectile’s maximum travel distance (d) and time (t), for trajectory angles of 0 to 90 (degrees), with
increments of 5 degrees. Here is the sample of the required table:
Angle t d
(deg) (sec) (m)
0.000000 0.000000 0.000000
5.000000 1.778689 177.192018
10.000000 3.543840 349.000146
You don’t have to worry about the format or the number of digits after the decimal point. The default format is
acceptable.
Function projectile_travel_time: receives two double arguments, the trajectory angle (), and the initial velocity
() and returns the projectile travel time (t).
Function projectile_travel_distance: receives two double arguments, the trajectory angle (), and the initial
velocity () and returns the projectile maximum horizontal distance (d).
Function degree_to_radian: receives an angle in degrees and converts to radian. This function is needed,
because the C library function sin needs its argument value to be in radian.
Notes:
• To use the C library function sin, you need to include header file math.h.
• When compiling from command line, if you are using Cygwin, Geany, or XCode, use –lm option to link
the math library:
gcc -Wall –lm lab1exe_F.c
• For constant values of , and gravitation acceleration, g, the following lines are already included in the
given file:
#define 3.141592654
#define G 9.8
/*
* lab1exe_F.c
* Created by Mahmood Moussavi
* Completed by: Abdul Shakoor Raed
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
const double G = 9.8; /* gravitation acceleration 9.8 m/s^2 */
const double PI = 3.141592654;
void create_table(double v);
double Projectile_travel_time(double a, double v);
double Projectile_travel_distance(double a, double v);
double degree_to_radian(double d);
int main(void)
{
int n;
double velocity;
printf ("Please enter the initial velocity with which the projectile is launched (m/sec): ");
n = scanf("%lf" ,&velocity);
if(n != 1)
{
printf("Input is invalid. Bye...");
exit(1);
}
int Function;
create_table:
int main() {
double initial_velocity;
// Prompt user for the initial velocity (in m/s)
printf("Enter the initial velocity (m/s): ");
scanf("%lf", &initial_velocity);
// Print table header
printf("\nAngle (degrees)\tMaximum Distance (m)\tTime of Flight (s)\n");
// Loop through the specified angles from 0 to 90 degrees with 5-degree increments
for (int angle = 0; angle <= 90; angle += 5) {
double angle_rad = angle * M_PI / 180.0; // Convert the degrees to radians
// Calculate maximum distance (d) and time of flight (t) respectively
double sin_theta = sin(angle_rad);
double cos_theta = cos(angle_rad);
double max_distance = (initial_velocity * initial_velocity * sin(2 * angle_rad)) / GRAVITY;
double time_of_flight = (2 * initial_velocity * sin_theta) / GRAVITY;
// Print results in the table
printf("%d\t\t\t%.2f\t\t\t%.2f\n", angle, max_distance, time_of_flight);
}
return 0;
}
int Function;
Projectile_travel_time:
double projectile_travel_time(double angle_degrees, double initial_velocity) {
double angle_radians = angle_degrees * M_PI / 180.0; // Convert degrees to radians
return (2 * initial_velocity * sin(angle_radians)) / GRAVITY;
}
int main() {
double angle, velocity;
// Prompt user for input
printf("Enter trajectory angle (degrees): ");
scanf("%lf", &angle);
printf("Enter initial velocity (m/s): ");
scanf("%lf", &velocity);
// Calculate travel time using the function
double travel_time = projectile_travel_time(angle, velocity);
// Display the result
printf("Projectile travel time: %.2f seconds\n", travel_time);
return 0;
}
int Function;
projectile_travel_distance:
double projectile_travel_distance(double angle_degrees, double initial_velocity) {
double angle_radians = angle_degrees * M_PI / 180.0; // Convert degrees to radians
return (initial_velocity * initial_velocity * sin(2 * angle_radians)) / GRAVITY;
}
int main() {
double angle, velocity;
// Prompt user for input
printf("Enter trajectory angle (degrees): ");
scanf("%lf", &angle);
printf("Enter initial velocity (m/s): ");
scanf("%lf", &velocity);
// Calculate max horizontal distance using the function
double max_distance = projectile_travel_distance(angle, velocity);
// Show result
printf("Projectile maximum horizontal distance: %.2f meters\n", max_distance);
return 0;
}
double degree_to_radian(double angle_degrees) {
return angle_degrees * M_PI / 180.0; // Convert degrees to radians
}
int main() {
double angle_degrees;
// Prompt user for input
printf("Enter angle in degrees: ");
scanf("%lf", &angle_degrees);
// Convert degrees to radians using the function double angle_radians = degree_to_radian(angle_degrees);
// Display result
printf("%.2f degrees is equal to %.2f radians\n", angle_degrees, angle_radians);
return 0;
}
while (velocity < 0 )
{
printf ("please enter a positive number for velocity: ");
n = scanf("%lf", &velocity);
if(n != 1)
{
printf("The input is invalid. Bye...");
exit(1);
}
}
// create_table(velocity);
return 0;
}
/* UNCOMMENT THE CALL TO THE create_table IN THE main FUNCTION, AND COMPLETE THE PROGRAM */
Exercise 6 for the 2nd time after solving a few errors:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
const double G = 9.8; /* gravitation acceleration 9.8 m/s^2 */
const double PI = 3.141592654;
void create_table(double v);
double Projectile_travel_time(double a, double v);
double Projectile_travel_distance(double a, double v);
double degree_to_radian(double d);
int main(void)
{
int n;
double velocity;
printf ("Please enter the initial velocity with which the projectile is launched (m/sec): ");
n = scanf("%lf" ,&velocity);
if(n != 1)
{
printf("Input is invalid. Bye...");
exit(1);
}
int Function;
create_table:
int main() {
double initial_velocity;
// Prompt user for the initial velocity (in m/s)
printf("Enter the initial velocity (m/s): ");
scanf("%lf", &initial_velocity);
// Print table header
printf("\nAngle (degrees)\tMaximum Distance (m)\tTime of Flight (s)\n");
// Loop through the specified angles from 0 to 90 degrees with 5-degree increments
for (int angle = 0; angle <= 90; angle += 5) {
double angle_rad = angle * M_PI / 180.0; // Convert the degrees to radians
// Calculate maximum distance (d) and time of flight (t) respectively
double sin_theta = sin(angle_rad);
double cos_theta = cos(angle_rad);
double max_distance;
max_distance = (initial_velocity * initial_velocity * sin(2 * angle_rad)) / GRAVITY;
double time_of_flight = (2 * initial_velocity * sin_theta) / GRAVITY;
// Print results in the table
printf("%d\t\t\t%.2f\t\t\t%.2f\n", angle, max_distance, time_of_flight);
}
return 0;
}
int Function;
Projectile_travel_time:
double projectile_travel_time(double angle_degrees, double initial_velocity) {
double angle_radians;
angle_radians = angle_degrees * M_PI / 180.0; // Convert degrees to radians
return (2 * initial_velocity * sin(angle_radians)) / GRAVITY;
}
__attribute__((unused)) int main() {
double angle, velocity;
// Prompt user for input
printf("Enter trajectory angle (degrees): ");
scanf("%lf", &angle);
printf("Enter initial velocity (m/s): ");
scanf("%lf", &velocity);
// Calculate travel time using the function
double travel_time = projectile_travel_time(angle, velocity);
// Display the result
printf("Projectile travel time: %.2f seconds\n", travel_time);
return 0;
}
int Function;
projectile_travel_distance:
double projectile_travel_distance(double angle_degrees, double initial_velocity) {
double angle_radians;
angle_radians = angle_degrees * M_PI / 180.0; // Convert degrees to radians
return (initial_velocity * initial_velocity * sin(2 * angle_radians)) / GRAVITY;
}
int main() {
double angle, velocity;
// Prompt user for input
printf("Enter trajectory angle (degrees): ");
scanf("%lf", &angle);
printf("Enter initial velocity (m/s): ");
scanf("%lf", &velocity);
// Calculate max horizontal distance using the function
double max_distance = projectile_travel_distance(angle, velocity);
// Show result
printf("Projectile maximum horizontal distance: %.2f meters\n", max_distance);
return 0;
}
double degree_to_radian(double angle_degrees) {
return angle_degrees * M_PI / 180.0; // Convert degrees to radians
}
int main() {
double angle_degrees;
// Prompt user for input
printf("Enter angle in degrees: ");
scanf("%lf", &angle_degrees);
// Convert degrees to radians using the function
double angle_radians = degree_to_radian(angle_degrees);
// Display result
printf("%.2f degrees is equal to %.2f radians\n", angle_degrees, angle_radians);
return 0;
}
while (velocity < 0 )
{
printf ("please enter a positive number for velocity: ");
n = scanf("%lf", &velocity);
if(n != 1)
{
printf("The input is invalid. Bye...");
exit(1);
}
}
// create_table(velocity);
return 0;
}
Exercise F 3rd time
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
const double G = 9.8; /* gravitation acceleration 9.8 m/s^2 */
const double PI = 3.141592654;
void create_table(double v);
double Projectile_travel_time(double a, double v);
double Projectile_travel_distance(double a, double v);
double degree_to_radian(double d);
int main(void)
{
int n;
double velocity;
printf ("Please enter the initial velocity with which the projectile is launched (m/sec): ");
n = scanf("%lf" ,&velocity);
if(n != 1)
{
printf("Input is invalid. Bye...");
exit(1);
}
int Function;
create_table:
int main() {
double initial_velocity;
// Prompt user for the initial velocity (in m/s)
printf("Enter the initial velocity (m/s): ");
scanf("%lf", &initial_velocity);
// Print table header
printf("\nAngle (degrees)\tMaximum Distance (m)\tTime of Flight (s)\n");
// Go through the specified angles with 5-degree increments from 0 to 90 degrees
for (int angle = 0; angle <= 90; angle += 5)
{
double angle_rad;
angle_rad = const double PI * angle / 180.0; // Convert the degrees to radians
// Calculate maximum distance (d) and time of flight (t) respectively
double sin_theta = sin(angle_rad);
double cos_theta = cos(angle_rad);
double max_distance;
max_distance = (initial_velocity * initial_velocity * sin(2 * angle_rad)) / const double G;
double time_of_flight;
time_of_flight = (2 * initial_velocity * sin_theta) / const double G;
// Print results in the table
printf("%d\t\t\t%.2f\t\t\t%.2f\n", angle, max_distance, time_of_flight);
}
return 0;
}
int Function;
Projectile_travel_time:
double projectile_travel_time(double angle_degrees, double initial_velocity) {
double angle_radians;
angle_radians = const double PI * angle_degrees / 180.0; // Converts degrees to radians
return (2 * initial_velocity * sin(angle_radians)) / const double G;
}
__attribute__((unused)) int main() {
double angle, velocity;
// Prompt user for input
printf("Enter respective trajectory angle (degrees): ");
scanf("%lf", &angle);
printf("Enter initial velocity (m/s): ");
scanf("%lf", &velocity);
// Calculate travel time using the function
double travel_time;
travel_time = projectile_travel_time(angle, velocity);
// Display the result
printf("Projectile travel time: %.2f seconds\n", travel_time);
return 0;
}
int Function;
projectile_travel_distance:
double projectile_travel_distance(double angle_degrees, double initial_velocity) {
double angle_radians;
angle_radians = const double PI * angle_degrees / 180.0; // Convert degrees to radians
return (initial_velocity * initial_velocity * sin(2 * angle_radians)) / const double G;
}
int main() {
double angle, velocity;
// Prompt user for input
printf("Enter trajectory angle (degrees): ");
scanf("%lf", &angle);
printf("Enter initial velocity (m/s): ");
scanf("%lf", &velocity);
// Calculate max horizontal distance using the function
double max_distance = projectile_travel_distance(angle, velocity);
// Show result
printf("Projectile maximum horizontal distance: %.2f meters\n", max_distance);
return 0;
}
double degree_to_radian(double angle_degrees)
{
return const double PI * angle_degrees/ 180.0; // Convert degrees into radians respectively
}
int main() {
double angle_degrees;
// Prompt user for input
printf("Enter angle in degrees: ");
scanf("%lf", &angle_degrees);
// Convert degrees to radians using the function
double angle_radians = degree_to_radian(angle_degrees);
// Display result
printf("%.2f degrees is equal to %.2f radians\n", angle_degrees, angle_radians);
return 0;
}
while (velocity < 0 )
{
printf ("please enter a positive number for velocity: ");
n = scanf("%lf", &velocity);
if(n != 1)
{
printf("The input is invalid. Bye...");
exit(1);
}
}
// create_table(velocity);
return 0;
}
Exercise F 4th time
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
const double G = 9.8; /* gravitation acceleration 9.8 m/s^2 */
const double PI = 3.141592654;
void create_table(double v);
double Projectile_travel_time(double a, double v);
double Projectile_travel_distance(double a, double v);
double degree_to_radian(double d);
int main(void)
{
int n;
double velocity;
printf ("Please enter the initial velocity with which the projectile is launched (m/sec): ");
n = scanf("%lf" ,&velocity);
if(n != 1)
{
printf("Input is invalid. Bye...");
exit(1);
}
int Function;
create_table:
int main() {
double initial_velocity;
// Prompt user for the initial velocity (in m/s)
printf("Enter the initial velocity (m/s): ");
scanf("%lf", &initial_velocity);
// Print table header
printf("\nAngle (degrees)\tMaximum Distance (m)\tTime of Flight (s)\n");
// Scan specified angles with 5-degree increments and from 0 to 90 degrees
for (int angle = 0; angle <= 90; angle += 5)
{
double angle_rad;
angle_rad = (*angle) * (const double PI/180.0) / 180.0; // Convert the degrees to radians
// Calculate maximum distance (d) and time of flight (t) respectively
double sin_theta = sin(angle_rad);
double cos_theta = cos(angle_rad);
double max_distance;
max_distance = (initial_velocity * initial_velocity * sin(2 * angle_rad)) / ,const double G;
double time_of_flight;
time_of_flight = (2 * initial_velocity * sin_theta) / ,const double G;
// Print results in the table
printf("%d\t\t\t%.2f\t\t\t%.2f\n", angle, max_distance, time_of_flight);
}
return 0;
}
int Function;
Projectile_travel_time:
double projectile_travel_time(double angle_degrees, double initial_velocity) {
double angle_radians;
angle_radians = ,const double PI * angle_degrees / 180.0; // Converts degrees to radians
return (2 * initial_velocity * sin(angle_radians)) / ,const double G;
}
__attribute__((unused)) int main() {
double angle, velocity;
// Prompt user for input
printf("Enter respective trajectory angle (degrees): ");
scanf("%lf", &angle);
printf("Enter initial velocity (m/s): ");
scanf("%lf", &velocity);
// Calculate travel time using the function
double travel_time;
travel_time = projectile_travel_time(angle, velocity);
// Display the result
printf("Projectile travel time: %.2f seconds\n", travel_time);
return 0;
}
int Function;
projectile_travel_distance:
double projectile_travel_distance(double angle_degrees, double initial_velocity)
{
double angle_radians;
angle_radians = ,const double PI * angle_degrees / 180.0; // Convert degrees to radians
return (initial_velocity * initial_velocity * sin(2 * angle_radians)) / ,const double G;
}
int main() {
double angle, velocity;
// Prompt user for input
printf("Enter trajectory angle (degrees): ");
scanf("%lf", &angle);
printf("Enter initial velocity (m/s): ");
scanf("%lf", &velocity);
// Calculate max horizontal distance using the function
double max_distance = projectile_travel_distance(angle, velocity);
// Show result
printf("Projectile maximum horizontal distance: %.2f meters\n", max_distance);
return 0;
}
double degree_to_radian(double angle_degrees)
{
return ,const double PI * angle_degrees/ 180.0; // Convert degrees into radians respectively
}
int main() {
double angle_degrees;
// Prompt user for input
printf("Enter angle in degrees: ");
scanf("%lf", &angle_degrees);
// Convert degrees to radians using the function
double angle_radians = degree_to_radian(angle_degrees);
// Display result
printf("%.2f degrees is equal to %.2f radians\n", angle_degrees, angle_radians);
return 0;
}
while (velocity < 0 )
{
printf ("please enter a positive number for velocity: ");
n = scanf("%lf", &velocity);
if(n != 1)
{
printf("The input is invalid. Bye...");
exit(1);
}
}
// create_table(velocity);
return 0;
}
Exercise F 5th time, works best among all of em with a few errors to rectify:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
const double G = 9.8; /* gravitation acceleration 9.8 m/s^2 */
const double PI = 3.141592654;
void create_table(double v);
double Projectile_travel_time(double a, double v);
double Projectile_travel_distance(double a, double v);
double degree_to_radian(double d);
int main(void)
{
int n;
double velocity;
printf ("Please enter the initial velocity with which the projectile is launched (m/sec): ");
n = scanf("%lf" ,&velocity);
if(n != 1)
{
printf("Input is invalid. Bye...");
exit(1);
}
int Function;
create_table:
int main() {
double initial_velocity;
// Prompt user for the initial velocity (in m/s)
printf("Enter the initial velocity (m/s): ");
scanf("%lf", &initial_velocity);
// Print table header
printf("\nAngle (degrees)\tMaximum Distance (m)\tTime of Flight (s)\n");
// Scan specified angles with 5-degree increments and from 0 to 90 degrees
for (int angle = 0; angle <= 90; angle += 5)
{
double angle_rad;
angle_rad = (,const double PI * angle ) / 180.0; // Convert the degrees to radians
// Calculate maximum distance (d) and time of flight (t) respectively
double sin_theta = sin(angle_rad);
double cos_theta = cos(angle_rad);
double max_distance;
max_distance = (initial_velocity * initial_velocity * sin(2 * angle_rad)) / ,const double G;
double time_of_flight;
time_of_flight = (2 * initial_velocity * sin_theta) / ,const double G;
// Print results in the table
printf("%d\t\t\t%.2f\t\t\t%.2f\n", angle, max_distance, time_of_flight);
}
return 0;
}
int Function;
Projectile_travel_time:
double projectile_travel_time(double angle_degrees, double initial_velocity) {
double angle_radians;
angle_radians = ,const double PI * angle_degrees / 180.0; // Converts degrees to radians
return (2 * initial_velocity * sin(angle_radians)) / ,const double G;
}
__attribute__((unused)) int main() {
double angle, velocity;
// Prompt user for input
printf("Enter respective trajectory angle (degrees): ");
scanf("%lf", &angle);
printf("Enter initial velocity (m/s): ");
scanf("%lf", &velocity);
// Calculate travel time using the function
double travel_time;
travel_time = projectile_travel_time(angle, velocity);
// Display the result
printf("Projectile travel time: %.2f seconds\n", travel_time);
return 0;
}
int Function;
projectile_travel_distance:
double projectile_travel_distance(double angle_degrees, double initial_velocity)
{
double angle_radians;
angle_radians = ,const double PI * angle_degrees / 180.0; // Convert degrees to radians
return (initial_velocity * initial_velocity * sin(2 * angle_radians)) / ,const double G;
}
int main() {
double angle, velocity;
// Prompt user for input
printf("Enter trajectory angle (degrees): ");
scanf("%lf", &angle);
printf("Enter initial velocity (m/s): ");
scanf("%lf", &velocity);
// Calculate max horizontal distance using the function
double max_distance = projectile_travel_distance(angle, velocity);
// Show result
printf("Projectile maximum horizontal distance: %.2f meters\n", max_distance);
return 0;
}
double degree_to_radian(double angle_degrees)
{
return ,const double PI * angle_degrees/ 180.0; // Convert degrees into radians respectively
}
int main() {
double angle_degrees;
// Prompt user for input
printf("Enter angle in degrees: ");
scanf("%lf", &angle_degrees);
// Convert degrees to radians using the function
double angle_radians = degree_to_radian(angle_degrees);
// Display result
printf("%.2f degrees is equal to %.2f radians\n", angle_degrees, angle_radians);
return 0;
}
while (velocity < 0 )
{
printf ("please enter a positive number for velocity: ");
n = scanf("%lf", &velocity);
if(n != 1)
{
printf("The input is invalid. Bye...");
exit(1);
}
}
// create_table(velocity);
return 0;
}
Exercise F 6th time works best with lesser and efficient lines of code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
const double G = 9.8; /* gravitation acceleration 9.8 m/s^2 */
const double PI = 3.141592654;
void create_table(double v);
double Projectile_travel_time(double a, double v);
double Projectile_travel_distance(double a, double v);
double degree_to_radian(double d);
int main(void) {
int n;
double velocity;
printf("Please enter the initial velocity with which the projectile is launched (m/sec): ");
n = scanf("%lf", &velocity);
if (n != 1) {
printf("Input is invalid. Bye...");
exit(1);
}
while (velocity < 0) {
printf("please enter a positive number for velocity: ");
n = scanf("%lf", &velocity);
if (n != 1) {
printf("The input is invalid. Bye...");
exit(1);
}
}
create_table(velocity);
return 0;
} int Function;
create_table:
int main() {
double initial_velocity;
// Prompt user for the initial velocity (in m/s)
printf("Enter the initial velocity (m/s): ");
scanf("%lf", &initial_velocity);
// Print table header
printf("\nAngle (degrees)\tMaximum Distance (m)\tTime of Flight (s)\n");
// Scan specified angles with 5-degree increments and from 0 to 90 degrees
for (int angle = 0; angle <= 90; angle += 5)
{
double angle_rad;
angle_rad = (,const double PI * angle ) / 180.0; // Convert the degrees to radians
// Calculate maximum distance (d) and time of flight (t) respectively
double sin_theta = sin(angle_rad);
double cos_theta = cos(angle_rad);
double max_distance;
max_distance = (initial_velocity * initial_velocity * sin(2 * angle_rad)) / ,const double G;
double time_of_flight;
time_of_flight = (2 * initial_velocity * sin_theta) / ,const double G;
// Print results in the table
printf("%d\t\t\t%.2f\t\t\t%.2f\n", angle, max_distance, time_of_flight);
}
return 0;
}
int Function;
Projectile_travel_time:
double projectile_travel_time(double angle_degrees, double initial_velocity) {
double angle_radians;
angle_radians = ,const double PI * angle_degrees / 180.0; // Converts degrees to radians
return (2 * initial_velocity * sin(angle_radians)) / ,const double G;
}
__attribute__((unused)) int main() {
double angle, velocity;
// Prompt user for input
printf("Enter respective trajectory angle (degrees): ");
scanf("%lf", &angle);
printf("Enter initial velocity (m/s): ");
scanf("%lf", &velocity);
// Calculate travel time using the function
double travel_time;
travel_time = projectile_travel_time(angle, velocity);
// Display the result
printf("Projectile travel time: %.2f seconds\n", travel_time);
return 0;
}
int Function;
projectile_travel_distance:
double projectile_travel_distance(double angle_degrees, double initial_velocity)
{
double angle_radians;
angle_radians = ,const double PI * angle_degrees / 180.0; // Convert degrees to radians
return (initial_velocity * initial_velocity * sin(2 * angle_radians)) / ,const double G;
}
int main() {
double angle, velocity;
// Prompt user for input
printf("Enter trajectory angle (degrees): ");
scanf("%lf", &angle);
printf("Enter initial velocity (m/s): ");
scanf("%lf", &velocity);
// Calculate max horizontal distance using the function
double max_distance = projectile_travel_distance(angle, velocity);
// Show result
printf("Projectile maximum horizontal distance: %.2f meters\n", max_distance);
return 0;
}
double degree_to_radian(double angle_degrees)
{
return ,const double PI * angle_degrees/ 180.0; // Convert degrees into radians respectively
}
int main() {
double angle_degrees;
// Prompt user for input
printf("Enter angle in degrees: ");
scanf("%lf", &angle_degrees);
// Convert degrees to radians using the function
double angle_radians = degree_to_radian(angle_degrees);
// Display result
printf("%.2f degrees is equal to %.2f radians\n", angle_degrees, angle_radians);
return 0;
}
Finally, Exercise F with only 1 small error:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
const double G = 9.8; /* gravitation acceleration 9.8 m/s^2 */
const double PI = 3.141592654;
void create_table(double v);
double Projectile_travel_time(double a, double v);
double Projectile_travel_distance(double a, double v);
double degree_to_radian(double d);
int main(void) {
int n;
double velocity;
printf("Please enter the initial velocity with which the projectile is launched (m/sec): ");
n = scanf("%lf", &velocity);
if (n != 1) {
printf("Input is invalid. Bye...");
exit(1);
}
while (velocity < 0) {
printf("please enter a positive number for velocity: ");
n = scanf("%lf", &velocity);
if (n != 1) {
printf("The input is invalid. Bye...");
exit(1);
}
}
create_table(velocity);
return 0;
}
int Function;
void create_table(double initial_velocity);
{
; double initial_velocity;
// Prompt user for the initial velocity (in m/s)
printf("Enter the initial velocity (m/s): ");
scanf("%lf", &initial_velocity);
// Print table header
printf("\nAngle (degrees)\tMaximum Distance (m)\tTime of Flight (s)\n");
// Scan specified angles with 5-degree increments and from 0 to 90 degrees
for (int angle = 0; angle <= 90; angle += 5)
{
double angle_rad;
angle_rad = (PI * angle ) / 180.0; // Convert the degrees to radians
// Calculate maximum distance (d) and time of flight (t) respectively
double sin_theta = sin(angle_rad);
double cos_theta = cos(angle_rad);
double max_distance;
max_distance = (initial_velocity * initial_velocity * sin(2 * angle_rad)) / G;
double time_of_flight;
time_of_flight = (2 * initial_velocity * sin_theta) / G;
// Print results in the table
printf("%d\t\t\t%.2f\t\t\t%.2f\n", angle, max_distance, time_of_flight);
}
return 0;
}
double projectile_travel_time(double angle_degrees, double initial_velocity) {
double angle_radians;
angle_radians = PI * angle_degrees / 180.0; // Converts degrees to radians
return (2 * initial_velocity * sin(angle_radians)) / G;
}
double projectile_travel_distance(double angle_degrees, double initial_velocity)
{
double angle_radians;
angle_radians = PI * angle_degrees / 180.0; // Convert degrees to radians
return (initial_velocity * initial_velocity * sin(2 * angle_radians)) / G;
}
double degree_to_radian(double angle_degrees)
{
return PI * angle_degrees/ 180.0; // Convert degrees into radians respectively
}
some errors were encountered which can be solved later
the below program is not recommended though because of errors and change in code
#include <stdio.H>
#include <stdlib.H>
#include <math.H>
Const double G = 9.Eight; /* gravitation acceleration 9.Eight m/s^2 */
Const double PI = 3.141592654;
Void create_table(double v);
Double Projectile_travel_time(double a, double v);
Double Projectile_travel_distance(double a, double v);
Double degree_to_radian(double d);
Int principal(void)
int n;
double speed;
printf ("Please input the preliminary velocity with which the projectile is launched (m/sec): ");
n = scanf("%lf" ,&speed);
if(n != 1)
printf("enter is invalid. Bye...");
exit(1);
int feature;
create_table:
int principal() double initial_velocity;
// set off user for the initial pace (in m/s)
printf("input the preliminary speed (m/s): ");
scanf("%lf", &initial_velocity);
// Print table header
printf("nAngle (tiers)tMaximum Distance (m)tTime of Flight (s)n");
// undergo the specified angles from 0 to 90 degrees with five-diploma increments
for (int attitude = 0; perspective <= 90; attitude += five) double angle_rad = perspective * M_PI / one hundred eighty.Zero; // Convert ranges into radians
// Calculate maximum distance (d) and time of flight (t) respectively
double sin_theta = sin(angle_rad);
double cos_theta = cos(angle_rad);
double max_distance;
max_distance = (initial_velocity * initial_velocity * sin(2 * angle_rad)) / GRAVITY;
double time_of_flight = (2 * initial_velocity * sin_theta) / GRAVITY;
// Print outcomes within the desk
printf("%dttt%.2fttt%.2fn", angle, max_distance, time_of_flight);
go back 0;
int characteristic;
Projectile_travel_time:
__attribute__((unused)) double projectile_travel_time(double angle_degrees, double initial_velocity) double angle_radians;
angle_radians = angle_degrees * M_PI / a hundred and eighty.0; // Convert tiers to radians
return (2 * initial_velocity * sin(angle_radians)) / GRAVITY;
int predominant() double attitude, pace;
// set off consumer for input
printf("input trajectory perspective (degrees): ");
scanf("%lf", &angle);
printf("input preliminary pace (m/s): ");
scanf("%lf", &speed);
// Calculate journey time using the characteristic
double travel_time = projectile_travel_time(perspective, pace);
// display the result
printf("Projectile tour time: %.2f secondsn", travel_time);
go back zero;
int feature;
projectile_travel_distance:
__attribute__((unused)) double projectile_travel_distance(double angle_degrees, double initial_velocity) double angle_radians;
angle_radians = angle_degrees * M_PI / a hundred and eighty.Zero; // Convert tiers to radians
return (initial_velocity * initial_velocity * sin(2 * angle_radians)) / GRAVITY;
int most important() double attitude, speed;
// prompt person for input
printf("enter trajectory angle (degrees): ");
scanf("%lf", &angle);
printf("enter initial velocity (m/s): ");
scanf("%lf", &velocity);
// Calculate max horizontal distance using the function
double max_distance = projectile_travel_distance(attitude, speed);
// show end result
printf("Projectile most horizontal distance: %.2f metersn", max_distance);
return zero;
__attribute__((unused)) double degree_to_radian(double angle_degrees) go back angle_degrees * M_PI / 180.Zero; // Convert ranges into radians
int predominant() double angle_degrees;
// spark off user for input
printf("input perspective in ranges: ");
scanf("%lf", &angle_degrees);
// Convert ranges to radians the usage of the characteristic
double angle_radians = degree_to_radian(angle_degrees);
// show end result
printf("%.2f stages is equal to %.2f radiansn", angle_degrees, angle_radians);
return 0;
whilst (speed < zero )
printf ("please enter a tremendous range for velocity: ");
n = scanf("%lf", &velocity);
if(n != 1)
printf("The enter is invalid. Bye...");
exit(1);
// create_table(pace);
go back 0;
_____
Exercise D for the last time after debugging, is not recommended though because of the greater amount of errors and change in code.
#include <stdio.H>
#include <stdlib.H>
#include <math.H>
Const double G = 9.Eight; /* gravitation acceleration 9.Eight m/s^2 */
Const double PI = 3.141592654;
Void create_table(double v);
Double Projectile_travel_time(double a, double v);
Double Projectile_travel_distance(double a, double v);
Double degree_to_radian(double d);
Int principal(void)
int n;
double speed;
printf ("Please input the preliminary velocity with which the projectile is launched (m/sec): ");
n = scanf("%lf" ,&speed);
if(n != 1)
printf("enter is invalid. Bye...");
exit(1);
int feature;
create_table:
int principal() double initial_velocity;
// set off user for initial pace (in m/s)
printf("input the preliminary speed (m/s): ");
scanf("%lf", &initial_velocity);
// Print out the table header
printf("nAngle (tiers)tMaximum Distance (m)tTime of Flight (s)n");
// undergo specified angles from 0 to 90 degrees with five-diploma increments
for (int attitude = 0; perspective <= 90; attitude += five) double angle_rad = perspective * M_PI / one hundred eighty.Zero; // Convert ranges into radians
// Calculate maximum distance (d) and time of flight (t) respectively
double sin_theta = sin(angle_rad);
double cos_theta = cos(angle_rad);
double max_distance;
max_distance = (initial_velocity * initial_velocity * sin(2 * angle_rad)) / GRAVITY;
double time_of_flight = (2 * initial_velocity * sin_theta) / GRAVITY;
// Print outcomes within desk
printf("%dttt%.2fttt%.2fn", angle, max_distance, time_of_flight);
go back 0;
int characteristic;
Projectile_travel_time:
__attribute__((unused)) double projectile_travel_time(double angle_degrees, double initial_velocity) double angle_radians;
angle_radians = angle_degrees * M_PI / a hundred and eighty.0; // Convert tiers to radians
return (2 * initial_velocity * sin(angle_radians)) / GRAVITY;
int predominant() double attitude, pace;
// set off consumer for input
printf("input trajectory perspective (degrees): ");
scanf("%lf", &angle);
printf("input preliminary pace (m/s): ");
scanf("%lf", &speed);
// Calculate journey time using the characteristic
double travel_time = projectile_travel_time(perspective, pace);
// display the result
printf("Projectile tour time: %.2f secondsn", travel_time);
go back zero;
int feature;
projectile_travel_distance:
__attribute__((unused)) double projectile_travel_distance(double angle_degrees, double initial_velocity) double angle_radians;
angle_radians = angle_degrees * M_PI / a hundred and eighty.Zero; // Convert tiers to radians
return (initial_velocity * initial_velocity * sin(2 * angle_radians)) / GRAVITY;
int most important() double attitude, speed;
// prompt user for input
printf("enter trajectory angle (degrees): ");
scanf("%lf", &angle);
printf("enter initial velocity (m/s): ");
scanf("%lf", &velocity);
// Calculate max horizontal distance using the function
double max_distance = projectile_travel_distance(attitude, speed);
// show end result
printf("Projectile most horizontal distance: %.2f metersn", max_distance);
return zero;
__attribute__((unused)) double degree_to_radian(double angle_degrees) go back angle_degrees * M_PI / 180.Zero; // Convert ranges into radians
int predominant() double angle_degrees;
// spark off user for input
printf("input perspective in ranges: ");
scanf("%lf", &angle_degrees);
// Convert ranges to radians with the usage of the characteristic
double angle_radians = degree_to_radian(angle_degrees);
// show end result
printf("%.2f stages is equal to %.2f radiansn", angle_degrees, angle_radians);
return 0;
whilst (speed < zero )
printf ("please enter a tremendous range for velocity: ");
n = scanf("%lf", &velocity);
if(n != 1)
printf("The enter is invalid. Bye...");
exit(1);
// create_table(pace);
go back 0;
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
check alternative code for Exercise D in chatgpt and then write all the code on ur own without copy paste.
warning: 'return' with no value, in function returning non-vo
return; in line 12 , exercise A
note: declared here
3 | int main(void)
| ^~~~
error: expected '=', ',', ';', 'asm' or '__attribute__' at end of input
13 | }cd
| ^
note: declared here
4 | int main(void) {
| ^~~~
note: declared here
4 | int main(void) {
| ^~~~
these errors are resolved only error is : warning: data definition has no type or storage class
warning: type defaults to 'int' in declaration of 'cd'
In function 'main'
warning: 'return' with no value. in function returning non-void
line 13 || return;
note: declared here
line 4 || ;int main(void) {
these errors are also resolved now.
____________________________________________________________________________________________________________________________________________________________
in Exercise D, only one error is encountered:
fatal error: conio.h: No such file or directory
line 2|| #include<conio.h>
this is resolved too.
____________________________________________________________________________________________________________________________________________________________
in Exercise F, a lot of errors were there:
52 | double max_distance = (initial_velocity * initial_velocity * sin(2 * angle_rad)) / GRAVITY;
| error: redeclaration of 'Function' with no linkage
61 | int Function;
| ^~~~~~~~
___________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Here are the changes made:
Removed the extra create_table function declaration and definition as it was causing a syntax error.
Moved the input validation for velocity to a single if statement to simplify the code.
Removed the unnecessary int Function; statement.
Moved the angle to radians conversion to the degree_to_radian function to avoid duplicating the code.
Corrected the function names in the main function when calling projectile_travel_time and projectile_travel_distance.
With these changes, the code should work as expected. It calculates and prints the maximum distance and time of flight for various launch angles given the initial velocity of the projectile.
corrected code from gpt:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
const double G = 9.8; /* gravitation acceleration 9.8 m/s^2 */
const double PI = 3.141592654;
double projectile_travel_time(double angle_degrees, double initial_velocity);
double projectile_travel_distance(double angle_degrees, double initial_velocity);
double degree_to_radian(double angle_degrees);
int main(void) {
int n;
double velocity;
printf("Please enter the initial velocity with which the projectile is launched (m/sec): ");
n = scanf("%lf", &velocity);
if (n != 1 || velocity < 0) {
printf("Input is invalid. Bye...");
exit(1);
}
printf("\nAngle (degrees)\tMaximum Distance (m)\tTime of Flight (s)\n");
for (int angle = 0; angle <= 90; angle += 5) {
double angle_rad = degree_to_radian(angle);
double max_distance = projectile_travel_distance(angle, velocity);
double time_of_flight = projectile_travel_time(angle, velocity);
printf("%d\t\t\t%.2f\t\t\t%.2f\n", angle, max_distance, time_of_flight);
}
return 0;
}
double projectile_travel_time(double angle_degrees, double initial_velocity) {
double angle_radians = degree_to_radian(angle_degrees);
return (2 * initial_velocity * sin(angle_radians)) / G;
}
double projectile_travel_distance(double angle_degrees, double initial_velocity) {
double angle_radians = degree_to_radian(angle_degrees);
return (initial_velocity * initial_velocity * sin(2 * angle_radians)) / G;
}
double degree_to_radian(double angle_degrees) {
return PI * angle_degrees / 180.0;
}
________________________________________________________________________________________________________________________________________________________________________________________
/*
* lab1exe_F.c
* Created by Mahmood Moussavi
* Completed by: Abdul Shakoor Raed
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
const double G = 9.8; /* gravitation acceleration 9.8 m/s^2 */
const double PI = 3.141592654;
// Function prototypes
void create_table(double v);
double projectile_travel_time(double a, double v);
double projectile_travel_distance(double a, double v);
double degree_to_radian(double d);
int main(void) {
int n;
double velocity;
printf("Please enter the initial velocity with which the projectile is launched (m/sec): ");
n = scanf("%lf", &velocity);
if (n != 1 || velocity < 0) {
printf("Input is invalid. Bye...\n");
exit(1);
}
while (velocity < 0 )
{
printf ("please enter a positive number for velocity: ");
n = scanf("%lf", &velocity);
if(n != 1)
{
printf("Invalid input. Bye...");
exit(1);
}
}
create_table(velocity);
return 0;
}
void create_table(double initial_velocity) {
// Print table header
printf("\nAngle (degrees)\tMaximum Distance (m)\tTime of Flight (s)\n");
// Scan specified angles with 5-degree increments and from 0 to 90 degrees
for (int angle = 0; angle <= 90; angle += 5) {
double angle_rad = degree_to_radian(angle);
// Calculate maximum distance (d) and time of flight (t) respectively
double max_distance = projectile_travel_distance(angle_rad, initial_velocity);
double time_of_flight = projectile_travel_time(angle_rad, initial_velocity);
// Print results in the table
printf("%d\t\t\t%.2f\t\t\t%.2f\n", angle, max_distance, time_of_flight);
}
}
double projectile_travel_time(double angle_radians, double initial_velocity) {
return (2 * initial_velocity * sin(angle_radians)) / G;
}
double projectile_travel_distance(double angle_radians, double initial_velocity) {
return (initial_velocity * initial_velocity * sin(2 * angle_radians)) / G;
}
double degree_to_radian(double angle_degrees) {
return PI * angle_degrees / 180.0; // Convert degrees into radians
}
Download