473,385 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.  
  14. int menu(); 
  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.         {
  27.          menu();
  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.  
  102. int menu()
  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

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Guenther Sohler | last post by:
I am looking for a apropriate c library to solve my sparse linear equation system. I have n variables and m equations. m might be less, equal or greater then n!!!! If there are less...
2
by: Anatoly | last post by:
Hi All !!! Can anyone give me link to the C++ source or to the compiled program unit (compatible with Microsoft VC++), designed for the solving of the algebraic linear systems with rare matrices...
3
by: sql guy123 | last post by:
This is a real challenge. I hope someone is smart enough to know how to do this. I have a table TABLE1
13
by: RS | last post by:
Hi all, I am looking for a fast, efficient (tuned for speed, not size) and mature C++ code to do some numerical work with. I have looked at the Template Numerical Toolkit, which the...
1
by: wirecom | last post by:
Hi all, I am seeking a module that will do the equivalent of linear regression in 3D to yield a best fit a plane through a set of points (X1, Y1, Z1), (X1, Y1, Z1),... (Xn, Yn, Zn). The...
4
by: Thomas | last post by:
Hi, I've to solve a system of linear equations and inequations in C++. Are there (free) librarys for that topic? Regards, Thomas
7
by: spranto | last post by:
Hi to you all, This is my first post. I'm having troubles finding one suitable code line to solve a 3 non linear equation system. I'm here asking if someone allready done that and if it possible...
7
by: Bigs | last post by:
Ok, I have been working on a Linear Equation program that will draw a line on my graph. But, I am not sure how to set up the y=mx+b in Java to return the (x1,y1) and (x2, y2) using only the slope and...
10
by: Constantine AI | last post by:
Hi i am having a little problem with an equation function that was created from all your help previously. The function works fine itself but with a small glitch within it. Here is the function...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.