Using a timer I call the slot checkBookings() repeatedly.
I am able to compile and run the program, but it crashes when executing the above FOR loop.
Error:"ASSERT failure in QList::at: "index out of range", file ../../../../Qt/2010.05/qt/include/QtCore/../../src/corelib/tools/qlist.h, line 455
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function."
My code is:
timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(checkBookings()));
timer->start(500000);
void Canvas::checkBookings()
{
QString dateStr;
for(int i= 0;i<=qlist.count();i++)
{
dateStr = qList.at(i).at(6);
}
}
Replace <= with < in your for loop. Like this:
for(int i= 0;i<qlist.count();i++)
{
dateStr = qList.at(i).at(6);
}
The reason is qList.count() is the number of items in the list so you do not ever want to try to use qList.at(qlist.count())
Edit:
By having <= in the for loop remember that the at last iteration of the for loop i=qlist.count(). So then when the code executes the statement in the loop it essentially does this:
dateStr = qList.at(qList.count()).at(6);
Related
#include <stdio.h>
#include <cs50.h>
void draw(int n);
int main(void)
{
int height = get_int("number:");
draw(height);
}
void draw(int n)
{
if(n <= 0)
{
return;
}
draw(n - 1);
for(int i = 0 ; i < n ; i++)
{
printf("#");
}
printf("\n");
}
Iam learing recursion topic, suppose the user input 4 when the compiler completes the if part the value of n is '0' and returning when i debug , but then the for loop starts the value of 'n' becomes '1' and also 'i' doesn't change it constantly 0 why is that iam expected n becomes 0 after the if draw(n - 1) completes.
I will try to make this explanation as simple as I can. First things first, To begin with, when using recursion, you would be noticing the calling of a method within itself with a different argument. In your case it is the draw method.
Now each time, a draw method is called inside another draw method, the outer method(in this case the draw that is called first) stops its flow of execution till the completion of the inner draw.
So when you called draw(4), it ran all the code till it reached line 5 in draw method, and called draw(3). Your for loop of draw(4) is not executed yet. This will continue till draw(1) calls a draw(0). At this stage, draw(0) will return out and the draw(1) will continue its for loop from where it left. So you would find that here n=1, leading to the first print of # and then a new line after it. Once the operation completes in here, it continues with where it left for draw(2). Which is the for loop in draw(2) where the value of n=2. And here it does two print of # and then a new line. This continues.
Now for the question why i is always 0, it is to do with what we call scopes in programming, you can see that each time the i is declared fresh in the loop and assigned a value 0. This means, each time a for loop is hit, the value of i is reinitialised to 0. If you had a global var i out side of your draw method, you would have had the value of i being retained.
I did try my best to put things in as simple form as possible but feel free to let me know if you needed more clarity.
class Solution{
ArrayList subsetSums(ArrayList arr, int N){
int sum=0;
ArrayList<Integer> temparr = new ArrayList<>();
for(int i=1;i<=arr.size();i++)
{
for(int j = 0; i < arr.size()-i+1 ; j++)
temparr.add(recur(arr,i,j,sum));
}
return temparr;
}
int recur(ArrayList<Integer> arr,int i,int j,int sum)
{
int index = j;
int len = i;
int Sum = sum;
if(len==0)
{
return Sum;
}
Sum += arr.get(index);
return recur(arr,len--,index++,Sum);
}
}
,,,
I'm getting stack overflow error in 'return recur(arr,len--,index++,Sum);'
'''
I think, the main problem here (see comments for potential other problems) is the way you are trying to pass changed arguments to the recursive invocation:
recur(arr,len--,index++,Sum)
Actually this will call recur with the unchanged values of len and index because the operators ++ and -- (when written on the right side of a variable) are defined to return the original value of the variable and then update the variable's value.
Use (I would prefer this)
recur(arr, len-1, index+1, Sum)
or (okay, but the assignment is not needed)
recur(arr, --len, ++index, Sum)
to actually pass the modified value to the function.
Java has a recursion limit. The way to fix this is replace the recursion with a loop. (Or change the function so it does not recur as much. Infinite loops are a problem just as infinite recursion is).
A few tips for the future:
Google the documentation for errors
State the language with a tag in posts
Don't use formatting of line 1
Debug with print statements
I'm trying to assign my node value to a pointer, but gdb gives me segmentation fault when the code is ran. What can I do?
void biggerPotion(No* node, int bottleSize, int *aux){
if(node == NULL)
return;
maiorPocao(node>left, bottleSize, aux);
maiorPocao(node->right, bottleSize, aux);
if((node->value >= garra) && (node-> value < *aux))
*aux = node->value; //here is the issue
}
Other relevant parts of the code are:
for(i=0; i< nBottles;i++){
a = 1000; //i declared that
biggerPotion(potions,bottleSize[i],&a);
}
Okay, since the errant line is:
*aux = node->value;
then either aux is the problem or node is (because they're the only two pointers being dereferenced on that line).
I would print them both out before executing that if block just to be certain:
fprintf(stderr, "node is %p, aux is %p\n", node, aux);
Given the large use of node and small use of aux, it's probably the latter that's causing the issue, in which case you should examine what you're passing to the top-level call of biggerPortion. You should post that top-level call, including the declaration of whatever variable you're passing in.
In any case, you can test that by simply changing:
*aux = node->value;
into:
{
int temp = node->value;
}
If the problem disappears then it's definitely the aux pointer being wrong somehow. Make sure you are actually passing in a pointer, such as with:
int myVar;
biggerPotion(rootNodePtr, 42, &myVar);
So recently I have been working on a project which involves the use of while loops to manipulate global variables. My hope is to use the while loop as a sort of counter. However, I am finding that when the while loop has terminated, the value of the global variable reverts to zero. Any ideas on how to fix this?
Here is an example of what I am talking about:
int i = 0;
setup() {
pinMode(3, OUTPUT);
}
loop() {
while (i < 10) {
i++;
}
analogWrite(3, i);
}
After the while loop has terminated, I find that no signal is written to pin 3, and i has been reset to zero. is there any way for it to retain its value?
I understand that the example given is very simple, and the project I am currently working on is much more complicated, but I am just wondering if it is even possible for a variable to retain its value after being incremented in a while loop.
I fixed you code so it would compile (another clue you didn't actually test THAT code) and added some debug prints to prove that i stays at 10.
int i = 0;
void setup() {
pinMode(3, OUTPUT);
Serial.begin(115200);
}
void loop() {
Serial.print("Loop start i = ");
Serial.println(i);
while (i < 10) {
Serial.println("Running while loop");
i++;
}
analogWrite(3, i);
Serial.print("Loop end i = ");
Serial.println(i);
}
Produces as output:
Loop start i = 0
Running while loop
Running while loop
Running while loop
Running while loop
Running while loop
Running while loop
Running while loop
Running while loop
Running while loop
Running while loop
Loop end i = 10
Loop start i = 10
Loop end i = 10
Loop start i = 10
Loop end i = 10
Loop start i = 10
Loop end i = 10
Loop start i = 10
Loop end i = 10
Loop start i = 10
Loop end i = 10
Loop start i = 10
Loop end i = 10
Loop start i = 10
Loop end i = 10
are you sure you want to write outside of your while loop... all that's doing is incrementing i to 10 exiting the loop, then writing to pint 3...
also... re global... no unless you write i to another variable at the start of your function "loop" ie z ... int z = i; then at the end of the function i = z;
globals are bad mmmkkkaaay!! pass variables as parameters.
I am trying to reverse the words within a QStringList. Below is the code up to now, but I keep on getting a "Index out of Range" error. From the error it would seem that I am trying to use data that is out of scope, but not able to figure out my problem.
QString reversed;
QStringList reversedlist;
QStringlist list = input.split(" ");
for (int i=0;i<list.length(); ++1)
reversedlist[i] = list[list.length() -1 -i];
reversed = reversedlist.join(" ");`
Thanks
As pointed out by #ThorngardSO, the reversedlist is initially empty, and you are trying to access the invalid index in your loop code. You should add values to the list using one of the following functions:
STL-compatible function push_back() (inserts value at the end of the list)
STL-compatible function push_front() (inserts value at the beginning of the list)
Qt function append() (Qt alternative for push_back)
Qt function prepend() (Qt alternative for push_front)
As you see, prepend() inserts the element at the beginning of the list, that is why it makes the reversing of the list very simple:
for (int i = 0; i < list.length(); ++i) {
reversedlist.prepend(list[i]);
}
Also, note that there is a typo in your loop: it should be ++i instead of ++1.
Your reversedList is initially empty. You have to actually append the items, like this:
reversedlist.push_back (list[list.length () - 1 - i]);
Naturally, trying to access non-existing items via reversedList[i] does not work and throws the index-out-out-range error.
You got the index out of range as there is no sting inside the QStringList reversedlist. So when your code reach the line reversedlist[0], it throw the "index out of range" error. And you can read the value using [0] and cant assign.
if you want to assign the value to a particular index of the QStringList.
QString reversed;
QStringList reversedlist;
QString input="123 456 789 000";
QStringList list = input.split(" ");
for (int i=0;i<list.length(); ++i){
//value to particular index
reversedlist.insert(i,list[list.length() -1 -i]);
}
reversed = reversedlist.join(" ");
qDebug() << reversed;