Code:
// ------------------------------------------------------------------------------------------
// Includes
// ------------------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#include <math.h>
// ------------------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// Prototypes
// ------------------------------------------------------------------------------------------
void DisplayInstructions();
void DisplayMessage(int intPrintCount);
int GetLargerValue(int intValue1, int intValue2);
int GetLargestValue(int intValue1, int intValue2, int intValue3, int intValue4, int intValue5,
int intValue6, int intValue7);
int CalculateSphereVolume(int intDiameter);
void PassIntByPointer(int* pintValue);
float SolvePythagoreanTheorum(float* psngA, float* psngB, float* psngC);
float FindQuadraticRoots(int intA, int intB, int intC, float* psngRoot1, float* psngRoot2);
int DisplayFirst20Factorials(int intFactorial);
int DisplayUserFactorial(int intFactorial);
void main()
{
int intLargerValue = 0;
int intLargestValue = 0;
int intVolume = 0;
int pintValue = 0;
float psngA = 0;
float psngB = 0;
float psngC = 0;
float sngDiscriminant = 0;
float psngRoot1 = 0;
float psngRoot2 = 0;
int intFactorial = 0;
int n = 1;
int intUserInput = 0;
//Problem #1 - Display Instructions
printf("Problem #1 - Display Instructions\n");
DisplayInstructions();
printf("\n");
//Problem #2 - Display Message
printf("Problem #2 - Display Message X Times\n");
DisplayMessage(10);
printf("\n");
//Problem #3 - Get Larger Value
printf("Problem #3 - Get Larger Value\n");
intLargerValue = GetLargerValue(5, 10);
printf("The larger value is %d\n", intLargerValue);
printf("\n");
//Problem #4 - Get Largest Value
printf("Problem #4 - Get Largest Value\n");
intLargestValue = GetLargestValue(4, 17, 23, 56, 7, 5, 10);
printf("The largest value is %d\n", intLargestValue);
printf("\n");
//Problem #5 - Calculate Sphere Volume
printf("Problem #5 - Calculate the volume of a sphere\n");
intVolume = CalculateSphereVolume(40);
printf("The volume of the sphere is %d\n", intVolume);
printf("\n");
//Problem #6 - Print a Pointer by Reference
printf("Problem #6 - Print a Pointer by Reference\n");
PassIntByPointer(&pintValue);
printf("The value change from 0 to %d\n", pintValue);
printf("\n");
//Problem #7 - SolvePythagoreanTheorum
printf("Problem #7 - Solve Pythagorean Theorum\n");
SolvePythagoreanTheorum(&psngA, &psngB, &psngC);
printf("A = %f, B = %f, C = %f\n", psngA, psngB, psngC);
printf("\n");
//Problem #8 - FindQuadraticRoots
printf("Problem #7 - Find the Quadratic Roots\n");
FindQuadraticRoots(20, 30, 40, &psngRoot1, &psngRoot2);
if (sngDiscriminant = 1)
{
printf("Root 1 = %f, Root 2 = %f\n", psngRoot1, psngRoot2);
}
if (sngDiscriminant = 0)
{
("The single root is %f\n", psngRoot1, psngRoot2);
}
else
{
printf("No roots in real numbers\n");
}
printf("\n");
//Problem #9 - DisplayFirst20Factorials
printf("Problem #9 - Display First 20 Factorials\n");
DisplayFirst20Factorials(&intFactorial);
for (n = 1; n <= 20; ++n)
{
printf(" %d! = %d\n", n, intFactorial);
if (n % 10 == 0)
{
printf("\n");
}
}
printf("\n");
//Problem #10- DisplayUserFactorial
printf("Problem #10- Solve Pythagorean Theorum\n");
DisplayUserFactorial(&intFactorial);
printf("The factorial of %d is %d", intUserInput, intFactorial);
printf("\n");
system("pause");
}
// ------------------------------------------------------------------------------------------
// Name: Display Instructions
// Abstract: Tell the user what's going on
// ------------------------------------------------------------------------------------------
void DisplayInstructions( )
{
printf( "This program will demonstrate how to make and use procedures in C.\n" );
printf( "In addition it will demonstrate how to pass values and variables into\n" );
printf( "a procedure as parameters.It will demonstrate how to return a value\n" );
printf( "from a function using the return keyword.It will demonstrate how to\n" );
printf( "emulate passing variables by reference using pointers\n" );
}
// ------------------------------------------------------------------------------------------
// Name: Display Message
// Abstract: Print the message X number of times
// ------------------------------------------------------------------------------------------
void DisplayMessage( int intPrintCount )
{
int intIndex = 0;
for (intIndex = 1; intIndex <= intPrintCount; intIndex += 1)
{
printf("I Love Star Trek.\n");
}
}
// ------------------------------------------------------------------------------------------
// Name: Get Larger Value
// Abstract: Return the larger of the two values
// ------------------------------------------------------------------------------------------
int GetLargerValue(int intValue1, int intValue2)
{
int intLargerValue = 0;
if (intValue1 > intValue2)
{
intLargerValue = intValue1;
}
else
{
intLargerValue = intValue2;
}
return intLargerValue;
}
// ------------------------------------------------------------------------------------------
// Name: Get Largest Value
// Abstract: Return the largest of seven values
// ------------------------------------------------------------------------------------------
int GetLargestValue(int intValue1, int intValue2, int intValue3, int intValue4, int intValue5,
int intValue6, int intValue7)
{
int intLargestValue = 0;
if (intValue1 > intValue2 & intValue1 > intValue3 & intValue1 > intValue4 & intValue1 > intValue5
& intValue1 > intValue6 & intValue1 > intValue7)
{
intLargestValue = intValue1;
}
else if (intValue2 > intValue1 & intValue2 > intValue3 & intValue2 > intValue4 & intValue2 > intValue5
& intValue2 > intValue6 & intValue2 > intValue7)
{
intLargestValue = intValue2;
}
else if (intValue3 > intValue1 & intValue3 > intValue2 & intValue3 > intValue4 & intValue3 > intValue5
& intValue3 > intValue3 & intValue3 > intValue7)
{
intLargestValue = intValue3;
}
else if (intValue4 > intValue1 & intValue4 > intValue2 & intValue4 > intValue3 & intValue4 > intValue5
& intValue4 > intValue6 & intValue4 > intValue7)
{
intLargestValue = intValue4;
}
else if (intValue5 > intValue1 & intValue5 > intValue2 & intValue5 > intValue3 & intValue5 > intValue4
& intValue5 > intValue6 & intValue5 > intValue7)
{
intLargestValue = intValue5;
}
else if (intValue6 > intValue1 & intValue6 > intValue2 & intValue6 > intValue3 & intValue6 > intValue4
& intValue6 > intValue5 & intValue6 > intValue7)
{
intLargestValue = intValue6;
}
else
{
intLargestValue = intValue7;
}
return intLargestValue;
}
///getPi
float GetPi()
{
return 3.14159f;
}
// ------------------------------------------------------------------------------------------
// Name: CalculateSphereVolume
// Abstract: Return the volume of a sphere
// ------------------------------------------------------------------------------------------
///getting wrong answer
int CalculateSphereVolume(int intDiameter)
{
int intVolume = 0;
int intRadius = intDiameter / 2;
//calculate volume
intVolume = ((4 / 3) * GetPi() * (intRadius^3));
return intVolume;
}
// ------------------------------------------------------------------------------------------
// Name: PassIntByPointer
// Abstract: Display Pointer By Reference
// ------------------------------------------------------------------------------------------
void PassIntByPointer(int* pintValue)
{
*pintValue = 15;
}
// ------------------------------------------------------------------------------------------
// Name: SolvePythagoreanTheorum
// Abstract: Solve Pythagorean Theorum
// ------------------------------------------------------------------------------------------
float SolvePythagoreanTheorum(float* psngA, float* psngB, float* psngC)
{
*psngA = 3;
*psngB = 0;
*psngC = 5;
if (*psngA = 0)
{
*psngA = (*psngB * *psngB + *psngC * *psngC);
}
else if (*psngB = 0)
{
*psngB = (*psngA * *psngA + *psngC * *psngC);
}
else
{
*psngC = (*psngA * *psngA + *psngB * *psngB);
}
return *psngA, *psngB, *psngC;
}
// ------------------------------------------------------------------------------------------
// Name: FindQuadraticRoots
// Abstract: Find Quadratic Roots
// ------------------------------------------------------------------------------------------
float FindQuadraticRoots(int intA, int intB, int intC, float* psngRoot1, float* psngRoot2)
{
float sngDiscriminant = intB ^ 2 - 4 * intA * intC;
if (sngDiscriminant > 0)
{
/*If the discriminant is greater than zero, the procedure should calculate both roots
and save the results at the addresses stored in psngRoot1 and psngRoot2.
Return a value of one from the procedure*/
*psngRoot1 = (-intB + sqrt(-intB ^ 2 - 4 * intA * intC)) / 2 * intA;
*psngRoot2 = (-intB + sqrt(-intB ^ 2 - 4 * intA * intC)) / 2 * intA;
return *psngRoot1, *psngRoot2;
}
if (sngDiscriminant = 0)
{
/*If the discriminant is zero, the procedure should calculate the one root
and save the result at both addresses stored in psngRoot1 and psngRoot2.
Return a value of zero from the procedure.*/
*psngRoot1 = (-intB + sqrt(-intB ^ 2 - 4 * intA * intC)) / 2 * intA;
*psngRoot2 = (-intB + sqrt(-intB ^ 2 - 4 * intA * intC)) / 2 * intA;
return *psngRoot1, *psngRoot2;
}
else if (sngDiscriminant > 0)
{
/*If the discriminant is less than zero return a value of negative one.*/
sngDiscriminant = -1;
}
return *psngRoot1, *psngRoot2;
}
// ------------------------------------------------------------------------------------------
// Name: DisplayFirst20Factorials
// Abstract: Display First 20 Factorials
// ------------------------------------------------------------------------------------------
int DisplayFirst20Factorials(int intFactorial)
{
int n = 1;
for (n = 1; n <= 20; ++n)
{
intFactorial *= n;
}
return 0, intFactorial;
}
// ------------------------------------------------------------------------------------------
// Name: DisplayUserFactorial
// Abstract: Solve Pythagorean Theorum
// ------------------------------------------------------------------------------------------
int DisplayUserFactorial(int intFactorial)
{
int intUserInput = 0;
do
{
printf("Please enter a number from 1 to 100:");
scanf_s(" %d", &intUserInput);
} while (intUserInput <= 1 & intUserInput >= 100);
intUserInput = intUserInput;
for (int n = 1; n <= n; n++)
{
intFactorial *= n;
}
return intFactorial, intUserInput;
}