2 Replies - 2151 Views - Last Post: 01 October 2012 - 10:47 AM Rate Topic: -----

#1 bjm62692  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-September 12

Invalid operands Errors/Expected ')' before ';' token

Posted 01 October 2012 - 06:48 AM

This is my code.
/*
Program Name: Pi
Author: Brittany McCord
Class: Introduction to Programming With C & MATLAB
Section: 1
Program Intent: Express Pi using two infinite series equations
Input Data: The number of terms to approximate by
Output Data: two separate approximations of pi
*/

/*---------------
Include Section
----------------*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>

/*--------------------------- main function --------------------------------
Purpose: quantify values of pi using 2 equations and a specified number of terms
Returns: Approximate values of pi
----------------------------------------------------------------------------*/
int 
main( ) {
    long double approx1 (int terms);
    long double approx2 (int terms);
    int terms;
    long double pi1;
    long double pi2;
    terms = 1;
    while (terms != 0) {
        printf("Enter the Number of Terms to Approximate pi, or 0 to Terminate: \n");
        scanf("%n", &terms);
        if (terms < 0) {
            printf("Wrong Input. Please Try Again. \n");
            }
        else if (terms == 1) {
            approx1 (terms);
            printf("The First Approximation of pi to 1 term is: %.8Lf \n" &pi1);
            approx2 (terms);
            printf("The Second Approximation of pi to 1 term is: %.8Lf \n" &pi2);
            }
        if (terms > 1) {
            approx1 (terms);
            printf("The First Approximation of pi to %n terms is: %.8Lf \n" &terms; &pi1);
            approx2 (terms);
            printf("The Second Approximation of pi to %n terms is: %.8Lf \n" &terms; &pi2);
            }
       }
    printf("*****PROGRAM TERMINATED***** \n");
    return (EXIT_SUCCESS);
}

/*--------------------------- approx1 funtion --------------------------------
Purpose: Approximates value of pi using the simplistic equation
Returns: Approximate value of pi
----------------------------------------------------------------------------*/
long double approx1 (int terms) {
    long double pi1;
    long double num;
    long double denom;
    int i;
    num = 4;
    denom = 1;
    for (i = 1; i <= terms; i++) {
        if (i % 2 != 0 ) {
            pi1 += num / denom;
            }
        else {
            pi1 -= num / denom;
            }
        denom += 2.0;
        }
    return pi1;
        }

/*--------------------------- approx2 funtion --------------------------------
Purpose: Approximates value of pi using the square root equation
Returns: Approximate value of pi
----------------------------------------------------------------------------*/
long double approx2 (int terms) {
    long double pi2;
    long double num;
    long double denom;
    int i;
    num = 12;
    denom = 1;
    for (i = 1; i <= terms; i++) {
        if (i % 2 != 0 ) {
            pi2 += num / (denom * denom);
            }
        else {
            pi2 -= num / (denom * denom);
            }
        denom += 1.0;
        }
    pi2 = sqrt(pi2);
    return pi2;
}




These are the errors.
pi.c: In function 'main':
pi.c:39:75: error: invalid operands to binary & (have 'char *' and 'long double')
pi.c:41:76: error: invalid operands to binary & (have 'char *' and 'long double')
pi.c:45:77: error: invalid operands to binary & (have 'char *' and 'int')
pi.c:45:83: error: expected ')' before ';' token
pi.c:47:78: error: invalid operands to binary & (have 'char *' and 'int')
pi.c:47:84: error: expected ')' before ';' token
pi.c: In function 'approx1':
pi.c:65:17: error: expected ')' before ';' token
pi.c: In function 'approx2':
pi.c:88:17: error: expected ')' before ';' token





I do not completely understand what they are refering to. I have been fiddling around with it trying to fix it, but nothing seems to be working. Please help me understand the problem. Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Invalid operands Errors/Expected ')' before ';' token

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,315
  • Joined: 25-December 09

Re: Invalid operands Errors/Expected ')' before ';' token

Posted 01 October 2012 - 07:08 AM

It looks like you need to study up on the documentation of printf() in the following code:
           printf("The First Approximation of pi to 1 term is: %.8Lf \n" &pi1);

You are passing the address of pi1 as a parameter, you only need to pass the variable, you don't need an ampersand. But you do need a comma between the first parameter and the second.
           printf("The First Approximation of pi to 1 term is: %.8Lf \n", pi1);


In the following code you have the same problem as above plus you have substituted a semicolon for a comma.
            printf("The First Approximation of pi to %n terms is: %.8Lf \n" &terms; &pi1);

It should be:
            printf("The First Approximation of pi to %n terms is: %.8Lf \n", terms, pi1);



Jim

This post has been edited by jimblumberg: 01 October 2012 - 08:13 AM

Was This Post Helpful? 1
  • +
  • -

#3 bjm62692  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-September 12

Re: Invalid operands Errors/Expected ')' before ';' token

Posted 01 October 2012 - 10:47 AM

View Postjimblumberg, on 01 October 2012 - 07:08 AM, said:

It looks like you need to study up on the documentation of printf() in the following code:
           printf("The First Approximation of pi to 1 term is: %.8Lf \n" &pi1);

You are passing the address of pi1 as a parameter, you only need to pass the variable, you don't need an ampersand. But you do need a comma between the first parameter and the second.
           printf("The First Approximation of pi to 1 term is: %.8Lf \n", pi1);


In the following code you have the same problem as above plus you have substituted a semicolon for a comma.
            printf("The First Approximation of pi to %n terms is: %.8Lf \n" &terms; &pi1);

It should be:
            printf("The First Approximation of pi to %n terms is: %.8Lf \n", terms, pi1);



Jim



This is a huge help. Thank you so much. I am completely new to programming, so I tend to make the more obvious errors.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1