![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 3
Rep Power: 0
![]() |
C easy one.. [if] [else]...help please
hello guys.. i cant figure out how to solve this one...
Question : Write down an algorithm to find out and display the roots of the equation X2 – 8 X + 15 by testing all integers in the range 0 <= x <= 10.Note: Those values of x for which above function evaluates to 0 are considered as the roots of the equation. i dunno how/where to put the else/if... it later told me "misplace else" urgh... can someone provide me the code.. i`m using borland c++ thanks.. i`m still a newbie.. and learning by myself.. Last edited by halimunan; Mar 15th, 2005 at 10:36 AM. |
|
|
|
|
|
#2 |
|
Professional Programmer
|
paste the code that you have so far, we can look at that, you cant learn from other people writing code for you.
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Feb 2005
Posts: 89
Rep Power: 4
![]() |
i think they just want you to evaluate
f(x)=x2-8x+15 for all values of x in the range, and report any time f(x) = 0 piece of cake. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2005
Posts: 3
Rep Power: 0
![]() |
this is my code..
i tried this one with if and else .. but it wont work.. this seems incomplete
#include <stdio.h>
#include <conio.h>
int x, result;
main()
{
{
printf("\nInsert value less than eleven and more than zero!: ");
scanf("%d", &x);
result = (x*x) - (8*x) + 15;
printf("\nX is > %d", result);
}
getch();
return 0;
}and this one too.. but this seems wrong.. #include <stdio.h>
#include <conio.h>
main()
{
int x;
int y;
printf("this programme is to show the root of the equation x^2-8x+15");
printf("\n\nvalue of x \tresult \n");
for (x=0; x<=10; x++)
{
y=x*x-8*x+15;
printf ("%2d\t\t%2d\t",x,y);
if (y==0)
printf("%2d is a root for the equation \n",x);
else
printf("%2d is not a root for the equation \n",x);
}
printf("\nThe value that has a result '0' is the root for the equation.");
getch();
return 0;
}can u guys helpme out.. thanks |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
The 2nd one you've shown is perfect
why do you think it's wrong? value of x result 0 15 0 is not a root for the equation 1 8 1 is not a root for the equation 2 3 2 is not a root for the equation 3 0 3 is a root for the equation 4 -1 4 is not a root for the equation 5 0 5 is a root for the equation 6 3 6 is not a root for the equation 7 8 7 is not a root for the equation 8 15 8 is not a root for the equation 9 24 9 is not a root for the equation 10 35 10 is not a root for the equation The value that has a result '0' is the root for the equation. |
|
|
|
|
|
#6 |
|
Programming Guru
![]() ![]() ![]() |
You are making it unnecessarily complicated...
Use a while loop... with a simple equation. You're answers will be 3 and 5. Pay particularly close attention to your order of operations. Here is some code... but remember, your assignment was for the ALGORITHM not the code... so it would be in your best interests to understand what this does before you turn it in. Chances are you will be tested on this material. #include <iostream>
#include <stdlib.h>
using namespace std;
int main (void)
{
// f(x)=x^2-8x+15
int x = 0;
while (x < 11)
{
int val = ((x*x)-(8*x))+15;
cout << "F(" << x << ") = (" << x << " ^ 2) - (8 * " << x << ") + 15 = " << val << endl;
x++;
}
system("PAUSE");
return 0;
}
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#7 |
|
Programming Guru
![]() |
lol you could do that in a nice for loop would be better then a while as you are using while in the wrong instance i think anyways.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main (void)
{
// f(x)=x^2-8x+15
for (int x = 0; x < 11;x++)
{
int val = ((x*x)-(8*x))+15;
cout << "F(" << x << ") = (" << x << " ^ 2) - (8 * " << x << ") + 15 = " << val << endl;
}
system("PAUSE");
return 0;
}i was always taught that you use while loops when you dont no a definate result to end the loop eg. while (start != end){ do something } |
|
|
|
|
|
#8 |
|
Programming Guru
![]() ![]() ![]() |
Wrong instance? A while loop or for loop will work fine...
The while loop was less typing, I'm a fan of that. I'm not sure why you were taught that about the while loop, did they give any reasoning or logic behind that? To me, it seems like a matter of choice and how you want to handle the exit condition.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#9 |
|
Programming Guru
![]() |
i think its just personal prefereance, and my lecturers also tend to talk rubbish half the time. I just like pretty code
![]() |
|
|
|
|
|
#10 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
for loops and while loops are generally interchangeable, but as Berto says, most people generally use for loops when they're counting, and while loops when there's no definite end to the loop.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|