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

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).

Related

C++ Priciples and Practice Exercise - Finding primes from input value n [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Found this answer to Ch4Ex15 of Stroustrups beginner book, the question is to find the first n amount of primes:
#include "std_lib_facilities.h"
bool prime (vector<int> table, int number) {
for (int i = 0; i < table.size(); ++i)
if (number%table[i] == 0) return false;
return true;
}
int main () {
int count, next;
cout << "Input the number of primes\n";
cin >> count;
vector<int> table;
next = 2;
while (table.size() < count) {
if (prime(table,next)) table.push_back(next);
++next;
}
for (int n = 0; n < table.size(); ++n)
cout << table[n] << " ";
cout << endl;
// keep_window_open();
return 0;
}
Two things I'm struggling to understand:
Why is there a section of code outside int main at the top, is the executed after int main?
How do these statements work (are they double conditions?)
bool prime (vector<int> table, int number)
and
if (prime(table,next))
Thanks,
Sean
The things you are asking are quite fundamental to the language of C and C++. A read through the first 2-3 chapters of any good C++ textbook would answer these questions for you.
The example code defines 2 functions: prime and main.
The code outside main is the definition of a prime function. It is defined (created) there for you to call later, in the main function.
These are two separate things. The first thing you mention is the definition of the function prime, the second is the call to that function.

Function doesn't print anything c++ [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
#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?

no matching function for call to [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 6 years ago.
Improve this question
#include <iostream>
using namespace std;
int n;
void displaysum(double mat[n][n])
{
double sum= 0;
for(int j=0;j<n;j++)
sum += mat[j][j];
cout<<"Sum of Diagnols Elements is \n"<<sum;
}
int main()
{
cout << "what are the number of rows or column in the matrix" << endl;
cin >> n;
double matrix[n][n];
for (int row = 0; row < n; row++)
{
for (int column = 0; column < n; column++)
cin >> matrix[row][column];
}
displaysum(matrix)
return 0;
}
I don't understand why I get an error for no matching function to call inXCODE. Even if I try to change the variable in my function prototype it still gives me the same error.
I don't understand why I get an error for no matching function to call inXCODE.
The basic issue is that C++ wants the second dimension to be constant at compile-time for the sake of type-checking. If you want to get around this you'll have to use pointers (AFAIK). You can make this work by changing the declaration of displaysum to
void displaysum(double **mat)
and making appropriate allocations for matrix in the original function.
If you don't like this, well, welcome to C++'s type system. In the function declaration, double mat[n][n] is seen as double (*)[n]. This actually makes sense, but why it doesn't see matrix as being of that type is because n isn't constant. You can change the call
displaysum(matrix);
to this:
displaysum(static_cast<double (*)[n]>(matrix);
and receive the curious error
static_cast from 'double (*)[n]' to 'double (*)[n]' is not allowed
(and that's not the strangest error you'll get out of the type system)

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

how to sort an array in c++ using command line turbo c++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
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.
Improve this question
I have an exercise for sort an array in c++. I'm coding with command line programming from turbo c++.
You can use this code, it uses bubble sort algorithm.
#include <stdio.h>
#include <iostream>
using namespace std;
void sort(int *,int);
int main()
{
int arr[10] = {2,3,4,12,5,0,2,5,1,20};
sort(arr,10);
for(int i = 0;i<10;i++)
cout << arr[i] << " ";
return 0;
}
void sort(int * ar,int length)
{
for(int i = 0;i<length;i++)
{
for(int j = i;j<length;j++)
{
if(ar[j] < ar[i])
{
int swap = ar[i];
ar[i] = ar[j];
ar[j] = swap;
}
}
}
}
EDIT:
As I said it's based on the bubble algorithm. It sequentially checks the indexes from first one to last one and automatically puts the smallest number in first place, second smallest in second place and so on. You can see here or here for more information.
Here is how i did it. Hope this is what you mean.
Read the command line arguments
Convert the string to integer using atoi function
Sort using any sorting algorithm (bubble sort used here)
Print the result
To see how bubble sort works check out this.
#include<iostream.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
int i,j,temp, arr[10];
for(i=1; i<argc; ++i)
{
arr[i-1] = atoi(argv[i]);
}
/* Sort the array using bubble sort alogrithm */
for(i=0; i<argc-2; ++i)
{
for(j=0; j<argc-2; ++j)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
/* Print the result */
cout<<"Sorted array : ";
for(i=0; i<argc-1; ++i)
{
cout<<" "<<arr[i];
}
return 0;
}