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
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 9 months ago.
Improve this question
#include<iostream>
using namespace std;
int getSum(int *arr, int size) {
if(size == 0) {
return 0;
}
if(size == 1 )
{
return arr[0];
}
int remainingPart = getSum(arr+1, size-1);
int sum = arr[0] + remainingPart;
return sum;
}
int main() {
int arr[100], n;
cin >> n;
for(int i = 0; i < n; i++){
cin >> arr[n];
}
int sum = getSum(arr, n);
cout << "Sum is " << sum << endl;
return 0;
}
OUTPUT
3
1
2
3
Sum is -1827678256
When i run the code with custom input it gives the correct output. But when i input data from the user it gives an incorrect answer. Why so?
cin >> arr[n];
should be
cin >> arr[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 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 3 years ago.
Improve this question
I was writing a code which displayed first three multiples of an integer using functions. But here when I run this it shows only the first multiple.
#include <iostream>
#include <vector>
// Defined first_three_multiples() here:
std::vector <int> first_three_multiples(int num) {
std::vector <int> output;
for (int i=1; i<4; i++) {
output.push_back(num*i);
return output;
}
}
int main() {
for (int element : first_three_multiples(8)) {
std::cout << element << "\n";
}
}
Expected Output: 8 16 24
Actual Output: 8
You have put the return statement inside the for loop. It is advised to follow proper code indentations in order to avoid these kind of errors from happening again.
#include <iostream>
#include <vector>
std::vector<int> first_three_multiples(int num)
{
std::vector<int> output;
for (int i = 1; i < 4; i++) {
output.push_back(num * i);
# you put a return statement here, which returns only 8
}
return output; # this would return all three values
}
int main()
{
for (int element : first_three_multiples(8)) {
std::cout << element << "\n";
}
}
Simple error
for (int i=1; i<4; i++) {
output.push_back(num*i);
return output;
}
should be
for (int i=1; i<4; i++) {
output.push_back(num*i);
}
return output;
It pays to get your indentation right as it makes errors like this much easier to spot (and avoid in the first place).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
Im trying to create a program that does various math operations, and i wanted to start with calculating prime numbers within a given range. However, when i try to execute the code, it just returns exit status -1. What is wrong with the program and how do i fix it?
#include <iostream>
#include <vector>
using namespace std;
void getPrimes(int min, int max) {
int range = max - min;
std::vector< int > possible_values;
for (int q = 0; q < range; q++) {
possible_values.push_back(min + q);
}
for (int i = 0; i < range; i++) {
int num_of_factors = 0;
int num = possible_values.at(i);
for (int c = 0; c < num; c++) {
if (num % c == 0) {
num_of_factors++;
}
}
if (num_of_factors == 0) {
std::cout << num << endl;
}
}
}
int main() {
int min, max;
std::cout << "min: ";
std::cin >> min;
std::cout << "max: ";
std::cin >> max;
getPrimes(min, max);
}
this loop should start from 2 because :
c%0 is undefined behavior
every number %1 is 0 so you can see why num_of_factors is never 0
for (int c = 2; c < num; c++) {
if (num % c == 0) {
num_of_factors++;
}
}
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.