0% found this document useful (0 votes)
21 views11 pages

Engine Ering Analys IS: Name: Amna Ali Alkhalidi ID:202020019

The document contains an engineering analysis C++ assignment submitted by Amna Ali Alkhalidi with ID 202020019. The assignment contains 4 problems: 1) Calculate the distance between two coordinates, 2) Find the square root of various values, 3) Calculate the derivative and integral of a function, 4) Convert polar to Cartesian coordinates. The code provided uses standard C++ libraries and functions like sqrt, pow, cout to solve each problem.

Uploaded by

Amoon Alkhalidi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views11 pages

Engine Ering Analys IS: Name: Amna Ali Alkhalidi ID:202020019

The document contains an engineering analysis C++ assignment submitted by Amna Ali Alkhalidi with ID 202020019. The assignment contains 4 problems: 1) Calculate the distance between two coordinates, 2) Find the square root of various values, 3) Calculate the derivative and integral of a function, 4) Convert polar to Cartesian coordinates. The code provided uses standard C++ libraries and functions like sqrt, pow, cout to solve each problem.

Uploaded by

Amoon Alkhalidi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

ENGINE

ERING
ANALYS
IS
C++
Assignment 1

Name : amna ali Alkhalidi


ID:202020019
- Find the distance between two city coordinates

#include <iostream>

#include <cmath>

using namespace std;

int main()

double x1, y1, x2, y2, distance;

cout << "Enter the x and y coordinates of the first point: ";

cin >> x1 >> y1;

cout << "Enter the x and y coordinates of the second point: ";

cin >> x2 >> y2;

distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));

cout << "The distance between (" << x1 << ", " << y1 << ") and (" << x2 << ", " << y2 << ") is " << distance
<<"KM" <<endl;

return 0;

1|Page
2|Page
2- Find the root of any function. Use Functions:

#include <iostream>

#include <cmath>

using namespace std;

int main() {

//Square root of 0

cout << "Square root of 0 = " << sqrt(0) << endl;

//Square root of a perfect square no.

cout << "Square root of 9 = " << sqrt(9) << endl;

//Square root of no that isn't a perfect square

cout << "Square root of 10 = " << sqrt(10) << endl;

//Square root of decimal number

cout << "Square root of 0.15 = " << sqrt(0.15) << endl;

//Square root of a negative number

cout << "Square root of -19 = " << sqrt(-19) << endl;

return 0;

3|Page
4|Page
3- Find derivative and integration of a function:

#include <iostream>

#include <functional>

std::function<double(double)> Deriv( double(*Foo)(double x) )

auto f = [Foo](double x) -> double

const double mtDx = 1.0e-6;

double x1 = Foo(x+mtDx);

double x0 = Foo(x);

return ( x1 - x0 ) / mtDx;

};

return f;

double Foo(double x)

return x*x;

5|Page
double Bar(double x)

return x*x*x;

int main()

std::cout << Deriv(Foo)(10) << std::endl;

std::cout << Deriv(Bar)(10) << std::endl;

6|Page
4- Convert from polar to Cartesian coordinates:

// C++ program for the above approach

#include <bits/stdc++.h>

using namespace std;

// Function to convert degree to radian

double ConvertDegToRad(double degree)

double pi = 3.14159;

return (degree * (pi / 180.0));

// Function to convert the polar

// coordinate to cartesian

void ConvertToCartesian(

pair<double, double> polar)

// Convert degrees to radian

polar.second = ConvertDegToRad(

polar.second);

// Applying the formula:

// x = rcos(theta), y = rsin(theta)

7|Page
pair<double, double> cartesian

= { polar.first * cos(polar.second),

polar.first * sin(polar.second) };

// Print cartesian coordinates

printf("%0.3f, %0.3f",

cartesian.first,

cartesian.second);

// Driver Code

int main()

// Given polar coordinates

pair<double,

double>

polar = { 1.4142, 45 };

// Function to convert polar

// coordinates to equivalent

// cartesian coordinates

ConvertToCartesian(polar);

return 0;

8|Page
9|Page
10 | P a g e

You might also like