Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 15th, 2005, 10:15 AM   #1
halimunan
Newbie
 
Join Date: Mar 2005
Posts: 3
Rep Power: 0 halimunan is on a distinguished road
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.
halimunan is offline   Reply With Quote
Old Mar 15th, 2005, 10:48 AM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
paste the code that you have so far, we can look at that, you cant learn from other people writing code for you.
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Mar 15th, 2005, 2:49 PM   #3
brkstf
Programmer
 
brkstf's Avatar
 
Join Date: Feb 2005
Posts: 89
Rep Power: 4 brkstf is on a distinguished road
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.
brkstf is offline   Reply With Quote
Old Mar 17th, 2005, 11:40 AM   #4
halimunan
Newbie
 
Join Date: Mar 2005
Posts: 3
Rep Power: 0 halimunan is on a distinguished road
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
halimunan is offline   Reply With Quote
Old Mar 17th, 2005, 2:05 PM   #5
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
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.
spydoor is offline   Reply With Quote
Old Mar 17th, 2005, 2:17 PM   #6
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Mar 18th, 2005, 2:21 AM   #7
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
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
}
Berto is offline   Reply With Quote
Old Mar 18th, 2005, 8:46 AM   #8
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Mar 21st, 2005, 3:45 AM   #9
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
i think its just personal prefereance, and my lecturers also tend to talk rubbish half the time. I just like pretty code
Berto is offline   Reply With Quote
Old Mar 21st, 2005, 10:16 AM   #10
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:11 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC