954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Return Array to main from function

I have a function that takes an array, calculates the ln of that array into another array called lnt. I would like to pass lnt back to the main. I know it has something to do with int *foo or something but I cannot seem to get a code to work.

// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"
#include <math.h>

#using <mscorlib.dll>
using namespace System::IO;
using namespace System;
const int n=10;

//function declaration
int** FindLN(double t[n]);


int _tmain()
{
	String* path = "DailyReport(2).csv";
	String* MRVF= "Merrill - Valley Farms";
	
	//create array of times of outages
	//n = number of outages 
	
	double t[n]={15.70, 29.39, 41.14, 56.47, 75.61, 98.83 ,112.42, 125.61, 129.39, 133.45};
	
	
	FindLN( t);
	int ** lnt = foo(5);
	
	return 0;
}
	int** FindLN(double t[n])
{
	int index;
	double lnt[n];
	for (index=0; index<n; index++)
	{
		lnt[index]=log(t[index]);
		//Console::WriteLine(lnt[index]);
		return lnt;
	}
}
JordanHam
Light Poster
41 posts since Jan 2011
Reputation Points: 8
Solved Threads: 0
 

First, move your return statement on Line 40 outside of your for loop. As written, the function returns at the end of the first iteration of the loop instead of at the end of the function.

Second, FindLN() returns an int*, but lnt is a double*. You better double-check what you actually need and adjust one or the other accordingly.

Third, on Line 28, you attempt to call the function foo(). There is no function in your program called "foo".

Fix those, then let us know if you have any other issues.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

I have it set up to what I think will pass the pointer of my array lnt, how exactly do I call or reference it in the main now?

// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"
#include <math.h>

#using <mscorlib.dll>
using namespace System::IO;
using namespace System;
const int n=10;

//function declaration
double* FindLN(double t[n]);


int _tmain()
{
	String* path = "DailyReport(2).csv";
	String* MRVF= "Merrill - Valley Farms";
	
	//create array of times of outages
	//n = number of outages 
	
	double t[n]={15.70, 29.39, 41.14, 56.47, 75.61, 98.83 ,112.42, 125.61, 129.39, 133.45};
	
	
	FindLN( t);
	
	return 0;
}
	double * FindLN(double t[n])
{
	int index;
	double lnt[n];
	for (index=0; index<n; index++)
	{
		lnt[index]=log(t[index]);
		//Console::WriteLine(lnt[index]);
	}
	return lnt;
}
JordanHam
Light Poster
41 posts since Jan 2011
Reputation Points: 8
Solved Threads: 0
 

The pointer is a return value. You need a variable of appropriate data type that is local to main() that can be used to catch the return.

int someFunc();

int main() {
  int myReturnValue = 0;

  cout << "The value of myReturnValue is: " << myReturnValue << endl;

  myReturnValue = someFunc();

  cout << "The value of myReturnValue is now: " << myReturnValue << endl;

  return 0;
}

int someFunc() {
  return 1;
}
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

That makes sense, but they give me an error of "function does not take 0 arguements" when I use the code

myReturnValue = FindLN();


because my FindLN() function takes the array "double t[n]". When I put that value into the code it gives even more errors. I guess what I am asking is what goes in the () of the line

myReturnValue = FindLN();

// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"
#include <math.h>

#using <mscorlib.dll>
using namespace System::IO;
using namespace System;
const int n=10;

//function declaration
double* FindLN(double t[n]);


int _tmain()
{
	String* path = "DailyReport(2).csv";
	String* MRVF= "Merrill - Valley Farms";
	
	//create array of times of outages
	//n = number of outages 
	
	double t[n]={15.70, 29.39, 41.14, 56.47, 75.61, 98.83 ,112.42, 125.61, 129.39, 133.45};
	int myReturnValue = 0;   
	myReturnValue = FindLN();
	
	FindLN( t);
	
	return 0;
}
double * FindLN(double t[n])
{
	int index;
	double lnt[n];
	for (index=0; index<n; index++)
	{
		lnt[index]=log(t[index]);
		//Console::WriteLine(lnt[index]);
	}
	return lnt;
}
JordanHam
Light Poster
41 posts since Jan 2011
Reputation Points: 8
Solved Threads: 0
 

If i was not passing the first array to the function, this would be very easy

JordanHam
Light Poster
41 posts since Jan 2011
Reputation Points: 8
Solved Threads: 0
 

It still is very easy. What I posted was just an example. You have to adapt it to your code... Change the data types, names, AND FUNCTION ARGUMENTS as needed to suit the context.

You already know how to call FindLN() properly. How were you calling it before? The only difference is you're adding an assignment operator as part of the statement.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: