|
-
October 20th, 2010, 11:38 AM
#1
Returning vectors from functions
I am developing some code to run some basic CFD calculations for a class project. I am still learning C++ as I go. What I am trying to do specifically with this code is return a vector from a function. When I try to compile the code, I get a compiler error from Dev-C++. My code is
Code:
/* ME7310 Project 2: Diffusion - Combustion Equation
Due: 11-1-2010
P2diffusionconvection.cpp
*/
#include <fstream>
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
const long double a = 2.5;
const long double alpha = .005;
const long double xmax = 1;
const long double tf = .2;
long double uinit(long double del_x, int num_x) {
long double ui[num_x];
long double x[num_x];
for (int i = 0; i <= num_x; i++) {
x[i] = i * del_x;
if (x[i] == .2){
ui[i] = .5;}
else if (x[i] != .2){
if (x[i] < .2)
ui[i] = 1;
if (x[i] > .2)
ui[i] = 0;
}
}
return ui; //(line 32)
}
long double fvftcs(long double t, long double del_t, long double del_x) {
return 'd';
}
long double fvfouwc(long double t, long double del_t, long double del_x) {
return 'b';
}
long double fdftcs(long double t, long double del_t, long double del_x) {
return 'c';
}
long double fdfouwc(long double t, long double del_t, long double del_x) {
return 'v';
}
long double fdlwc(long double t, long double del_t, long double del_x) {
return 'x';
}
long double fdmcc(long double t, long double del_t, long double del_x) {
return 'z';
}
int main() {
long double t; //end time
long double del_t; //change in time
long double dnum_t; //dummy number of time steps
long int num_t; //number of time steps
long double del_x; //change in x
long double dnum_x; //dummy number of steps on x
long int num_x; //number of steps on x
int choose;
char file_name[32];
cout << "********************************************" << endl;
cout << "*ME7310 Project 2: Diffusion-Convection PDE*" << endl;
cout << "* Due: 11-1-2010*" << endl;
cout << "********************************************" << endl;
//input file name
cout << "\nEnter the file name: ";
cin >> file_name;
ofstream outputfile(file_name, ios::out);
//input parameters
cout << "Enter time (s): ";
cin >> t;
outputfile << "Time:," << t << "\n";
cout << "Enter delta t (s): ";
cin >> del_t;
outputfile << "Delta t:," << del_t << "\n";
cout << "Enter delta x (m): ";
cin >> del_x;
outputfile << "Delta x:," << del_x << "\n";
//find num_t and num_x
dnum_t = t / del_t;
num_t = static_cast<int>(dnum_t);
cout << "\nNumber of time steps: " << num_t << endl;
outputfile << "Time steps:," << num_t << "\n";
dnum_x = xmax / del_x;
num_x = static_cast<int>(dnum_x);
cout << "Number of x steps" << num_x << endl;
outputfile << "X steps:," << num_x << "\n";
long double x[num_x];
outputfile << "\nInitial Condition:" << "\n" << "\n";
for (int i = 0; i <= num_x; i++) {
x[i] = i * del_x;
outputfile << x[i] << ",";
}
outputfile << "\n";
long double ui[num_x];
ui = uinit(del_x, num_x); //(line 116)
for (int i = 0; i <= num_x; i++) {
outputfile << ui[i] << ",";
}
outputfile << "\n";
cout << "\n";
cout << "Choose explicit scheme to use." << endl;
cout << "1: Finite Volume: FTCS for both convection and diffusion" << endl;
cout << "2: Finite Volume: First order upwind for convection, FTCS for diffusion" << endl;
cout << "3: Finite Difference: FTCS for both convection and diffusion" << endl;
cout << "4: Finite Difference: First order upwind for convection, FTCS for diffusion" << endl;
cout << "5: Finite Difference: Lax-Wendroff for convection, FTCS for diffusion" << endl;
cout << "6: Finite Difference: MacCormack for convection, FTCS for diffusion" << endl;
cout << "7: Exit" << endl;
while (choose != 7){
cin >> choose;
if (choose == 1) {
outputfile << "\nFinite Volume: FTCS for both convection and diffusion.";
outputfile << "\n" << fvftcs(t, del_t, del_x) << "\n";
}
if (choose == 2) {
outputfile << "\nFinite Volume: First order upwind for convection, FTCS for diffusion.";
outputfile << "\n" << fvfouwc(t, del_t, del_x) << "\n";
}
if (choose == 3) {
outputfile << "\nFinite Difference: FTCS for both convection and diffusion.";
outputfile << "\n" << fdftcs(t, del_t, del_x) << "\n";
}
if (choose == 4) {
outputfile << "\nFinite Difference: First order upwind for convection, FTCS for diffusion.";
outputfile << "\n" << fdfouwc(t, del_t, del_x) << "\n";
}
if (choose == 5) {
outputfile << "\nLax-Wendroff for convection, FTCS for diffusion.";
outputfile << "\n" << fdlwc(t, del_t, del_x) << "\n";
}
if (choose == 6) {
outputfile << "\nFinite Difference: MacCormack for convection, FTCS for diffusion.";
outputfile << "\n" << fdmcc(t, del_t, del_x) << "\n";
}
if (choose == 7) {
system("PAUSE");
return EXIT_SUCCESS;
}
}
}
The compiler errors occur on line 32 and line 116.
The error for line 32 is " In function `long double uinit(long double, int)': cannot convert `long double*' to `long double' in return "
The error for line 116 is " In function `int main()': incompatible types in assignment of `long double' to `long double[((unsigned int)((int)num_x))]' "
I have not been able to tell what these errors mean, let alone fix them.
In essence, what this function, and the other as of yet undefined functions, need to do is take the stated inputs and return a vector as the output after performing the required mathematical operations.
Can anyone tell me what I did wrong and possibly how to fix it?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|