Computer problems,Computer help
*AX SOFT>>>Programming & Design

C programming simple question?


i have to find the roots of a second order equation (i.e. 2x^2 +2x +2) I scan in the the coefficients one by one as a variable. The question is how do i pass those variables and there values to another function so i can use them to calculate the roots with the given equations?

I'm not sure I understand your question... if you have variables a, b and c, just pass them into your method like this :

findRoots(a, b, c);

That can't really be your question can it? That is what you asked for....

*** EDIT ***

I think I understand what you're worried about now. You need to be able to return 2 numbers right? Here's how I would do it:
Have the method return an integer status. If it returns 0, it was successful, if it returns anything < 0 no roots existed (imaginary) and if it returns anything > 0, only one root existed. You can pass arguments to it like this:

assuming :
int status, a, b, c;
double root1, root2;

status = findRoots(a, b, c, &root1, &root2);


Then
int findRoots(int a, int b, int c, double *root1, double *root2)
{
//do your calculations
//set root1 and root2 and set a status
return status_code;
}

When you get back to your main(), the root1 and root2 will be updated. This is known as passing parameters by Reference, instead of by value.

/* Program to calculate the roots of a quadratic equation */

#include <stdio.h> /* printf() and scanf() */
#include <math.h> /* sqrt() */

main()
{
double aa, bb, cc, aux, root1, root2;

aa = 1.0;

printf("\nProgram to find the roots of a quadratic equation\n");
printf("of the form a*x*x + b*x + c = 0\n\n");
printf("Entering 0 for the value of a stops the program\n\n");

while ( aa != 0.0 )
{
/* Enter coefficients for equation */
printf("Enter value for a : ");
scanf("%lf", &aa);
if ( aa == 0.0 )
{
/* Exit the while() loop, unless we want a division by 0 */
break;
}
printf("Enter value for b : ");
scanf("%lf", &bb);
printf("Enter value for c : ");
scanf("%lf", &cc);

/* Better make sure there is a real root for these coefficients */
aux = bb * bb - 4 * aa * cc;

if ( aux > 0.0 ) /* There are 2 solutions */
{
aux = sqrt( aux ); /* Our 'aux' variable now holds the square root
of its previous value, we do this because
we don't want to calculate the square root
more than once */
/* We have already made sure that aa !=0, so a division is safe */
root1 = ( -bb + aux ) / ( 2 * aa );
root2 = ( -bb - aux ) / ( 2 * aa );
printf("\nThe roots %.4lf and %.4lf were found\n\n", root1, root2 );
}
else if ( aux == 0.0 ) /* There is only one solution */
{
root1 = -bb / ( 2 * aa );
printf("\nA single root, %.4lf, was found\n\n", root1 );
}
else /* There is no real solution */
{
printf("\nNo real roots were found\n\n");
}
} /* End while() loop */

printf("\nEnd of program, goodbye!\n\n");

return;
}


// one more solution at http://www.electrofriends.com/source_cod...

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • How to get image links to work?

    Myspace does NOT like outside websites that compete with their own. I had to simply list my yahoo website on the site. There is no way to get around it, because their site filters are set to catc...

  • Hotmail.com?

    the new hotmail thing is now @live.com/ca/ etc.. you can also get some other cool endings like @live.fr,girl.girl, etc google it and you'll find a code that you paste into the address bar th...

  • I have to do a project in java language in my college curriculum...please suggest me any good topic?

    write a program that reads in a serialized sudoku puzzle and prints out the solution. I had to code one of these in college and it was a blast!

    ...
  • Very simple math help in C programming?

    9 3 6 answers are: ivalue1=9 ivalue2=3 ivalue3=6 so first i start from ivalue3,ie easy c compiler precedence first give preference to brackets iValue3 = 3 * (5%3) ; so first 5%3,it ta...

  • Can somebody give me the names of other video uploading sites...?

    Hi, now a days most of the social networking websites come with a video upload facility. I could name a few: rediff ishare hi5 google...there are lots...just browse it or google it!..u would f...

  • Java menu Hell... Please help!!!! 10 PTS TO THE FIRST ANSWER THAT WORKS?

    hi You send me the files you want to update and i will update it for you Regards Asim

    ...
  • Java help! Do while boolean is false loop?

    import java.util.Scanner; public class ans1 { public static void main (String [] args) { Boolean good = false; Scanner keyboard = new Scanner(System.in); Double a; Stri...

  • HTML and Javascript Program?

    <script type="text/javascript"> function leng() { var str=document.form1.getinput.value; alert("length of String : " +str.length); } </script> <form name...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster