Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,143,418 members, 7,781,212 topics. Date: Friday, 29 March 2024 at 10:43 AM

C++ Experts Please Come To My Aid.... - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / C++ Experts Please Come To My Aid.... (1136 Views)

Help Needed Urgently On Visual Basic. Experts Please! / Official Thread For C# Experts And Beginners / C++ Experts Pls Help Me . (2) (3) (4)

(1) (Reply) (Go Down)

C++ Experts Please Come To My Aid.... by Nobody: 7:51pm On Jul 24, 2014
hello guys, am kinda new to this awesome compiler called cplusplus, so far so good have written few programs on a cgpa calculator, etc... but i discovered that my knowledge on c++ is mainly on the input/output platform,,, ok see what i mean, i want to start writting programs on Calculus, for example

Case 1.

1. write a program that can add three numbers, this one is quite easy and of course requires you to define how your input variable should be ( int, float, etc) but it will only display your output in a Number format.

Case 2.

2. write a program that can find the differential or integral of a function, lets say ( y = 2x^3).. this is where am having a problem and is really ostracizing me from learning this program, of course from the about function when you diffenciate it you get
dy/dx = 6x^2 but how can i display this as the output?

.
Re: C++ Experts Please Come To My Aid.... by davidsmith8900: 7:55pm On Jul 24, 2014
Ezechinwa: hello guys, am kinda new to this awesome compiler called cplusplus, so far so good have written few programs on a cgpa calculator, etc... but i discovered that my knowledge on c++ is mainly on the input/output platform,,, ok see what i mean, i want to start writting programs on Calculus, for example

Case 1.

1. write a program that can add three numbers, this one is quite easy and of course requires you to define how your input variable should be ( int, float, etc) but it will only display your output in a Number format.

Case 2.

2. write a program that can find the differential or integral of a function, lets say ( y = 2x^3).. this is where am having a problem and is really ostracizing me from learning this program, of course from the about function when you diffenciate it you get
dy/dx = 6x^2 but how can i display this as the output?

.

What are you trying to display as output? The graph? The equation? The final answer? Can u please be more clear on your questions, if you want it answered?
Re: C++ Experts Please Come To My Aid.... by Nobody: 8:04pm On Jul 24, 2014
davidsmith8900:

What are you trying to display as output? The graph? The equation? The final answer? Can u please be more clear on your questions, if you want it answered?

i want my output (answer) to be dy/dx = 6x^2 rather than just a whole number...

1 Like

Re: C++ Experts Please Come To My Aid.... by davidsmith8900: 8:12pm On Jul 24, 2014
Ezechinwa:

i want my output (answer) to be dy/dx = 6x^2 rather than just a whole number...

Okay, I see what you are saying. The input is an equation or certain x/y data and as an output you want to find an equation.

Here is a video that can get you started ~>
https://www.youtube.com/watch?v=VvPBbNNMC-A

Here are 2 topics that have answered your question before:

A. http://stackoverflow.com/questions/2982167/integration-math-in-c

B. http://stackoverflow.com/questions/1564543/c-math-library-with-integration

This tutorial might be able to help you out ~> http://www.cplusplus.com/doc/tutorial/variables/

THIS LOOKS LIKE WHAT YOU ARE LOOKING FOR ~> http://www.cplusplus.com/forum/beginner/91632/ BUT DOUBLE CHECK ME ON THAT.
Re: C++ Experts Please Come To My Aid.... by Nobody: 8:18pm On Jul 24, 2014
i will read up the links... and buy enough mb for watch the video.. thanks

1 Like

Re: C++ Experts Please Come To My Aid.... by davidsmith8900: 8:20pm On Jul 24, 2014
Ezechinwa: i will read up the links... and buy enough mb for watch the video.. thanks

What is your input? Can we see a source code? What do you have, so you wont have to buy enough mb just to watch a video.
Re: C++ Experts Please Come To My Aid.... by WhiZTiM(m): 8:50pm On Jul 24, 2014
Are you sure you know exactly what you want to do? And are you sure you are ready to do it?

Its called "Symbolic Differentiation" or Automatic Differentiation.

Its complex and orders of magnitude much harder than it seems.

Your biggest problems includes:
* Input parsing
* Data Structure and Abstraction
* Chaining of Operation Graph(Forward and Rervese accumulation)
* Output formatting

In simple implementation, you basicall imbue your expressions into an Accumulation tree... (Chain rule or Function of a function) ....And you apply appropriate operation from depest node to root...

First try making a calculator.... Learn parsing and solving expressions using stacks and then decision/operation trees...

http://www.sciencedirect.com/science/article/pii/S0377042700004222?np=y

and

http://en.m.wikipedia.org/wiki/Automatic_differentiation

There are programming libraries that will to certain extent, cushion the pain of implementing an Automatic Differentiating program...

Check out one of the CAS Software I use in my professional field... Maxima... It does a hell of symbolic calculus and lots of symbolic/precise maths....

http://maxima.sourceforge.net


I hope you know some Numerical Methods...??
Numeric methods are better industrially suited on computers... due to insane vectorized speeds on cache lines....

Symbolic is more applicable to academia/technical writing...

I dunno your experience level with Programming, Computing and Undergraduate Maths ....but I hope I made little sense??

1 Like

Re: C++ Experts Please Come To My Aid.... by Nobody: 12:21am On Jul 25, 2014
WhiZTiM: Are you sure you know exactly what you want to do? And are you sure you are ready to do it?

Its called "Symbolic Differentiation" or Automatic Differentiation.

Its complex and orders of magnitude much harder than it seems.

Your biggest problems includes:
* Input parsing
* Data Structure and Abstraction
* Chaining of Operation Graph(Forward and Rervese accumulation)
* Output formatting

In simple implementation, you basicall imbue your expressions into an Accumulation tree... (Chain rule or Function of a function) ....And you apply appropriate operation from depest node to root...

First try making a calculator.... Learn parsing and solving expressions using stacks and then decision/operation trees...

http://www.sciencedirect.com/science/article/pii/S0377042700004222?np=y

and

http://en.m.wikipedia.org/wiki/Automatic_differentiation

There are programming libraries that will to certain extent, cushion the pain of implementing an Automatic Differentiating program...

Check out one of the CAS Software I use in my professional field... Maxima... It does a hell of symbolic calculus and lots of symbolic/precise maths....

http://maxima.sourceforge.net


I hope you know some Numerical Methods...??
Numeric methods are better industrially suited on computers... due to insane vectorized speeds on cache lines....

Symbolic is more applicable to academia/technical writing...

I dunno your experience level with Programming, Computing and Undergraduate Maths ....but I hope I made little sense??

yeah, lots of sense bro,,,, i think am mathematically sound, and i will put hard effort to learn it, i know with time i will only get better....

and you also made a good point, precisely about libuaries that support dy.dx.... thanks...
Re: C++ Experts Please Come To My Aid.... by Nobody: 12:52am On Jul 25, 2014
davidsmith8900:

What is your input? Can we see a source code? What do you have, so you wont have to buy enough mb just to watch a video.

#include <iostream>
#include <math.h>
#include <cstdlib>

using namespace std ;
int main ()
{
double y ; // " output variable"
double x ; // " input variable "
double n; // " power"
double c; // " coefficent"
double q ; // " power constant"
double w ; // " power reduction "


cout << " ENTER power of the function"
cin >> n ;

cout << " ENTER co-efficient of x "
cin >> c ;

q= (n * c) ; // initial manupulation
w= ( n-1) ; // initial manupulation 2

y = pow ( q , w ) ; // final differential valve

cout << " the differencial of the equation is " ;
cout << q << "(x) ^ " << w ;

// " note ^ means power "

system ( "PAUSE"wink ;
return 0 ;

}
Re: C++ Experts Please Come To My Aid.... by Nobody: 12:58am On Jul 25, 2014
Ezechinwa:

#include <iostream>
#include <math.h>
#include <cstdlib>

using namespace std ;
int main ()
{
double y ; // " output variable"
double x ; // " input variable "
double n; // " power"
double c; // " coefficent"
double q ; // " power constant"
double w ; // " power reduction "


cout << " ENTER power of the function"
cin >> n ;

cout << " ENTER co-efficient of x "
cin >> c ;

q= (n * c) ; // initial manupulation
w= ( n-1) ; // initial manupulation 2

y = pow ( q , w ) ; // final differential valve

cout << " the differencial of the equation is " ;
cout << q << "(x) ^ " << w ;

// " note ^ means power "

system ( "PAUSE"wink ;
return 0 ;

}


the problem here is that if i should display my output to be y, where y = ( nx^ (n-1)) because x is a not a variable type char, it will return your valve as a number,,, rather than another function...
also remember differenciation is a link of functions tending to zero, so numbers are not relevent...
Re: C++ Experts Please Come To My Aid.... by asalimpo(m): 4:31pm On Jul 25, 2014
Your problem isnt hard for basc differentiation cases.
I dnt code in c++ but java .
The algorithm for solvg it shud b cross platform.
If ur really determined u can solve it. Just focus concentrate and think,step by step.


Pseudo code:
=============
differentiate the expression: 2xr3
read "2x raise to power 3"
ans= 6xr2.

1) get the input as a string.
Input = 2xr3
2) convert the strng input into an expression object. (this is where oop concepts kick in.u know oop?)
oop = object oriented programmg.

3.) process d expression object to get an answer . Which will b anoda expression object.

Step2: creatg d expression object from string object
==========================
take the string argument and break it up at partcular points of interest.
Inputstring=2xr3.
- brk it up at the power character,i.e ,'r' in this case.
You will store the resultants in a list: an array or a list
(read up on arrays and containers)
the result will b = [2x][3] // we ignored d power character.
This list has a size of 2. because it contains 2 elements.
From d list the power is the second element i.e 3.
- wrkg with d list is cumbersome but possible (and faster) so we'll put the elents into objects. That handle d dirty work.
-
create an object called 'Term'
representg d "terms in an expression".
(you need oop here)
//using java here .. Chnge to cpp
class Term
{
string sterm;
int num;
char variable=null;
//set to null initially
Term(string arg)
{
1. Parse the argumnt to a number
if it wrks, store d result in the
number variable i.e num
the argument is a num
2. If it fails
(you know about exception handling? Read it up)
then the arg isnt a number.
It is a number and a variable
in the form:<num><var>
where 'var' is an alphabet character.
Now,split the arg into 2 parts
the number part and d variable part: we ar back to string parsing again.
2.1 = you will get two elements
after d parse , ur results are stored in a container:an array,or list.
It shud b in d form
[num][var].
2.2
now process d elements in d container of 2.1
convert the first element 'num' from srtng type to number type
. Use the srting to int parse function of ur language.
Wrap the parse call in a fail-safe block,to handle problems arisg durg d conversion process.
In java this is a try-catch block.
The process is called exception handling.
Any error arisg at this stage ,shudnt b processed further.
So , 2x, when parsed should become [2][x]
all string types.
'2' will then b converted to number type i.e int through string to number parsg.
If all goes well,
you'll get a number,e.g 2 in this case. Store it in the class field, 'num' store the character variable in d class field, 'variable'.
All this code takes place in d Term's constructor
the main work in this class is over.

(... To b continued )


* Wat compiler are usg and how dyu get it ,install it and compile with it*

1 Like

Re: C++ Experts Please Come To My Aid.... by asalimpo(m): 5:42pm On Jul 25, 2014
... Continuing

all d previos wrk is happing in a class called Term which represents d terms in an expression.

A term takes a string argument in its constructor and converts it internally into a) a number or b) a number and a character variable (representing a sun expression).

A constructor is a method (similar to a function) tht a class invokes wen it wants to create new copies of itself.i.e wen its about to be instantiated.

You will want to know wat type of term object u are dealg with in ur program.
Is it a number , or an expression term?
So you'll add methods to ur class to answer this questions.


class Term
{
// ... Constructor and class fields
int num;
char variable=null;
string rawArgument;
public Term(string argument)
{
//.... Parsing and assigning of argument takes place
or exception is thrown.
}

// getters
int getNum(){ return num;}
char getVariable(){return variable;}

// is it a number term
boolean isNum(){ return variable==null;}
//is it a sub expression term e.g 2x
boolean isExp(){
return !isNum();}

//get the initial argument

string getArg(){return argument;}
}


now you've got to create the calculus class representg d expression.

Lets call it Differentiator.
It takes two Terms and produces an answer representg the result of d differentiation.


class Differentiator
{
Term Rt;// right of d power sign
Term Lt;// left of d power sign
Expression result;
//the left term is d power value.

//constructor
Differentiator(Term rt,Term lt)
{
//perfrm som chcks to
// make sure d arguments ar valid bf assigning
// e.g the left term must b a num
//the right term must b an expression
}

//constructor2
//constructor tht takes string //arguments instead of Term //arguments

Differentiator(String rt,String lt)
{
this(Term(rt),Term(lt));
}

//computes and returns a result
//representg a differentiation.

Term compute()
{
//if d result had been computed bf simply return the ans.
If(ans != null)
return ans;
else
{
// compute i.e differentiate
// the result cud b a Term or an
//expression, a group of terms
/*
formula:
(Numpart of RightTerm(Rt)*Letf term)+variable part of Rt. = first term of result.
+ numberpart of Lt-1 = second term(Lt) of result.
Compute this result then pack the answer into an expression.
*/

char v = Rt.getVariable();
int rtnum=Rt.getNum();
int ltnum=Lt.getNum();

string ftres/*first term of result*/ = toString(rtnum*ltnum+v);
/*compute and convert to string so u can create a Term object.
- not very efficient technque,though
*/

Term rtResT = new Term(ftres);
//right term of result
//next compute the left term of d result.
/*it is letfterm numberpart of expression - 1.
*/
string ltres = toString(Lt.getNum()-1);
//put result in a term object.
Term ltResT = new Term(ltres);

// now create your expression oject and pack d terms into it

Expression exp = new Expression(rtResT,ltResT);
//set answer field to this
ans = exp;
// return your ans
return ans;
}
}
}


to b continued ...
Re: C++ Experts Please Come To My Aid.... by Nobody: 5:49pm On Jul 25, 2014
@asalimpo.... don't know how to thank u enough.... well am using devcpp... and i will put your post into practise when i return.... i sure will update you... once again thanks
Re: C++ Experts Please Come To My Aid.... by asalimpo(m): 6:19pm On Jul 25, 2014
continuing ...
The Expression Object returned by d differentiator wasnt defined.
Here's a rough sketch.

class Expression
{
char powerSign='r';
Term rT;//right term
Term lt;//left of power sign

//constuctor
Expression(Term rightT,Term leftT)
{
// run validity checks and assign
// values
}

//constructor2
Expression(Term rt,Term lt,char powerSign)
{
this(rt,lt);
//validate char bf assigng
}

//constructor3 for single term //expressions only
//e.g 6x

Expression(Term rt)
{
//validity checks.
//assign the argument to d right //term
}
//getters
getRt(){return rt;}
getLt(){return lt;}
getSign(){return powerSign;}
toString(){ return rt+powersign+lt;}

/*sometimes d expression can b resolved to a single term, if the power value is 0. Chck fr this
*/
boolean isResolvable()
{
return lt==null || lt.getNum()==0;
}

Expression resolve()
{
/*
Resolve d expression to simpler form
if it's not resolvable simply return itself
else edit the right term ,if necessary and return it
*/
if(!resolvable())
return this;
else
{
//create a new expression
Term nt = new Term(toString(rt.getNum()));
Expression nuExp = new Expression(nt);
return nuExp;
}
}

1 Like

Re: C++ Experts Please Come To My Aid.... by asalimpo(m): 6:50pm On Jul 25, 2014
Wrapping up ...

So far 3 objects hav been created to solve d differentiation problem.
Term,Expression,Differentiator,Parser(not shown)

now we'll create a fourth object. Called calculusProgram . It has two methods, differentiate and integrate . Integration isnt shown.
differentiate - takes in a string expression and return a string result representing the answer.
It should throw an error wen thngs go wrong.

The steps first:
get the input string - the expression to b differentiated

- Parse the input string into an expression object


- pass the expression object to a Differentiator for differiation.
Invoke its 'compute' method to get a result . The result is an expression object


convert the result to a sting by calling the resultant expression's object 'toString' method
.


 

class calculusProgram
{
string differentiate(string expressionstring){
Parser parser = new Parser();
Expression exp = parser.toExpression(expressionString);
Differentiator differ = new Differentiator(exp);
Expression result = differ.compute();
//resolve the result
result = result.resolve();
//return a string answer
return result.toString();
}
}






the code hasnt been tested (debugged and compiled)
- it's in java (but shud b easy to port to c++).
- add a constructor to d differentiator class that makes it take an Expression as argument.
After validation,it shud assign the terms in the Expression object to its internal fields (Rt,Lt)

1 Like

(1) (Reply)

Help On Andriod Studio / Check Out My Python Code For Giving You The Date/time You Are Currently. / Website Or Software Programming?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 60
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.