1 Replies - 1706 Views - Last Post: 02 May 2009 - 01:16 PM Rate Topic: -----

#1 hamza elhamrawy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-May 09

Help for c++ code error.....

Posted 02 May 2009 - 01:10 PM

hi,
Find areal root of equation cosx=3x-1 correct to 3 desimal places using iteration method.

answer:

#include<iostream.h>
#include<math.h>
#include<conio.h>
#define EPS 0.00005
#define F(X) (cos*x +1)/3
#define f(x) cos*x -3*x +1
void ITER();
void main()
{
	cout << "\n\t Solution by ITERATION method \n" << endl;   
	cout <<"\n\t Eqution is:";
	cout <<"\n\t\t\t\t cos*x +3*x +1=0\n\n";
	ITER();
	getch();
}
void ITER()
{
	float x1,x2,x0,f0,f1,f2,error;
	int i=0,n;
	for(x1=1;;x1++)
	{
		f1=f(x1);
		if(f1>0)
			break;
	}
	for(x0=x1-1;;x0--)
	{
		f0=f(x0);
		if(f0<0)
			break;
	}
	x2=(x0=x1)/2;
	cout <<"Enter the number of uterations:";
	cin >>"%d",&n;
	cout <<"\n\n\t\t The 1 approximation to the root is: %f",x2;
	for(;i<n-1;i++)
	{
		f2=f(x2);
		cout <<"\n\n\t\tThe %d approximation to the root is: %f",i+2,f2;
		x2=f(x2);
		error=fabs(f2-f1);
		if(error<EPS)
			break;
		f1=f2;
	}
	if(error>EPS)
		cout <<"\n\n\t NOTE:- The number of iterations are not sufficient.";
	cout <<"\n\n\n\t\t---------------------------------";
	cout <<"\n\t\t\t The root is %.4f",f2;
	cout <<"\n\t\t\t----------------------------------";
}




When it was implemented on microsoft visual c++ Gave me four errors and one warning

 error C2296: '*' : illegal, left operand has type 'double (__cdecl *)(double)'
 error C2296: '*' : illegal, left operand has type 'double (__cdecl *)(double)'
 error C2296: '*' : illegal, left operand has type 'double (__cdecl *)(double)'
 error C2296: '*' : illegal, left operand has type 'double (__cdecl *)(double)'
 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data


What do I do?
Thank you very much,

Is This A Good Question/Topic? 0
  • +

Replies To: Help for c++ code error.....

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Help for c++ code error.....

Posted 02 May 2009 - 01:16 PM

You need to use cmath's cos, which is cos(value) not cos* value

I don't even know what cos*x would give you. cos of nothing is undefined
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1