Thread: Writing math in C

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    20

    Lightbulb Writing math in C

    Hello everyone, this is my first time posting here so any help would be appreciated. i am currently taking a class on C language, its an intro class. I have no previous knowledge on how to work with C language and i was wondering if anyone could help me with the following problems. i am using Codeblocks.

    Write a C program that outputs to the screen the following arithmetic operations
    ¡ 7 + 3 * 5 – 2
    ¡ 4 + 7 / 3
    ¡ 8 % 3 * 6
    ¡ (7 + 3) * 5 – 2


    Write a program to calculate distance between two points.
    In your code do the following:
    Include <math.h>
    Ask the user to enter 2 points (x1,y1) and (x2,y2).
    Calculate distance using the formula: sqrt ( (y2-y1)^2 + (x2-x1)^2)
    Use sqrt and pow functions

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by powerfox0 View Post
    Hello everyone, this is my first time posting here so any help would be appreciated. i am currently taking a class on C language, its an intro class. I have no previous knowledge on how to work with C language and i was wondering if anyone could help me with the following problems. i am using Codeblocks.

    Write a C program that outputs to the screen the following arithmetic operations
    ¡ 7 + 3 * 5 – 2
    ¡ 4 + 7 / 3
    ¡ 8 % 3 * 6
    ¡ (7 + 3) * 5 – 2


    Write a program to calculate distance between two points.
    In your code do the following:
    Include <math.h>
    Ask the user to enter 2 points (x1,y1) and (x2,y2).
    Calculate distance using the formula: sqrt ( (y2-y1)^2 + (x2-x1)^2)
    Use sqrt and pow functions
    Hi,
    Have you attempted to solve this?
    Announcements - General Programming Boards

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    20
    Quote Originally Posted by bos1234 View Post
    Hi,
    Have you attempted to solve this?
    Announcements - General Programming Boards
    well i have done it mathematically, I just have no prior knowledge of writing anything in code, what would be the basic steps?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Please go to the top of this website, and click on the "C Tutorial" button - it's an excellent tutorial. (Follow C, not C++).

    As far as the match goes, remember the order of operations from math. C is very similar. If you don't have the table of precedence of these operations, download one from the net (Google is your friend). The Tutorial may also have that table.

    That might get you through this, but do post back (with your code), if you run into problems. Without code, your questions can't be specifically and authoritatively, addressed. It's a huge waste of time, if we can't see the actual code you're talking about.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    20
    this is for the second algorithm, but I keep encountering an error at last line, says error: expected declaration or statement at end of input. Any help?


    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>


    int main ()
    {
    float x1, x2, y1, y2, distance;
    printf ("Enter value of x1: ");
    scanf ("%f", &x1);
    printf ("Enter value of x2: ");
    scanf ("%f", &x2);
    printf ("Enter value of y1: ");
    scanf ("%f", &y1);
    printf ("Enter value of y2: ");
    scanf ("%f", &y2);
    distance=sqrt(pow(y2-y1,2)+pow(x2-x1,2));
    printf ("Value of distance: %g\n", distance);
    putchar ('\n');
    system ("pause");
    return 0;

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Where is your closing brace at the end?
    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.

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    20
    Quote Originally Posted by Salem View Post
    Where is your closing brace at the end?
    THANK YOU! yes got the second part working perfectly, now just to get the first part underway

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by powerfox0 View Post
    THANK YOU! yes got the second part working perfectly, now just to get the first part underway
    Hi,
    With the first question, do you want to display the answer?

    1. declare a variable
    2. assign the arithmetic operation to that variable
    3. print variable

  9. #9
    Registered User
    Join Date
    Sep 2013
    Posts
    20
    here is what I got for the first part, but once again i am encountering errors, specifically 2 errors at line 12 saying error: stray '\226' in program and error: expected ';' before numerical constant

    any help? this is the code below:

    Code:
    int main()
    {
        int sum;
        sum = 7+3*5-2;
        printf("the sum of the first equation is %d", sum);
        int sum2;
        sum2 = 4+7/3;
        printf("the sum of the second equation is %d", sum2);
        int sum3;
        sum3 = 8%3*6;
        printf("the sum of the third equation is %d", sum3);
        int sum4;
        sum4 = (7+3)*5–2;
        printf("the sum of the fourth equation is %d", sum4);
        return EXIT_SUCCESS;
    }
    Last edited by powerfox0; 09-01-2013 at 11:55 PM.

  10. #10
    Registered User
    Join Date
    Sep 2013
    Posts
    20
    also how can i get each answer to display on a seperate line?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by powerfox0 View Post
    also how can i get each answer to display on a seperate line?
    You had it in yout first code...
    printf ("Value of distance: %g\n", distance);
    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.

  12. #12
    Registered User
    Join Date
    Sep 2013
    Posts
    20
    Quote Originally Posted by Salem View Post
    You had it in yout first code...
    printf ("Value of distance: %g\n", distance);
    understood, now can you help me with the errors? it seems to be only with the last sum.... what might it be?

  13. #13
    Registered User
    Join Date
    Sep 2013
    Posts
    20
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    int main()
    {
        int sum1;
        sum1 = 7+3*5-2;
        printf("the first equation is 7+3*5-2 \n");
        printf("the answer of the first equation is %d\n", sum1);
        int sum2;
        sum2 = 4+7/3;
        printf("the second equation is 4+7/3 \n");
        printf("the answer of the second equation is %d\n", sum2);
        int sum3;
        sum3 = 8%3*6;
        printf("the third equation is 8/3*6 \n");
        printf("the answer of the third equation is %d\n", sum3);
        int sum4;
        sum4 = (7+3)*5-2;
        printf("the fourth equation is (7+3)*5-2 \n");
        printf("the answer of the fourth equation is %d\n", sum4);
        return EXIT_SUCCESS;
    }
    thank you to everyone for their help.
    this is my final code for part 1, it seems to be working. can someone please verify that this is correct?
    Last edited by powerfox0; 09-02-2013 at 12:32 AM.

  14. #14
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by powerfox0 View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    int main()
    {
        int sum1;
        sum1 = 7+3*5-2;
        printf("the first equation is 7+3*5-2 \n");
        printf("the answer of the first equation is %d\n", sum1);
        int sum2;
        sum2 = 4+7/3;
        printf("the second equation is 4+7/3 \n");
        printf("the answer of the second equation is %d\n", sum2);
        int sum3;
        sum3 = 8%3*6;
        printf("the third equation is 8/3*6 \n");
        printf("the answer of the third equation is %d\n", sum3);
        int sum4;
        sum4 = (7+3)*5-2;
        printf("the fourth equation is (7+3)*5-2 \n");
        printf("the answer of the fourth equation is %d\n", sum4);
        return EXIT_SUCCESS;
    }
    thank you to everyone for their help.
    this is my final code for part 1, it seems to be working. can someone please verify that this is correct?
    Does it run for you? It does not run for me unless I declare the variables at the top (see code). I think its something to do with the different C standards. With C89, you have to declare variables at the top of the scope.

    Also, look at sum3. There is a mistake there. Can you find it? HINT: sum3 is the correct value but the sentence is incorrect

    Everything else seems to be good. Perhaps add paragraphs and comments to finish it off?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
        int sum1, sum2, sum3, sum4;
    
        sum1 = 7+3*5-2;
        printf("the first equation is 7+3*5-2 \n");
        printf("the answer of the first equation is %d\n", sum1);
       
    	//int sum2;
        sum2 = 4 + 7/3;
        printf("the second equation is 4+7/3 \n");
        printf("the answer of the second equation is %d\n", sum2);
        
    	//int sum3;
        sum3 = 8%3*6;
        printf("the third equation is 8/3*6 \n");
        printf("the answer of the third equation is %d\n", sum3);
       
    	//int sum4;
        sum4 = (7+3)*5-2;
        printf("the fourth equation is (7+3)*5-2 \n");
        printf("the answer of the fourth equation is %d\n", sum4);
    
    	getchar();getchar();
        
    	return 0;
    }

  15. #15
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by powerfox0 View Post
    this is my final code for part 1, it seems to be working. can someone please verify that this is correct?
    Judging from the assignment you're probably supposed to verify with a calculator or from pencil and paper. In C for example the following will give an unexpected result

    5 + 7/8

    the reason for the result is because of integer division and there is no rounding.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't use <math.h>
    By zakiuz in forum C Programming
    Replies: 6
    Last Post: 09-08-2011, 06:18 PM
  2. writing good code is like writing an artistic expression
    By renzokuken01 in forum C Programming
    Replies: 5
    Last Post: 02-03-2011, 08:48 PM
  3. bad math?
    By dead_captain in forum C++ Programming
    Replies: 4
    Last Post: 01-22-2008, 11:32 AM
  4. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  5. gcc and math.h
    By viaxd in forum C Programming
    Replies: 3
    Last Post: 10-08-2003, 06:18 AM

Tags for this Thread