I have seen a lot of debate on wheather to use arrays or vectors for passing arrays in functions. I have a simple program here that does not quite seem to work properly as what I intended. The goal is to take the arrays from f1() and f2() and pass all the elements to the main where the elements are added togethor. I get a "segmentation fault" error.
How would I use vectors as an alternate to arrays?
#include <iostream>
#include <cmath>
using namespace std;
double f1(int a);
double f2(int b);
int main()
{
int a1, b1;
double numTot[6];
f1(a1);
f2(b1);
// sum of 2 arrays element by element
for(int k=0; k<=5; k++)
{
numTot[k] = f1(a1) + f2(b1);
cout << "numTot[" << k << "]: " << numTot[k] << endl;
}
return 0;
}
double f1(int a2)
{
double num1[6];
// place 6 numbers in array num1
for(int t=0; t<=5; t++)
{
num1[t] = t+1.0;
cout << "num1[" << t << "]: " << num1[t] << endl;
}
return num1[a2];
}
double f2(int b2)
{
double num2[6];
// place 6 numbers in array num2
for(int j=0; j<=5; j++)
{
num2[j] = j+2.0;
cout << "num2[" << j << "]: " << num2[j] << endl;
}
return num2[b2];
}
Thanks if anyone can clarify this.
:banana: