Why var x display incorrectly? - c++

#include <iostream> // подключаем библиотеку ввода-вывода
#include <cmath> // подключаем библиотеку математических функций
using namespace std;
int main()
{
float a, x, y = 0; // объявление переменных
cout << "Enter a variable: ";
cin >> a; // запрос на ввод a
cout << "Enter x variable: ";
cin >> x; // запрос на ввод x
cout << "a = " << a << ", x = " << x; // вывод a и x
y = (pow(x, 3) + sqrt(1 + pow(x, 3))) / (a + exp(x)); // формула расчета
cout << y;
}
Enter a variable: 6
Enter x variable: 7
a = 6, x = 70.327894
I can't understand why x == 70, when it must be 7. Also I don't know why cout << y; doesn't work. If I delete y = ... and cout << y, x display correctly.
I think, that it's my fault, because I'm new in C++ and don't know syntax well.

You're simply printing everything on the same line, you can either:
Print it after your two first result:
cout << "a = " << a << ", x = " << x; // вывод a и x
y = (pow(x, 3) + sqrt(1 + pow(x, 3))) / (a + exp(x)); // формула расчета
cout << ", y = " << y;
Output:
Enter a variable: 6
Enter x variable: 7
a = 6, x = 7, y = 0.327894
Or print y on another line
cout << "a = " << a << ", x = " << x << "\n"; // returns to another line
y = (pow(x, 3) + sqrt(1 + pow(x, 3))) / (a + exp(x)); // формула расчета
cout << "y = " << y;
Output:
Enter a variable: 6
Enter x variable: 7
a = 6, x = 7
y = 0.327894

a = 6, x = 70.327894
this doesn't mean x =70.327894.
x is 7 and the result y is 0.327894
you need to carefully print to the terminal, use << endl; and add some more labels so you can easily recognize the output of the math calculation...
bottomline: your rocket formula is working fine and you need to take another coffee... :)

Related

How can I make lists print horizontally in C++?

I used a for loop to generate a list of strings, and I've been trying to make them print horizontally instead of vertically.
Currently:
the list is printed vertically, up to 10.
Goal: To have the list printed horizontally.
Code for reference:
#include <iostream>
using namespace std;
int main()
{
int x = 10;
int y, z = 0;
int result;
for (z = 1; z <= x; z++)
{
cout << '\n';
for (y = 1; y <= z; y++)
{
result = z * y;
cout << z << " * " << y << " = " << result << '\n';
}
cout << '\n';
}
}
For the first block of number (1x1 .. 5x5), here's something similar to your expected output.
for (int x = 1; x <=5 ; x++) {
for (int y = 1; y <= 5; y++) {
if (y < x) {
cout << " ";
}
else {
cout << setw(3) << y << " x " << setw(4) << x << " = " << setw(4) << x * y << " ";
}
}
cout << endl;
}
Prints out this:
1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 4 x 1 = 4 5 x 1 = 5
2 x 2 = 4 3 x 2 = 6 4 x 2 = 8 5 x 2 = 10
3 x 3 = 9 4 x 3 = 12 5 x 3 = 15
4 x 4 = 16 5 x 4 = 20
5 x 5 = 25
I'll leave it as an exercise for extending this to another set of rows for the 6 through 10 times tables.

My question is about recurrence of numbers and sequences in C++ and how to print them?

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int i;
const int N = 5;
for (i = 1; i <= N; i++){
double Yn = (1.0 / 2) * (Yn - 1) + (1.0 / 3) * (Yn - 2);
std::cout << i << " " << "= "<< " " << Yn;
std::cout << std::endl;
}
return 0;
}
I have an equation and a table of sequences.
Equation
Y[n] = 1/2*(Y[n-1]) + 1/3*(Y[n-2])
Y[1] = 0.5, Y[2] = 0.4
The table shown below should be printed by using for loops.
N -- Yn
1 -- 0.5
2 -- 0.4
3 -- 0.366667
4 -- 0.316667
5 -- 0.280556
All I have to do is to print the above table .
You wrote down the sequence wrong.
You have:
Yn = 1/2*(Yn - 1) + 1/3*(Yn - 2)
But it should be:
Yn = 1/2*(Yn-1) + 1/3*(Yn-2)
So you need to keep track of the previous two numbers in the sequence to calculate the next one:
int i=1;
const int N = 5;
double Y_prev1 = 0.4;
double Y_prev2 = 0.5;
std::cout << i++ << " " << "= "<< " " << Y_prev2 << std::endl;
std::cout << i++ << " " << "= "<< " " << Y_prev1 << std::endl;
for (; i <= N; i++){
double Y = (1.0 / 2) * (Y_prev1) + (1.0 / 3) * (Y_prev2);
std::cout << i << " " << "= "<< " " << Y;
std::cout << std::endl;
Y_prev2 = Y_prev1;
Y_prev1 = Y;
}
Output:
1 = 0.5
2 = 0.4
3 = 0.366667
4 = 0.316667
5 = 0.280556

C++ Beginner Assignment Operators

i'm a beginner in C++ and i'm doing this example code of assignment operators. I don't know what i'm doing wrong here.
#include <iostream>
using namespace std;
int main()
{
int x = 200; int y = 100;
x += y; cout << x << endl; //x += y; // x = x + y, x = 200 + 100, output 300.
x -= y; cout << x << endl; //x -= y; // x = x - y, x = 200 - 100, output 100.
x /= y; cout << x << endl; //x /= y; // x = x / y, x = 200 / 100, output 2.
x %= y; cout << x << endl; //x % y; // x = % y, x = 200 % 100, output 0.
return 0;
}
i'm getting the results for
x -= y = 200
x /= y = 2
x %= y = 2
when its suppose to be
x -= y = 100
x /= y = 2
x %= y = 0
its actually adding up the previous results. How do i stop the code from adding the result to the next line? Thanks!
As you mentioned in your comments in your code, the result of x += y is equivalent to x = x + y, which means the value of x will change. So it is no surprise that the new value of x, 300, is used in the next mathematical assignment.
If you want to avoid that, consider saving the result of x + y to another variable.
int x = 200;
int y = 100;
int x_addition = x + y;
std::cout << x_addition << std::endl;
Or, if its only usage is in displaying the result of the addition, do it all in one line.
int x = 200;
int y = 100;
std::cout << x + y << std::endl;
When x and y are variables and U is an operation x U= y is equivalent to:
x = x U y;
which means the original variable is being modified and assigned with the result.
You code is equivalent to:
x = x + y;
cout << x << endl; // x is now 300
x = x - y;
cout << x << endl; // x is now 200
x = x / y;
cout << x << endl; // x is now 2
x = x % y;
cout << x << endl; // x is now 2
If you want not to change x you want can save x in a temporary variable or just print the result:
int x = 200;
int y = 100;
cout << x - y << endl; // x unchanged
cout << x + y << endl; // x unchanged
cout << x / y << endl; // x unchanged
cout << x * y << endl; // x unchanged
Or:
int x = 200;
int x = 100;
int result = x + y;
cout << result << endl;
result = x - y;
cout << x << endl;
result = x / y;
cout << result << endl;
result = x % y;
cout << result << endl;
Note: since the = operator returns the value after the assignment cout << x += y << end can compile and print the assigned value.
Every time you are doing assignment like x += y;, it changing the value of x.
x += y;
is equivalent to
x = x + y;
Which means assignment. Value of x is changing. It's now 300. Same thing happening for latter assignments too.
Note: if you don't want to change the value of x and y, better do the assignments in other variable.
int x = 200, y = 100, t;
t = x + y; cout << t << endl;
t = x - y; cout << t << endl;
t = x * y; cout << t << endl;
t = (int) x / y; cout << t << endl;
You are expecting the original value of x and y to be kept forever, but it isn't. Assignments like x += y; or x = x + y; change the value of the variables (in this case x, that is, the one on the left side of the equal sign). The previous value is overwritten and therefore lost.
If you don't want to change the original values, you have to either assign the result to other variables (for example, one called temp, that you can overwrite every time), or you don't assign it at all, and you simply send to cout the result of the calculation.
As an example with a temp variable:
int x = 200; int y = 100; int temp;
temp = x + y; cout << temp << endl; //temp = x + y, temp = 200 + 100, output 300.
temp = x - y; cout << temp << endl; //temp = x - y, temp = 200 - 100, output 100.
Or, doing away with variables entirely:
cout << x + y << endl; // 200 + 100, output 300
cout << x - y << endl; // 200 - 100, output 100
Another way to see that the variables are overwritten (well, in this case it's only x) is to declare them as const:
const int x = 200; const int y = 100;
The program won't compile, because it tries to change constant variables.
Try resetting x back to 200 after each assignment. Like this:
#include <iostream>
using namespace std;
int main()
{
int x = 200; int y = 100;
x += y; cout << x << endl;
x = 200;
x /= y; cout << x << endl;
x = 200;
x %= y; cout << x << endl;
return 0;
}
You are using the accumulation operators so you're changing the value of 'x'.
x += y; // x = 200 + 100 = 300
x -= y; // x = 300 - 100 = 200
x /= y; // x = 200 / 100 = 2
x %= y; // x = 2 % 100 = 2
Instead you could do:
int x = 200;
int y = 100;
cout << x + y << endl;
cout << x - y << endl;
cout << x / y << endl;
cout << x % y << endl;
Or:
int x = 200;
int y = 100;
int result;
result = x + y;
cout << result << endl;
result = x - y;
cout << result << endl;
result = x / y;
cout << result << endl;
result = x % y;
cout << result << endl;
You are not counting on Addition operations value
x=200,y=100;
x+=y;//x=200+100=300,x=300,y=100
x-=y;//x=300-100=200,x=200,y=100
Here you are forgetting that there is ADDITION OPERATION before subtraction

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

Operator Precedence Puzzle

I have variables with their initial value defined as: x = 10, y = 4, z = 1 and what's those variable values in y>>=x&0x2&&z ?
I would do:
y >>= ((Fun1) && z)
Fun1 = x&0x2, that is the bit operation of 1010 & 0010 = 0010, or 2 in decimal;
Fun1 && z returns 1, or 0001 in binary
Then my question is what is the operation of
y >>= 0001
gives me?
#include <iostream>
using namespace std;
int main()
{
int x = 10, y = 4, z = 1;
y >>= x&0x2&&z;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
cout << "z: " << z << endl;
return 0;
}
When in doubt use braces
When in doubt use braces
When in doubt use braces
y >>= 1
is the same as
y = y >> 1
So it should effectively integer-divide y by 2.