I am making a program for my class where I handle equation problems such as:
2a-3b+5c=10
3a-2b-3c=-5 where the answer would be:
5a-5b+2c=5

This is what I have so far. It still isn't perfect cause I still haven't figured out the "=" part. I do have it performing the math though. But the answers are coming as:
5a5b5c-5a5b5c+2a2b2c. I don't understand why. Any help would be much appreciated.

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

// Declarations
const int length=26;
int num_var,
i,j;
char variable_array [length];
int equ_1 [length],
equ_2 [length];



int main()
{
cout<< "How many variables are in your equation?" <<endl;
cin>> num_var;


cout<< "Enter the variables for the first equation," <<endl;
cout<< "while entering a negative sign if the preceding" <<endl;
cout<< "operator is a minus sign." <<endl;
for (i=0; i<26 && i<num_var; i++)
{
cin>> equ_1[i];
}


cout<< "Enter the variables for the second equation," <<endl;
cout<< "while entering a negative sign if the preceding" <<endl;
cout<< "operator is a minus sign." <<endl;
for (i=0; i<26 && i<num_var; i++)
{
cin>> equ_2[i];
}


strcpy (variable_array, "abcdefghijklmnopqrstuvwxyz");


for (i=0; i<26 && i<num_var; i++)
{
	equ_1[i] = equ_1[i] + equ_2[i];
}

cout<< "Your answer is:";
for (i=0; i<26 && i<num_var; i++)
{
	for (j=0; j<26 && j<num_var; j++)
	{
		cout<< equ_1[i] << variable_array[j];
	}
}
}

Recommended Answers

All 3 Replies

I think "code snippets" are intended to be "here is my working code, I thought it may help others". In your case, with a question, you should just make a normal "thread".

changed to normal thread.

The obvious [once you have seen it] trick to make your output easy to read is this:

char first=' ';
for(int i=0;i<num_var;i++)  
{
  if (equ_1[i]<0)
    cout<<equ_1[i]<<variable_array[i];
  else if (equ_1[i]>0)
    std::cout<<first<<equ_1[i]<<variable_array[i];
  if (equ_1[i]) first='+';
}

You can note that you don't need a separate loop index for variable_array, and that using first, allows you to have a + between each number. It also deals with the classic problem that you normally don't want to write 0a+4b=10, but 4b=10. There are slightly more elegant ways to do this but it adds some advance concepts.

As to your original question, all you do is not that if you have eqn: a+b=40, it is the same as a+b-40=0, and you can treat the 40 as a value that doesnt have a variable, e.g. starting abc etc from equ_1[1] and reserving equ_1[0] for the constant

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.