Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
The following code results in an infinite loop. The value of a does not seem to be going lower than 1. Please help.
int main()
{
init(4);
return 0;
}
void init(int a)
{
int j;
cout<<"After declaring: "<<j;
j = a;
cout<<"After initializing: "<<j;
a--;
while(a>0)
{
init(a);
}
}
First, you are accessing an uninitialized variable. This introduces undefined behaviour into your program; I'd remove this section.
Second, your while-loop runs as long as a > 0, but in the body of the loop, the value of a is never changed. Note that when calling init, parameter a is passed by value, i.e. the value of a is copied; Each instance of init will get it's own a, and it will never change the value of the caller's a.
Recursive functions usually are used instead of a loop. Nevertheless, you need an "anchor" to tell the recursion where to stop:
void init(int a)
{
int j = a;
cout<<"After initializing: " << j;
if (a>0) {
init (a-1);
}
}
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 1 year ago.
Improve this question
While doing a program, I came up with an unintended behaviour in dynamic arrays. After I did a few tests, I ended up with this code.
#include <iostream>
unsigned long index;
template<typename T>
class A
{
public:
void Set()
{
T* target = new T[index];
for (unsigned int i = 0; i < index; i++)
{
target[i] = i;
}
this->value_ = target;
index++;
}
T* value_;
};
int main()
{
A<int> test;
test.Set();
std::cout << test.value_[0] << '\n';
test.Set();
std::cout << test.value_[1];
}
Returning the following values:
11104472
11075776
The values change each time I execute the program, however, I thought the output would be "0" and "1". I suppose they are memory addresses, but why is the program returning them and not the expected output?
Just look which values index will have. It is initialized with 0. So in the first test.Set(); you create an 0 length array. To even read it later is wrong and leads to so called undefined behavior. In the second call of set, index will have a value of 1, so you create an array of length 1 and initialize that first element on position 0 correctly with 0, but you then try to print out test.value_[1] which is not the first element but the second. Remember array indices start with 0. So this is also an out of bounds read i.e. a wrong doing punishable by random behavior.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
first post here so be gentle. I'm trying execute a recursive binary search. I have tried different variations of code to try to make the function work but i still doesnt.
Here's my code:
bool contained(int x, const int* pBegin, const int* pEnd){
if(x==*pBegin) { //
return pBegin;
}
int size = pEnd - pBegin; //size of the array
int mid= size/2; //the middle of the array
int const* pMid = pBegin + mid; //The address of the element in the middle of the array
if(x>*pMid) //Condition that looks if x is to the left of the array
return contained(x,pMid,pEnd);
else if(x<*pMid) //Condition that looks if x is to the right of the array
return contained(x,pBegin,pMid-1);
}
int main()
{
cout << "Binary search, test: " << endl;
int arr[] = {1,2,3,4,5,7,8,9};
int size = 9;
for(int i=0; i<size; i+=1)
arr[i] = i;
bool find = containedInSortedarray(6, &arr[0], &arr[size]);
cout << "Found " << find << endl;
}
For example here, when i execute my bool-contained function with a premade array and let it search for the value 6, it should eventually come to the conclusion that the element does not exist, yet my output says it does. Have i missed something in my code?
Thanks in advance!
There are a couple of issues with your code:
You never return false.
You return pBegin which is not a bool, (sadly in your case) it is implicitly convertible to one, but not in the way you want it too. Turn on your compiler warnings all the way up - -Wextra -Wall -pedantic -Werror should be the bare minimum, especially if you are a beginner.
Be precise about the interval your function searches in. func(x,a,b) - does it include b? Based on contained(x,pBegin,pMid-1); it seems it does, but in the case of containedInSortedarray(6, &arr[0], &arr[size]); hopefully not.
Dereferencing &arr[size] is UB.
What is the purpose of the for loop in main?
Having the size detached from the array is a disaster waiting to happen. At least use sizeof(arr)/sizeof(arr[0]). Better yet, use std::array or a std::vector.
I would suggest searching [a,b) because it can be easily be divided into [a,mid), [mid,end). That way your mid computation is already correct. You can then call it like containedInSortedarray(x,arr,arr+arr_size)
The base condition should catch the arrays of size 0 and 1 - both can be trivially tested for presence of x.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an exercise like that. The requirement is to write code in the section
#include <iostream>
int minus (int a, int b) {
return a - b;
}
void calculate(int a, int b)
{
int (*myfunc)(int, int);
// STUDENT ANSWER BEGIN
// STUDENT ANSWER END
int ans = (*myfunc)(a, b);
printf("Output of calculation is %d.\n", ans);
}
int main() {
calculate(1,2);
return 0;
}
When I write
int (*myfunc) (int, int) = add;
int temp = (*myfunc)(a, b);
cout << temp;
the value of temp = -1 as expected (just for test) but I can't get the value of ans. So, I figure that two *(myfunc) is different. How can I fix that?
Thank you very much!
When you write:
int (*myfunc) (int, int) = add;
you are creating a new variable named myfunc. If you do it inside the same scope as the original myfunc, it is a compilation error, but if you write it inside a new scope (which the indentation of the code you pasted suggest, even if you haven't showed the brackets), you are shadowing the original variable with the new one. By using the name myfunc inside that scope, you are referring to the new variable.
The solution is simple. Instead of declaring a new variable, simply assign to the existing one:
myfunc = add; // or minus
EDIT: I misread the question and interpreted "but I can't get the value of ans" as "but I can't get the value into ans". But I'll leave this answer here in case that is what OP meant...
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 2 years ago.
Improve this question
I am declaring an array inside main() and without initializing the array, I am printing the array by passing it to a function. It is printing the same value as many time as I run this program.
As we know after declaring an array, it is initialized with random values. Then how it can print 1,2,3,4,5,...... for all the time.
void print(int ar[],int n){
for(int i=0;i<n;i++)
cout<<ar[I]<<" ";
cout<<endl;
}
OUTPUT :
0 1 2 3 4
When I am printing array inside main() using a for loop, then I got random values.
int main(){
int ar[5]={0};
for(int i=0;i<5;i++)
cout<<ar[i]<<" ";
}
OUTPUT : -2 6422280 1978757101 4201168 6422352
But, when I am making change to this array inside function, then it is not reflecting change outside that function.
#include<bits/stdc++.h>
using namespace std;
void flush(int ar[],int n){
for(int i=0;i<n;i++)
ar[i]=0;
}
void setar(int ar[],int n){
for(int i=0;i<n;i++)
ar[i]=1;
}
void print(int ar[],int n){
for(int i=0;i<n;i++)
cout<<ar[i]<<" ";
cout<<endl;
}
int main(){
int ar[5]={0};
flush(ar,5);
print(ar,5);
setar(ar,5);
print(ar,5);
}
As we know array is passed by reference, so change made inside function, should be visible outside function also.
SYSTEM :
Windows 10
gcc compiler
VSCode studio
C++ 14
well, in the print function what is it doing is that it prints the value of the integer "i", not the values of the array.
So, instead of printing "i" you should print ar[i]
cout<<ar[i]<<" ";
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
int f_point(int kek,int *lol) {
*lol *= *lol;
return kek;
}
int main {
int x;
std::cin >> x;
int *adress_of_x = &x;
int a,b = f_point(x,&x); //how does it work?
std::cout << a << LINE_JUMP;
std::cout << b << LINE_JUMP;
}
For example, if I give 2 to program then I will get 0 and 2. Why?
b = f_point(x,&x) in this statement value of first parameter is 2.
Your function is not changing the value of first parameter and returns the same value.
Your are passing first parameter by value so it has no relation with the updated value of x. Variable a is uninitialized, so it is taking a garbage value.
int a,b = f_point(x,&x); //how does it work?
The variable declaration leaves a uninitialized and initializes b from the result of f_point(x,&x);.
Since it's an uninitialized variable, accessing the value of a in the
std::cout << a << LINE_JUMP;
statement leads to undefined behavior of your program. Having an output of 0 is just one of any possibilities (including your fridge explodes unexpectedly or little demons flying out of your nostrils).