Cannot convert 'int*' to 'int**' for argument '1'? [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 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]

Related

segmentation fault in recursive binary searching [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 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;
}

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);
}

I have to calculate difference of sum of diagonal elements of a square matrix [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 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';

issue with std vector in c++ [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 7 years ago.
Improve this question
I have an issue with my code, I can fill the table but when I display it, it display 0 for me, here is my code
#include<iostream>
#include<vector>
using namespace std;
int n;
void lire_tab(vector<int> A);
void tri_tab(vector<int> A);
void aff_tab(vector<int> A);
int main()
{
cout<<"donnez la taille du tableau: ";
cin>>n;
vector<int> A(n);
lire_tab(A);
aff_tab (A);
tri_tab (A);
aff_tab (A);
return 0;
}
void lire_tab(vector<int> A)
{
for(int i=0;i<n;i++)
{
cout<<"A["<<i<<"]= ";
cin>>A[i];
}
cout<<"________________________"<<endl;
}
void tri_tab(vector<int> A)
{
int temp;
temp=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++){
if(A[i]<A[j])
{
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
}
}
Can you help me please, I'm really lost in this, I don't really figure out why it print 0
Your functions are using passing by value, therefore, a copy is seen within the function.
Please read about passing by reference
void lire_tab(vector<int>& A);
Instead of accepting the vector by value
void lire_tab(vector<int> A)
You want to accept the vector by reference
void lire_tab(vector<int>& A)
Otherwise you are only modifying a function-local copy of your vector, not the original vector that was passed in.

how to initialize an array without any error? [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 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;
}