Thread: Geometric Calculator

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    1

    Geometric Calculator

    Hello everyone, I am still working on this project, and have actually made a lot of progress toward its completion. I am only having issues with the final part, a two-dimensional array I have to create to store the area of base (base x width) and height whenever the user wishes to calculate the volume of a rectangular prism. The array must have 2 rows and the number of columns will be determined by how many times the user calculates the volume of the prism(max 20 columns). This is an example of what is supposed to be shown after the user exits the program having calculated one or more prisms:

    You calculated the volume of 3 rectangular prisms:

    Area of base: 45.0 Height: 3.0 Volume: 135.0

    Area of base: 74.88 Height: 6.0 Volume: 449.28

    Area of base: 38.48 Height: 4.0 Volume: 153.92

    I have attached what I have worked out so far, any helpful hints or improvements are welcome and greatly appreciated.

    Inlined by salem
    Code:
    //This program will ask the user to input a number between 1 and 2 in order to calculate the area of 5 shapes or the volume of another 4 shapes.
    //After the user inputs his choice, the program will ask for necessary measurements in order to calculate area or volume, such as length, width, height,
    //radius, etc.
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>				//Used for the PI function.
    #include "geometry.h"			//Used to access all the functions on a separate header file.
    
    
    int main()
    {
    double displayVolume[2][20] = {};
    int choice;
    int a, b;
    char answer=' ';
    char name[30] = " ";
    char calcResults[128];
    
    //Ask user for his first and last name, then print out in CAPS and welcome him/her to the program.
    printf("Please, enter your first and last names separated by a space: ");
    fgets(name, 30, stdin);
    strupr(name);
    printf("\nHello %s\nWelcome to My Geometric Calculator.\n", name);
    
    //Write the results of each calculation to a file.
    //Allow user to choose the name of the file by using the function strcat.
    printf("\nEnter .txt file name to save future calculations: ");
    scanf("%123s", calcResults);
    strcat(calcResults, ".txt");
    FILE *inputf;
    inputf = fopen(calcResults, "w");
    
    //Test if the file is functioning properly; if not, display an error message.
        if (inputf == NULL)
        {
        printf("Error opening file!\n");
        return 1;
        }
    
    do
    {
        choice = selectedOption();
    
    //Switch statement in order to allow the choice between different shapes to calculate area or volume. Print to file
    	switch(choice)
    	{
    		case 1:
    		 	printf("\nEnter data to calculate area of Parallelogram.");
    			printf("\n\nBase: ");
    			scanf("%f", &base);
    			printf("\nHeight: ");
    			scanf("%f", &height);
    			printf("\nThe Area of the Parallelogram is %.2f\n", parallelogramArea(base, height));
    			fprintf(inputf, "\nThe Area of the Parallelogram is %.2f\n", parallelogramArea(base, height));
    		break;
    
    		case 2:
    			printf("\nEnter data to calculate area of a triangle.");
    			printf("\n\nBase: ");
       			scanf("%f", &base);
    		    printf("\nHeight: ");
    		    scanf("%f", &height);
      			printf("\nThe Area of the Triangle is %.2f\n", triangleArea(base, height));
      			fprintf(inputf, "\nThe Area of the Triangle is %.2f\n", triangleArea(base, height));
    		break;
    
    	    case 3:
    			printf("\nEnter data to calculate area of a trapezoid.");
    			printf("\n\nBase: ");
       			scanf("%f", &base);
    		    printf("\nBase Two: ");
    		    scanf("%f", &baseTwo);
    		    printf("\nHeight: ");
    		    scanf("%f", &height);
      			printf("\nThe Area of the Trapezoid is %.2f\n", trapezoidArea(base, baseTwo, height));
      			fprintf(inputf, "\nThe Area of the Trapezoid is %.2f\n", trapezoidArea(base, baseTwo, height));
    		break;
    
    		case 4:
    			printf("\nEnter data to calculate area of a circle. \n\nRadius: ");
                scanf("%f", &radius);
                printf("\nThe Area of the Circle is %.2f\n", circleArea(radius));
                fprintf(inputf, "\nThe Area of the Circle is %.2f\n", circleArea(radius));
    		break;
    
    		case 5:
    			printf("\nEnter data to calculate area of a regular polygon.");
    			printf("\n\nLength: ");
    			scanf("%f", &length);
    			printf("\nWidth: ");
    			scanf("%f", &width);
    			printf("\nThe Area of the Regular Polygon is %.2f\n", regPolygonArea(length, width));
    			fprintf(inputf, "\nThe Area of the Regular Polygon is %.2f\n", regPolygonArea(length, width));
    		break;
    
    		case 6:
    			printf("\nEnter data to calculate volume of a rectangular prism.");
    			printf("\n\nLength: ");
       			scanf("%f", &length);
    		    printf("\nWidth: ");
    		    scanf("%f", &width);
    		    printf("\nHeight: ");
    		    scanf("%f", &height);
                displayVolume[0][1] = length * width;
                displayVolume[1][1] = height;
      			printf("\nThe Volume of the Rectangular Prism is %.2f\n", rectPrismVolume(length, width, height));
        		fprintf(inputf, "\nThe Volume of the Rectangular Prism is %.2f\n", rectPrismVolume(length, width, height));
    		break;
    
    		case 7:
    			printf("\nEnter data to calculate volume of a right circular cylinder.");
    			printf("\n\nRadius: ");
    			scanf("%f", &radius);
    			printf("\nHeight: ");
    			scanf("%f", &height);
    			printf("\nThe Volume of the Right Circular Cylinder is %.2f\n", rightCircCylinderVolume(radius, height));
    			fprintf(inputf, "\nThe Volume of the Right Circular Cylinder is %.2f\n", rightCircCylinderVolume(radius, height));
    		break;
    
    		case 8:
    			printf("\nEnter data to calculate volume of a right square pyramid.");
    			printf("\n\nBase Edge: ");
    			scanf("%f", &baseEdge);
    			printf("\nHeight: ");
    			scanf("%f", &height);
    			printf("\nThe Volume of the Right Square Pyramid is %.2f\n", rightSqrPyramidVolume(baseEdge, height));
    			fprintf(inputf, "\nThe Volume of the Right Square Pyramid is %.2f\n", rightSqrPyramidVolume(baseEdge, height));
    		break;
    
    		case 9:
    			printf("\nEnter data to calculate volume of a right circular cone.");
    			printf("\n\nRadius: ");
    			scanf("%f", &radius);
    			printf("\nHeight: ");
    			scanf("%f", &height);
    			printf("\nThe Volume of the Right Circular Cone is %.2f\n", rightCircConeVolume(radius, height));
    			fprintf(inputf, "\nThe Volume of the Right Circular Cone is %.2f\n", rightCircConeVolume(radius, height));
    		break;
    		default: puts("You did not enter a valid number.");
        }
    
    //Allow user to try again by using an if statement.
    printf("\nTry again?\n");
    printf("\nEnter Y or y for yes or any other value to exit:\n");
    scanf("%s", &answer);
    }while(answer == 'Y' || answer == 'y');
    
    //Use array to display the Volume calculations done by the user.
        for(a = 0; a <= 1; a++)
        {
            for(b = 0; b <= 19; b++)
            {
        printf("You calculated the volume of  Rectangular Prisms: \n");
        printf("Area of Base: %.2f", a, displayVolume[a][1]);
        printf("\tHeight: %.2f", b, displayVolume[1][b]);
        printf("\tVolume: %.2f\n", rectPrismVolume(length, width, height));
            }
        }
    
    //Close the file.
    fclose(inputf);
    return 0;
    }
    
    //The menuArea function is used in order to prompt the user to select a shape and calculate its area, returning the choice at the end.
    int menuArea()
    {
        int figure;
        printf("\nSelect figure: \n");
        //Printf with figures for area
        printf("\t1- Parallelogram\n\t2- Triangle\n\t3- Trapezoid\n\t4- Circle\n\t5- Regular Polygon\n");
    	printf("\n\nEnter your option: ");
    	scanf("%d", &figure);
    
    	return figure;
    }
    
    //The menuVolume function is used in order to prompt the user to select a shape and calculate its volume, returning the choice at the end.
    int menuVolume()
    {
    	int option;
        printf("\nSelect figure: \n");
        //Printf with figures for volume
        printf("\t1- Rectangular prism\n\t2- Right circular cylinder\n\t3- Right square pyramid\n\t4- Right circular cone\n");
    	printf("\n\nEnter your option: ");
    	scanf("%d", &option);
    
    	return option+5;
    }
    
    //The selectedOption function is used in order to return the user's choice between area and volume, returning the choice at the end.
    int selectedOption()
    {
    	int choice;
    
        printf("\nSelect one of the following options: ");
        printf("\n\t1- Calculate area\n\t2- Calculate volume\n");
        printf("\nEnter your option: ");
        scanf("%d", &choice);
        switch(choice)
        {
            case 1: choice = menuArea();
            break;
            case 2: choice = menuVolume();
            break;
        }
        return(choice);
    }
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    cross-posted here, so already a waste of time -> Geometric Calculator Array - C And C++ | Dream.In.Code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Salem View Post
    cross-posted here, so already a waste of time -> Geometric Calculator Array - C And C++ | Dream.In.Code
    What?!?! And here I am having spent the last 5 hours studying and refining the code :-( Just my luck!

    Edit: Hodor, Hodor, HODOR!
    Last edited by Hodor; 12-07-2015 at 12:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. geometric algebra algorithm
    By dreamvig in forum C++ Programming
    Replies: 10
    Last Post: 08-25-2015, 05:12 AM
  2. Quick question on geometric series'
    By bevin in forum C Programming
    Replies: 8
    Last Post: 11-14-2014, 06:48 AM
  3. Geometric search/intersection problem
    By getsmart1 in forum Tech Board
    Replies: 9
    Last Post: 09-24-2012, 08:52 AM
  4. Arithmetic/Geometric/Harmonic
    By DJ_Steve in forum C Programming
    Replies: 12
    Last Post: 08-23-2009, 08:34 PM
  5. Geometric Libraries and Coordinate planes
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 11-11-2001, 02:46 PM

Tags for this Thread