Like Tree1Likes
  • 1 Post By ledow

Mathematical Functions

This is a discussion on Mathematical Functions within the C Programming forums, part of the General Programming Boards category; Ive been given an assignment and have finish setting up the information and now I cant get it to run. ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    6

    Smile Mathematical Functions

    Ive been given an assignment and have finish setting up the information and now I cant get it to run. Ive looked at the code several times and still cant find the problem. Could anyone find what the problem is so i can get this thing to work and hand it in before its too late?


    Code:
    #include<stdio.h>
    #include<math.h>
    
    
    
    
    
    
    int options();
    int time_to_swap();
    int get_hypote();
    int get_sine_form();
    int get_euclids (int argc, const char * argv[]);
    int mid_point();
    
    
    #define PI 3.14159265
    
    
    int main()
    {
       int four;   //fourth function
       int second;
       char choice;                      //option to end program
       int begin;
       int temp;
       int five;
       int third;
    
    
    
    
       begin = options();
    
    
       do{
    
    
       second = mid_point();   //finds midpoint
    
    
       third = time_to_swap();      //swaps
    
    
       four = get_hypote();    //find hypotenuse
    
    
       five = get_sine_form();    //calculates sine of a number
    
    
       temp = get_euclids (int argc, const char * argv[]);   //find the GCD
    
    
    
    
       printf("\n To end this program please enter the letter  'n'.\n");
       scanf("%c",&choice);
       getchar();
    
    
       }while(choice != 'n');
    }
    
    
    int options()      //prints out what the overall program will do
    {
       printf("Throughout the program it will perform the following: \n");
       printf("1. Linear Algebra.\n");
       printf("2. Pointers.\n");
       printf("3. Mathematical Functions\n");
       printf("4. Statistics.\n");
       printf("4. Finding GCD using  Euclid's algorithm.\n\n\n");
    
    
       return 0;
    }
    
    
    int mid_point()      //solves midpoint of the coordinates
    {
    
    
    	double midx,midy,x1,y1,x2,y2;
    	printf("Enter x1:\n");
    	scanf("%lf",x1);
    	printf("Enter x2:\n");
    	scanf("%d",x2);
    	printf("Enter y1:\n");
    	scanf("%lf",y1);
    	printf("Enter y1:\n");
    	scanf("%d",y1);
    
    
    	midx = (x1 + x2) / 2;
    	midy = (y1 + y2) / 2;
    
    
        printf("\n");
    	printf("%d,%d,%d,%d",x1,x2,y1,y2);
    	printf("%d,%d",midx,midy);;
    
    
         return 0;
    }
    
    
    
    
    /* Part 2-Pointers */ //where swapping three variables take place
    int time_to_swap()
    {
       int x, y, z,temp;
    
    
       printf("Enter the value of x and y and z\n");
       scanf("%d%d%d", &x, &y, &z);
    
    
       printf("Before Swapping\nx = %d\ny = %d\nz = %d\n",x,y,z);
    
    
       temp = x;
       x = z;
       z = y;
       y = temp;
    
    
       printf("After Swapping\nx = %d\ny = %d\nz = %d\n",x,y,z);
    
    
       return 0;
    }
    
    
    
    
    /* Part 3- Mathematical Functions */      //Calculates hypotenuse
    
    
    int get_hypote()
    {
        float side1, side2, ansa, hypote;
        int i = side1;
    
    
        printf("\nThis part of the program will calculate the hypotenuse of a right triangle(Part 1 of 3).\n\n");
        printf("Enter side one of the triangle:\n");
        scanf("%f",&side1);  //Asks user for side 1
    
    
        printf("Enter side two of the triangle:\n");
        scanf("%f",&side2);  //Asks user for side 2
    
    
        ansa = (side1 * side1 + side2 * side2);
        hypote = sqrt(ansa); //does the square root of ansa
    
    
    
    
        printf("The lenth of the hypotenuse is: %f \n", hypote);
    
    
    	/*  Done with part 1 of part 3  */
    
    
    	printf("Part 2 of 3, Uses the given sides and raises one number to the power of the other\n");
    
    
    
    
    	printf("%.2f\n", pow(i, side2));
    
    
    	/*  Done with part 2 of part 3  */
    	printf("Part 3 of 3, Uses any number given and places it into the sine formula and calculates answer.\n");
    }
    
    
    
    
    
    
    int get_sine_form()   //calculates the sine formula
      {
    
    
    
    
    
    
    		double degree, answer;
    		printf("Please enter a degree of your choice:\n");
    		scanf("%lf",degree);
    		answer = sin (degree*PI/180);
    		printf ("The sine of %lf degrees is %lf.\n", degree, answer );
    
    
    		return 0;
       }
    
    
    	/*  Done with part 3 of part 3  */
    
    
    
    
    /* Part 4- Statistics *****************/
    
    
    
    
    
    
    
    
    
    
    /*Extra Credit Part */
    int get_euclids (int argc, const char * argv[])
    {
    
    
    
    
         int a, A, b, B, c, d, m, n, q, r = 1, t, x;
    
    
    
    
         printf("This part of the program will help you find the Greatest Common Divisor of two numbers\n");   //calculates the GCD of two given numbers
         printf("m = ");
         scanf ("%d", &m);
         printf("n = ");
         scanf ("%d", &n);
         printf("\n\nWorking numbers\n\n");
         printf("A\t a\t B\t b\t c\t d\t q\t r\n");   //calculating the working numbers that would work for the GCF
         printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>n");
    
    
         A = b = 1;
         a = B = 0;
         c = m;
         d = n;
    
    
    
    
         while (r != 0)
              {
                   q = (c / d);
                   r = (c % d);
    
    
                   printf("%d\t %d\t %d\t %d\t %d\t %d\t %d\t %d\n",
                        A, a, B, b, c, d, q, r);
    
    
                   x = d;
                   c = d;
                   d = r;
                   t = A;
                   A = a;
                   a = (t - (q * a));
                   t = B;
                   B = b;
                   b = (t - (q * b));
              }
    
    
         printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
         printf("From the numbers entered, the GCD of %d and %d is %d\n\n", m, n, x);   //shows GCD
    
    
         return 0
    
    
    
    
    
    
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    6
    These are the instructions given: 2) Pointers

    One function should be passed pointers to 3 variables, and should swap all 3 numbers.
    3) Mathematical Functions


    Write a function that reads in two sides of a triangle and then calculates the hypotenuse of the triangle. Remember that a^2 + b^2 = c^2
    Use the hypot function from math.h
    Then, use those 2 sides to raise one number to the power of the other by using the c function pow to do that.
    Finally, use the sin function to find the sine value of a number, like at This site.

    >>>>>>>>>>>>>>>Write a program that reads n student's scores and calculates the number of students in each of the
    following 5 groups: 90 ~ 100, 80 ~ 89, 70 ~ 79, 60 ~ 69, and 0 ~ 59.

    It will also calculate the overall average, and print out the results<<<<<<<<<<<<Could anyone help me in finding out how to write this peice of code. I cant seem to do it I knw its using arrays but im not that familiar

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Erm... No.

    We don't do your homework. Certainly not if you just dump-and-run.

    But if you'd bothered to work with warnings enabled, you might have found out a lot for yourself:

    ..\test.c: In function 'main':
    ..\test.c:51: error: expected expression before 'int'
    ..\test.c:51: error: too few arguments to function 'get_euclids'
    ..\test.c: In function 'mid_point':
    ..\test.c:85: warning: format '%lf' expects type 'double *', but argument 2 has type 'double'
    ..\test.c:87: warning: format '%d' expects type 'int *', but argument 2 has type 'double'
    ..\test.c:89: warning: format '%lf' expects type 'double *', but argument 2 has type 'double'
    ..\test.c:91: warning: format '%d' expects type 'int *', but argument 2 has type 'double'
    ..\test.c:99: warning: format '%d' expects type 'int', but argument 2 has type 'double'
    ..\test.c:99: warning: format '%d' expects type 'int', but argument 3 has type 'double'
    ..\test.c:99: warning: format '%d' expects type 'int', but argument 4 has type 'double'
    ..\test.c:99: warning: format '%d' expects type 'int', but argument 5 has type 'double'
    ..\test.c:100: warning: format '%d' expects type 'int', but argument 2 has type 'double'
    ..\test.c:100: warning: format '%d' expects type 'int', but argument 3 has type 'double'
    ..\test.c: In function 'get_sine_form':
    ..\test.c:194: warning: format '%lf' expects type 'double *', but argument 2 has type 'double'
    ..\test.c: In function 'get_euclids':
    ..\test.c:284: error: expected ';' before '}' token
    Salem likes this.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    6
    Im so very sorry If it sounded like I was asking for you to do "all" the work. That is the exact opposite. All I wanted was just for someone to help me find the errors, for example-Point out the line its from- and ill fix it. Because From what ive seen it always says "before this line" and thats the problem I have, finding that specific line. If you could please help me out with what i just explained that would be highly appreciated.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    142
    According to ledow's errors and warnings, you have errors on lines 51 and 284 and warnings on lines 85, 87, 89, 91, 99, 100, and 194. Sometimes the actual error is before the line that gets the diagnostic, but in this case, with the exception of possibly the one on 284, the rest are probably on the line indicated.

    Does your compiler not include the line number with its errors and warnings?

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    i will try to explain the message

    ..\test.c:51: error: expected expression before 'int'

    thats your code
    Code:
       temp = get_euclids (int argc, const char * argv[]);   //find the GCD
    the compiler sees that you want to assign the return of get_euclids () to the variable temp
    This is correct up to "temp = get_euclids (" but then it unexpectedly sees "int" that is a type. A type cannot be an argument for a function call. It has to be an expression. Thats exactly what the compiler tells you.
    It is your job now to find out what the real cause is.
    In this case you have specified the types of the arguments by mistake
    it should have been
    Code:
       temp = get_euclids (argc, argv );   //find the GCD
    Kurt

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    6
    Thanks for really helping me out there. But for Zuk's explanation I had no idea how to do GCD in C. So I had someone help me on the internet, now. What you see under /*Extra Credit Part */ works on its own. I compiled and run it and it works, now problem is, will the changes affect anypart of it? Because I knw that some functions need integers to return, Just asking

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    6
    Thanks guys. I fixed everything and got the program to run for a few seconds. This is my code:

    Code:
    #include<stdio.h>
    #include<math.h>
    
    
    
    
    
    
    int options();
    int time_to_swap();
    int get_hypote();
    int get_sine_form();
    int get_euclid();
    int mid_point();
    
    
    #define PI 3.14159265
    
    
    int main()
    {
       int four;   //fourth function
       int second;
       char choice;                      //option to end program
       int begin;
       int temp;
       int five;
       int third;
    
    
    
    
       begin = options();
    
    
       do{
    
    
       second = mid_point();   //finds midpoint
    
    
       third = time_to_swap();      //swaps
    
    
       four = get_hypote();    //find hypotenuse
    
    
       five = get_sine_form();    //calculates sine of a number
    
    
       temp = get_euclid();   //find the GCD
    
    
    
    
       printf("\n To end this program please enter the letter  'n'.\n");
       scanf("%c",&choice);
       getchar();
    
    
       }while(choice != 'n');
    }
    
    
    int options()      //prints out what the overall program will do
    {
       printf("Throughout the program it will perform the following: \n");
       printf("1. Linear Algebra.\n");
       printf("2. Pointers.\n");
       printf("3. Mathematical Functions\n");
       printf("4. Statistics.\n");
       printf("4. Finding GCD using  Euclid's algorithm.\n\n\n");
    
    
       return 0;
    }
    
    
    int mid_point()      //solves midpoint of the coordinates
    {
    
    
    	double midx,midy,x1,y1,x2,y2;
    	printf("Enter x1:\n");
    	scanf("%lf",x1);
    	printf("Enter x2:\n");
    	scanf("%d",x2);
    	printf("Enter y1:\n");
    	scanf("%lf",y1);
    	printf("Enter y1:\n");
    	scanf("%d",y1);
    
    
    	midx = (x1 + x2) / 2;
    	midy = (y1 + y2) / 2;
    
    
        printf("\n");
    	printf("%d,%d,%d,%d",x1,x2,y1,y2);
    	printf("%d,%d",midx,midy);;
    
    
         return 0;
    }
    
    
    
    
    /* Part 2-Pointers */ //where swapping three variables take place
    int time_to_swap()
    {
       int x, y, z,temp;
    
    
       printf("Enter the value of x and y and z\n");
       scanf("%d%d%d", &x, &y, &z);
    
    
       printf("Before Swapping\nx = %d\ny = %d\nz = %d\n",x,y,z);
    
    
       temp = x;
       x = z;
       z = y;
       y = temp;
    
    
       printf("After Swapping\nx = %d\ny = %d\nz = %d\n",x,y,z);
    
    
       return 0;
    }
    
    
    
    
    /* Part 3- Mathematical Functions */      //Calculates hypotenuse
    
    
    int get_hypote()
    {
        float side1, side2, ansa, hypote;
        int i = side1;
    
    
        printf("\nThis part of the program will calculate the hypotenuse of a right triangle(Part 1 of 3).\n\n");
        printf("Enter side one of the triangle:\n");
        scanf("%f",&side1);  //Asks user for side 1
    
    
        printf("Enter side two of the triangle:\n");
        scanf("%f",&side2);  //Asks user for side 2
    
    
        ansa = (side1 * side1 + side2 * side2);
        hypote = sqrt(ansa); //does the square root of ansa
    
    
    
    
        printf("The lenth of the hypotenuse is: %f \n", hypote);
    
    
    	/*  Done with part 1 of part 3  */
    
    
    	printf("Part 2 of 3, Uses the given sides and raises one number to the power of the other\n");
    
    
    
    
    	printf("%.2f\n", pow(i, side2));
    
    
    	/*  Done with part 2 of part 3  */
    	printf("Part 3 of 3, Uses any number given and places it into the sine formula and calculates answer.\n");
    }
    
    
    
    
    
    
    int get_sine_form()   //calculates the sine formula
      {
    
    
    
    
    
    
    		double degree, answer;
    		printf("Please enter a degree of your choice:\n");
    		scanf("%lf",degree);
    		answer = sin (degree*PI/180);
    		printf ("The sine of %lf degrees is %lf.\n", degree, answer );
    
    
    		return 0;
       }
    
    
    	/*  Done with part 3 of part 3  */
    
    
    
    
    /* Part 4- Statistics *****************/
    
    
    
    
    
    
    
    
    
    
    /*Extra Credit Part */
    int get_euclid()
    {
    
    
    
    
         int a, A, b, B, c, d, Num1, Num2, q, r = 1, t, x;
    
    
    
    
         printf("Type in any two numbers and this function will calculate the GCD for you.\n");
         printf("Num1 = ");
         scanf ("%d", &Num1);
         printf("Num2 = ");
         scanf ("%d", &Num2);
         printf("These are the numbers that work\n\n");
         printf("A\t a\t B\t b\t c\t d\t q\t r\n");
         printf("------------------------------------------------------------------\n");
    
    
         A = b = 1;
         a = B = 0;
         c = Num1;
         d = Num2;
    
    
    
    
         while (r != 0)
              {
                   q = (c / d);
                   r = (c % d);
    
    
                   printf("%d\t %d\t %d\t %d\t %d\t %d\t %d\t %d\n",
                        A, a, B, b, c, d, q, r);
    
    
                   x = d;
                   c = d;
                   d = r;
                   t = A;
                   A = a;
                   a = (t - (q * a));
                   t = B;
                   B = b;
                   b = (t - (q * b));
              }
    
    
         printf("------------------------------------------------------------------\n");
         printf("\nThe Greatest Common Divisor of %d and %d is %d\n\n", Num1, Num2, x);
    
    
         return 0;
    }
    Here is a picture of what happened: Name:  problem.jpg
Views: 160
Size:  56.9 KB

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by skyrix View Post
    I fixed everything and got the program to run for a few seconds.
    No you have not.
    The scanf's in mid_point() and get_sine_form() are still wrong.

    Kurt

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    That's likely caused by the "warnings" already mentioned by ledow:

    ..\test.c:85: warning: format '%lf' expects type 'double *', but argument 2 has type 'double'
    ..\test.c:87: warning: format '%d' expects type 'int *', but argument 2 has type 'double'
    ..\test.c:89: warning: format '%lf' expects type 'double *', but argument 2 has type 'double'
    ..\test.c:91: warning: format '%d' expects type 'int *', but argument 2 has type 'double'
    ..\test.c:99: warning: format '%d' expects type 'int', but argument 2 has type 'double'
    ..\test.c:99: warning: format '%d' expects type 'int', but argument 3 has type 'double'
    ..\test.c:99: warning: format '%d' expects type 'int', but argument 4 has type 'double'
    ..\test.c:99: warning: format '%d' expects type 'int', but argument 5 has type 'double'
    ..\test.c:100: warning: format '%d' expects type 'int', but argument 2 has type 'double'
    ..\test.c:100: warning: format '%d' expects type 'int', but argument 3 has type 'double'
    You need to store data to a location (a pointer) when using scanf. As an example, this:
    Code:
    scanf("%lf",degree);
    ...needs to be:
    Code:
    scanf("%lf",&degree);



    Also, this:
    Code:
    scanf("%c",&choice);
    Should probably be this:
    Code:
    scanf(" %c",&choice);
    The difference is subtle but important.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    6
    So HK_MP5KPDW. That goes for all scanf's?. And I just wanted to say to all of you who helped me. I greatly Appreciate, I cant thank you guys enough. You are indeed the best. Ill make the changes and reply to you guys if anything else is wrong

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    What you read into with scanf must be an address(pointer). The scanf function must know where to store the data it will be reading/converting. Whether or not that means you use the address-of operator (&) to obtain that address depends on the variable you are dealing with. Variables that are already defined as pointers or that can be interpreted as an address (an array for example) may not require a &. Most other variables however would require the use of & to get the address of said variable:
    Code:
    // Requires & because "val" is not a pointer
    int val;
    printf("Enter an integer value: ");
    scanf("%d",&val);
    
    
    // Does not require & because "valptr" is a pointer
    int val;
    int * valptr = &val;  // valptr is a pointer that points to "val"
    printf("Enter an integer value: ");
    scanf("%d",valptr);  // user entered value will end up in "val" (pointed to by valptr)
    
    
    // Does not require & because array variable "name" decays
    // to a pointer to the first element of the array in question
    char name[20];
    printf("Enter your first name: ");
    scanf("%s",name);
    
    
    // Does require & because valarray[loop] represents an individual
    // value and not an address
    int valarray[10];
    int loop;
    printf("Enter 10 integers: ");
    for( loop = 0; loop < 10; ++loop )
      scanf("%d",&valarray[loop]);
    
    
    
    // Does not require & because valarray+loop represents
    // an address (plus an offset)
    int valarray[10];
    int loop;
    printf("Enter 10 integers: ");
    for( loop = 0; loop < 10; ++loop )
      scanf("%d",valarray+loop);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mathematical Error
    By toonlover in forum C++ Programming
    Replies: 6
    Last Post: 11-25-2006, 09:21 PM
  2. Mathematical problem
    By Warlax in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2006, 02:38 AM
  3. Need mathematical help
    By Magos in forum C++ Programming
    Replies: 10
    Last Post: 03-24-2002, 09:42 AM
  4. Mathematical Question
    By rmullen3 in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2001, 08:44 PM
  5. How do I use mathematical operators?
    By Cockney in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 08:56 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21