Passing a double array to a function

Status
Not open for further replies.

iFargle

Linux / HPC / K8s SME
Staff member
Messages
4,403
Location
N/A
Halp :tongue:
Code:
Code:
#include <iostream>
using namespace std;

//Function Declarations
	void get_input(int &, double *);
	
int main()
{
	int max = 0;
	double num [max];
	
	get_input(max, num);
	
	return 0;
}

void get_input(int & max, double num[])
{
	while (max > 50 || max < 1)
	{		
		cout << "How many doubles do you wish to add?  "; cin >> max;
		if (max > 50 || max < 1)
		{
			cout << endl << "That is an invalid size.  You must enter an integer between 1 and 50." << endl;
		}
	}
	
	for (int i = 1; i <= max; i++)
	{
		cout << "\nMaximum:      " << max << endl;
		cout << "Enter number " << i << ":  "; cin >> num[i];
		cout << "Number " << i << ":  " << num[i] << endl;
	}
}

Output:
Code:
How many doubles do you wish to add? 5

Maximum: 5
Enter number 1: 1
Number 1: 1

Maximum: 5
Enter number 2: 2
Number 2: 2

Maximum: 1073741824
Enter number 3: 3
Number 3: 3

Maximum: 1073741824
Enter number 4: 4
Number 4: 4

Maximum: 1073741824
Enter number 5: 5
Number 5: 5

Maximum: 1073741824
Enter number 6: 

. . .
etc,etc

Max should stay the same. I don't even see HOW it's changing. It feels like a "deeper" problem than the code.. if that makes sense.
 
Code:
	while (max > 50 || max < 1)
	{		
		cout << "How many doubles do you wish to add?  "; cin >> max;
		if (max > 50 || max < 1)
		{
			cout << endl << "That is an invalid size.  You must enter an integer between 1 and 50." << endl;
		}
	}

Why do you keep looping the cin portion of code? Are you doing this for error checking, I assume? The only place I see max being changed is right there, unless a pointer is messing it up or something.

Looking into it now... will check on it more when I get home from work.
 
Since you don't know the size of Max at the start of the program, and you're creating the array right away, you'll need to use dynamic arrays:

Code:
#include <iostream>
using namespace std;

//Function Declarations
	void get_input(int &, double num[]);               //change declaration arguments to match new array type

int main()
{
	int max = 0;
	double * num;                //use dynamic arrays -- create the dynamic array variable here. 

	get_input(max, num);

	return 0;
}

void get_input(int & max, double num[])
{
	while (max > 50 || max < 1)
	{
		cout << "How many doubles do you wish to add?  ";
		cin >> max;
		if (max > 50 || max < 1)
		{
			cout << endl << "That is an invalid size.  You must enter an integer between 1 and 50." << endl;
		}
	}

    num = new double [max];                  //initialize the space for the array!
	for (int i = 0; i < max; i++)
	{
		cout << "\nMaximum:      " << max << endl;
		cout << "Enter number " << i + 1 << ":  ";
		cin >> num[i];
		cout << "Number " << i + 1 << ":  " << num[i] << endl;
	}
}

I put in comments on the lines that are important. In the for loop, I changed the index to start at 0, since arrays are 0 based in C++. Otherwise, you'll go outside the bounds of the array if you start at 1, and start assigning to the limit.

E.g., Array of size 5, and the array positions are 0 - 4. When i = 5, it'll try to assign into num[5], which is not a valid position, and give you an index out of bounds error.
 
He may not know the value of max, but he ought to know the maximum length of num. Not ideal, of course, but what is ideal with this sort of thing?

He could always make an arbitrarily large array (say an array of 100), and then set the max size, and base the counter off of that size. Not as efficient as it takes up unnecessary space, though.

Always more than one way to do something :).
 
Half a homework assignment and half my own.. the assignment doesn't require arrays, but I figured it's as good a time as any to learn it :tongue:

Thanks guys. I'll test next time I'm on a computer with a compiler :tongue:
 
Another question not dealing with the array... Why does the "Max" variable change in the for loop?
 
Most likely a pointer issue. In your original code, you didn't necessarily allocate space for the array. You allocated it as a size of 0 array to begin with, and then later started assigning data into the positions. After a few numbers, it ended up failing. (I think) you were basically just reading/writing to different parts of memory that it shouldn't have been. Not 100% sure on this one, but I'm guessing that's what it was doing.
 
Status
Not open for further replies.
Back
Top Bottom