Linear equation code problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • company1484
    New Member
    • Oct 2007
    • 5

    #1

    Linear equation code problem

    This program calculates the solution (x,y) of a system of linear equations in 2 variables. This is my third C programming assignment so be gentle. problem: 1)on my int menu() function definition, how would I include the
    scanf( "%d", &choice ); in the function definition for it to run right. It runs right without it, but I need it in there.



    [CODE=c]#include <stdio.h>

    void displayMessage( );

    int hasSolution(dou ble a, double b, double d, double e);

    double getX(double a, double b, double c, double d, double e, double f);

    double getY(double a, double b, double c, double d, double e, double f);

    void displaySolution s(double x, double y);


    int menu();


    int main() {
    double a, b, c, d, e, f;
    double x, y;
    int choice;
    displayMessage( );

    do
    {
    do
    {
    menu();
    scanf( "%d", &choice );
    } while (choice < 1 || choice > 3);
    switch (choice)

    {

    case 1:
    printf( "Enter a, b, and c: " );
    scanf( "%lf", &a );
    scanf( "%lf", &b );
    scanf( "%lf", &c );
    printf( "Enter d, e, and f: " );
    scanf( "%lf", &d );
    scanf( "%lf", &e );
    scanf( "%lf", &f );
    break;
    case 2:
    if (hasSolution(a, b, d, e))
    {
    x = getX(a, b, c, d, e, f);
    y = getY(a, b, c, d, e, f);
    displaySolution s(x,y);
    }
    else
    {
    printf( "The system has no solution\n" );

    }
    break;
    case 3:
    printf( "Thanks for using my program\n" );
    }
    } while (choice != 3);


    return 0;
    }



    void displayMessage( ){
    printf( "This program calculates solutions to systems of linear equations of the form\n\n");
    printf( " aX + bY = c\n" );
    printf( " dX + eY = f\n\n" );
    return;
    }

    int hasSolution(dou ble a, double b, double d, double e){
    int s;
    s=(a*e - d*b != 0);
    return s;
    }


    double getX(double a, double b, double c, double d, double e, double f){
    double x;
    x=(c * e - f * b) / (a * e - d * b);
    return x;
    }


    double getY(double a, double b, double c, double d, double e, double f){
    double y;
    y=(a * f - d * c) / (a * e - d * b);
    return y;
    }


    void displaySolution s(double x, double y)
    {
    printf ("The solution is (%lf, %lf)\n", x, y );
    return;
    }

    int menu()
    {
    printf( "\nEnter 1 to enter a new system\n" );
    printf( " 2 to find the solution of the system\n" );
    printf( " 3 to exit\n" );
    printf( "Choice: " );
    return 0;
    }[/CODE]
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    You would put that into your menu. I'm not sure what you are confused on - your code looks right, you seem to know what you need, you just have to move the code in there, and then make sure it returns properly. Could you say what you're stuck on?

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Basically, you would make your menu function return the scanf'd variable. Put int choice inside the menu function, set it to the scanf statement, and return choice. Back in main(), keep int choice, but instead of scanf'ing it, initialize it with the return value of menu().

      Comment

      Working...