Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Horribly confused

Options
  • 23-01-2006 2:31pm
    #1
    Closed Accounts Posts: 1,299 ✭✭✭


    Ok I need some pointers on the det2 and det3 function,
    I am having trouble defining them berfore and after my code,
    here it is at the moment, but as you can see it just doesn't seem to work, I am doing this in 1st year engineering in college and don't want the answers necessarily just an idea of how to get it to work;

    #include <iostream>
    using namespace std;
    double det2 (doublex);
    double det3 (double x);

    int main()
    {
    double a11,a12,a13,a22,a23,a31,a32,a33,a21,det3,yes, det2;

    cout << " Is it a 2x2 matrix (yes/no)? :" ;
    if (yes){
    cout << "Enter a value for a11,a12,a21,a22 :";
    cin >> a11,a12,a21,a22 ;
    cout << " The determinant of that 2x2 matris is : " << det2 << endl;
    }
    else
    cout << "You have chosen a 3x3 matrix " <<endl;
    cout << "Enter a value for a11,a12,a13,a21,a22,a23,a31,a32,a33 :";
    cin >> a11,a12,a13,a21,a22,a23,a31,a32,a33;
    cout << " The determinant of that 3x3 matris is : " << det3 <<endl;







    system("PAUSE");
    return 0;
    }
    double det2(double x)

    double ,a11,a12,a22,a21;
    return (a11*a22)-(a21*a12);

    }

    double det3 (double x){
    double y,det2,a11,a12,a13,a21,a22,a23,a31,a32,a33;
    y=(a11*(a22,a23,a32,a33)) - (a12*det2(a21,a23,a31,a33)) + (a13*det2(a21,a22,a31,a32))
    return y;
    }


Comments

  • Registered Users Posts: 23 RazorEdge


    Opening braces will always be matched by closing braces. You are missing one.

    You need to re-read your notes on how to pass parameters to a function. You are doing it incorrectly.

    While you are at it , read how to call functions as well.:)

    There are a few misplaced commas hanging around.

    There is an error in your function prototypes as well.

    "Yes" variable needs to be initialised in some way.


  • Registered Users Posts: 20,894 ✭✭✭✭Stark


    You don't need to define the det2 and det3 functions within your main method. Similarly in det3 function definition, you don't need to define det2 in order to use it.

    In your main method, det2 and det3 should always use parameters. "cout << det2;" won't work. You have to use det2(some value).


  • Closed Accounts Posts: 1,299 ✭✭✭Sandals


    ok this is all going over my head, Razor edge whats a function prototype?
    #include <iostream>
    using namespace std;
    double det2 (a11,a12,a22,a21);
    double det3 (double x);

    int main()
    {
    double a11,a12,a13,a22,a23,a31,a32,a33,a21, ;
    int yes;

    cout << " Is it a 2x2 matrix yes/no? :" ;
    if (yes){
    cout << "Enter a value for a11,a12,a21,a22 :";
    cin >> a11,a12,a21,a22 ;
    cout << " The determinant of that 2x2 matris is : " << det2(a11,a22,a21,a12) << endl;
    }
    else{
    cout << "You have chosen a 3x3 matrix " <<endl;
    cout << "Enter a value for a11,a12,a13,a21,a22,a23,a31,a32,a33 :";
    cin >> a11,a12,a13,a21,a22,a23,a31,a32,a33;
    cout << " The determinant of that 3x3 matris is : " << det3(a11,a12,a13,a21,a22,a23,a31,a32,a33) <<endl;
    }





    }
    system("PAUSE");
    return 0;
    }
    double det2(a11,a12,a22,a21)

    double ,a11,a12,a22,a21;
    return (a11*a22)-(a21*a12);

    }

    double det3 (double a11,a12,a13,a21,a22,a23,a31,a32,a33){
    double y,det2(a11,a12,a22,a21),a11,a12,a13,a21,a22,a23,a31,a32,a33;
    y=(a11*(a22,a23,a32,a33)) - (a12*det2(a21,a23,a31,a33)) + (a13*det2(a21,a22,a31,a32))
    return y;
    }

    did that fix anything am i doing the det 2 right at the start should it be

    det2 (double x)
    or
    det2 (a11,a12,a21,a22)
    or
    det2 (a11*a22-a12*a21)

    ?


    thanks a million for trying to help me.


  • Registered Users Posts: 20,894 ✭✭✭✭Stark


    The function prototype is where you specify the header of the function but not the body.
    So double det3(double x); is a function prototype.

    You always specify the type of the parameter when defining the function. So

    double det3(double x) is value, double det3(x) is invalid.

    When passing the value of a variable to a function on the other hand, you don't specify the type as that would be re-declaring the variable.

    eg:
    #include <iostream>
    using namespace std;
    
    int multiply(int a, int b);
    
    int main()
    {
    int a,b;
    
    cout << "Please specify 2 numbers";
    cin >> a,b;
    cout << "Their product is " << multiply(a,b) << endl;
    
    return 0;
    }
    
    int multiply(int a, int b)
    {
    return a*b;
    }
    

    PS: notice my sample program is simpler than yours. Start with very simple code until you have all the errors ironed out, then start adding more advanced code. The code you posted is very difficult to debug with all the little mistakes.

    Note you can also define your function above your main method meaning you can do your function prototyping and definition all in one go:
    #include <iostream>
    using namespace std;
    
    int multiply(int a, int b)
    {
    return a*b;
    }
    
    int main()
    {
    int a,b;
    
    cout << "Please specify 2 numbers";
    cin >> a,b;
    cout << "Their product is " << multiply(a,b) << endl;
    
    return 0;
    }
    
    


Advertisement