Passing a double array to a function - Techist - Tech Forum

Go Back   Techist - Tech Forum > Computer Software > Programming
Click Here to Login
Closed Thread
 
Thread Tools Display Modes
 
Old 05-23-2012, 03:52 PM   #1 (permalink)
 
iFargle's Avatar
 
Join Date: Jan 2009
Location: United States
Posts: 3,993
Default Passing a double array to a function

Halp
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.
__________________

__________________
7700k • 32GB RAM • 512GB 960 Pro • GTX 1080 • NCase M1
15" Retina MBP || 64GB Nexus 6P || 64GB iPad Air 2
Battle.net: iFargle #1561 || Steam/Origin: iFargle
iFargle is offline  
Old 05-23-2012, 04:23 PM   #2 (permalink)
Private Joker
 
carnageX's Avatar
 
Join Date: Feb 2007
Location: South Dakota
Posts: 24,444
Default Re: Passing a double array to a function

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.
__________________

__________________
Laptop: MSI GT70 2OC-059us | i7-4700MQ | 16GB | GTX 770m | 500GB SSD / 750GB HDD | 17.3" | Win10 Pro
Desktop: 4690k | 12GB g.Skill RipJaws | GTX 970 | 520hx | Z87X-UD4H | Corsair Vengeance C70 | Corsair H110 | Acer 25" | Acer 22" | Win10
Mobile: Samsung Galaxy Note 5


If I help you, or you just like what I said, rep me by clicking the under my post
carnageX is online now  
Old 05-23-2012, 04:51 PM   #3 (permalink)
Seg Fault'n
 
Join Date: Jul 2005
Location: The dried husk where America came from
Posts: 5,801
Default Re: Passing a double array to a function

Quote:
Originally Posted by iFargle View Post
It feels like a "deeper" problem than the code.. if that makes sense.
You could describe it as a deeper problem but not difficult to fix, especially when you start cutting out the stuff you don't need.
Is this school work or just something you're doing on your own?
__________________


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
"Get your money and let's go to court!" - Craig Mudie, MS
kmote is offline  
Old 05-23-2012, 04:55 PM   #4 (permalink)
Private Joker
 
carnageX's Avatar
 
Join Date: Feb 2007
Location: South Dakota
Posts: 24,444
Default Re: Passing a double array to a function

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.
__________________
Laptop: MSI GT70 2OC-059us | i7-4700MQ | 16GB | GTX 770m | 500GB SSD / 750GB HDD | 17.3" | Win10 Pro
Desktop: 4690k | 12GB g.Skill RipJaws | GTX 970 | 520hx | Z87X-UD4H | Corsair Vengeance C70 | Corsair H110 | Acer 25" | Acer 22" | Win10
Mobile: Samsung Galaxy Note 5


If I help you, or you just like what I said, rep me by clicking the under my post
carnageX is online now  
Old 05-23-2012, 05:32 PM   #5 (permalink)
Seg Fault'n
 
Join Date: Jul 2005
Location: The dried husk where America came from
Posts: 5,801
Default Re: Passing a double array to a function

Quote:
Originally Posted by carnageX View Post
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
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?
__________________


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
"Get your money and let's go to court!" - Craig Mudie, MS
kmote is offline  
Old 05-23-2012, 06:14 PM   #6 (permalink)
Private Joker
 
carnageX's Avatar
 
Join Date: Feb 2007
Location: South Dakota
Posts: 24,444
Default Re: Passing a double array to a function

Quote:
Originally Posted by kmote View Post
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 .
__________________
Laptop: MSI GT70 2OC-059us | i7-4700MQ | 16GB | GTX 770m | 500GB SSD / 750GB HDD | 17.3" | Win10 Pro
Desktop: 4690k | 12GB g.Skill RipJaws | GTX 970 | 520hx | Z87X-UD4H | Corsair Vengeance C70 | Corsair H110 | Acer 25" | Acer 22" | Win10
Mobile: Samsung Galaxy Note 5


If I help you, or you just like what I said, rep me by clicking the under my post
carnageX is online now  
Old 05-23-2012, 07:45 PM   #7 (permalink)
 
iFargle's Avatar
 
Join Date: Jan 2009
Location: United States
Posts: 3,993
Default Re: Passing a double array to a function

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

Thanks guys. I'll test next time I'm on a computer with a compiler
__________________
7700k • 32GB RAM • 512GB 960 Pro • GTX 1080 • NCase M1
15" Retina MBP || 64GB Nexus 6P || 64GB iPad Air 2
Battle.net: iFargle #1561 || Steam/Origin: iFargle
iFargle is offline  
Old 05-23-2012, 08:07 PM   #8 (permalink)
Private Joker
 
carnageX's Avatar
 
Join Date: Feb 2007
Location: South Dakota
Posts: 24,444
Default Re: Passing a double array to a function

Glad to help, let us know if you have any other problems.
__________________
Laptop: MSI GT70 2OC-059us | i7-4700MQ | 16GB | GTX 770m | 500GB SSD / 750GB HDD | 17.3" | Win10 Pro
Desktop: 4690k | 12GB g.Skill RipJaws | GTX 970 | 520hx | Z87X-UD4H | Corsair Vengeance C70 | Corsair H110 | Acer 25" | Acer 22" | Win10
Mobile: Samsung Galaxy Note 5


If I help you, or you just like what I said, rep me by clicking the under my post
carnageX is online now  
Old 05-23-2012, 09:05 PM   #9 (permalink)
 
iFargle's Avatar
 
Join Date: Jan 2009
Location: United States
Posts: 3,993
Default Re: Passing a double array to a function

Another question not dealing with the array... Why does the "Max" variable change in the for loop?
__________________
7700k • 32GB RAM • 512GB 960 Pro • GTX 1080 • NCase M1
15" Retina MBP || 64GB Nexus 6P || 64GB iPad Air 2
Battle.net: iFargle #1561 || Steam/Origin: iFargle
iFargle is offline  
Old 05-24-2012, 12:20 AM   #10 (permalink)
Private Joker
 
carnageX's Avatar
 
Join Date: Feb 2007
Location: South Dakota
Posts: 24,444
Default Re: Passing a double array to a function

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.
__________________

__________________
Laptop: MSI GT70 2OC-059us | i7-4700MQ | 16GB | GTX 770m | 500GB SSD / 750GB HDD | 17.3" | Win10 Pro
Desktop: 4690k | 12GB g.Skill RipJaws | GTX 970 | 520hx | Z87X-UD4H | Corsair Vengeance C70 | Corsair H110 | Acer 25" | Acer 22" | Win10
Mobile: Samsung Galaxy Note 5


If I help you, or you just like what I said, rep me by clicking the under my post
carnageX is online now  
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing php variables to javascript - need help reedjasonf Programming 6 11-06-2009 01:10 PM
Passing variables using in the URL Osiris Tips, Tricks & Tutorials 0 03-08-2009 07:26 PM
Configure function for double right-click Osiris Tips, Tricks & Tutorials 0 02-22-2008 09:30 AM
C# Variable Passing Question Baez Programming 3 02-19-2008 11:10 AM
Function using array Mohan Giri Programming 1 05-03-2005 04:52 PM



Copyright 2002- Social Knowledge, LLC All Rights Reserved.

All times are GMT -5. The time now is 09:18 AM.


Powered by vBulletin® Version 3.8.8 Beta 1
Copyright ©2000 - 2017, vBulletin Solutions, Inc.