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.
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 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 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.
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 4 years ago.
Improve this question
My expected output of the following code is "1 -1 3 4 5 6".
(arr[1]=2 should change to arr[1]=-1). When I run this code,
nothing changes, and I can't understand why.
What would be the difference if parameter "seek" will be received by "find" function by value and not by reference?
When I declare that "seek" is a value, the program not running (0xC0000005)... but why does this happen?
#include <iostream>
int& find (int arr[], int size, int& seek)
{
for (int i; i<size; i++) {
if (arr[i]==seek) return arr[i];
}
return seek;
}
void print (int arr[], int size)
{
for (int i=0; i<size; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
int main ()
{
int arr[]={1,2,3,4,5,6};
int size = sizeof arr / sizeof *arr;
int seek=2;
find (arr, size, seek) = -1;
print(arr, size);
return 0;
}
for (int i; i<size; i++) {
You need to initialise i here
for (int i = 0; i<size; i++) {
As for the second part - see #Some programmer dude's comment
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]