Array Function

This is a discussion on Array Function within the C++ Programming forums, part of the General Programming Boards category; I have this code to store data from an array; Code: double main() { ifstream data; double n[N]; string filename; ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    16

    Array Function

    I have this code to store data from an array;

    Code:
    double main()
    {
    	ifstream data;
    	double n[N];
    	string filename;
    	int count(0);
    
    	cout << "What is the filename where the data is located?" << endl;
    	cin >> filename;
    
    	data.open(filename.c_str());
    	
    	while(count < (N-1) && !data.eof())
    	{
    		data >> n[count];
    		cout << n[count];
    		count++;
    	}
    	return 0;
    }

    My question is, how do I make this callable from another function so that the other function recieves the data uin the array?

    Any help would be much appreciated.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Code:
    double main()
    This is interesting.

    I'm not sure exactly what you're asking. If you want to know how to pass an array to a function. It's done like this:

    Code:
    void pass_array(int array[]) {
         // The call is like this: pass_array(array);
         // Arrays are always passed by reference so if you edit them in the fucntion
         // they're editted globally.
        }
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    Hmmm. Okay, I'm going to try that.

  4. #4
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    I though arrays are always passed by reference...

    Code:
    int foo (int *array)
    {
    //code]
    }
    
    int main()
    {
    int array[10] = {1, 2,3,4,5,6,7,8,9}
    int array[10] = foo(&array);
    My Website
    010000110010101100101011
    Add Color To Your Code!

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    They are always passed by reference, but what you're doing there is passing the value of an address of an address in that call.

    arrays are defined the way I posted.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    This is where I have my problem.

    Code:
    void pass_array(int array[])
    What goes inside the square brackets?

    Number of elements?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    The size of the array. You can also leave it empty if you'd like, but your function needs to know the size of the array somehow.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Yeah, sorry. You don't need the size in the prototype, but the function definition needs it.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    Okay, maybe it will help if i state the end goal cause I am still confused and clearly no good at C++. I have to make a calculator function to calculate the median of an array. I know how to do that part. The part that is confusing me is that the reading of the array has to be performed by a separate function and then passed back into main to be sorted. Any further suggestions cause I'm lost.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Just do as we said.

    Pass the array to the function and you can read it in from there. You could make the function value returning, which would be the median.
    Sent from my iPadŽ

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    This problem seems to be cropping up in several posts lately:
    Code:
    double main()
    All the names you use in your program for variable names and function names should be unique. In every program, there is already something called 'main'. Use another name for your function.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    OK, this is what I have and it's clearly not working. Anyone know what my C++ defecient brain is doing wrong.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    const int N = 1000;
    
    
    void pass_array(int array[N]);
    void main(int array[N])
    {
    	pass_array(array);
    	for (int i=0; i<5; i++)
    	{
    		cout << array[i] << endl;
    	}
    }
    
    void pass_array(int array[N])
    {
    	ifstream data;
    	string filename;
    	int count(0);
    	int array[N];
    
    	cout << "What is the filename where the data is located?" << endl;
    	cin >> filename;
    
    	data.open(filename.c_str());
    	
    	while(count < (N-1) && !data.eof())
    	{
    		data >> array[count];
    		count++;
    	}
    
    }

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I though arrays are always passed by reference
    They are...automatically, so you can denote the function parameter a couple of different ways:
    Code:
    #include <iostream>
    using namespace std;
    
    void func1(int anArray[])
    {
    	cout<<anArray[0]<<endl;
    }
    
    void func2(int* pInt)
    {
    	cout<<pInt[0]<<endl;
    }
    
    int main()
    {
    	int arr[]={10, 20, 30};
    
    	func1(arr);
    	func2(arr);
     
    	return 0;
    }
    Last edited by 7stud; 12-01-2005 at 07:37 PM.

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Main should be defined as an integer not void. You don't pass the array parameter to main, so take that out of the arguement list there, you don't redefine the array in the function, it's already defined in the arguement list:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    const int N = 1000;
    void pass_array(int array[N]);
    
    int main()
    {
        int array[N];
    	pass_array(array);
    	for (int i=0; i<5; i++)
    	{
    		cout << array[i] << endl;
    	}
    }
    
    void pass_array(int array[N])
    {
    	ifstream data;
    	string filename;
    	int count(0);
    	cout << "What is the filename where the data is located?" << endl;
    	cin >> filename;
    
    	data.open(filename.c_str());
    	
    	while(count < (N-1) && !data.eof())
    	{
    		data >> array[count];
    		count++;
    	}
    }
    Sent from my iPadŽ

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    OK, this is what I have and it's clearly not working. Anyone know what my C++ defecient brain is doing wrong.
    Every begining C++ program should start with this format:
    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
    	
     
    	return 0;
    }
    Then you add code as needed.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 12:19 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. Replies: 3
    Last Post: 03-23-2002, 03:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21