Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-07-2004, 09:14 PM   #1 (permalink)
becool04
Registered User
 
Join Date: Nov 2004
Posts: 2
becool04 is on a distinguished road
Unhappy help plz!!

write a C++ program that finds the x, which is the root between -100 to 100 in linear equation y = m * x + b. The values of m and b are read of the external data file. In this program must use loops and use if statement if nessecery.
The following is the source code i wrote which doesnt work...
#include <iostream.h>
#include <fstream.h>
#include <math.h>

int main ()
{
double m = 0.0;
double b = 0.0;
double x = 0.0;
int y = 0.;

ifstream infile ( "C:/WINDOWS/Temp/output.txt");

infile >> m;
infile >> b;
cout << "m = " << m << endl;
cout << "b = " << b << endl;


for ( x = -100.; x <= 100.; x = x + 0.001)
{
y = m * x + b;
if ( y = 0)

{
cout << x <<endl;
}


}
cout << x << endl;
return 0;
}//end of main
becool04 is offline   Reply With Quote
Old 11-07-2004, 10:03 PM   #2 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 151
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
When you say it doesn't work, what do you mean? What is/are the errors you get?
__________________
Current Project
Redline is offline   Reply With Quote
Old 11-07-2004, 10:24 PM   #3 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 151
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Also, it looks like your program is only trying to find the x intercept? If that's true, it's unnecessary (and inaccurate) to use a loop. The formula to find the x intercept is

x = -b/m

where:
b = y intercept
m = slope

Explanation (if you want it)

The standard equation is y = mx + b. If you're looking for the x intercept, that is to say the exact point where a line with a given slope(m) and y intercept(b) cross the x axis, then the y value for that point would be zero. Plug that zero into the equation and solve for x to find the forumula for the x intercept:

y = m * x + b (y = 0)
0 = m * x + b (subtract b from both sides)
-b = m * x (divide both sides by m)
-b/m = x

So you could replace your for loop with the following code and achive the same results

Code:
x = -b/m;
cout << x <<endl;
That's a little easier and cleaner isn't it? The reason why you can't really do it the other way is this; when you increment x by .001 per cycle, the exact point at which y = 0 can and will get skipped over. The only time that would work is when x does not exceed 3 decimal places. I hope this was helpful
__________________
Current Project
Redline is offline   Reply With Quote
Old 11-08-2004, 12:45 AM   #4 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 151
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Here is my working interpretation of your code (it's also my first C++ program :rock: )
Code:
#include <iostream.h>
#include <fstream.h>

int main ()
{
	char temp[100];
	double m;
	double b;
	double x;

	ifstream  datafile ("test.txt");
	if (! datafile.is_open())
	{
		cout << "Error opening file"; exit (1);
	}

	datafile.getline(temp, 100);
	m = atof(temp);
	datafile.getline(temp, 100);
	b = atof(temp);
	cout << "m = " << m << endl;
	cout << "b = " << b << endl;

	x = -b/m;
	if (x >= -100 && x <= 100) {
		cout << "X Intercept: " << x << endl;
	}
	else {
		cout << "X Intercept is out bounds";
	}
	return 0;
}
__________________
Current Project
Redline is offline   Reply With Quote
Old 11-08-2004, 04:15 AM   #5 (permalink)
becool04
Registered User
 
Join Date: Nov 2004
Posts: 2
becool04 is on a distinguished road
we haven't gotten that far yet. so far, i need to use the ifstream datafile and the loop and if statement. Thank you for your program, very appreaciated.
becool04 is offline   Reply With Quote
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP deperately need help plz!!! RusH MS Technologies ( ASP, VB, C#, .NET ) 2 10-03-2004 05:42 PM
i have a quetion that needs answered plz... Termite HTML, XML, Javascript, AJAX 6 05-15-2004 09:28 PM
i have a child window which i want to print at the same time hiding plz help deepanakr HTML, XML, Javascript, AJAX 0 05-10-2004 10:08 PM
plz help panic-- Lounge 4 04-07-2003 06:21 AM
colo in L.A. area needed .. plz sde Lounge 1 12-21-2002 05:53 AM


All times are GMT -8. The time now is 09:56 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting