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 7 months ago.
Improve this question
I'm writing a simple program to search for an element in a 2D matrix. It's not giving me an active/named exception, but it just throws me a Runtime error.
Any help with as to why this is happening?
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
for (int i=0 ;i< matrix.size(); ++i){
int m = matrix[i].size() -1 ;
int n = matrix.size()-1;
if (matrix[n][m] < target){
return false;
}
else if (matrix[i][m] < target){
i++;
} else {
for (int j=0;j <= m; j++ ){
if (matrix[i][j] == target){
return true;
} else if (matrix[i][j] != target) {
continue;
}
}
if (matrix[i][m] != target ){
return false;
}
}
}throw;
}
};
}throw;
A throw without following expression can be used inside an exception handler to re-throw the currently handled exception.
However, at this point your program does not have an active exception. In this case, throw calls std::terminate().
This seems to be an edit artifact.
Related
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 yesterday.
Improve this question
Im trying to assign a value to a class form a 2d array using a function then passing that value from the class to another function. (sorry if theres any errors in my code im just a beginner for c++)
class Alien
{
public:
int HP = 100;
int ATK = 0;
int rowlocation;
int columnlocation;
};
Board[50][50]
char alienlocation(int &numofrows,int &numofcolumns)
{
Alien alien;
for (int i = 0; i < numofrows; i++)
{
for (int j = 0; j < numofcolumns; i++)
{
if (Board[i][j] == 'A')
{
alien.rowlocation = i;
alien.columnlocation =j;
return alien.rowlocation, alien.columnlocation;
}
}
}
return 0;
}
void alienmovement()
{
Board[alien.rowlocation][alien.columnlocation] = '.';
Board[alien.rowlocation - 1][alien.columnlocation] = 'A';
alien.rowlocation = alien.rowlocation - 1;
}
im trying to move "A" in the array to a certain direction but ended up not moving at all or it just keeps going back into default position.
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 16 days ago.
Improve this question
My code has something strange. It doesn't have any syntax errors, and when I run it, it runs and takes input, and then immediately terminates. What is the issue?
I have tried to run the code in VS Code and Codeblocks, and I am faced with the same error. I am stuck right now.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int>a(2 * n);
for (int i = 0;i < 2 * n;i++)
{
cin >> a[i];
}
int mx = 0;
vector<int>w;
for (int i = 0;i < 2 * n;i++)
{
vector<int>::iterator it = find(w.begin(), w.end(), a[i]);
if (it != w.end())
{
w.push_back(a[i]);
}
else
{
w.erase(it);
}
if (mx < w.size())
{
mx = w.size();
}
}
cout << mx << endl;
}
In your 2nd loop, you are calling w.erase(it) when it is w.end(), which is undefined behavior:
https://en.cppreference.com/w/cpp/container/vector/erase
The iterator pos must be valid and dereferenceable. Thus the end() iterator (which is valid, but is not dereferenceable) cannot be used as a value for pos.
You are trying to push the integer if it is found, and erase it if it is missing. That logic is backwards. You need to use == instead of != in your if condition to reverse the logic:
if (it == w.end()) // <--
{
w.push_back(a[i]);
}
else
{
w.erase(it);
}
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
This is code for the intersecting arrays. It seems to work for most of the cases until
nums1 = [1]
nums2 = [1,1]
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
vector<int> temp;
int i = 0;
int j = 0;
for (i = 0; i < nums1.size(); i++) {
for (j = 0; j < nums2.size(); j++) {
if ( (nums1.size() == nums2.size()) && (nums1[i] < nums1.size() - 1) ) {
if (nums1[i] == nums2[j])
temp.push_back(nums1[i]);
}
else if ( (nums1[i] == nums2[j]) && (i < nums1.size()) ) {
temp.push_back(nums1[i]);
i++;
}
}
}
return temp;
}
};
when i = 1, I get the error, but with the condition that (nums1[i] == nums2[j]) && (i < nums1.size()), why does it still go there and get stuck?
LeetCode 349
The order of your checks are wrong. You need to check if i is a valid index before indexing, like this:
else if ( (i < nums1.size()) && (nums1[i] == nums2[j]) ) {
otherwise you invoke undefined behavior (which could cause a run-time error).
Similarly, in all the cases where you are indexing into a vector, you need to be sure that the index is valid.
The operands of && are evaluated left to right, post-confditions will be checked if pre-conditions are all valid.
BTW, modifying the index in a for-loop is not recommended. In this case, you are likely to do repeated work. if you really want to modify the index to make your algorithm works, a while-loop is better.
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 4 years ago.
Improve this question
So I am a newbie in C++, I do code in JS, for some reason I can't figure out the mistake here, please help me out. thanks!
#include <iostream>
using namespace std;
int search(char input, char sentence[]){
for(int i = 2; i != '\0'; i++){
if(sentence[i] == input){
return i;
}else{ return -1; }
}
}
int main()
{
char key[20] = "hey my name is sid!";
char ser = 'm';
cout << search(ser,key);
return 0;
}
Your condition in the for loop is wrong, you are not checking the string only the index. Also if your character did not match, you do not want to exit immediately.
The correct code would be:
for(int i = 0; sentence[i] != '\0'; i++)
{
if(sentence[i] == input)
{
return i;
}
}
return -1;
If you want to start the search at the third character you should first ensure that your string has at least three elements:
if(strlen(sentence)>=3)
{
for(int i = 2; sentence[i] != '\0'; i++)
{
...
}
...
}
For your search function, you are only really checking the first character in the loop, then returning. You should move "return -1;" outside of the for loop as then it will only be called after the entire string was checked for the value and it was not found.
int search(char input, char sentence[]) {
for (int i = 2; sentence[i] != '\0'; i++) {
if (sentence[i] == input) {
return i;
}
}
return -1;
}
in addition to changing the conditions of the for loop as other users mentioned.
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
I have a problem with this simple code. Propably the problem is with using the vector class:
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
vector<int> left, right;
int cnt=0,k,j;
if (A.size()==1) return 0;
if (A.size()==2 && A[0]==A[1]) return 1;
if (A.size()==2 && A[0]!=A[1]) return 0;
if (A.size()==3 && A[0]==A[1] && A[1]==A[2]) return 2;
if (A.size()==3 && A[0]==A[1] && A[1]!=A[2]) return 0;
for (size_t i=0; i<(A.size()-1); ++i)
{
left.assign( A.begin(), A.begin() + i );
right.assign( A.begin() + i + 1, A.end());
sort(left.begin(), right.end());
sort(right.begin(), right.end());
j=left.size()/2;
k=right.size()/2;
if (left[j]==right[k])
{
cnt++;
}
}
return cnt;
}
after execution it return :
stderr:
Segmentation Fault
left[j] is out-of-bounds if i==0 because left.size() is i, so 0.
Similar for right[k].
Also the case A.size() == 0 is not considered and will wrap around A.size()-1.
There is also a typo in sort(left.begin(), right.end()): right.end() should be left.end().