My goal for this program is to get input from the user for value 'y' and then multiply that value by 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and then print the 10 results. Right now, when the user enters a value for 'y', the program just returns a list of zeros. Any idea what I'm doing wrong?
#include <iostream>
using namespace std;
int main(){
int x;
int y;
int z;
x = 0;
z = x * y;
cout << "enter y" << endl;
cin >> y;
while (x < 10) {
cout << z << endl;
x = x + 1;
}
}
The problem you have is that you don't actually calculate z inside the loop, so it will always be the value you calculated outside the loop. Statements and expressions can't be done retroactively.
But you have a worse problem than that, because you use y before you initialize it, which leads to undefined behavior.
You need to change the location of the y input to be before the calculation of z that should be inside the loop
The statement z = x * y should be inside your while loop for it to work.
What is happening now is that,each time it is printing the same value of z which is calculated initially as x(which is 0) and y(which is some garbage value as it has not been initialized) to give 0
The first problem occurring in your code is that try to assign z by multiplying x with y, but y is not assigned to a certain value => ERROR! That means you should get your user input before you assign z.
You may wonder why you get ten outputs of '0': Inside the loop you only print the value assigned to z to the console but z is already assigned before and not changed during the repetition.
#include <iostream>
using namespace std;
int main(){
int x;
int y;
int z;
x = 0;
z = x * y; // y is never assigned to a value
cout << "enter y" << endl;
cin >> y; // y got assigned here!
while (x < 10) {
cout << z << endl; // z stays the same in the loop
x = x + 1;
}
}
Related
So essentially, I'm trying to code a small block that should create or generate coefficients for polynomial n-degree that can be represented through vector which is a=[a0, a1..an] given the basic formula is
Well, the issue is when doing a function to get value of polynomial P from point "x" it returns value from entirely using Horner's Rule which it's not the same result as intended although not sure which one I should put on. The math basic explanation tells me at least something out of:
E.g: n=2; (A[i] stack terms by 4, 2, 1) and calculates P(value of x) = 4 * x ^ 0 – 2 * x ^ 1 + 1 * x ^ 2 = 4 – 2x + x2 = x2 – 2x + 4 = 4
With other words , can't find the culprit when for "x" value is meant to go through "i" variable numbered wrong by exponent and the result gets output for P(0)=7 while it shouldn't be and concrete as in P(0) = 0 ^ 2 – 2 * 0 + 4 = 4
Here's a little snippet went through so far, I would appreciate if someone could point me in the right direction.
double horner(const double&x, const int& n, const double& nn) {
if (n < 0)
return nn;
else {
double m; cin>>m;
return horner(x, n-1, nn*x+m);
}
}
int main() {
int n;double x;
cout << "n=";cin >> n;
cout << "x=";cin >> x;
cout << "P(0)=" << horner(x, n, 0);
return 0;
}
Edit: My brain farted for a moment somewhere while was coding and continuously revising the case, I forgot to mention what exactly are the parts of each variables for the program to avoid confusion yes, so:
n, polynomial grade;
p, polynomial coefficient;
x, the point in which evaluates;
And here given the grade equation to polynomial
which any negative and positive input terms adding by exponent to these coefficients are the steps that holds the result exception, hence the reason Horner's rule that it reduces the number of multiplication operations.
Edit: After few hours, managed to fix with polynomial evaluating issue, the only question remains how I'd suppose to generate coefficients using method std::vector ?
float honer(float p[], int n, float x)
{
int i;
float val;
val = p[n];
for (i = n - 1; i >= 0; i--)
val = val * x + p[i];
return val;
}
int main()
{
float p[20]; // Coefficient of the initial polynomial
int n; // Polynomial degree -n
float x; // Value that evaluates P -> X
cout << "(n) = ";
cin >> n;
for (int i = n; i >= 0; i--)
{
cout << "A[" << i << "]=";
cin >> p[i];
}
cout << "x:= ";
cin >> x;
cout << "P(" << x << ")=" << honer(p, n, x);
//Result (input):
//n: 2,
//P[i]: 4, -2, 1 -> x: 0, 1
// Result (output):
//P() = 4
}
Expect some certain output scenarios given below input if assigned:
I saw some codes on the web and trying to figure out how this works. I tried to leave comments on each lines but I cannot understand why y[0] changes to 5555. I'm guessing y[0] might change to numbers[0], but why?
x value is still 1. Well.. is this because y[0] = 1; has no int data type?
#include
using namespace std;
void m(int, int []);
/*this code explains a variable m that m consists of two parts, int and int array*/
int main()
{
int x = 1; /* x value is declared to 1*/
int y[10]; /*Array y[10] is declared but value is not given*/
y[0] = 1; /*Array y's first value is declared to 1 but data type is not given*/
m(x, y); /*This invokes m with x and y*/
cout << "x is " << x << endl;
cout << "y[0] is " << y[0] << endl;
return 0;
}
void m(int number, int numbers[]) /*variable names in m are given, number and numbers.*/
{
number = 1001; /*number has int 1001 value*/
numbers[0] = 5555; /*This overrides y to numbers[], so y[0] =1 changes to numbers[0] = 5555.*/
}
/*This program displays
* x is 1
* y[0] is 5005
* y[0] value has changed but x has not.
* */
I'm guessing y[0] might change to numbers[0], but why? x value is still 1.
Don't guess please. Your code works as commonly expected.
number = 1001; doesn't influence x in any way.
number is a local copy (as passed by value).
numbers decays to a pointer to the 1st element of the original array, thus it is changed outside the functions scope.
Well.. is this because y[0] = 1; has no int data type?
No, as explained above. y[0] actually is of type int.
int numbers[] is almost equivalent to int* numbers in this situation. You are not passing the vector as an immutable object but as a reference. So both numbers ( the local variable from the function ) and y ( the local variable from main ) will be pointing to the same memory address.
#include <iostream>
using namespace std;
int main()
{
double x = 1;
double y = 2;
int i = 1;
do
{
y /= 2.0;
x+= y;
++i;
cout << i;
}
while (x < 2.4);
}
I thought the output would be 2, but that is not correct. Can someone explain why?
Have a look at the condition which controls whether or not your loop will execute again. At the end of the first loop, the value of x is 2.0. Since this is less than 2.4, the loop runs a second time. In total that means i is incremented twice raising it to 3.
So in the following bit of code, I've been trying to figure out as to why the output of the code comes out to be...
X = 2 and Y = 2
when I originally thought it would be x = 1 and y = 1. I'm still a lil mind boggled with C++ coming into the semester and a little explanation with someone with more knowledge than me on this can hopefully mesh this concept into my head.
int main()
{
int x = 0;
int& y = x;
y++;
x++;
std::cout << "x = " << x << " y = " << y << std::endl;
}
x and y are not different than each other. Reference means the other name of x is y. So when you call y it calls x which means if you increase y it increases x. Then you increase x again and x becomes 2. And because y represents x, when you call y it calls x and you see 2 again.
key point is what the reference sign meant:
int& y = x;
It represent that you are assigning an alias to 'x', so 'y' is actually sharing the same memory address as 'x' do (physically).
doing
y++;
will change the value inside that memory address, which is shared by both 'x' && 'y'. Same as the operation
x++;
As a result, you did 2 increment on the same memory address with intial value of '0', which the value becomes '2'.
same idea, since both 'x' and 'y' are pointing to that exact same memory address, printing 'x' and 'y' will give you the same value.
I have made the following code, whose output should generate a point uniformly at random on the unit circle centered at the origin:
#include "unif.h"
#include <iostream>
#include <cmath>
using namespace std;
void point_on_circle(double& x, double& y)
{
double r;
do
{
double x = unif(-1.,1.);
double y = unif(-1.,1.);
double r = x*x + y*y;
}
while (r >=1.0);
x = x / sqrt(r);
y = y / sqrt(r);
}
int main()
{
cout << "Pair of points on the circle found is " << x << " and " << y << endl;
cout << "Let's verify: x^2+y^2=" << x*x+y*y << endl;
return 0;
}
The header "unif.h" is just a file that contains a function void unif(double x, double y), that produces uniformly random numbers in the interval (x,y), and it works perfectly (already tested).
The problem is that when I build the program then it gives me (of course) the error in the main:
"error: 'x' was not declared in this scope"
which is clear since of course x is defined outside the main and never defined in main(). I cannot figure out how to tell the compiler that the values of x and y found by the function point_on_circle should be "carried" inside the main. How could I fix this code?
Thanks in advance
In your main method you did not declare a variable called x nor y. Moreover, you also have scoping issues in your point_on_circle(double& x, double& y) function with the variable r.
Please review C++ scoping.
Because you defined x in the do-while loop, so you cannot use it outside the loop, since those definitions hide the parameters x and y. Define it before the loop:
void point_on_circle(double& x, double& y)
{
double r;
do
{
x = unif(-1.,1.);
y = unif(-1.,1.);
r = x*x + y*y;
}while (r >=1.0);
x = x / sqrt(r);
y = y / sqrt(r);
}
You have a few issues.
1) you need to declare x and y inside main.
2) you never, ever actually call point_on_circle. At all.
3) finally, as others noted, you mask parameters x and y in your do loop.
With all of that said, it looks like you're attempting to find a random point on the unit circle. with that in mind, I would remove the do loop entirely and just do this:
void point_on_circle(double& x, double& y)
{
double r;
x = unif(-1.,1.);
y = unif(-1.,1.);
r = x*x + y*y;
x = x / sqrt(r);
y = y / sqrt(r);
}
It gives the exact same result while avoiding a (potential) endless loop, and certainly avoids useless extra processing.