Function print with error

Asked

Viewed 142 times

6

I made a program that calculates Fibonacci numbers in X and Y using Z as an auxiliary.

When I pass the vector from the Fibonacci function to the function imprime, she doesn’t print. I did a test by putting a cout within the for of the calculation and found that the error is precisely in the function imprime, bearing in mind that the cout in the calculation does everything exactly as it had to do. I suspect the problem is exactly in the for of imprime.

I believe this information will help: my role imprime generates an error that I don’t know how to solve:

C4018 '<': Unsigned Mismatch

Program. h:

#pragma once
#ifndef __MEU_H_INCLUDED__
#define __MEU_H_INCLUDED__

#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>



void imprime(std::vector<int>& v);
void fibonacci(int x, int y, std::vector<int> fi, int numeroElementos);

#endif 

cpp of implementation:

#include "stdafx.h"
#include "meu.h"


using std::cout;
using std::endl;


void imprime(std::vector<int>& v)
{

    for(int x = 0; x < v.size(); x++)
        cout << v[x];
}

void fibonacci(int x, int y, std::vector<int> fi, int numeroElementos)
{

    int z = 0;

    for (int b = 0; b <= numeroElementos; b++)
    {
        z = x + y;
        fi.push_back(z);
        x = y;
        y = z;      
    }           
}

Main.cpp :

#include "stdafx.h"
#include "meu.h"

int main()
{
    int valor1 = 0;
    int valor2 = 0;
    int NumeroElementos = 0;
    std::vector<int> numeros;

     while (std::cin >> valor1 >> valor2 >> NumeroElementos) {
         fibonacci(valor1, valor2, numeros, NumeroElementos);
         imprime(numeros);
    }





    system("pause");

    return 0;
}

2 answers

4


You would need to pass the vector always by reference, as you did correctly in the function imprime(). Then it seems that the error is in it, but actually it occurred before and the vector is not correctly filled as expected. I would simplify the code like this:

#include <iostream>
#include <vector>
using namespace std;

void imprime(vector<int>& v) {
    for(int x = 0; x < v.size(); x++) cout << v[x] << " ";
}

void fibonacci(int x, int y, vector<int>& fi) {
    int z = 0;
    for (int b = 0; b < fi.size(); b++) {
        z = x + y;
        fi[b] = z;
        x = y;
        y = z;      
    }           
}

int main() {
    int valor1, valor2, numeroElementos;
    while (cin >> valor1 >> valor2 >> numeroElementos) {
        vector<int> numeros(numeroElementos);
        fibonacci(valor1, valor2, numeros);
        imprime(numeros);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

3

Change this:

void fibonacci(int x, int y, std::vector<int> fi, int numeroElementos);

therefore:

void fibonacci(int x, int y, std::vector<int>& fi, int numeroElementos);

And make the same change in the function definition. Also, declare the vector numeros within the while, gets like this:

while (std::cin >> valor1 >> valor2 >> NumeroElementos) {
    std::vector<int> numeros;
    fibonacci(valor1, valor2, numeros, NumeroElementos);
    imprime(numeros);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.