• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C++ Arrays and Functions..

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Tacoman667

Member
Joined
Apr 28, 2001
Location
Kingwood, TX
I wrote a program for my class and have run into a logic error. I wrote:

amount[maxnum] = extend();

Then, in my funtion declaration I wrote:

float extend()
{
int x;
float num[maxnum];

for (x = 0; x < maxnum; x++)
num[x] = price[x] * quantity[x];

return num[maxnum];
}

My output ends up being a freaking crazy number in exponential form. Am I missing a rule on returning a array value and assigning it to the origional array within the main() function?
 
i haven't programed c++ in years but uh, your declaring float num array to the length maxnum, but your max num is going to be returned by the extend(); function, that won't work since none of those varibles exist yet...
 
The problem is that your for loop sets the num array from index 0 to maxnum - 1. You return the index at maxnum, which has not be initialized, and is in fact, outside the bounds of the array. It's hard to say how to fix this without seeing the rest of your code, but if you return num[maxnum - 1], you'll be returning the last element of your array, which seems to be what you intended.
 
The entire code:

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

const int maxnum = 10;
float price[maxnum] = {10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98};
float quantity[maxnum] = {4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.8};
float extend();

int main()
{
float amount[maxnum];
int x;

amount[maxnum] = extend();

cout << "\n Price Quantity Amount";
cout << "\n ----- -------- ------";

for(x = 0; x < maxnum; x++)
{
cout << "\n" << setw(5) << setiosflags(ios::fixed) << setprecision(2)
<< price[x] << " " << setw(5) << setiosflags(ios::fixed)
<< setprecision(2) << quantity[x] << " " << setw(6)
<< setiosflags(ios::fixed) << setprecision(2) << amount[x];
}

cout << "\n\n\nPress any key to continue...";
getch();

return 0;
}

float extend()
{
int x;
float num[maxnum];

for (x = 0; x < maxnum; x++)
num[x] = price[x] * quantity[x];

return num[maxnum];
}
 
I think you are trying to return an array from a function. You can not do this in C++. The usual way to do this is to pass an array reference to a function and modify its values. Since this is a reference, those values will still be changed.

Code:
#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;

const int maxnum = 10;
float price[maxnum] = {10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98};
float quantity[maxnum] = {4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.8};
[COLOR=Blue]void extend(float result[maxnum]);[/COLOR]

int main()
{
	float amount[maxnum];
	int x;

	[COLOR=Blue]extend(amount);[/COLOR]

	cout << "\n Price Quantity Amount";
	cout << "\n ----- -------- ------";

	for(x = 0; x < maxnum; x++)
	{
		cout << "\n" << setw(5) << setiosflags(ios::fixed) << setprecision(2)
			<< price[x] << " " << setw(5) << setiosflags(ios::fixed)
			<< setprecision(2) << quantity[x] << " " << setw(6) 
			<< setiosflags(ios::fixed) << setprecision(2) << amount[x];
	} 

	cout << "\n\n\nPress any key to continue...";
	getch();

	return 0;
}

[COLOR=Blue]void extend(float num[maxnum])
{
	int x;

	for (x = 0; x < maxnum; x++)
		num[x] = price[x] * quantity[x]; 

}[/COLOR]
 
That's what I needed to know. My function will only be the equation I suppose, and I have to use the for statement to assign each element of my amount[] array within the main() function when i call the extend() function, right?
 
Tacoman667 said:
That's what I needed to know. My function will only be the equation I suppose, and I have to use the for statement to assign each element of my amount[] array within the main() function when i call the extend() function, right?

You could do it that way, but the way he showed in the example works as well. You simply pass the address of the array to the function (aka passing by reference instead of by value), and let the function fill the array for you. Then, in main() after the call to the function, the array is filled with what you're expecting. This is a much better way to do it than polluting your main() function with task-specific code as far as readability goes.
 
But, doesnt VOID function() cause nothing to be passed back to the main()? How would this help the program if the calculations in the function never leave the function in order to be assigned to the array and then get displayed at the end of the main() function?
 
When you pass an array into a function it is passed by reference. This means that instead of copying the arrays values into the local space for the function, it just copies the location in memory that the array is stored at. Then, when the function modifies it, it modifies it in its original location, not a local copy like with non-array variables.

So num in the extend() function is the same as amount in the main() function, not a copy of it. It doesn't need to be returned.

It'll make more sense when you learn about pointers.
 
Pointers is next chapter. But my question now, is can you pass an array to a function, set values for all of the elements, then pass the entire array of elements back into the origional array in main()? I know the array declared in the function will be destroyed after the return statement but can this be done before?
 
Tacoman667 said:
But my question now, is can you pass an array to a function, set values for all of the elements, then pass the entire array of elements back into the origional array in main()?
No, not quite. You can't return an array from a function. But what you can do is pass an array to the function. It will be passed by reference since it is an array. This means that if you change the array values in the function it will change the array values in the caller.

When you pass an array to a function you don't get another array. What you get is a reference to the same array. This is different than when you pass an integer, when you get another integer. Instead you get a reference to the same array.

In the code I wrote above the num variable in the extend() function is a reference. When the function is called from main() the num variable is set to be a reference to the ammount variable from the main() function. When you change the reference you change what it refers to, that is, the ammount variable from main().

The easist way to return an array from a function is to pass an array as a paramater and use the changed values. There are other methods, but they are more advanced, dealing with pointers or templates.
 
Well, technically that's not quite true. You can return an array from a function, but only as a pointer to an array, which brings us back full circle. I have written several C functions that malloc space, create a string and return the string to the calling function... but alas, he hasn't gotten into pointers yet, so it is a moot point.

Fair warning, Tacoman667, it's all been easy up to now. Address/pointer use is probably the most difficult concept of C/C++ programming to grasp, especially when you start getting into pointers to arrays of pointers, linked lists, binary trees, etc. However, if you stick with it, when the light bulb goes off and you get it, you'll be glad. They are extremely useful.
 
Back