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];
	}
}
}

Dani AI

Generated

Quick diagnosis for : the duplicated output (5a5b5c-5a5b5c+2a2b2c) comes from printing each coefficient against every variable because of the nested loops. There are two other common problems in the posted code: the fixed 26-char array is being overwritten by the 26-letter string (no room for the terminating NUL), and the coefficient arrays can contain garbage unless you initialize them. Also you need to treat the equation constant (RHS) as an extra "coefficient" so addition works correctly.

Use a vector sized num_var+1 and treat the last slot as the constant (RHS moved to the left). After adding the two vectors, print the result in the usual A x + B y = C form (compute C = -combined[last]). Example printing logic:

std::string vars = "abcdefghijklmnopqrstuvwxyz";
int rhs = -combined[num_var];     // combined last slot moved back to RHS
bool first = true;
for (int i = 0; i < num_var; ++i) {
  int c = combined[i];
  if (c == 0) continue;
  if (!first && c > 0) std::cout << '+';
  std::cout << c << vars[i];
  first = false;
}
std::cout << '=' << rhs << '\n';

To add two equations safely, initialize vectors to zero, move each equation's RHS into the last slot as a negative, then add elementwise:

std::vector<int> A(num_var+1,0), B(num_var+1,0);
// fill A[0..num_var-1] with coefficients; read RHS and set A[num_var] = -RHS
// same for B
for (int i = 0; i <= num_var; ++i) A[i] += B[i];
// print A with the code above

Practical tips: check num_var <= 26 (or accept custom variable names), use std::string for variable letters to avoid buffer overflow, and prefer vectors (they zero-initialize). was right to reserve a slot for the constant; keeping it as the last index tends to keep variable indices simple. With those fixes your example will print 5a-5b+2c=5.

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.