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.
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 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.
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 6 years ago.
Improve this question
I have a task to make a code which will write 100 first numbers of an equation (or a function, I don't know what this is)
A(n) = (A(n-1))^2 -n*A(n-2) where A(1) = 1 and A(2) = 1
It has to be solved recursively. I have written this code so far
#include <iostream>
using namespace std;
int rekurzija(int n){
if(n=1){
return 1;
}
if(n=2){
return 1;
}
if(n>2){
return rekurzija(n-1)*rekurzija(n-1)-n*rekurzija(n-2);
}
}
int main(){
for(int n=1;n<101;n=n+1){
cout << rekurzija(n) << endl;
}
}
The problem is that the program returns 1 hundred times instead of 1,1,-2,0,...(instead of actually solving this function). What is wrong in this code?
You are using simple assignment operator = instead of Is equals to relational operator == in your rekurzija() function for if conditions
if(n = 1) //here `n = 1`is an assignment statement
{
//something...
}
What happens if you use = instead of ==?
The if condition will always evaluate to be true if the assigned value in the assignment statement is non-zero number.
Note: An assignment to zero evaluates to be false i.e, for if(n = 0), the if block will not be entered. You don't have any such if blocks in your code.
So your first if is always evaluated to be true because you are assigning a non-zero value i.e, 1 and thus your function always returns 1. that's the reason why you get 100 1's as your answer.
So, instead try changing all the if conditions to something like:
if(n == 1)
{
//something...
}
This would check if n is equals to 1 or not. If n is equal to 1 then the if block is entered, else it would not enter the if block and the next if condition is checked.
Note: Just remember this while using the = and == operators
= is for assignment
== is for comparison
When you compare things in C++ you need to do it like:
if (a == b)
and not
if (a = b)
The latter will assign b to a and return the value of a.
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 6 years ago.
Improve this question
I'm trying to generate and print out 5 numbers from 1 to 5, but not in sequence. I'm using a self-written function, 'appearBefore' that will check whether the number has appeared before.
The function appearBefore will return '0' if the number has not appeared before, and '1' if the function has appeared before.
At the moment, the do-while loop doesn't get out of the loop even when 0 is returned. The program never ends. Any recommendations on what I can do?
EDITS - The downvotes sure comes fast. I have added the counter++, but it still does not work. Perhaps someone can advice on the inner-loop?
while (count < 5) {
repeat = 1;
do {
randomNumber = rand() % 4 + 1;
cout << randomNumber;
repeat = appearBefore(randomNumber);
cout << " " << repeat << endl;
} while (repeat == 1);
//Add the number into an array of numbers that have appeared before
checker[counterForChecker] = randomNumber;
counterForChecker++;
counter++;
}
This is the function appearBefore (the variables are global variables):
int appearBefore(int number) {
int x = 0;
int match = 0;
while (x < counterForChecker+1) {
if (checker[x] == number) {
match = 1;
break;
}
else {
match = 0;
}
x++;
}
return match;
}
You check for count < 5 while increasing counterForChecker.
Set the while condition to
while (counterForChecker < 5)
or increase the counter
counter++; // counterForChecker++;
(Assuming that counter++; actually says count++;...)
If x and k are positive, x % k is a number between 0 and k - 1.
So you have four possible values to choose from (1,2,3,4), and you're looping until you've found five unique values.
That will never end well.
To generate numbers from 1 to 5, use rand() % 5 + 1;
You'll want to change the variable count. This is now not done, so since the value does not change the loop will not end.
The loop is running while count < 5 and you never increment count.
Did you mean to use:
while (counterForChecker < 5)
If not, add:
++count;
at the end of the while loop
Use a debugger and step through the code, looking at the values of the variables as you do so. You'll learn more about how everything is working.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
That is the code:
point [1][2][1] = 3;
cout << point[1][2][1] << endl;
point [1][3][0] = 4;
cout << point[1][2][1] << endl;
And that is what the console says when I run the application:
3
4
If I change to
point[1][3][0] = 5;
it says
3
5
How can I remove this annoying error? I cant continue that way.
When your variable is declared as
int point[100][100][1];
Then the valid indexes are respectively 0...99, 0...99, 0...0.
Your access to point[1][2][1] is therefore quite inappropriate. Depending on which index you make out of range, you might access an area outside the array entirely, or in a different slice of the array.
If you really want to access array elements arbitrarily, then I suggest you discard the triple-subscript notation and use:
int point[m][n][p];
int* p = &point[0][0][0];
p[x*n*p + y*p + z]
Now you are in control over row-major vs column-major access, and any computation that yields an offset less than m*n*p is valid.
Note that in your case m=n=100 and p=1, so that point[1][3][0] is p[1*100*1 + 3*1 + 0] = p[103] and also point[1][2][1] is p[1*100*1 + 2*1 + 1] = p[103]. So both really are the same location.