Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This lab was designed to reinforce programming concepts using control structures

ID: 3843228 • Letter: T

Question

This lab was designed to reinforce programming concepts using control structures.

In this lab, you will practice:

• Built in math functions in the C standard library

• Function prototyping, calling, definitions, and return types

• Working with larger programs and the divide and conquer strategy

Lab assignment:

In this lab you will write a total of three functions:

The first and second functions will find the magnitude and angle in degrees of a two-dimensional vector

(unrelated to the triangle of part 1). To find the magnitude of the vector, the built-in math functions

pow() and sqrt() should be used. To find the angle, the built-in math function atan2() should be

used. This function is nice because it can return an angle in all four quadrants, unlike most arctan or tan-1

calculations. Both of these functions will require two double input parameters (the x and y components),

and both will return a double result. Note that you must include <math.h> to use the built-in math

functions. The prototypes for these functions are:

double magnitude2(double x, double y);

double angle2(double x, double y);

The last function will accept an integer grade from 1 to 100 as a parameter and return an upper-case

character representing that grade. For a range of 90 to 100, the grade will be ‘A’, for 80 to 89 the grade

will be ‘B’ and so on. A grade of 59 and below is an ‘F’ grade. The function prototype for this function is:

char getGrade(int grade);

It is important to get in the habit of being able to identify what parameters are necessary (how many and

of what types) for a function to perform its job, and also the return type of the function. Notice in this

laboratory the difference between calling a function that has no return value (void) compared with a

function that does return a value.

Note: it is useful to work on each function individually and test them by calling the function in main().

Do not attempt to do everything at once. Realize that even if a function is defined, it does not execute

unless it is used/called inside of main() or another function. So, you can use this fact to determine which

one to test and which not to test.

The structure of the program is laid out on the following page. The main portion of the program is already

complete (the portion which uses the functions you will write/implement). It is your task to write the code

that goes inside of each function at the bottom.

#include "stdafx.h"

#include <stdio.h>

#include <math.h> // necessary to use math functions like pow(), sqrt(), etc.

// Function prototype (declarations) for the functions you will create.

double magnitude2(double x, double y);

double angle2(double x, double y);

char getGrade(int grade);

double radians2degrees(double radians);

#define PI (3.14159265359) // define used to refer to number Pi

#define DEGREE_SYMBOL (248) // ASCII code for degree symbol

int main()

{

// Test the radians2degrees() function, check they work with various inputs

double degrees;

degrees = radians2degrees(PI);

printf("PI radians = %.1f%c ", degrees, DEGREE_SYMBOL);

degrees = radians2degrees(PI / 2);

printf("PI/2 radians = %.1f%c ", degrees, DEGREE_SYMBOL);

double mag;

// Test magnitude2() function, check they work with various inputs

mag = magnitude2(3.0, 4.0);

printf("Magnitude (3.0, 4.0) = %f ", mag);

// Alternatively, variables can be saved by calling the functions

// directly in printf(), as shown below:

printf("Magnitude (5.0, 12.0) = %f ", magnitude2(5.0, 12.0));

// Test angle2() function, note the use of degree symbol which can be found

// by printing out the ASCII table for your computer/software and looking for it

// the ASCII code is 248 for this particular computer/software

printf("Angle (2.0, 2.0) = %.2f%c ", angle2(2.0, 2.0), DEGREE_SYMBOL);

printf("Angle (-2.0, -2.0) = %.2f%c ", angle2(-2.0, -2.0), DEGREE_SYMBOL);

// Test the getGrade() function

printf(" Grade 90 is %c ", getGrade(90));

printf("Grade 55 is %c ", getGrade(55));

return 0; // main still returns 0 at the end like before

}

// function body for the "magnitude2() function. Use pow() and sqrt() math

// functions to find and return the magnitude of the vector

double magnitude2(double x, double y)

{

double m = 0;

// write the code here to find magnitude of vector

return m; // this function returns a double

}

// function body for the "angle2()" function. Use atan2(y,x) to find angle

// then convert to degrees before returning. atan2() will return angle in all

// 4 quadrants (contrasted with arctan on a calculator)

double angle2(double x, double y)

{

double a = 0;

// Write the code here to find the angle in degrees of the vector

// use the provided “radians2degrees()” function to convert to degrees

return a; // also returns a double type variable

}

// function body for the radians2degrees() function

// this function converts a double angle in radians to degrees

// the degree value as a double type is returned

double radians2degrees(double radians)

{

double degrees = 0;

// this converts radians to degrees

degrees = radians*180.0 / PI;

return degrees; // returns a double type variable

}

// function body for getGrade() function. The function returns a character

// representing the grade parameter that was passed into the function.

char getGrade(int grade)

{

char c = 0;

// code to find the character representing the integer grade

return c; // return the character

}

Sample Output:

PI radians = 180.0°

PI/2 radians = 90.0°

Magnitude (3.0, 4.0) = 5.000000

Magnitude (5.0, 12.0) = 13.000000

Angle (2.0, 2.0) = 45.00°

Angle (-2.0, -2.0) = -135.00°

Grade 90 is A

Grade 55 is F

Press any key to continue . . .

Explanation / Answer

// C code
#include "stdafx.h"
#include <stdio.h>
#include <math.h> // necessary to use math functions like pow(), sqrt(), etc.
// Function prototype (declarations) for the functions you will create.
double magnitude2(double x, double y);
double angle2(double x, double y);
char getGrade(int grade);
double radians2degrees(double radians);
#define PI (3.14159265359) // define used to refer to number Pi
#define DEGREE_SYMBOL (248) // ASCII code for degree symbol
int main()
{
// Test the radians2degrees() function, check they work with various inputs
double degrees;
degrees = radians2degrees(PI);
printf("PI radians = %.1f%c ", degrees, DEGREE_SYMBOL);
degrees = radians2degrees(PI / 2);
printf("PI/2 radians = %.1f%c ", degrees, DEGREE_SYMBOL);
double mag;
// Test magnitude2() function, check they work with various inputs
mag = magnitude2(3.0, 4.0);
printf("Magnitude (3.0, 4.0) = %f ", mag);
// Alternatively, variables can be saved by calling the functions
// directly in printf(), as shown below:
printf("Magnitude (5.0, 12.0) = %f ", magnitude2(5.0, 12.0));
// Test angle2() function, note the use of degree symbol which can be found
// by printing out the ASCII table for your computer/software and looking for it
// the ASCII code is 248 for this particular computer/software
printf("Angle (2.0, 2.0) = %.2f%c ", angle2(2.0, 2.0), DEGREE_SYMBOL);
printf("Angle (-2.0, -2.0) = %.2f%c ", angle2(-2.0, -2.0), DEGREE_SYMBOL);
// Test the getGrade() function
printf(" Grade 90 is %c ", getGrade(90));
printf("Grade 55 is %c ", getGrade(55));
return 0; // main still returns 0 at the end like before
}
// function body for the "magnitude2() function. Use pow() and sqrt() math
// functions to find and return the magnitude of the vector
double magnitude2(double x, double y)
{
double m = 0;
// write the code here to find magnitude of vector
m = sqrt(x*x + y*y);

return m; // this function returns a double
}
// function body for the "angle2()" function. Use atan2(y,x) to find angle
// then convert to degrees before returning. atan2() will return angle in all
// 4 quadrants (contrasted with arctan on a calculator)
double angle2(double x, double y)
{
double a = 0;
// Write the code here to find the angle in degrees of the vector
// use the provided “radians2degrees()” function to convert to degrees
a = (atan2(y,x)*180)/PI;
return a; // also returns a double type variable
}
// function body for the radians2degrees() function
// this function converts a double angle in radians to degrees
// the degree value as a double type is returned
double radians2degrees(double radians)
{
double degrees = 0;
// this converts radians to degrees
degrees = radians*180.0 / PI;
return degrees; // returns a double type variable
}
// function body for getGrade() function. The function returns a character
// representing the grade parameter that was passed into the function.
char getGrade(int grade)
{
char c = 0;
// code to find the character representing the integer grade
if(grade >= 90)
   c = 'A';
else if(grade >= 80)
   c = 'B';
else if(grade >= 70)
   c = 'C';
else if(grade >= 60)
   c = 'D';
else
   c = 'F';

return c; // return the character
}

/*

Sample Output:
PI radians = 180.0°
PI/2 radians = 90.0°
Magnitude (3.0, 4.0) = 5.000000
Magnitude (5.0, 12.0) = 13.000000
Angle (2.0, 2.0) = 45.00°
Angle (-2.0, -2.0) = -135.00°
Grade 90 is A
Grade 55 is F

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote