variable length array bound evaluates to non-positive value 0 [duplicate] - c++

This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Variable Length Array (VLA) in C++ compilers
(2 answers)
Closed 2 days ago.
class Solution {
public:
int fib(int n)
{
int arr[n];
arr[0]=0;
arr[1]=1;
if(n==0)
{
return arr[0];
}
if(n==1 || n==2)
{
return arr[1];
}
// arr[2]=1;
for(int i=2;i<=n-1;i++)
{
arr[i]=arr[i-1]+arr[i-2];
}
return arr[n-1];
}
};
what could be the error in here, I am new to this found I a bit difficult to analyse what exactly is wrong there...
looking for A prompt answer thanks in advance.

Related

C++ It is legal ? When list is empty, insert a val by position [duplicate]

This question already has answers here:
Why is the phrase: "undefined behavior means the compiler can do anything it wants" true?
(2 answers)
Consistency of undefined behavior for a fixed compiler
(2 answers)
Closed 6 days ago.
This post was edited and submitted for review 4 days ago.
When I am praticing in leetcode 406
I find out the answer , but I want to know that is undefined behavior ?
if list is empy ,then I call list.insert(++it,10) how it work ?
I look up the https://cplusplus.com/reference/list/list/insert/ but no answer .
class Solution {
public:
static bool cmp(const vector<int> a, const vector<int> b) {
if (a[0] == b[0]) return a[1] < b[1];
return a[0] > b[0];
}
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort (people.begin(), people.end(), cmp);
list<vector<int>> que;
for (int i = 0; i < people.size(); i++) {
int position = people[i][1];
std::list<vector<int>>::iterator it = que.begin();
while (position--) {
it++;
}
que.insert(it, people[i]); // undefined behavior ?
}
return vector<vector<int>>(que.begin(), que.end());
}
};
look up the https://cplusplus.com/reference/list/list/insert/

I am trying to write a two sum function but I keep getting an extra value on my output [duplicate]

This question already has answers here:
If statements without brackets
(3 answers)
Is 'If Else' statement indentation important or not in C++? [duplicate]
(4 answers)
Closed 10 months ago.
I am trying to defamiliarize myself with c++ so I have been trying to solve problems on LeetCode. I came across this very simple 2 sum problem. Basically if given some array of numbers and a target value, I need to be able to return the indices of the numbers in that array that sum to the target, it is specified that in the data there is only one solution. So if my target is 9 and I have [2,7,11,15] my output should be [0,1]. However my output for this is [0,1,0]. It has been a while since I coded in C++ so I was hoping someone could explain what might be happening. From my understanding when my code finds the write pairing it should add those values to vector v. Then vector v should have a size of 2 so that if statement (v.empty()) should be false and thus no more values should be able to be added to it. Which is why I am confused that more values are still being added.
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
std::vector<int> v;
for (int i = 0; i < nums.size(); i++)
{
for (int j = 0; j < nums.size(); j++)
{
if (nums[i] + nums[j] == target)
{
if (v.empty())
v.push_back(i);
v.push_back(j);
}
}
}
return v;
}
};

for and while loop continues to loop even though it shouldn't C++ [duplicate]

This question already has answers here:
C++ crashes in a 'for' loop with a negative expression
(8 answers)
subtraction from size_t results in large positive number [duplicate]
(2 answers)
Closed 1 year ago.
Not a duplicate of C++ for loop continue to loop even though it shouldn't
I found out that for loop continues even though the conditions are false. An example is here
int isSubstring(std::string s1, std::string s2)
{
for (int i = 0; i < s2.size() - s1.size(); i++) {
}
return -1;
}
If I call it like isSubtring("abcd", "a"); it should've returned -1 right? But it doesn't. In Visual Studio debugger I saw the value of i goes up forever.
But, if call this
for (int i = 0; i < -3; i++) {
std::cout << "\ni=" << i;
}
It behaves as intended. This also applies for while loop. My question is, why is that?
Edit: removed unnecessary code

Given an even number ( greater than 2 ), return two prime numbers whose sum will be equal to given number [duplicate]

This question already has answers here:
Segmentation fault on large array sizes
(7 answers)
Closed 5 years ago.
vector<int> Solution::primesum(int n) {
int prime[n+1];
for(int i=0;i<n+1;i++){
prime[i]=1;
}
prime[0]=0;
prime[1]=0;
for(int i=2;i<=sqrt(n);i++){
if(prime[i]==1){
for(int j=2;i*j<=n;j++)
prime[i*j]=0;
}
}
vector <int> sum;
for(int i=2;i<=(n)/2;i++){
if(prime[i]==1&&prime[n-i]==1){
sum.push_back(i);
sum.push_back(n-i);
return sum;
}
}
return sum;
}
The above code is working fine for smaller inputs but when I try to provide it with numbers something like >100000000, it is showing segmentation fault.Can someone please help me? Thanks!
Most likely int prime[n+1] leads to a stackoverflow (100'000'000 requires ~400 MB on the stack). I'd recommend using std::vector<int> prime(n+1) instead.

How to push an element into an array in C++? [duplicate]

This question already has answers here:
C++ array (Beginner)
(4 answers)
PUSH an array C++?
(4 answers)
Closed 6 years ago.
I need to write a function that:
Goes through all numbers from 50 to 100
Checks what numbers are divisible by 6
Adds those numbers to an array
Writes that array
Writes how many elements there are in that array
I've searched for a function push in c++ but looks like there is none...
Got this far, doesnt wok...
int pushIntoArray(int array[], int size){
int index = 0;
int newArray[100];
for (int i=0; i<=size; i++){
if(array[i] % 6 == 0 && array[i] <= 100 && array[i] >= 50){
newArray[index] = array[i];
index++;
}
}
}
You can't. Arrays ir C++ (and C too) are fixed-size. And for a good reason too. They are a very low level concept, quite similar to a pointer. The memory for an array is allocated once, at the start, and doesn't change after that. An array is just a bunch of bytes in memory.
A "push" operation would require to change the size of the array, which would mean allocating new memory, copying the contents from the old one, and deleting the old memory. And that's the simple, non-optimized version.
So, no, an array cannot do this. However the standard library includes a std::vector class which does exactly that and more. That's the one you want.
As said in the comment, when the size of an arrays isn't fixed, use std::vector
And use tham like in the code below:
#include <iostream>
#include <vector>
std::vector<int> pushIntoVector(std::vector<int> values) {
std::vector<int> result; //A vector of integers
for (int a : values) // Goes through each values of the vector
if (a <= 100 && a >= 50 && !(a % 6)) {
std::cout << a << "\n";
result.push_back(a);
}
std::cout << result.size() << "\n";
return result;
}
int main()
{
std::vector<int> vec;
vec.push_back(36); //Not printed: < 50
vec.push_back(60); //Printed
vec.push_back(72); //Printed
vec.push_back(90); //Printed
vec.push_back(91); //Not printed: 91%6 != 0
vec.push_back(105); //Not printed: > 100
pushIntoVector(vec); //Do whatever you want with the returned vector
while (1);
return 0;
}