Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 20th, 2006, 2:21 PM   #1
can342man
Newbie
 
can342man's Avatar
 
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0 can342man is on a distinguished road
Arrays or Vectors?

I have seen a lot of debate on wheather to use arrays or vectors for passing arrays in functions. I have a simple program here that does not quite seem to work properly as what I intended. The goal is to take the arrays from f1() and f2() and pass all the elements to the main where the elements are added togethor. I get a "segmentation fault" error.

How would I use vectors as an alternate to arrays?

#include <iostream>
#include <cmath>
using namespace std;

double f1(int a);
double f2(int b);

int main()
{
	int a1, b1;
	double numTot[6];

	f1(a1);
	f2(b1);

	// sum of 2 arrays element by element
	for(int k=0; k<=5; k++)
	{
		numTot[k] = f1(a1) + f2(b1);
		cout << "numTot[" << k << "]: " <<  numTot[k] << endl;
	}
	return 0;
}

double f1(int a2)
{
	double num1[6];

	// place 6 numbers in array num1
	for(int t=0; t<=5; t++)
	{
		num1[t] = t+1.0;
		cout << "num1[" << t << "]: " << num1[t] << endl;
	}
	return num1[a2];
}

double f2(int b2)
{
	double num2[6];

	// place 6 numbers in array num2
	for(int j=0; j<=5; j++)
	{
		num2[j] = j+2.0;
		cout << "num2[" << j << "]: " <<  num2[j] << endl;
	}
	return num2[b2];
}

Thanks if anyone can clarify this.

:banana:
__________________
Greatness courts failure and solitude.
--- Anonymous
can342man is offline   Reply With Quote
Old Apr 20th, 2006, 3:31 PM   #2
mikaoj
Programmer
 
mikaoj's Avatar
 
Join Date: Aug 2005
Location: Norway
Posts: 56
Rep Power: 0 mikaoj is an unknown quantity at this point
Use a pointer.
And pass the array through the function.
void func(double *table, int len)
__________________
Heh.

Last edited by mikaoj; Apr 20th, 2006 at 3:44 PM.
mikaoj is offline   Reply With Quote
Old Apr 20th, 2006, 3:57 PM   #3
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 256
Rep Power: 3 Cache is on a distinguished road
The seg fault is caused by a1 and b1 being used to index an array before they have been initialized to a value within the valid range.

Here is a vector version of your code. I've not changed any logic, so if the output isn't what you expected, that's your fault

#include <iostream>
#include <vector>

using namespace std;

// TODO: Const correctness.

double f1(int a, double arg2);

int main()
{
	try
	{
		// a1 and b1 must be initialized to some number
		// withing the bounds of vecNum.
		int a1 = 0, b1 = 0;
		vector<double> vecNumTot( 6, 0 );

		f1( a1, 1.0 );
		f1( b1, 2.0 );
		
		// sum of 2 vector element by element
		vector<double>::iterator i = vecNumTot.begin();
		for( int j = 0; i != vecNumTot.end(); ++i, ++j )
		{
			*i = f1( a1 , 1.0 ) + f1( b1, 2.0 );
			cout << "vecNumTot[" << j << "]: " <<  *i << endl;
		}
	} catch ( std::out_of_range& )
	{
		// TODO: Error hadnling.
		
		// NOTE: This try/catch block isn't needed if
		// there's no chance a1/b1 are passed to
		// f1 with a value exceeding vecNum.end().
	}
	
	cin.get();
	return 0;
}

double f1( int a, double arg2 )
{
	vector<double> vecNum( 6, 0 );

	// place 6 numbers in int vector vecNum1
	vector<double>::iterator i = vecNum.begin();
	for( int j = 0; i != vecNum.end(); ++i, ++j )
	{
		*i = j + arg2;
		cout << "vecNum[" << j << "]: " << *i << endl;
	}
	
	// If 'a' is out of range, an exception will
	// be thrown.
	return vecNum.at( a );
}
Cache is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:47 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC