Defining functions outside main - problem

This is a discussion on Defining functions outside main - problem within the C++ Programming forums, part of the General Programming Boards category; I am trying to define a function outside main, and the function will be used inside a loop(think thats where ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    9

    Defining functions outside main - problem

    I am trying to define a function outside main, and the function will be used inside a loop(think thats where my probelms lie), here are the relevant bits of code. would be grateful of any help you could give me some advice/help (NB: i have attached the full code):

    Code:
    float f1(float Co1[10], float x3[100], int i);
    
    int main ()
    
    (
    
    .....
    
    float Co[10]; float x[100]; int i;
    
    
    int result;
    
    for(i=0;i<15;i++)
    {
    result= f1(Co, x, i);
    
    coutf(x)= "<<result<<endl;
    
    if(fabs(f1(Co,x,i))<0.00000001)
    {
    cout<<"The value of f(x) at the root is: "<<result<<endl;
    cout<<endl;
    return(0);
    }
    }
    }
    
    
    float f1(float Co[10], float x[100], int i)
    {
    float result;
    result=((Co1[0]*x[i]*x[i])+(Co1[1]*x[i])+Co1[2]);
    return(result);
    }
    Attached Files Attached Files
    Last edited by helpmeout; 03-21-2006 at 05:48 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    NB: i have attached the full code
    Maybe that code will erase every file on my computer.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    i didnt know you could do that with a cpp! erm, i have now written the vital bits of code in. sorry

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Umm, so what's the problem? It looks like you fixed the syntax error.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    it doesnt build, come up with a few errors, argument from int to float, assignment from int to float

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    redefintions of i and undeclared variables, sort those errors out.
    When no one helps you out. Call google();

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    redefinitions of i and undeclared variables......as in ive used i twice? dont really understand what your saying.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    yes thats what it means. i run your prog on my machine and this is what it comes up with.
    ompiling...
    test3232.cpp
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(7) : error C2143: syntax error : missing ')' before ';'
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(7) : error C2091: function returns function
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(12) : error C2143: syntax error : missing ';' before 'for'
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(12) : error C2143: syntax error : missing ')' before ';'
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(12) : error C2143: syntax error : missing ';' before '<'
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(12) : error C2501: 'i' : missing storage-class or type specifiers
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(12) : error C2086: 'i' : redefinition
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(12) : error C2143: syntax error : missing ';' before '<'
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(12) : error C2143: syntax error : missing ';' before '++'
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(12) : error C2501: 'i' : missing storage-class or type specifiers
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(12) : error C2086: 'i' : redefinition
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(12) : error C2143: syntax error : missing ';' before '++'
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(12) : error C2059: syntax error : ')'
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(13) : error C2143: syntax error : missing ';' before '{'
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(13) : error C2447: missing function header (old-style formal list?)
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(16) : error C2001: newline in constant
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(31) : error C2065: 'Co1' : undeclared identifier
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(31) : error C2109: subscript requires array or pointer type
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(31) : error C2109: subscript requires array or pointer type
    D:\Program Files\Microsoft Visual Studio\MyProjects\test3232\test3232.cpp(31) : error C2109: subscript requires array or pointer type
    Error executing cl.exe.

    test3232.obj - 20 error(s), 0 warning(s)
    When no one helps you out. Call google();

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    oh dear, i still dont understand what i have to change though! argh

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    When I downloaded the attachment, I only received one error and one warning. The error was on the line:
    Code:
    if(fabs(f1(i))<E)
    //looks like it should be
    if(fabs(f1(Co, x, i))<E)
    And the warning is:
    "warning C4244: '=' : conversion from 'float' to 'int', possible loss of data"
    on this line:
    Code:
    result= f1(Co, x, i);
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    yeah, but what does that mean? does no-one here actually know what this is? or does everyone just copy it without understanding it???

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    but what does that mean
    f1() takes a float as an argument, but you are trying to pass an int. It is possible to cast to float, but then f1() takes 3 arguments, and you only provide one, so the problem runs deeper than that.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    ok, im starting to understand a bit more, my programme takes f1 to be float but wants to make it an int? how do i fix the problem? i've tried everything to try and get it to work and nothing is working!

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Before you start thinking about casting, examine:
    Code:
    float f1(float Co1[10], float x3[100], int i);
    and its use in:
    Code:
    f1(i)
    Can you see what's wrong?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    no, i was told by my supervisor that i was meant to write f1 as a function of i when it was in the loop!
    can you at least tell me which bit is wrong, the float f1(....) or the f1(...) bit?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program skips functions in main
    By En-Motion in forum C++ Programming
    Replies: 5
    Last Post: 02-18-2009, 08:35 PM
  2. Problem with functions
    By WTFSEAN in forum C Programming
    Replies: 5
    Last Post: 05-10-2008, 02:01 AM
  3. Problem: Functions
    By Dmitri in forum C Programming
    Replies: 21
    Last Post: 11-06-2005, 09:40 AM
  4. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM
  5. Big Problem With Passing an Matrix to Main Function
    By Maragato in forum C Programming
    Replies: 4
    Last Post: 06-14-2004, 11:06 PM

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