Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Trying to write a function that sees if the number in the array is increasing or decreasing compared to the previous.
Getting an infinite loop.
for(int col=0; col < 5; col++) {
newArray[col][0] = printthis[col][0];
for(int row = 2; row < 5; row++) {
cout << col << "\t" << row << "\n";
if(stoi(printthis[col][row]) > stoi(printthis[col][--row])) {
newArray[col][row] = "Up";
}
else {
newArray[col][row] = "Down";
} //if else
}//inner loop
}
Here the loop index is decreased, so it will always stay at value 2, note the --row:
if(stoi(printthis[col][row]) > stoi(printthis[col][--row])){
You probably want:
if(stoi(printthis[col][row]) > stoi(printthis[col][row-1])){
Also the loop should probably start at row = 1 instead of 2, to compare to the first row instead of second.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I tried to write a simple code that gives all 3 chars combinations of alphabet letters, but it doesn't read the ASCII code (90) character which is 'Z' . Also why letters repeat in combinations in any 90 line?
Here is the code:
{
int i, j, k;
char v[90];
for(i=65; i<=90; ++i)
v[i]=(unsigned char)i;
for(i=65; i<=90; ++i)
{
for(j=65; j<=90; ++j)
if(i!=j)
{
for(k=65; k<=90; ++k)
if(j!=k && i!=k)
cout<< v[i] << v[j] << v[k] << ' ';
cout<<endl;
}
cout<<endl;
}
return 0;
}
The top element of a 90-element array v is v[89].
You can't use v[90]; it doesn't exist.
Either make it char v[91] instead, or change the way you use arrays (there's no need for so many elements here).
char v[90] makes an array with 90 elements, numbered 0 through 89. By accessing element 90 of the list, you are accessing bits of memory used by other things, and thus you can't expect what you put in there to stay there. Initializing the array as 91 elements long should fix both problems.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Could you guys please walk and help me with this practice question?
I'm unable to figure out how the answer is 5.
int arr[12] = { 1,3,5,0,7,2,0,4,4,0,8,8 };
int count = 0;
for (int i = 0; i<11; i++) {
if (arr[i] = arr[i + 1])
count++;
else
count--;
}
cout << count << endl;
In your example you have :
if (arr[i] = arr[i + 1])
which is the =, not ==. It is assigning not checking for equality. So in the example:
if (a = 3) {
You will assign a to 3 and check if 3 is true, which it is. This leads to an easy look at why the answer is 5:
arr=> { 1,3,5,0,7,2,0,4,4,0,8,8 };
count=> 1,2,1,2,3,2,3,4,3,4,5
And if you are interested, look at the array after you have completed. It will look like this:
{3,5,0,7,2,0,4,4,0,8,8,8} // Everything has been moved down 1 (except for the final member)
See a live example of this here.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm trying to understand how this following code runs without any infinite loops.
int main()
{
int count = 1;
for (;count <= 5; count++)
{
int count = 1;
cout << count << "\n";
}
system("pause");
return 0;
}
The inner int count = 1 in the loop's body declares a new variable with a value of 1, which is distinct from the loop's count.
The variables count are not the same, they have different scopes:
int count = 1; ///--------------- Scope 1
for (;count <= 5; count++)
{ ///---------------------------- Scope 2
int count = 1;
cout << count << "\n";
} ///---------------------------- End Scope 2
///------------------------------ End Scope 1
So the int count = 1 in Scope 2 is used in the cout because it is resolved as the lowest level scope. But one the for loop scope is finished, this count is destroyed and the count declared in scope 1 is used. This is why you dont get an infinite loop.
The second count is not the same as the first count. You declare a new variable with a new memory address. Say that the memory address for the first count is a. The memory address for the second count is then b. So a variable at memory address b is equal to 1, but it does in no way affect the count at address a. You can check this by writing cout << &count << "\n" both before and after you declare a new count. This will give you their memory address.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Couldn't find an answer on google because I didn't know how to phrase is.
I have a regular function as below and would like to update the variable number in the first if statement. I've tried all sorts of combos but nothing works.
int main()
{
int apple, number;
cout << "Enter you number"<< endl;
cin >> apple;
if (apple == 1){
number = 2;
}
else {
number = 3;
cout << number << endl;
}
How would I change the above so I get 2 to output to the screen?
Thanks in advance!
You need to use
if (apple == 1)
instead of
if (apple = 1)
== is used for comparison. Also to note that your code will always assign the value 2 to the variable apple as in your condition you are not comparing rather you are assigning. So in your case the output will always be 2.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
In order to convert a given number to binary I wrote this code
//Binary conversion
int num,count=0;
int bi[15];
cout<<"Enter number";
cin>>num;
while(num>=1){
bi[count]=num%2;
num=num/2;
count++;
}
for(int i=0;i<=count;i++){
cout<<bi[count-i];
}
But the answer is wrong.It gives a -85993460 at the front.
If I want to convert 10 the result would be -859934601010.
Can someone please point out what's wrong with this code
When i is zero, the expression count-i is one position after the last entry of the array; this is undefined behavior, so an arbitrary number, such as -85993460, can be printed, or the program could crash.
To print your array backwards, use bi[count-1-i] instead, and end the loop upon reaching count:
for(int i=0 ; i != count ; i++) {
cout<<bi[count-1-i];
}
Your loop limits are off-by-one - the loop should be
for(int i=1;i<=count;i++){
cout<<bi[count-i];
}