Function doesn't print anything c++ [closed] - c++

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 5 years ago.
Improve this question
#include <iostream>
using namespace std;
int compute_binare_code(int n, int a[][4]){
int i,j;
int bC = 0, p = 1;
for(i = 1;i <= n;i++){
for(j = i+1;j <= n;j++){ //just counting bC
bC += a[i][j]*p;
p =p*2;
}
}
return bC;
}
int main(){
cout << "mata3";
int a[4][4],b[5][5],i,j;
for(i=1;i<=4;i++)
for(j=1;j<=4;j++)
if( (i+j)%2 == 0)
a[i][j]=0;
else
a[i][j]=1;
cout<<"mata1";
cout<<compute_binare_code(4, a);
cout<<"mata2";
return 0;
}
When i run this program, it doesn't give any error but it runs in background forever. It does not print anything, not even "mata3". Can someone explain me why?

Arrays in C++ are indexed from 0.
Your for loop increments i/j from 1 to 4, but it should be 0 to 3.
See also Accessing an array out of bounds gives no error, why?

Related

can someone please explain why the output of this code is 30? [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 4 months ago.
Improve this question
Side note: I've been trying to trace this code, but every time I end up with output, it seems
to be far more or less than the right answer which is 30. If anybody can explain in to me, I'd be more than appreciated. Thanks
Always make sure to add { and } to loops so that you avoid thinking that some instruction is within a loop when it's actually not.
The code you added in the screenshot is equivalent to the following
#include <iostream>
using namespace std;
int main() {
int t = 0;
for (int c = 0; c < 3; ++c) {
// +8
for (int v = 0; v < 4; ++v) {
for (int h = 0; h < 2; ++h) {
++t;
}
}
// +2
++t;
++t;
}
cout << t << endl;
return 0;
}
So, each iteration of the first loop (variable c) adds 10 to t. There are three iterations of the first loop, the total value added to t is 30.

My code seems to have a syntax error in finding the maximum number [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 5 years ago.
Improve this question
What is wrong with my code?
It has to find the maximum between n numbers.
#include<iostream>
using namespace std;
int main()
{
int n,i = 0;
cin >> n;
int a[n];
while(i < n)
{
cin >> a[i];
i++;
}
i=1;
while(i <= (n + 1))
{
if (a[i] > a[0])
{
a[0] = a[i];
}
i++;
}
cout << a[0];
return 0;
}
This is meant to be a learning thing for you so I don't want to do your code for you. That being said I would look at while(i <= (n + 1)). You are stepping outside of the bounds of your array.

Sum of the first 5 even numbers from the array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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
So, I need to get the sum of the first 5 even numbers from my array, this is the code that I've got so far, have no clue what to do next. It runs, but the result is incorrect
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int niz[10];
cout << "Unesi 10 brojeva:";
for (int i = 0; i < 10; i++) {
cin >> niz[i];
}
int suma = 0;
int parni[5];
int j = 0;
for (int i = 0; i < 10; i++) {
if (niz[i] % 2 == 0) {
niz[i] == parni[j];
j++;
if (j == 5) {
break;
}
}
}
for (int i = 0; i < 5; i++) {
suma = parni[i] + suma;
}
cout << suma;
system("PAUSE");
return 0;
}
This line:
niz[i] == parni[j];
does nothing. (It tests if niz[i] happens to be equal to the current, uninitialized value of parni[j], and throws away the result of the comparison.)
You want to store niz[i] in parni[j], so do this:
parni[j] = niz[i];
Incidentally, there is a problem if there are fewer than 5 even numbers in the niz array. In that case, you still sum up all five entries of the parni array, using uninitialized values, which is Bad. One way to avoid this is to just sum up the even entries as you find them, without using a secondary array.
IE, do suma += niz[i] at the line in question, and get rid of parni altogether.
Unless you're really required to use arrays here, vectors will work much more nicely.
You can also use a couple of standard algorithms to make your life easier (especially std::copy_if and std::accumulate).
// for the moment I'll ignore the code to read the input from the user:
auto input = read_input();
auto pos = std::remove_if(input.begin(), input.end(),
[](int i) { return i % 2 != 0; });
// assume that `input` always contains at least 5 elements
pos = std::min(pos, input.begin() + 5);
sum = std::accumulate(input.begin(), pos, 0);

program which check perfect numbers - something gets wrong [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 8 years ago.
Improve this question
I have to write a function which gets a number, and prints all the perfect numbers that are smaller than the number given. If there are not any, the function prints an appropriate message.
The program I have just written is compiled well, but it's working only partially. If the input number is 5, for instance, for some reason I don't get any message that there are not perfect numbers in this range (=until 5) .
Can someone please explain to me what's wrong in this program?
I would appreciate any help!
#include<iostream>
using namespace std;
void check (int num, int & j);
void main()
{
int num,j, count;
cout << "List all the perfect numbers less than: ";
cin >> num;
check (num, j);
}
void check (int num, int & j)
{
int i,sum, count=0;
for(j=2;j<num;num++)
{
sum=0;
for(i=1;i<j;i++)
{
if(j%i == 0)
sum += i;
}
if(sum == j)
{cout << j <<endl;
count+=1;
}
}
if (count==0)
cout<<"there are no perfect numbers"<<endl;
}
There is an infinite loop in your code:
for(j=2;j<num;num++)
I think it should be
for(j=2;j<num;j++)

Using a vector as a parameter (C++) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm trying to print the even numbers of the first 25 Fibonacci numbers. However, I think I have a problem when using a vector as a parameter for my function below. Do you see what I'm doing wrong?
#include <iostream>
#include <vector>
using namespace std;
int main(){
int j=1, k=1, sum;
vector<int> myvector(25);
for (int i=0; i<25; i++) {
//cout << j << " ";
myvector[i] = j;
sum=j+k;
j=k;
k=sum;
}
findeven(myvector);
system("pause");
}
int findeven (vector<int>){
for (int i = 0, i < 25; i++){
if (vector[i] % 2 == 0){
cout << vector[i];
}
}
else{
}
}
vector<int> is just a type name. You need to name the parameter to be able to use it. You also can't use a type name as variable as you attempt to do in your loop. Fixed code:
int findeven( vector<int> v ) {
if (v[i] % 2 == 0)
cout << v[i];
//...
}
Since you don't change the vector inside the function, it'd be a good idea to pass it by const reference to avoid copying it:
int findeven( const vector<int>& v );
You'll also need to make the function visible before you use it. Right now, it's defined after main function and you'll get an error because you're trying to call it where the compiler hasn't seen its declaration yet. Put it before main (or at least its declaration).