I'm working on homework for my c++ class we have just started arrays. I cannot figure out why the function reverse_array() does not print to the console when the function show_array() prints to the console without an issue. I've attempted googling the issue without success. I am just beginning c++ so it could be something small that I'm overlooking,. I appreciate any help.
#include<iostream>
using namespace std;
double dbval[5];
void fill_array(int x, double dbval[]);
void show_array(int x, double dbval[]);
void reverse_array(int x, double dbval[]);
int main(){
int x = 0;
fill_array(x,dbval);
show_array(x,dbval);
reverse_array(x,dbval);
return 0;
}
void fill_array(int x, double dbval[]){
int count = 0;
for (x = 0; x < 5; x++){
cin >> dbval[x];
if(!cin){
break;
}
}
for(x = 0; x < 5; x++){
count = count + 1;
}
cout << "Entries " <<int(count);
cout <<endl;
}
void show_array(int x, double dbval[]){
for (x = 0; x<5;x++){
cout << dbval[x] << " ";
}
cout << endl;
}
void reverse_array(int x, double dbval[]){
for(x = 5; x < 5; x--){
cout << dbval[x] << " ";
}
cout << endl;
}
This loop for(x = 5; x < 5 ; x--) failed because it did not satisfy your condition the first time it checked (x = 5, but it needs to be smaller than 5 to enter the loop)
Change to this:
void reverse_array(int x, double dbval[]){
for(x = 4; x >= 0 ; x--){
cout << dbval[x] << " ";
}
cout << endl;
}
So, i am currently doing a project. And while debuging i noticed that sometimes, a variable from the array changes value without me changing it.
The array is created to hold some values from tree structure.
int* tablica_synow1;
tablica_synow1 = (int*)malloc(sizeof(int) * number_of_sons(wskaznik_wierzcholki[zmienna1][i]));
//cout << "Initializing values to tablica_synow1" << endl;
for (int k = 0; k < ilu_synow(wskaznik_wierzcholki[zmienna2][j]); k++) {
if (k == 0)
tmp = wskaznik_wierzcholki[zmienna2][j]->son;
tablica_synow1[k] = tmp->key;
//cout << "tablica_synow1 [" << k << "]" << tablica_synow1[k] << endl;
tmp = tmp->brother;
}
//cout <<"end of initialization"<< endl;
after this I do some other things in my code that don't involde tablica_synow1
and then when i want to use it
for (int m = 0; m < ilu_synow(wskaznik_wierzcholki[zmienna2][j]); m++) {
int x = tablica_synow1[m];
cout << "tablica synow1 [" << m << "]" << tablica_synow1[m] << endl;
int y = i;
int a;
if (x > 0)
a = tablica2[y][zwroc_indeks(wartosci_tab[zmienna2], x)];
else
a = tablica4[y][x * (-1)];
if (a > najwieksza)
najwieksza = a;
}
the last element goes from 2 to -123781237 and it breaks my code
image
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
The purpose of this code is to add the elements in the two arrays, but in reverse order. I don't understand what I did wrong for this not to Compile(Syntax,Loop, or Array Error??). Could you point me in the right direction? Thank you!!
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
const int ARRAY1_LEN = 3;
const int ARRAY2_LEN = 2;
int MyInts1[ARRAY1_LEN] = { 35, -3, 0};
int MyInts2[ARRAY2_LEN] = {20, -1};
cout << "Multiplying each int in MyInt1 by each in MyInts2 ... But Backwards:" << endl;
for(int Array1Index = 0; Array1Index < ARRAY1_LEN - 1; Array1Index--);
for(int Array2Index = 0; Array2Index < ARRAY2_LEN -1; Array2Index--);
cout << MyInts1[Array1Index] << " x " << MyInts2[ Array2Index ] << " = " << MyInts1[Array1Index] * MyInts2[Array2Index] << endl;
return 0;
}
Your logic is not correct. You are starting at index 0 and then going backward. It means, that you are entering negative range (-1, -2, -3, ...) and every negative number satisfies loop condition.
It rather should be:
int main()
{
const int ARRAY1_LEN = 3;
const int ARRAY2_LEN = 2;
int MyInts1[ARRAY1_LEN] = { 35, -3, 0 };
int MyInts2[ARRAY2_LEN] = { 20, -1 };
cout << "Multiplying each int in MyInt1 by each in MyInts2 ... But Backwards:" << endl;
int start_1 = ARRAY1_LEN > 0 ? ARRAY1_LEN - 1 : 0;
int start_2 = ARRAY2_LEN > 0 ? ARRAY2_LEN - 1 : 0;
for(int Array1Index = start_1; Array1Index >= 0; Array1Index--)
{
for(int Array2Index = start_2; Array2Index >= 0; Array2Index--)
{
cout << MyInts1[Array1Index] << " x " << MyInts2[ Array2Index ] << " = " << MyInts1[Array1Index] * MyInts2[Array2Index] << endl;
}
}
return 0;
}
This code will also work properly if at least one of the arrays will be empty.
Oh, and one more thing: your code was also totally wrong, because you had semicolons (;) after for, which means, that every loop had an empty body. So, even if your fors were correct, you wouldn't see anything anyway.
There are two errors in your program:
The whole loop body consits only of the semicolons after the for loop instead of the code you actually want to run inside the loops. This also explains, why your code doesn't compile: your multiplication is not part of the loop bodies and as a result Array1Index and Array2Index - which are defined in the loop headers - do no longer exist.
Although you are decreasing the arry indices, you still start at 0, so you would access negative array indices.
So your code should actually look like this:
for (int Array1Index = ARRAY1_LEN - 1 ; Array1Index >= 0; Array1Index--){
for (int Array2Index = ARRAY2_LEN - 1; Array2Index >=0; Array2Index--){
cout << MyInts1[Array1Index] << " x " << MyInts2[Array2Index] << " = " << MyInts1[Array1Index] * MyInts2[Array2Index] << endl;
}
}
#include <iostream>
int main()
{
std::cout << " Enter the small number " << std::endl;
int v1 = 0;
std::cin >> v1;
std::cout << " Enter the large number " << std::endl;
int v2 = 0;
std::cin >> v2;
int x = 0;
while (x <= v2-1) {
x = v1+1;
++x;
}
std::cout << x << std::endl;
return 0;
}
This code gives no error but i couldn't get any output after input of two numbers. But, it works with a for loop like this;
{
for (int x = v1+1; x <= v2-1; ++x)
std::cout << x << std::endl;
return 0;
}
what is the mistake i should avoid using while loop ?
int x = 0;
while (x <= v2-1) {
x = v1+1;
++x;}
You get an infinite loop here, the value of x is always either v1+1, or v1+2.
use this:
int x = v1+1;
while (x++ <= v2-1) {
cout<<x;
}