| Thread Closed |
Inputting an array in to a function or returning an array? (C++) |
Share Thread |
| Jan18-09, 12:08 AM | #1 |
|
Recognitions:
|
Inputting an array in to a function or returning an array? (C++)
I recently came up with a bit of a mathematical problem for myself and decided I needed a bit of code to help me. My understanding of C++ is pretty basic and I seem to have found myself stuck mixing arrays and functions.
I wrote the function: Code:
int worko (int c[], int tri)
{
//turns the array in to the number it represents
int ans = 0;
int i;
for (i=0; i < tri; i++)
{
ans = ans + 2^c[i]*(i+1);
}
return ans;
}
Code:
int test = worko(poss[],tri); Also I want to write a function which goes something like: Code:
int addone[] (int d[], int tri, int log)
{
d[0] = d[0]+1;
int j;
for (j=0; j < tri; j++)
{
if (d[i] > log)
{
d[j]=0;
d[j+1]++;
}
}
return d[];
}
, any idea what I need to be looking at to get around this? Thanks for any help at all.
|
| Jan18-09, 12:15 AM | #2 |
|
|
You almost surely want to be using std::vector instead of ordinary arrays.
But if you insist on using ordinary arrays, then they cannot be passed as a return value -- you have to pass them as arguments to your function 'by reference', so the function can modify them in place. Also, you seem to misunderstand the syntax of ordinary arrays. [] is a decoration used to describe the type when declaring a variable: it is not part of the name of the variable. By the way, I'm not sure exactly what the first function is supposed to be doing... but allow me to remind you that ^ is not exponentiation: it is bitwise exclusive-or. |
| Jan18-09, 12:29 AM | #3 |
|
Recognitions:
|
Thanks! I've not programed in C++ in about 2 years. I did forget about ^ (goes and looks up what the exponential is).
I have absolutely no insistence in using standard arrays, I'll go and look up whatever "std::vector" is .P.S The idea of the first function is that it takes the array [x1,...,xn] and calculates the value 1*2x1 + 2*2x2 + ... + n*2xn. I'm trying to work out the set of integers which can't be represented in that form, where {xi}i = 1,...,n is a monotonically decreasing sequence of non-negative integers. For example, 5 and 11 can't be represented in that form, I have a good idea which numbers in general can't be represented in that form but I wanted to quickly write something in to the computer to confirm my idea for a few more integers. |
| Jan18-09, 01:54 PM | #4 |
| Jan18-09, 02:16 PM | #5 |
|
|
As a preview: in C++, to write a function which takes an array, and returns the array scaled by a constant factor, the type signature would look like:
Code:
float* scale(float scalefactor, float arr[], int length); Code:
float* scale(float scalefactor, float arr[], int length) {
float* ans = new float[length];
for (int i = 0; i < length; i++) {
ans[i] = scalefactor * arr[i];
}
return ans;
}
Code:
float arr[] = {1,2,3};
float* ans = scale(10, arr, 3);
std::cout << ans[0] << " " << ans[1] << " " << ans[2] << "\n";
// prints "10 20 30"
Code:
float* ans = scale(10, arr, 3); Code:
float* ans = scale(10, arr[], 3); |
| Jan25-09, 06:52 PM | #6 |
|
|
I usually pass arrays around with pointers...just make sure they are in sequential memory addresses when allocating the memory like so:
const int nTri = 6; int rgC[nTri] = { 0 }; int nRes = worko(rgC, nTri); |
| Thread Closed |
Similar discussions for: Inputting an array in to a function or returning an array? (C++)
|
||||
| Thread | Forum | Replies | ||
| Passing a 2d array to a function in C | Programming & Comp Sci | 29 | ||
| read array | Programming & Comp Sci | 4 | ||
| converting from an array of function values to coordinate array of different length | Programming & Comp Sci | 3 | ||
| An array of questions. | General Astronomy | 7 | ||
| Average of an array | General Math | 3 | ||