How to use a void () function to print a vector in c ++?

advertisements

I have a function which calculates the squares of the numbers from 1 - 100. I would like to use another function to print the results. I would like to know how to make a function like that not only works with vectors but with any type.

Here's what i have so far:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>

using namespace std;

vector<double> Sqrs(int val, double sqr, vector<double> result){

for(val = 1;val < 100;val++){
    sqr = sqrt(val);
    result.push_back(sqr);
}
return result;
}
void Print(int val, vector<double> result){
    for(unsigned int i = 0;i < result.size();i++){
        cout << val << setw(10);
        cout << &result << endl;
    }
}
int main()
{
    Print();
    return 0;
}

The problem is that it says I have too few arguments on Print();. I dont know what arguments to put because when I put the val, result, or with int val, vector<double>result, it gives me a lot of errors. I just don't know how to make a separate function to print something I have used in another function other than main.

EDIT: It seems that i have asked the question stupidly. I would like to know for example i have a function that calculates 2 numbers named int val (int a, int b) and that function returns a value "c". So now i would like to make another function named Print and use the returned value from the other function val so i can print it. Then in the main function i would just call the Print() function.


A brief translation to English of your program at the highest level is:

  • Here are the libraries I want to use.
  • This is what I mean if I ever ask you to do Sqrs with an int, a double, and a vector<double>
  • This is what I mean if I ever ask you to do Print with an int and a vector<double>

and finally the thing you actually tell the program to do is

  • Print with nothing
  • Output 0

So as you can see, this code doesn't really seem to resemble any of the things you describe. Before you worry about trying to write very general code, you should work on trying to get a special case right first.