vendredi 26 février 2016

Calling a function in a project C++

I am tasked with a project of calling a function from another file instead of coding everything in the same script. I must generate 2 random integers from my main script then pass it to my side function compute it and display the result out on my main script.

Suppose value p = 5, q = 3, the side function will compute the sum: 33333 + 3333 + 333 + 33 + 3 = sum

Suppose value p = 3, q = 6, the side function will compute the sum: 666 + 66 + 6 = sum

I am required to generate 20 sets of such values randomly using c++11 random engine. I am able to get the gist of how the codes are suppose to be like but i am constantly confused about how to pass the function value, retain the function value and compute the final value.

Below is my error

    ||=== Build: Debug in lab5task1 (compiler: GNU GCC Compiler) ===|
    C:\Users\lteo007\Documents\lab5task1\lab5task1.cpp||In function 'int main()':|
    C:\Users\lteo007\Documents\lab5task1\lab5task1.cpp|29|error: expected primary-expression before 'int'|
    C:\Users\lteo007\Documents\lab5task1\lab5task1.cpp|29|error: expected primary-expression before 'int'|
    ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Below are my codes:

main code

#include <iostream>
#include <random>
#include <ctime>

using namespace std;

int generate_integer(int, int );
int add_integers(int, int );



int main()
{
    mt19937 rand_generator;

    rand_generator.seed(time(0)); //random seeding using current time

    uniform_int_distribution<int> rand_distribution(1,9);



    for(int i=0; i<20; i++)
    {
        int rand_num1, rand_num2;
        rand_num1 = rand_distribution(rand_generator);
        rand_num2 = rand_distribution(rand_generator);
        cout << "1st parameter" << rand_num1 << "        "  ;
        cout << "2nd parameter" << rand_num2 << "        "  ;
        cout << "sum" << add_integers(rand_num1,rand_num2)<< endl;
    }
    return 0;
}

and here is the side function

#include <iostream>
using namespace std;

int generate_integer(int m, int n);

int add_integer()
{
    int sum = 0;
    int p,q;

    for(int i = p; i > 0; i--)
        {
            sum = generate_integer(i,q) + sum;
            cout << sum << endl;
        }
    return sum;

}

int generate_integer(int m, int n)
{
    for(int i = 0; i < m; i++)
        {
            cout << n;
        }
}




Aucun commentaire:

Enregistrer un commentaire