Vector not defined [closed] - c++

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
I have added bits/stdc+.h and vector both.
Still this error is coming .
Can anyone tell me why this is happening.
#include <bits/stdc++.h>
#include<vector>
void rotate(int arr[], int n);
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int a[n] , i;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
rotate(a, n);
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
return 0;
}
// } Driver Code Ends
//User function Template for C++
void rotate(int arr[], int n)
{
vector<int> a;
a[0] = arr[n-1];
for(int i = 0 ; i<n-1 ;i++)
{
a.insert(a.back(), arr[i]);
}
for(int j : a)
cout<<j;
}
main.cpp:30:5: error: ‘vector’ was not declared in this scope
vector<int> a;
^~~~~~

Follow these: (EDITED)
(SOLUTION TO YOUR PROBLEM) Use, using namespace std as it means if the compiler finds something that is not declared in the current scope then it will go and check std.
Don't mix c and c++ syntax. Either use printf or cout.
Also check the 1st comment on this answer, as there is something you should know about "using namespace std" and "cout/cin".
No need to work two times, you can also declare and define your function at once.
Solution (but have an error in other parts)
#include <bits/stdc++.h>
using namespace std;
void rotate(int arr[], int n)
{
vector<int> a;
a[0] = arr[n - 1];
for (int i = 0 ; i < n - 1 ; i++)
{
a.insert(a.back(), arr[i]); // ITS YOUR SYNTAX, CONSIDER TO UPDATE IT
}
for (auto &it : a)
cout << it;
}
int main()
{
int t;
cin>>t;
while (t--)
{
int n;
cin>>t;
int a[n] , i;
for (i = 0; i < n; i++)
cin>>a[i];
rotate(a, n);
for (i = 0; i < n; i++)
cout<<a[i];
cout<<"\n";
}
return 0;
}
CHECK LINE NO 9 line,
and see if it's correct or not.
a.insert(a.back(), arr[i]); WRONG
You are doing something wrong there. Check this statement
error: ‘vector’ was not declared in this scope
Solved by using namespace
If you liked this answer. Please consider ticking this answer.:)

Related

Runtime Error: Runtime ErrorSegmentation Fault (SIGSEGV) [closed]

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
LINK TO THE ACTUALL PROBLEM
I am trying to solve a question on codechef but getting a runtime error. Although when i executed same code on virtual studio code, no error was observed.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
string s[n],b;
int i=0;
while(i<n)
{
i++;
cin>>s[i];
if(i==0) b=s[0];
else {
if(b.length()<=s[i].length()) b=s[i];
}
}
cout<<b;
}
return 0;
}
This is because you are incrementing i and then using it for reading the string s[i], so at one point it reads for the element n+1 which is not allocated so the error that occurs here is Segmentation Error.
What you have to do is increment the i after reading the string s[i]. You can correct this by moving the i++; statement to the end of the while block.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
string s[n],b;
int i=0;
while(i<n){
cin>>s[i];
if(i==0)
b=s[0];
else
if(b.length()<=s[i].length())
b=s[i];
i++;
}
cout<<b;
}
return 0;
}
#theWellHopeErr's answer is correct and gets accepted on geeksforgeeks when the fast i/o lines are removed. I have commented them out below.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
int n;
cin>>n;
string s[n],b;
int i=0;
while(i<n){
cin>>s[i];
if(i==0)
b=s[0];
else
if(b.length()<=s[i].length())
b=s[i];
i++;
}
cout<<b<<"\n";
}
return 0;
}
Also, string s[n] is not to be done. Always better to save memory. This can be done just by storing the max length in a variable maxLen and updating your result only when you encounter a longer string
int n;
cin>>n;
string result;
int maxLen = 0;
for(int i = 0; i < n; i++)
{
string s;
cin>>s;
if(s.length() > maxLen)
{
maxLen = s.length();
result = s;
}
}
cout<<result<<"\n";

How does change in Passing parameters changes the output of the Code? [closed]

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
When I pass only Sum as Parameter in My coin Exchange Problem to find no of ways to find the amount Sum..
Example
if the coins are {2,3,5} and the desired sum is 9 i get correct output as 8.
This code :
#include <bits/stdc++.h>
using namespace std;
int n, sum, a[105];
int fun(int sum) {
int S = 0;
if(sum == 0) return 1;
if(sum < 0) return 0;
for(int i=1;i<=n;i++) {
S += fun(sum - a[i]);
}
return S;
}
int main()
{
cin >> n;
cin >> sum;
for(int i=1;i<=n;i++ ) {
cin >> a[i];
}
cout << fun(sum);
return 0;
}
But when I also give Current Index as a parameter it gives me output as 3 which is wrong.
This code :
#include <bits/stdc++.h>
using namespace std;
int n, sum, a[105];
int fun(int sum, int idx) {
int S = 0;
if(sum == 0) return 1;
if(idx > n) return 0;
if(sum < 0) return 0;
for(int i=idx;i<=n;i++) {
S += fun(sum - a[i], i);
}
return S;
}
int main()
{
cin >> n;
cin >> sum;
for(int i=1;i<=n;i++ ) {
cin >> a[i];
}
cout << fun(sum,1);
return 0;
}
But WHY??
Is my parameter passing in this case is wrong?
You don't have the same result between your 2 solutions because in the first, at each "function call", you will iterate from 1 to N and in your second solution, you will iterate from Index to N. So the number of loop will be different.

Function not outputting [closed]

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'm trying to sort an array, but somehow the program doesn't output anything?
I tried writing the good old for in for sorting algorithm, doesn't work, then I tried STL, still doesn't work. Here's the code:
#include <iostream>
#include <algorithm>
using namespace std;
void in(int n, int v[]){
cin>>n;
for(int i=1; i<=n; i++){
cin>>v[i];
}
}
void sortf(int &n, int v[]){
sort(v+1, v+n+1);
}
void af(int n, int v[]){
for(int i=1; i<=n; i++){
cout<<v[i]<<" ";
}
}
int main(){
int n, v[1001];
in(n, v);
sortf(n, v);
af(n, v);
}
The variable n in main() is copied to the argument int n of the function in() and changes to the argument inside the function in() will not affect the variable n in main().
Because of that, sortf() and af() are using value of n, which is uninitialized and indeterminate.
To have functions modify caller's variables, you should use references.
#include <iostream>
#include <algorithm>
using namespace std;
void in(int& n, int v[]){ // add & to make n reference
cin>>n;
for(int i=1; i<=n; i++){
cin>>v[i];
}
}
void sortf(int &n, int v[]){
sort(v+1, v+n+1);
}
void af(int n, int v[]){
for(int i=1; i<=n; i++){
cout<<v[i]<<" ";
}
}
int main(){
int n, v[1001];
in(n, v);
sortf(n, v);
af(n, v);
}

my program wont work properly and my function keeps throwing segmentation fault [closed]

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 3 years ago.
Improve this question
so I have this code that with a function is supposed to take all the numbers in a 2D array and print them to the second power but my code keeps throwing segmentation fault and i don't know why
#include <bits/stdc++.h>
using namespace std;
void er(int arr[][100000000], int, int);
int main()
{
int n, m;
cin >> n >> m;
int arr[n][100000000];
er(arr, n, m);
return 0;
}
void er(int arr[][100000000], int n, int m)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
arr[i][j] *= arr[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << arr[i][j];
}
}
}
Using
int arr[n][100000000];
is problematic on two accounts.
VLAs are not standard C++. It is supported by some compilers as an extension.
The size 100000000 is too large for a variable on the stack. Changing that to 100 and making sure that m is less than or equal to 100 will most likely work as long as your compiler supports VLAs.
A better alternative would be to use std::vector.
int n, m;
cin >> n >> m;
std::vector<std::vector<int>> arr(n, std::vector<int>(m));
Of course, that will require you to change the function er accordingly.
In addition, please don't use
#include <bits/stdc++.h>
See Why should I not #include <bits/stdc++.h>? for further details.

sort the elements of an array [closed]

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 8 years ago.
Improve this question
I want to sort members of an array a[] with size s. I have firstly used a function to get the elements, and then another function to sort them in ascending order. The problem is in the sort function or in main or in both of them, because the execution of the program ends just after entering the data. Is there anyone here that can help me?
#include <iostream>
using namespace std;
void getdata() {
int s;
cin >> s;
int a[s];
for (int i=0; i<s; i++) {
cin >> a[i];
}
}
void sort(int a[], int s) {
for (int i=0; i<s-1; i++) {
for (int j=i+1; i<s; i++) {
if (a[i] > a[j]) swap(a[i], a[j]);
}
}
}
int main () {
int a[100],s;
getdata();
sort(a, s);
return 0;
}
You have a local definition of the array in your getdata() function:
void getdata() {
int s;
cin >> s;
int a[s]; // <<<
It stays local there and has nothing to do with the array you declared in main:
int main () {
int a[100],s; // <<<
You have to write your function such it takes these as parameters:
void getdata(int* a, int& s) {
cin >> s;
for (int i=0; i<s; i++) { // ...
and in main call
int main () {
int a[100],s;
getdata(a,s);
sort(a, s);
return 0;
}
UPDATE:
The condition in the inner for loop of your sort() function also looks pretty wrong, you probably meant j there not i:
for (int j=i+1; i<s; i++) {
// ^ ^
Always use std::vector if you have no definite advantage by using an array (and, in C++11, use std::array if s is known by compile time).
#include<vector>
#include<algorithm>
std::vector<int> a;
//fill it, then
std::sort(a.begin(),a.end());