Code:
#include <cstdlib>
#include <math.h>
#define EPSILON 0.000001
void sphere(float radius, float &surface, float &volume)
{
#define PI 3.14159265358979323846264338327950288419716939937510582097494459
float radSq = radius * radius;
surface = 4 * PI * radSq;
volume = (4 * PI * radius * radSq)/3;
}
float volCylinder(float radius, float height)
{
float volume = (PI * radius * radius * height);
return volume;
}
float sumFloats(float x[], int numFloats)
{
float sum = 0.0;
for(int i = 0 ; i < numFloats ; i++)
{
sum += x[i];
}
return sum;
}
double sine(float angle)
{
int d = 1;
double guess = 0;
int i = 1;
float x2p1 = angle;
for(guess = angle; guess * guess - angle < EPSILON || guess * guess - angle < -EPSILON; guess = (x2p1 / (d *((i * 2) +1)*(i *2))))
{
x2p1 = x2p1 * angle * angle * -1;
i = i +1;
}
return guess;
}
Code:
#include <fstream> // fstream.h contains file sequential IO methods.
#include <stdlib.h> // stdlib.h contains the exit method.
#include <stdio.h>
#include <string.h>
#include "mathlib.c"
using namespace std;
int main( )
{
char fileOutName[25];
char response;
int n = 0;
int choice = 0;
int again = 1;
float angle = 0.0;
float sum = 0.0;
float radius = 0.0;
float surface = 0.0;
float volume = 0.0;
float height = 0.0;
int filecount = 0; //The number of lines in the file.
FILE *outFile; // Logical output file name in program.
//Gets the file out name and then checks to see if it already exists
//If so then it asks for you to choose whether or not to override.
printf("What is the name of the output file (max 25 characters)?");
scanf("%s", &fileOutName);
if((outFile = fopen(fileOutName, "r" )) != NULL )
{
printf("\nA file by the name %s exists.\n", fileOutName);
printf("Do you wish to overwrite (Y or N): ");
//Skip <cr> in buffer with "%*c", i.e., one character
scanf("%*c %c", &response);
if( (response == 'n') || (response == 'N') )
{ // should close file first
printf("\nProgram aborted to prevent overwirte!");
exit(1);
}
}
outFile = fopen( fileOutName, "wt" );
if( outFile == NULL )
{
printf("Could not create the output file! Program terminating.");
exit(1);
}
while (again == 1)
{
printf("What would you like to do? 1 for Sphere, 2 for Cylinder, 3 for float addition, 4 for sin.\n");
scanf("%d", choice);
if (choice == 1)
{
printf("What is the radius of the sphere?\n");
scanf("%f", radius);
sphere(radius, surface, volume);
printf("The sphere with a radius of %f has a surface of %f and a volume of %f.\n", radius, surface, volume);
fprintf(outFile, "The sphere with a radius of %f has a surface of %f and a volume of %f. \n", radius, surface, volume);
printf("Go again? (1 for yes, 2 for no)\n");
scanf("%d", again);
}
else if (choice == 2)
{
printf("What is the radius of the cylinder?\n");
scanf("%f", radius);
printf("What is the height of the cylinder?\n");
scanf("%f", height);
volume = volCylinder(radius, height);
printf("The cylinder with a radius of %f and a height of %f has a volume of %f.\n", radius, height, volume);
fprintf(outFile, "The cylinder with a radius of %f and a height of %f has a volume of %f.\n", radius, height, volume);
printf("Go again? (1 for yes, 2 for no)\n");
scanf("%d", again);
}
else if (choice == 3)
{
printf("How many floats are being processed? \n");
scanf("%d", n);
float x[n];
for (i = 0; i < n; i++)
{
printf("What is the float?\n");
scanf("%f", x[i]);
}
for (j = 0; j < n - 1; j++)
{
for (k = j + 1; k < n; K++)
{
if (x[j] > x[k])
{
int temp = x[j];
x[j] = x[k];
x[k] = temp;
}
}
}
sum = sumFloats(x[], n);
printf("The sum of the floats is %f. \n", sum);
fprintf(outFile, "The sum of the floats is %f. \n", sum);
printf("Go again? (1 for yes, 2 for no)\n");
scanf("%d", again);
}
else if (choice == 4)
{
printf("What is the angle? \n");
scanf("%f", angle);
double answer = sin(angle);
printf("The sin of angle %f is %f. \n", angle, answer);
fprintf(outFile, "The sin of angle %f is %f. \n", angle, answer);
printf("Go again? (1 for yes, 2 for no)\n");
scanf("%d", again);
}
else
{
printf("Quitting.");
again = 2;
}
}
fclose(outFile);
return 0;
}