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 want to binary search more than once using the same function but it's showing segmentation fault. For one input its giving correct answer but when I am giving multiple input for binary search(i.e, q>1 acc. to my code) then its showing segmentation fault.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int binarySearch(int x[],int l, int r, long int m ){
if(r>=1){
int mid=(r-1)/2+l;
if (x[mid]==m) return mid;
if (x[mid]>m) return binarySearch(x,l,mid-1,m);
if (x[mid]<m) return binarySearch(x,mid+1,r,m);
}
return 0;
}
int main(){
int n,q;
cin>>n;
int x[n];
for(int i=0;i<n;i++){cin>>x[i];}
int t = sizeof(x) / sizeof(x[0]);
sort(x,x+t);
cin>>q;
while (q--){
long int m;
cin>>m;
cout<<binarySearch(x,0,t-1,m)<<endl;
}
return 0;
}
The condtion r>=1 should be r-l+1>=1 to check if there are some element in the range.
The initialization int mid=(r-1)/2+l; should be int mid=(r-l)/2+l; to correctly calculate the middle element.
Variable-Length Array int x[n]; is not in the standard C++. You should use heap allication int *x = new int[n]; instead. After that, n should be used for the initialization of t because the technique to obtain the number of elements in arrays won't work with pointers.
#include <iostream>
#include <algorithm>
using namespace std;
int binarySearch(int x[],int l, int r, long int m ){
if(r-l+1>=1){
int mid=(r-l)/2+l;
if (x[mid]==m) return mid;
if (x[mid]>m) return binarySearch(x,l,mid-1,m);
if (x[mid]<m) return binarySearch(x,mid+1,r,m);
}
return 0;
}
int main(){
int n,q;
cin>>n;
int *x = new int[n];
for(int i=0;i<n;i++){cin>>x[i];}
int t = n;
sort(x,x+t);
cin>>q;
while (q--){
long int m;
cin>>m;
cout<<binarySearch(x,0,t-1,m)<<endl;
}
delete[] x;
return 0;
}
Related
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.:)
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);
}
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
In the following program:
#include <iostream>
#include <cmath>
using namespace std;
int diagonalDifference(int x[][],int n)
{
int sum1=0,sum2=0,y;
for(int i=0;i<n;i++)
{
sum1+=x[i][i];
sum2+=x[i][n-1-i];
}
y=abs(sum1-sum2);
return y;
}
int main()
{
int n,**z;
cin>>n;
int arr[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
z=diagonalDifference(arr,n);
cout<<x;
return 0;
}
I get a compilation error I don't understand.
error:declaration of 'x' as multidimensional array must have bounds for all dimensions except the first
Could you help me fix it?
int[][] is not a valid type:
int diagonalDifference(int x[][],int n)
You declare z as an int**:
int n,**z;
But you assign it an int:
int diagonalDifference(int x[][],int n);
z=diagonalDifference(arr,n);
And finally you print x which does not exist:
cout<<x;
As rules of thumb:
declare only one variable per line, and give it a meaningful name;
declare what possibly can as const;
Don't use C-style arrays unless you have to; prefer std::vector for instance;
don't use using namespace std;
much more you need to learn.
.
int diagonalDifference(int x**,int n) { /* .... */ }
int matrix_size = 0;
std::cin >> matrix_size;
std::vector<std::vector<int>> matrix{matrix_size, std::vector<int>{matrix_size}};
/* fill the matrix */
const int diag_diff = diagonalDifference(matrix, matrix_size);
std::cout << diag_diff << '\n';
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 6 years ago.
Improve this question
I am trying to write a program which prints the cubes of inserted elements of array, and then prints the sum of elements divisible by 2, and not divisible by 3.
#include <iostream>
#include <math.h>
using namespace std;
void kub(int *niz[], int n)
{
int i=0;
while(i<n)
{
*niz[i]=pow(*niz[i],3);
i++;
}
}
void unesi(int* niz[],int n)
{
int i=0;
while(i<n)
{
cin>> *niz[i];
i++;
}
}
void stampaj(int* niz[],int n)
{
int i=0;
while(i<n)
{
if ((*niz[i]%2)==0 && (*niz[i]%2)!=0)
cout<<*niz[i]<<endl;
i++;
}
}
int main()
{
int n;
cin>>n;
int niz[n];
unesi(niz,n); /* <<= here */
kub(niz,n);
stampaj(niz,n);
return 0;
}
Everything is fine, until this line of code (marked "<<= here"): "unesi(niz,n);" What am I doing wrong?
Instead of:
void unesi(int* niz[],int n)
try:
void unesi(int niz[],int n)
niz[] decays to pointer, so no need to pointer to pointer
Same for the other functions. Last note, instead of cin>> *niz[i]; just use cin>> niz[i];
In C++,
int* nix[]
is actually translated into
int** nix
at compile time. To fix this you can use either
int* nix
int nix[]
Also in the function body you would probably need to change
*nix[i]
into
nix[i]
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 9 years ago.
Improve this question
My code is following for finding maximum in an array
#include <iostream>
using namespace std;
int main(){
int i;
int array[i]={1,2,3,4,5}
int temp;
for(int i=0;i<6;i++)
{
if(array[i]>temp)
temp=A[i];
}
cout<<"the maximum number is "<<temp<<endl;
return 0;
}
but im getting error in line of initializing array why is that so? how do we initialize array?
You can't define the size of an array with a runtime variable.
To fix this you can use constexpr:
constexpr int i = 5;
int array[i]={1,2,3,4,5};
or:
int array[]={1,2,3,4,5};
In the latter the size is deduced by the compiler.
Otherwise, if you need a runtime size, you'll have to use std::vector or any other "dynamic" container from the standard library:
int i = ...;
std::vector<int> array(i); // reserve `i` cells
try this :
int array[]={1,2,3,4,5};
First of all variable i was not initialized
int i;
So it has some arbitrary value.
Secondly the size of a defined array shall be a constant exprssion. So even if i would be initialized this definition
int array[i]={1,2,3,4,5}
is also invalid. Moreover you forgot to place a semicolon after the closing brace.
Also you did not initialized variable temp
int temp;
And at last this control statement of the loop
for(int i=0;i<6;i++)
is also incorrect because the array has only 5 elements.
And one more identifier A was not declared
temp=A[i];
The correct code could look as
#include <iostream>
using namespace std;
int main()
{
const int N = 5;
int array[N] = { 1, 2, 3, 4, 5 };
int max = array[0];
for ( int i = 1; i < N; i++ )
{
if ( max < array[i] ) max = array[i];
}
cout << "the maximum number is " << max << endl;
return 0;
}