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 3 years ago.
Improve this question
I was coding a function that loops through a 2d array, if the current element in the array is less than the element next to it, then I would add 1 to my int counter variable. The issue is, when I run the code on my windows based machine with a AMD ryzen 7 processor, it works correctly and the counter gets to 20 (on a 5x5 array). But when I run it on my 2014 Macbook with a intel core i5 processor, on both MacOS and Windows 10 (bootcamp) the counter only gets to 19 using the same exact block of code. Here is my loop
int counter = 0;
for(int i=0; i < ROWS; i++){
for(int j=0; j < COLUMNS; j++){
if(board[i][j] < board[i][j + 1]){
counter += 1;
}
}
}
I would think this would work in any situation.
Well it would be nice to have a minimal reproducible example like others said, I think I can see it from here.
using j+1 will let the index go out of bounds causing undefined behaviour.
Try :
for(int i=0; i < ROWS; i++){
for(int j=0; j < COLUMNS - 1; j++){
if(board[i][j] < board[i][j + 1]){
counter += 1;
}
}
}
This should avoid that problem by simply stopping one sooner
Related
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
For the given pseudo code I have to determine primitive operations and running time:
for( i=0; i<n; i++) ->1+n+n=2n+1
a[i] = 0; ->?
for( i=0; i<n; i++ ) ->2n+1
for( j=0; j<n; j++ ) ->n(2n+1)
a[i] += a[j] + i + j; ->?
Anyone to help me if it's correct and how to do the rows which I have put ?. please
As far as I understand, the for loop instruction going from 0 to n times would be executed n+1 times (once extra when loop index equals n itself, after which it breaks), so it would be n+1 and not 2n+1. Its contents would run for n times. Assigning values doesn't add additional complexity in terms of input size (n):
for(i = 0; i < n; i++) // 1+n
a[i] = 0; // runs for n times
Likewise, for the nested loops, the inner-loop statement will run for n+1 times, multiplied by n, as it in itself is the content of the outer loop, making the product of n(n+1). The contents would run for n*n times.
for(i = 0; i < n; i++) // n+1
for(j = 0; j < n; j++) // n(n+1) => n*n+n
a[i] += a[j] + i + j; // runs for n*n, for the dominant factor
With the innermost statements running for n*n times, the overall asymptotic time complexity would be O(n2).
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 3 years ago.
Improve this question
I'm trying to solve a Pascal's Triangle problem on Leetcode. I get this error when I run the code.
AddressSanitizer: heap-buffer-overflow on address 0x602000000014 at pc 0x000000407875 bp 0x7ffe13bd9300 WRITE of size 4 at 0x602000000014 thread T0.
How should I fix it?
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> tri(numRows);
vector<int> row;
row.push_back(1);
tri.push_back(row);
row.clear();
for (int i = 1; i < numRows; i++) {
row[0]=1;
row[i]=1;
for (int j = 1; j < i; j++) {
row[j] = tri[i-1][j] + tri[i-1][j-1];
}
tri[i] = row;
row.clear();
}
return tri;
}
};
When you call row.clear(), that wipes the row and sets the length to 0. As a result, when you try accessing row[0], row[i], etc, you're accessing memory that you shouldn't be touching.
Here's your problem:
row[0]=1;
row[i]=1;
At this point, the row vector is empty. So you are writing into thin air.
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
Question says:
You are given an integer N. You need to print the series of all prime numbers till N.
I want to know what's wrong with my code and suggestions will also be of great help.
#include<iostream>
using namespace std;
int main()
{
int N;
cin >> N;
int u;
for(int i = N; i > 0; i--)
{
u = 0;
for(int j = 2; j < N-1; j++)
{
if(i % j == 0)
{
u = 1;
}
}
if(u == 0)
{
cout << i << " ";
}
}
return 0;
}
First for future reference you should probably post that on code review, unless there is a specific problem then you should create a Minimal, Complete, and Verifiable post.
There is nothing inherently wrong other than you do not check that N>0 which could lead to an infinite loop, j should be going to i not N, and I think this would print 1 as a prime number which it is not. Here are some pointers:
Why are you going from N to 0? That seems a little counter intuitive compared from going from 2 to N (you can skip 1 since it is not prime)
If you are going to use a flag (u) then you should make it a bool which forces it to be either true or false
You do not need to do a flag, instead as soon as you find a divisor print the number and then break the inner loop like
for(j=2; j< N-1; j++){
if(i%j==0){
cout << i << " ";
break;
}
}
You do not need to have j go all the way to i, just the sqrt(i) since anything greater then the sqrt(i) that divides i must must be multiplied by some number smaller then the sqrt(i). So if i is not prime, then there must be a divisor below sqrt(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 8 years ago.
Improve this question
I am trying to copy data from one vector to another but am getting an error , "Invalid operands to binary expression 'int' and 'Card' " when I try to compile the following for loop:
for (int i = 0; i <= vectorOne[vecCapacity]; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
Would anyone have any suggestions?
I believe what you meant is
for (int i = 0; i <= vecCapacity; i++)
or even more likely
for (int i = 0; i < vecCapacity; i++)
The error message is clear enough: in this loop
for (int i = 0; i <= vectorOne[vecCapacity]; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
i has type int while vectorOne[vecCapacity] has type Card and there is no defined operator <= for these types.
So this loop makes no sense.
Maybe you mean
for (int i = 0; i < vecCapacity; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
Also take into account that you have to guarantee that the size of vectorTwo is not less than the size of vectorOne or at least vecCapacity.
You could use standard algorithm std::copy declared in header <algorithm>
For example
#include <algorithm>
//...
std::copy( vectorOne, vectorOne + vecCapacity, vectorTwo );
You should be looping from 0 to vectorOne's size.
for (int i = 0; i < vectorOne.size(); i++) { //step 3
vectorTwo[i] = vectorOne[i];
}`
`
Also, if you're doing it this way, make sure vectorTwo is big enough before the loop.
vectorTwo.resize(vectorOne.size());
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an integer array list and I want to make left right pointer array list to access this. But for more than 690.000 elements (for example 700.000) the program stops and says `Segmentation fault (core dumped). I have used std::vector before but I have to use simple array list because of speed. And This code is not about my program it is very simple way to tell my problem with code. And I have to use global variable because this code run in a class methods.
int k=698900;
int n1=k/2;
int n2=k/2;
int *left[n1];
int *right[n2];
int list[k];
for(int i=0; i<k; i++){
list[i] = i;
}
for(int i=0; i<n1; i++){
left[i] = &list[i];
}
for(int i=0; i<n2; i++){
right[i] = &list[i];
}
for(int i=0; i<n2; i++){
cout << *right[i] << endl;
}
As #PaulR already said in the comments your problem is a stack overflow. You can solve this problems a few ways. If you're categorically against changing your code then you can adjust the stack size in your compiler. How exactly that is done depends on your compiler (gcc example and VS example). You can also adjust it at run-time but that's an OS dependent function, so I'm not going to go into details about that.
The solution that I would prefer is allocate the memory on the heap. This can be done by either allocating the arrays on the heap int **left = new int*[n1]; but then you have to remember to call delete []left; when you don't need the memory anymore. The far more elegant solution would be to use a std::vector instead.
I challenge you to compile this with all optimizations on and tell me that it's significantly slower than your code.
int k=698900;
int n1=k/2;
int n2=k/2;
//not that I condone storing raw pointers in a vector
//admittedly this is a rather dangerous thing to do
//storing indices would be smarter because upon adding
//elements the memory could be relocated
std::vector<int *> left(n1);
std::vector<int *> right(n2);
std::vector<int> list(k);
for(int i=0; i<k; i++){
list[i] = i;
}
for(int i=0; i<n1; i++){
left[i] = &list[i];
}
for(int i=0; i<n2; i++){
right[i] = &list[i];
}
for(int i=0; i<n2; i++){
cout << *right[i] << endl;
}
Like #PaulR mentioned you can also allocate the arrays statically (static int *left[n1];) or globally (outside of any function or class declaration/definition)