Thread: return value for protype

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    9

    return value for protype

    i was writing a programm to find the square root of a number.first i thought to find how many iteration it goes.I got stuck int he middle please help me out.Here is my code.Where did i go wrong
    Code:
    #include <iostream>
    using namespace std;
    double epsilon( double , double, double );
    int main (void)
    {
        //int n,i;
        int count;
         double a[count],subject,epsilon;//,newepsilon;
        cout<<"Please input a postive real number:";
        cin>>subject;
        cout<<"Please input a postive real value for the relative accuracy epsilon:";
        cin>>epsilon;
        cout<<"count="<<count(subject,a[],epsilon);
    
        double count( double subject,double x[count], double epsilon);
            {   x[0]=subject/2.0;
                int n=0;
                int count=0;
                x[n+1]=((x[n] +(subject/x[n]))/2);
                newepsilon=((x[n+1]-x[n])/x[n+1]);
                while(epsilon > newspilon)
                {
                count=count+1;
                x[n+1]=((x[n] +(subject/x[n]))/2);
                newepsilon=((x[n+1]-x[n])/x[n+1]);
                }
                return count;}
    
    
        }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    At first, a typical structure of a program is like this
    Code:
    #include <iostream>
    
    using namespace std;
    
    bool myFunc(int a);
    
    int main()
    {
      
          int a = 0;
          bool answerFromFunction;
          
          answerFromFunction = myFunc(a);
    
          cout << answerFromFunction <<endl;
     
          return 0;
    }
    
    bool myFunc(int a)
    {
          if(a == 9) 
             return true;
          return false;
    }
    You have the definition of the function inside the main, which is wrong.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    i corrected it,but still having troubles.
    Code:
    #include <iostream>
    using namespace std;
    double epsilon( double , double, double );
    int main (void)
    {
        //int n,i;
        int ncount,count;
         double a[ncount],subject,epsilon;//,newepsilon;
        cout<<"Please input a postive real number:";
        cin>>subject;
        cout<<"Please input a postive real value for the relative accuracy epsilon:";
        cin>>epsilon;
        ncount=count(subject,a[ncount],epsilon);
        cout<<"count="<<ncount; 
        }
        double count( double subject,double x[count], double epsilon);
            {   x[0]=subject/2.0;
                int n=0;
                int count=0;
                x[n+1]=((x[n] +(subject/x[n]))/2);
                newepsilon=((x[n+1]-x[n])/x[n+1]);
                while(epsilon > newspilon)
                {
                count=count+1;
                x[n+1]=((x[n] +(subject/x[n]))/2);
                newepsilon=((x[n+1]-x[n])/x[n+1]);
                }
                return count;}
    
    
        }

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I think you have an extra closing bracket at the end of your code.

    Maybe you can improve the readability of your code by applying some rules of indentation. Actually is a must.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    yes i will try to do it.i have following errors
    recently i edited my code as
    Code:
    #include <iostream>
    using namespace std;
    double epsilon( double , double, double );
    int main (void)
    {
        //int n,i;
        int ncount,count;
         double a[ncount],subject,epsilon;//,newepsilon;
        cout<<"Please input a postive real number:";
        cin>>subject;
        cout<<"Please input a postive real value for the relative accuracy epsilon:";
        cin>>epsilon;
       ncount= count (subject, a, epsilon);
        cout<<"count="<<ncount;
        }
        double count ( double subject,double x[], double epsilon);
            {   x[0]=subject/2.0;
                int n=0;
                int count=0;
                x[n+1]=((x[n] +(subject/x[n]))/2);
                newepsilon=((x[n+1]-x[n])/x[n+1]);
                while(epsilon > newspilon)
                {
                count=count+1;
                x[n+1]=((x[n] +(subject/x[n]))/2);
                newepsilon=((x[n+1]-x[n])/x[n+1]);
                }
                return count;}
    but still having these errors
    Compiling: C:\Users\Documents\1.cpp
    C:\Users\Documents\1.cpp: In function 'int main()':
    C:\Users\Documents\1.cpp:13: error: 'count' cannot be used as a function
    C:\Users\Documents\1.cpp: At global scope:
    C:\Users\Documents\1.cpp:17: error: expected unqualified-id before '{' token
    Process terminated with status 1 (0 minutes, 1 seconds)
    2 errors, 0 warnings
    Last edited by liveplay23; 11-16-2012 at 11:32 AM.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The name of your function differs between the prototype and declaration. And you have a semicolon after the declaration that should not be there.

    Code:
    double epsilon( double , double, double );
    
    double count( double subject,double x[count], double epsilon);  // <--- no semicolon
    And read the indentation link that std10093 posted.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    thanks alot matticus.could u please look at this error
    Code:
    #include <iostream>
    using namespace std;
    double count( double , double, double );
    int main (void)
    {
        //int n,i;
        int ncount,count;
         double a[ncount],subject,epsilon;//,newepsilon;
        cout<<"Please input a postive real number:";
        cin>>subject;
        cout<<"Please input a postive real value for the relative accuracy epsilon:";
        cin>>epsilon;
       ncount= count (subject, a, epsilon);
        cout<<"count="<<ncount;
        }
        double count ( double subject,double x[], double epsilon)
            {   x[0]=subject/2.0;
                int n=0;
                int count=0;
                double newepsilon;
                x[n+1]=((x[n] +(subject/x[n]))/2);
                newepsilon=((x[n+1]-x[n])/x[n+1]);
                while(epsilon > newepsilon)
                {
                count=count+1;
                x[n+1]=((x[n] +(subject/x[n]))/2);
                newepsilon=((x[n+1]-x[n])/x[n+1]);
                }
                return count;}
    Compiling: C:\Users\Documents\1.cpp
    C:\Users\Documents\1.cpp: In function 'int main()':
    C:\Users\Documents\1.cpp:13: error: 'count' cannot be used as a function
    Process terminated with status 1 (0 minutes, 0 seconds)
    1 errors, 0 warnings

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I would suggest you to give different name to variable count from the name of the function.


    Quote Originally Posted by Matticus View Post
    And read the indentation link that std10093 posted.


    EDIT :
    But wait, in main count is not really a variable...So replace the line
    Code:
    int ncount,count;
    with this
    Code:
    int ncount;
    Also you are not giving a value for the length of the array.

    Also the second argument in the prototype should be double* , because you pass an array as argument....

    Then cope with run time erros
    Last edited by std10093; 11-16-2012 at 11:54 AM.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    plz check out the edit.i hope i did the indentation for better reading.ya the whole question was not to define the length of the array at first .user inputs the value for the square root and epsilon.We have to find how many times it will iterate until newepsilon value is less than epsilon value.

    Code:
    #include <iostream>
    using namespace std;
    int zahl( double , double, double );
    int main (void)
    {
    
    int ncount;
    double a[ncount];
    double subject,epsilon;//,newepsilon;
      cout<<"Please input a postive real number:";
      cin>>subject;
      cout<<"Please input a postive real value for the relative accuracy epsilon:";
      cin>>epsilon;
            ncount = zahl (subject, a, epsilon);
      cout<<"count="<<ncount;
    }
    int zahl ( double subject,double x[], double epsilon)
    {
        x[0]=subject/2.0;
        int n=0;
        int count=0;
        double newepsilon;
        x[n+1]=((x[n] +(subject/x[n]))/2);
        newepsilon=((x[n+1]-x[n])/x[n+1]);
            while(epsilon > newepsilon)
            {
                count=count+1;
                x[n+1]=((x[n] +(subject/x[n]))/2);
                newepsilon=((x[n+1]-x[n])/x[n+1]);
            }
    return count;
    }

    Compiling: C:\Users\Documents\1.cpp
    C:\Users\Documents\1.cpp: In function 'int main()':
    C:\Users\Documents\1.cpp:14: error: cannot convert 'double*' to 'double' for argument '2' to 'int zahl(double, double, double)'
    Process terminated with status 1 (0 minutes, 1 seconds)
    1 errors, 0 warnings

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Indentation improved

    Maybe you did not see my edit before.It said -> Also the second argument in the prototype should be double* , because you pass an array as argument....

    But if ncout is unknown to your program , how is your code suppose to set the size of the array?
    First you have to give value to the ncount and then create the array

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    do u mean double * new epsilon; is that do with pointers.then can i do newepsilon calculations.i have studied earlier that pointer holds the address of the pointing variable.so will it hold the calculated double value?
    This way my idea to tackle it.
    * create double a[ncount]; (which i dont know the value.)
    **do the calculations and find out how many times it iterate.
    ***then put the value to the array.(then i though that could my create desired length of array).
    correct me..plz

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Before main you have
    Code:
    int zahl( double , double, double );
    After main you have
    Code:
    int zahl ( double subject,double x[], double epsilon)
    Does the seem same to you?The second argument needs to be an array...So or have as second argument double* x or double x[]

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    guys thanks.but it s crashing now.either way x[] or *x
    Code:
    #include <iostream>
    using namespace std;
    int zahl( double , double x[], double );
    int main (void)
    {
    
    int ncount;
    double a[ncount];
    double subject,epsilon;//,newepsilon;
      cout<<"Please input a postive real number:";
      cin>>subject;
      cout<<"Please input a postive real value for the relative accuracy epsilon:";
      cin>>epsilon;
            ncount = zahl (subject, a, epsilon);
      cout<<"count="<<ncount;
    }
    int zahl ( double subject,double x[], double epsilon)
    {
        x[0]=subject/2.0;
        int n=0;
        int count=0;
        double newepsilon;
        x[n+1]=((x[n] +(subject/x[n]))/2);
        newepsilon=((x[n+1]-x[n])/x[n+1]);
            while(epsilon > newepsilon)
            {
                count=count+1;
                x[n+1]=((x[n] +(subject/x[n]))/2);
                newepsilon=((x[n+1]-x[n])/x[n+1]);
            }
    return count;
    }

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I said to you that ncount must take a value before playing the role of the size of the array!

  15. #15
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    thanks for ur patience std10093&matticus.atleast its started nw.I will post the complete solution in some time.Thank you guys u guys are awesome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reson behind following strcpy protype
    By sanddune008 in forum C Programming
    Replies: 4
    Last Post: 03-13-2012, 05:39 AM
  2. Replies: 1
    Last Post: 07-04-2007, 12:20 AM
  3. Replies: 12
    Last Post: 04-07-2007, 11:11 AM
  4. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  5. Replies: 4
    Last Post: 07-15-2005, 04:10 PM

Tags for this Thread