Organizing text file output into columns - c++

Everything is working like it's supposed too, except when I output to the text file I can not figure out how to keep all the output lined up. My teacher wouldn't help me and I've literally been doing trial and error with "fixed, setprecision, left, setw(), etc" for a few hours now.
//Project #5
//Start Date: November 17th
//Due Date: November 23rd
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using namespace std;
double f = 3.14159;
double upperBound = 0;
double lowerBound = 0;
double increment = 0;
double tempVal = 0;
double powVal = 0;
double expVal = 0;
double fact = 0;
double sinVal = 0;
double x = 0.0;
char userCont = 'Y';
int n = 0;
int i = 0;
//Factorial function using recursion...
double factorial(const int n) {
if (n <= 1) return 1;
fact = n * factorial(n - 1);
return fact;
}
//Power function using recursion...
double power(const double x, const int n) {
if (n <= 0)
return 1;
powVal = x * power(x, n - 1);
return powVal;
}
//my_sin function using power and factorial functions
double my_sin(const double x) {
sinVal = 0;
for (int k = 0; k < 50; k++) {
sinVal += power(-1, k) * (power(x, 2 * k + 1) / factorial(2 * k + 1));
}
return sinVal;
}
//my_exp(x) Function
double my_exp(const double x) {
expVal = 0;
for (int k = 0; k < 50; k++) {
expVal += power(x, k) / factorial(k);
}
return expVal;
}
int main() {
ofstream fout("output.text");
while (userCont == 'y' || userCont == 'Y') {
cout << "Enter lower and upper bounds: ";
cin >> upperBound >> lowerBound;
cout << "Enter Increment: ";
cin >> increment;
//Checking if upper and lower bounds are in the right order...
if (upperBound < lowerBound) {
tempVal = upperBound;
upperBound = lowerBound;
lowerBound = tempVal;
}
fout << "x sin(x) my_sin(x) e(x) " <<
"my_el(x) my_exp(x)" << endl;
//Loop to display and increase x by the incrememnt
for (x = lowerBound; x <= upperBound; x = x + increment) {
fout.precision(7);
fout << setw(8) << left << x << " ";
fout << setw(8) << my_sin(x) << setw(8) << " ";
fout << setw(8) << sin(x) << setw(8) << " ";
fout << setw(8) << exp(x) << setw(8) << " ";
fout << setw(8) << exp(x) << setw(8) << " ";
fout << my_exp(x) << endl;
}
cout << "Another (y/n)? ";
cin >> userCont;
}
return 0;
}
Here's how it's supposed to look

Some cleanup and the best I could to mimick the formatting, hopefully it is of some inspiration/assistance.
Important Note how I removed all the global variables. They're bad :(
Live On Coliru
// Project #5
// Start Date: November 17th
// Due Date: November 23rd
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
double factorial(const int n) {
if (n <= 1)
return 1;
return n * factorial(n - 1);
}
double power(const double x, const int n) {
if (n <= 0)
return 1;
return x * power(x, n - 1);
}
// my_sin function using power and factorial functions
double my_sin(const double x) {
double sinVal = 0;
for (int k = 0; k < 50; k++) {
sinVal += power(-1, k) * (power(x, 2 * k + 1) / factorial(2 * k + 1));
}
return sinVal;
}
// my_exp(x) Function
double my_exp(const double x) {
double expVal = 0;
for (int k = 0; k < 50; k++) {
expVal += power(x, k) / factorial(k);
}
return expVal;
}
int main() {
ofstream fout("output.text");
char userCont = 'Y';
while (userCont == 'y' || userCont == 'Y') {
cout << "Enter lower and upper bounds: ";
double upperBound, lowerBound;
cin >> upperBound >> lowerBound;
cout << "Enter Increment: ";
double increment = 0;
cin >> increment;
// Checking if upper and lower bounds are in the right order...
if (upperBound < lowerBound) {
std::swap(upperBound, lowerBound);
}
fout << " x sin(x) my_sin(x) e(x) my_el(x) my_exp(x)" << endl;
// Loop to display and increase x by the incrememnt
for (double x = lowerBound; x <= upperBound; x = x + increment) {
fout.precision(7);
fout << right << std::fixed << std::setprecision(7) << setw(12) << x << " ";
fout << right << std::fixed << std::setprecision(7) << setw(12) << my_sin(x) << setw(4) << " ";
fout << right << std::fixed << std::setprecision(7) << setw(12) << sin(x) << setw(4) << " ";
fout << right << std::fixed << std::setprecision(7) << setw(12) << exp(x) << setw(4) << " ";
fout << right << std::fixed << std::setprecision(7) << setw(12) << exp(x) << setw(4) << " ";
fout << my_exp(x) << endl;
}
cout << "Another (y/n)? ";
cin >> userCont;
}
}
Output:
x sin(x) my_sin(x) e(x) my_el(x) my_exp(x)
0.0000000 0.0000000 0.0000000 1.0000000 1.0000000 1.0000000
0.1000000 0.0998334 0.0998334 1.1051709 1.1051709 1.1051709
0.2000000 0.1986693 0.1986693 1.2214028 1.2214028 1.2214028
0.3000000 0.2955202 0.2955202 1.3498588 1.3498588 1.3498588
0.4000000 0.3894183 0.3894183 1.4918247 1.4918247 1.4918247
0.5000000 0.4794255 0.4794255 1.6487213 1.6487213 1.6487213
0.6000000 0.5646425 0.5646425 1.8221188 1.8221188 1.8221188
0.7000000 0.6442177 0.6442177 2.0137527 2.0137527 2.0137527
0.8000000 0.7173561 0.7173561 2.2255409 2.2255409 2.2255409
0.9000000 0.7833269 0.7833269 2.4596031 2.4596031 2.4596031
1.0000000 0.8414710 0.8414710 2.7182818 2.7182818 2.7182818
1.1000000 0.8912074 0.8912074 3.0041660 3.0041660 3.0041660
1.2000000 0.9320391 0.9320391 3.3201169 3.3201169 3.3201169
1.3000000 0.9635582 0.9635582 3.6692967 3.6692967 3.6692967
1.4000000 0.9854497 0.9854497 4.0552000 4.0552000 4.0552000
1.5000000 0.9974950 0.9974950 4.4816891 4.4816891 4.4816891
1.6000000 0.9995736 0.9995736 4.9530324 4.9530324 4.9530324
1.7000000 0.9916648 0.9916648 5.4739474 5.4739474 5.4739474
1.8000000 0.9738476 0.9738476 6.0496475 6.0496475 6.0496475
1.9000000 0.9463001 0.9463001 6.6858944 6.6858944 6.6858944
2.0000000 0.9092974 0.9092974 7.3890561 7.3890561 7.3890561
2.1000000 0.8632094 0.8632094 8.1661699 8.1661699 8.1661699
2.2000000 0.8084964 0.8084964 9.0250135 9.0250135 9.0250135
2.3000000 0.7457052 0.7457052 9.9741825 9.9741825 9.9741825
2.4000000 0.6754632 0.6754632 11.0231764 11.0231764 11.0231764
2.5000000 0.5984721 0.5984721 12.1824940 12.1824940 12.1824940
2.6000000 0.5155014 0.5155014 13.4637380 13.4637380 13.4637380
2.7000000 0.4273799 0.4273799 14.8797317 14.8797317 14.8797317
2.8000000 0.3349882 0.3349882 16.4446468 16.4446468 16.4446468
2.9000000 0.2392493 0.2392493 18.1741454 18.1741454 18.1741454
3.0000000 0.1411200 0.1411200 20.0855369 20.0855369 20.0855369
3.1000000 0.0415807 0.0415807 22.1979513 22.1979513 22.1979513
3.2000000 -0.0583741 -0.0583741 24.5325302 24.5325302 24.5325302
3.3000000 -0.1577457 -0.1577457 27.1126389 27.1126389 27.1126389
3.4000000 -0.2555411 -0.2555411 29.9641000 29.9641000 29.9641000
3.5000000 -0.3507832 -0.3507832 33.1154520 33.1154520 33.1154520
3.6000000 -0.4425204 -0.4425204 36.5982344 36.5982344 36.5982344
3.7000000 -0.5298361 -0.5298361 40.4473044 40.4473044 40.4473044
3.8000000 -0.6118579 -0.6118579 44.7011845 44.7011845 44.7011845
3.9000000 -0.6877662 -0.6877662 49.4024491 49.4024491 49.4024491
4.0000000 -0.7568025 -0.7568025 54.5981500 54.5981500 54.5981500
4.1000000 -0.8182771 -0.8182771 60.3402876 60.3402876 60.3402876
4.2000000 -0.8715758 -0.8715758 66.6863310 66.6863310 66.6863310
4.3000000 -0.9161659 -0.9161659 73.6997937 73.6997937 73.6997937
4.4000000 -0.9516021 -0.9516021 81.4508687 81.4508687 81.4508687
4.5000000 -0.9775301 -0.9775301 90.0171313 90.0171313 90.0171313
4.6000000 -0.9936910 -0.9936910 99.4843156 99.4843156 99.4843156
4.7000000 -0.9999233 -0.9999233 109.9471725 109.9471725 109.9471725
4.8000000 -0.9961646 -0.9961646 121.5104175 121.5104175 121.5104175
4.9000000 -0.9824526 -0.9824526 134.2897797 134.2897797 134.2897797
5.0000000 -0.9589243 -0.9589243 148.4131591 148.4131591 148.4131591
5.1000000 -0.9258147 -0.9258147 164.0219073 164.0219073 164.0219073
5.2000000 -0.8834547 -0.8834547 181.2722419 181.2722419 181.2722419
5.3000000 -0.8322674 -0.8322674 200.3368100 200.3368100 200.3368100
5.4000000 -0.7727645 -0.7727645 221.4064162 221.4064162 221.4064162
5.5000000 -0.7055403 -0.7055403 244.6919323 244.6919323 244.6919323
5.6000000 -0.6312666 -0.6312666 270.4264074 270.4264074 270.4264074
5.7000000 -0.5506855 -0.5506855 298.8674010 298.8674010 298.8674010
5.8000000 -0.4646022 -0.4646022 330.2995599 330.2995599 330.2995599
5.9000000 -0.3738767 -0.3738767 365.0374679 365.0374679 365.0374679
6.0000000 -0.2794155 -0.2794155 403.4287935 403.4287935 403.4287935
6.1000000 -0.1821625 -0.1821625 445.8577701 445.8577701 445.8577701
6.2000000 -0.0830894 -0.0830894 492.7490411 492.7490411 492.7490411
6.3000000 0.0168139 0.0168139 544.5719101 544.5719101 544.5719101
6.4000000 0.1165492 0.1165492 601.8450379 601.8450379 601.8450379
6.5000000 0.2151200 0.2151200 665.1416330 665.1416330 665.1416330
6.6000000 0.3115414 0.3115414 735.0951892 735.0951892 735.0951892
6.7000000 0.4048499 0.4048499 812.4058252 812.4058252 812.4058252
6.8000000 0.4941134 0.4941134 897.8472917 897.8472917 897.8472917
6.9000000 0.5784398 0.5784398 992.2747156 992.2747156 992.2747156
7.0000000 0.6569866 0.6569866 1096.6331584 1096.6331584 1096.6331584
7.1000000 0.7289690 0.7289690 1211.9670745 1211.9670745 1211.9670745
7.2000000 0.7936679 0.7936679 1339.4307644 1339.4307644 1339.4307644
7.3000000 0.8504366 0.8504366 1480.2999276 1480.2999276 1480.2999276
7.4000000 0.8987081 0.8987081 1635.9844300 1635.9844300 1635.9844300
7.5000000 0.9380000 0.9380000 1808.0424145 1808.0424145 1808.0424145
7.6000000 0.9679197 0.9679197 1998.1958951 1998.1958951 1998.1958951
7.7000000 0.9881682 0.9881682 2208.3479919 2208.3479919 2208.3479919
7.8000000 0.9985433 0.9985433 2440.6019776 2440.6019776 2440.6019776
7.9000000 0.9989413 0.9989413 2697.2823283 2697.2823283 2697.2823283
8.0000000 0.9893582 0.9893582 2980.9579870 2980.9579870 2980.9579870
8.1000000 0.9698898 0.9698898 3294.4680753 3294.4680753 3294.4680753
8.2000000 0.9407306 0.9407306 3640.9503073 3640.9503073 3640.9503073
8.3000000 0.9021718 0.9021718 4023.8723938 4023.8723938 4023.8723938
8.4000000 0.8545989 0.8545989 4447.0667477 4447.0667477 4447.0667477
8.5000000 0.7984871 0.7984871 4914.7688403 4914.7688403 4914.7688403
8.6000000 0.7343971 0.7343971 5431.6595914 5431.6595914 5431.6595914
8.7000000 0.6629692 0.6629692 6002.9122173 6002.9122173 6002.9122173
8.8000000 0.5849172 0.5849172 6634.2440063 6634.2440063 6634.2440063
8.9000000 0.5010209 0.5010209 7331.9735392 7331.9735392 7331.9735392
9.0000000 0.4121185 0.4121185 8103.0839276 8103.0839276 8103.0839276
9.1000000 0.3190984 0.3190984 8955.2927035 8955.2927035 8955.2927035
9.2000000 0.2228899 0.2228899 9897.1290587 9897.1290587 9897.1290587
9.3000000 0.1244544 0.1244544 10938.0192082 10938.0192082 10938.0192082
9.4000000 0.0247754 0.0247754 12088.3807302 12088.3807302 12088.3807302
9.5000000 -0.0751511 -0.0751511 13359.7268297 13359.7268297 13359.7268297
9.6000000 -0.1743268 -0.1743268 14764.7815656 14764.7815656 14764.7815656
9.7000000 -0.2717606 -0.2717606 16317.6071980 16317.6071980 16317.6071980
9.8000000 -0.3664791 -0.3664791 18033.7449278 18033.7449278 18033.7449278
9.9000000 -0.4575359 -0.4575359 19930.3704382 19930.3704382 19930.3704382
10.0000000 -0.5440211 -0.5440211 22026.4657948 22026.4657948 22026.4657948

Related

Functions should be correct, but why is the final output 0 here?

#include <iostream>
#include <iomanip>
using namespace std;
//Power function
float power (float base, int exp)
{
if (exp < 0)
{
if (base == 0)
{
cout << "Base cannot be 0.";
return -1;
}
}
if (exp == 0)
return 1;
if (exp == 1)
return base;
return base * power (base, exp - 1);
}
//Factorial function
int facto (int n)
{
return n <= 0 ? 1 : n * facto (n - 1);
}
//Cos function
float cosCalc (float rad)
{
float cos = 0;
int x;
for (x = 0; x < 10; x++)
{
cos += power (-1, x) * power (rad, 2 * x) / facto (2 * x);
}
return cos;
}
//Sin function
float sinCalc (float rad)
{
float sin = 0;
int x;
for (x = 0; x < 10; x++)
{
sin += power (-1, x) * power (rad, 2 * x + 1) / facto (2 * x + 1);
}
return sin;
}
//Main function
int main()
{
int choice;
//Title and Menu
cout << endl << "==============" << endl << " TRIGONOMETRY " << endl << "==============";
cout << endl << "Select:";
cout << endl << "1. Calculate Cos and Sin";
cout << endl << "9. Exit";
while (true)
{
//User Prompt
cout << endl << endl << "Please enter your choice. => ";
cin >> choice;
if (choice == 1)
{
int angle, anglePh;
float rad;
float pi = 3.14159265358979323846264338327950288419716;
char angleType;
float cos = 0;
float sin = 0;
cout << endl << "Please enter an angle. => ";
cin >> angle;
anglePh = angle;
angle %= 360;
rad = angle * pi / 180;
cout << anglePh << " degrees = " << rad << " radian";
cout << endl << "Calculating Cos...";
cosCalc (rad);
cout << endl << "Cos = " << fixed << cos;
cout << endl << "Calculating Sin...";
sinCalc (rad);
cout << endl << "Sin = " << fixed << sin;
}
if (choice == 9)
{
break;
}
}
}
I am building a program that calculates Sin and Cos off an angle input, and when I run it, it outputs 0.000000 for both Sin and Cos. I suspect there is something to do with me declaring float cos = 0 and float sin = 0 in the if loop for choice == 1, and I tried messing around with it but it either results in the program straight out giving me errors on launch, or I get the same outputs.
Any idea where I went wrong?
Thanks for your insight in advance, cheers!
Your cosin and sine function return a float, but in order to get that result you still have to store it in a variable.
So instead of:
cosCalc (rad);
Do:
rad = cosCalc (rad);
and the same for your sine function.

Google Kickstart Wandering Robot wrong answer

Hi I wrote a short program to try to answer Google Kickstart 2020 Round B problem Wandering Robot. I essentially tried to implement the analysis provided by Google, and my solution seems to work on all the sample cases I've tried, but it never passes Google's first test set.
Here's my code, and below it the problem description and a short explanation of my approach. What I really want to know is what I'm doing wrong, why I'm not passing the first test set, more than anything.
#include <iostream>
#include<math.h>
using namespace std;
#include <iomanip>
int W, H, L, U, R, D;
const int len = 100000;
double facts [len];
void factorial(){
facts[0] = log2(1);
for(int i=2; i<= len; i++){
facts[i-1] = log2(i) + facts[i-2];
}
}
bool impossible(){
if((L==1 && R==W) || (U==1 && D==H)){
return true;
}
return false;
}
double pass(int x, int y, int s, int t, int i){
double num = log2(x+y);
num = facts[x+y-1];
double prob = x+y;
double tot = 0;
double comb = 0;
//cout << x << " " << s << " " << y << " " << t << endl;
while(x!=s && y!=t){
double denom = facts[x-1] + facts[y-1];
comb = num - denom - prob;
// cout << num << endl;
tot += pow(2, comb);
x+=i;
y-=i;
}
//lower diagonal, hit the bottom
//x+1 = number of moves it takes
//to hit last square
if(y==H){
double p = pow(2,comb);
tot -= p;
x-=i;
for(int j=1; j<=x; j++){
p -= pow(0.5, prob);
p += pow(0.5, prob-j-1);
}
tot += p;
}
//upper diagonal, hit the right side
else if(x==W){
double p = pow(2,comb);
tot -= pow(2, comb);
y+=i;
for(int j=1; j<=y; j++){
p -= pow(0.5, prob);
p += pow(0.5, prob-j-1);
}
tot += p;
}
return tot;
}
int main() {
int t;
cin >> t; // read t. cin knows that t is an int, so it reads it as such.
for (int i = 1; i <= t; ++i) {
cin >> W >> H >> L >> U >> R >> D;
double tot = 0;
if(impossible()){
cout << "Case #" << i << ": 0.0" << endl;
}
//upper diagonal
else if(L==1 || D==H){
factorial();
tot = pass(R, U-2, W, -1, 1);
cout << "Case #" << i << ": " << fixed << setprecision(9) << tot << endl;
}
//lower diagonal
else if(R==W || U==1){
factorial();
tot = pass(L-2, D, -1, H, -1);
cout << "Case #" << i << ": " << fixed << setprecision(9) << tot << endl;
}
//symmetric
else if(D==R && L==U){
factorial();
tot = pass(R, U-2, W, -1, 1);
tot *=2;
cout << "Case #" << i << ": " << fixed << setprecision(9) << tot << endl;
}
//call count both
else{
factorial();
tot = pass(R, U-2, W, -1, 1);
tot += pass(L-2, D,-1, H, -1);
cout << "Case #" << i << ": " << fixed << setprecision(9) << tot << endl;
}
}
return 0;
}
The way that I tried to solve it was to find the probability of passing through the green squares and the reciprocal squares spanning from the upper right corner of the hole to the right edge of the arena.

Stack around the array is corrupted

I am using Visual Studios 2015, and I ran into a problem. Run-Time Check Failure #2 - Stack around the variable 'myArray' was corrupted. I am not sure where in the program that could cause some sort of corruption to my array. But when I did calculations involving manipulating the array, several numbers turned to 0.0000 instead of what they were originally. Could someone help?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double xmin, xmax;
const int POINTS = 20;
const double PI = 3.1416;
double increments;
double range, mean;
double total = 0;
double myArray[POINTS];
double number = myArray[0];
double mode = number;
int count = 1;
int countMode = 1;
cout << "Enter in a value for the minimum x value: ";
cin >> xmin;
cout << "Enter in a value for the maximum x value: ";
cin >> xmax;
if (xmin < 0)
increments = (abs(xmin) + xmax) / POINTS;
else
increments = (xmax - xmin) / POINTS;
int i = 0;
double x = xmin + increments * i;
double minimum = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
double maximum = myArray[19];
cout << setw(15) << "x |" << setw(15) << "f(x)" << endl;
cout << setw(32) << setfill('-') << " " << endl;
cout << setfill(' ');
for (i = 0; i <= POINTS; i++)
{
myArray[i] = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
x = xmin + increments * i;
cout << fixed << showpos << setw(15) << setprecision(2) << x << setw(15) << setprecision(4) << myArray[i] << endl;
if (myArray[i] <= minimum)
minimum = myArray[i];
if (myArray[i] > maximum)
maximum = myArray[i];
}
cout << endl;
range = maximum - minimum;
for (int count = 0; count <= POINTS; count++)
{
total += myArray[count];
}
mean = total / POINTS;
int temp;
bool swap;
do
{
swap = false;
for (int i = 0; i < POINTS - 1; i++)
{
if (myArray[i] > myArray[i + 1])
{
temp = myArray[i];
myArray[i] = myArray[i + 1];
myArray[i + 1] = temp;
swap = true;
}
}
} while (swap);
for (int i = 0; i <= POINTS; i++)
{
if (myArray[i] == number)
{
count++;
}
else
{
if (count > countMode)
{
countMode = count;
mode = number;
}
count = 1;
number = myArray[i];
}
}
cout << "The maximum value is: " << maximum << endl;
cout << "The minimum value is: " << minimum << endl;
cout << "The range is: " << range << endl;
cout << "The mean value is: " << mean << endl;
cout << "The median value is: " << (myArray[9] + myArray[10]) / 2 << endl;
cout << "The mode value is: " << mode << endl;
for (i = 0; i <= POINTS; i++)
cout << myArray[i] << endl;
system("pause");
return 0;
}
double myArray[POINTS];
myArray is an array of 20 doubles - myArray[0] through myArray[19].
for (i = 0; i <= POINTS; i++)
{
myArray[i] = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
This sets myArray[0] through myArray[20]. Accessing myArray[20] is a not allowed, because that is the 21st element of a 20-element array.
Note that the compiler won't always be nice enough to detect this problem for you. Visual C++ is doing you a favour here by causing the program to crash, believe it or not.

I want the average of the two biggest variables among the three variables n1, n2, n3. Can someone help me.

I want the average of the two biggest variables among the three variables n1, n2, n3. Can someone help me. I ask the user to enter three notes will be stored in variables n1, n2, n3. then I want the program to return the average of the two biggest variables.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
float ra[23], qte_alunos=0;
float n1[29],n2[33],n3[33],op1[22],op2[22], fina[22];
string nome[23], curso[23];
for (int i=0; i<3; i++){
cout << "digite RA: ";
cin >> ra[i];
cout << "digte nome: ";
cin >> nome[i];
cout << "digite curso: ";
cin >> curso[i];
cout << "digite N1: ";
cin >> n1[i];
cout << "digite N2: ";
cin >> n2[i];
cout << "digite N3: ";
cin >> n3[i];
if (n1[i] > n2[i] && n2[i] > n3[i]){
n1[i] = op1[i];
n2[i] = op2[i];
}
if (n2[i] > n3[i] && n3[i] > n1[i]){
n2[i] = op1[i];
n3[i] = op2[i];
}
if (n3[i] > n1[i] && n1[i] > n2[i]){
n3[i] = op1[i];
n1[i] = op2[i];
}
fina[i] = (op1[i]+op2[i])/2;
if (fina[i] > 6 ){
cout << "aprovado " << fina[i];
}
if (fina[i] > 4 && fina[i] < 5.9){
cout << "exame " << fina[i];
}
if (fina[i] < 4){
cout << "reprovado " << fina[i];
}
cout << "\n" << endl;
}
return 0;
}
If I get you right you want (sum(a, b, c) - min(a, b, c)) / 2:
#include <algorithm>
#include <iostream>
int main (int argc, const char **argv) {
double a = 1;
double b = 2;
double c = 3;
double min = std::min({a, b, c});
// double max = std::max({a, b, c});
double sum = a + b + c;
// double result = ((sum - min - max) + max) / 2;
// which is:
double result = (sum - min) / 2;
std::cout << result << '\n';
}

My c++ code for factoring isn't working

I am trying to create a c++ program that when I input two numbers (num1, combinationNum), it finds two numbers that multiply together to equal num1, but add together to equal combinationNum. It currently works for positive integers, but not negative. How do I make it work with negative integers? Also, If the equation isn't solvable, I would like it to print an error of some sort. Thanks!
Code:
//
// main.cpp
// Factor
//
// Created by Dani Smith on 2/13/14.
// Copyright (c) 2014 Dani Smith Productions. All rights reserved.
//
#include <iostream>
#include <cmath>
using namespace std;
void factors(int num, int comNum){
int a, b;
cout<<"The factors are ";
bool isPrime = true;
int root = (int)sqrt((double)num);
for(int i = 2; i <= root; i++){
if(num % i == 0 ){
isPrime = false;
//cout<<i<<",";
for(int x = 0; x<3; x++){
if(x==1){
a = i;
}
else if(x == 2){
b = i;
}
if(a + b == comNum){
cout << a << ", and " << b << ".";
}
}
}
}
//----------------------------------------
if(isPrime)cout<<"1 ";
cout<<endl;
}
int main(int argc, const char * argv[])
{
int num1 = 0, num2 = 0, multiple = 0, combinationNum = 0, output1 = 0, output2 = 0;
cout << "What number do you want to factor?\n";
cin >> num1;
cout << "What do you want them to add to?\n";
cin >> combinationNum;
factors(num1, combinationNum);
return 0;
}
To solve:
x + y == a
x * y == b
You have to solve
y == a - x
x * x - a * x + b == 0
So with delta == a * a - 4 * b, if delta positive, the solutions are
x1 = (a + sqrt(delta)) / 2
x2 = (a + sqrt(delta)) / 2
The code : (https://ideone.com/qwrSwa)
void solve(int sum, int mul)
{
std::cout << "solution for x + y = " << sum << std::endl
<< " x * y = " << mul << std::endl;
const int delta = sum * sum - 4 * mul;
if (delta < 0) {
std::cout << "No solution" << std::endl;
return;
}
const float sqrtdelta = sqrtf(delta);
const float x1 = (sum + sqrtdelta) / 2.f;
const float x2 = (sum - sqrtdelta) / 2.f;
std::cout << "x = " << x1 << ", y = " << sum - x1 << std::endl;
if (delta != 0) {
std::cout << "x = " << x2 << ", y = " << sum - x2 << std::endl;
}
}