Having some trouble with these assignments

closed account (oj87ko23)
Having trouble with these two problems. Not sure on what to include in the loops. I know i have to use if else statements and sentinel-controlled loops. I also know you have to declare <math.h> on top. Just not sure on what to put in if statements and the way my professor worded the problems confused me.

problem 1
Write a program to find the roots of a quadratic equation. your program should evaluate all three possibilities, same roots, complex conjugate, and different roots. The user is supposed to input coefficients of the equations as real numbers. You can use library function sqrt() to find the square root of an argument.
Sample output:
Enter coefficients of the equation:
Complex roots are 3+j4 and 3-j4

pretty lost on this one

Problem 2
write a program to evaluate the value of e^x by using the formula:

e^x= 1+ x/1! +x^2/2! + x^3/3!...
evaluate the above expression for different values of x and number of terms in the expression. The user should be allowed enter x and number of terms for which the expression is calculated.

Lab2
// lab 2b.cpp : Defines the entry point for the console application.
//
#include
"stdafx.h"
#include
<stdio.h>
int
_tmain()
{
double F, C;
printf(
"Enter temperature in Celcius: ");
scanf (
"%lf", &C);
F = (9.0/5.0) * C +32.0;
printf(
"The temperature in Fahrenheit is: %lf \n", F);
return 0;
}

would the code for problem 2 be similar to this code for temperature but using the equation
e^x= 1+ x/1! +x^2/2! + x^3/3!...?? Not sure if that is right but both problems seem similar in my eyes, I could be wrong though.


I'm not asking anyone to do my work so please don't write your smart *** comments here. Just looking for kick in the right direction. I'll be using scanf and printf commands, not cin and cout as some inscructors want. I'm using the book how to program sixth edition by paul deitel and harvey deitel. I've looked through the book and can't find any input that I could use. Maybe I'm missing key examples that could help. Also kind of confused on what to put in my if else statements. I know what they're for and how the book uses them, but on these problems, not sure on how to use them. We've covered up to chapter 3 in class, so I'm familiar with everything up to that. Thanks in advance to anyone who takes the time to help me.
Problem 1:

I'm not sure how much you know about the quadratic formula but you can tell how many roots it will give you based on the discriminant (b * b - 4 * a * c)

-Define 3 double variables named a,b,c or any names you choose.
-Get the user input for the variables with scanf. (Do a quick if statement to make sure a != 0)
-Calculate the discriminant and store it with something like double Desc = (b * b - 4 * a * c);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(Desc > 0)
{
	// There are 2 real roots
	//Print out the + and - values of the equation  -b +- (b * b - 4 * a * c))) / (2 * a)
}
else if(Desc == 0)
{
	// There is 1 real root
	//Print out results.
}
else
{
	//There are complex roots
	//Print out results (for this you will either want to use the <complex> C++ library or just print out the real/imaginary parts of the number individually using printf. 
        //This can be found on google pretty easily if you aren't sure of complex numbers).
}


Problem 2:

At least the first parts are similar to that temperature program you did. You will want to get the value of x and the amount of times to loop from the user (of course e is a constant). Then you will want to set up a while loop and loop as many times as the user specified (which you will store in a variable) adding the result to the final answer each time the loop runs (the rest of it is just writing the equation. Don't forget C++ does not use ^ for power you will need to use * in your equations.
Last edited on
closed account (oj87ko23)
im pretty familiar with quadratic formula. I just really don't understand problem 1. I just don't remember this type of math, its been at least 4 years. how would I write the if statement to make sure it doesn't equal 0? How would I do the calculations? What does desc mean? Sorry bro, but I'm still lost on this one. how would I write the real roots part in the statements?
closed account (oj87ko23)
lost on number two as well now lol..man this blows.
closed account (oj87ko23)
figured out number 1 finally. stuck on two though.
Post the code you have so far (or any specific problems) and I may be able to explain more about it.
closed account (oj87ko23)
#include <stdio.h>
#include <math.h>

int main() {

int x,n,counter;
printf("Enter value of x : ");
scanf ("%d",&x);
printf("Enter number of terms (n) : ");
scanf ("%d",&n);
counter=counter+1;

this is all I have :/ I'm not sure how to start my loops on this one.
closed account (oj87ko23)
In class all we've gotten to is while loops, for loops, and inner loops. I know the value of e=2.7182. I just can't figure out what should go inside the loops.
Last edited on
Topic archived. No new replies allowed.