473,385 Members | 1,875 Online

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.

Expand|Select|Wrap|Line Numbers
1. #include <stdio.h>
2.
3. void displayMessage();
4.
5. int hasSolution(double a, double b, double d, double e);
6.
7. double getX(double a, double b, double c, double d, double e, double f);
8.
9. double getY(double a, double b, double c, double d, double e, double f);
10.
11. void displaySolutions(double x, double y);
12.
13.
15.
16.
17. int main() {
18.     double a, b, c, d, e, f;
19.     double x, y;
20.     int choice;
21.     displayMessage();
22.
23. do
24.     {
25.         do
26.         {
28.          scanf( "%d", &choice );
29.         } while (choice < 1 || choice > 3);
30.         switch (choice)
31.
32.         {
33.
34.             case 1:
35.                 printf(  "Enter a, b, and c: " );
36.                 scanf( "%lf", &a );
37.                 scanf( "%lf", &b );
38.                 scanf( "%lf", &c );
39.                 printf(  "Enter d, e, and f: " );
40.                 scanf( "%lf", &d );
41.                 scanf( "%lf", &e );
42.                 scanf( "%lf", &f );
43.                 break;
44.            case 2:
45.                 if (hasSolution(a, b, d, e))
46.                 {
47.                     x = getX(a, b, c, d, e, f);
48.                     y = getY(a, b, c, d, e, f);
49.                     displaySolutions(x,y);
50.                 }
51.                     else
52.                     {
53.                         printf( "The system has no solution\n" );
54.
55.                     }
56.                break;
57.            case 3:
58.                printf(  "Thanks for using my program\n" );
59.         }
60.     } while (choice != 3);
61.
62.
63.     return 0;
64. }
65.
66.
67.
68. void displayMessage(){
69.        printf( "This program calculates solutions to systems of linear equations of the form\n\n");
70.        printf( "     aX + bY = c\n" );
71.        printf( "     dX + eY = f\n\n" );
72.        return;
73. }
74.
75. int hasSolution(double a, double b, double d, double e){
76.         int s;
77.         s=(a*e - d*b != 0);
78.         return s;
79. }
80.
81.
82. double getX(double a, double b, double c, double d, double e, double f){
83.         double x;
84.         x=(c * e - f * b) / (a * e - d * b);
85.         return x;
86. }
87.
88.
89. double getY(double a, double b, double c, double d, double e, double f){
90.         double  y;
91.         y=(a * f - d * c) / (a * e - d * b);
92.         return y;
93. }
94.
95.
96. void displaySolutions(double x, double y)
97. {
98. printf ("The solution is (%lf, %lf)\n", x, y );
99. return;
100. }
101.
103. {
104.             printf( "\nEnter  1 to enter a new system\n" );
105.             printf( "       2 to find the solution of the system\n" );
106.             printf( "       3 to exit\n" );
107.             printf( "Choice: " );
108. return 0;
109. }
Nov 14 '07 #1
2 6627
sicarie
4,677 Expert Mod 4TB
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?
Nov 15 '07 #2
Ganon11
3,652 Expert 2GB
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().
Nov 15 '07 #3