GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Jun-2008, 03:39
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Alter Reference to Array


Hello to all expect C++ programmer, i have a reference to an array but i want sort it using that reference.

How do i accomplish this ?

CPP / C++ / C Code:
#include<iostream>

using namespace std;

int& bucketSort(int &, int, int);


// ------------------------------------------------

int main(int argc, char *argv[])
{
	int number[] = {9, 4, 5, 3, 2, 8, 15, 13, 
		1, 6, 11, 7, 10, 12, 14};

	int &aReference = number;

	aReference = bucketSort(aReference, 15, 15);

	return 0;
}
// ------------------------------------------------
int& bucketSort(int &aReference, int size, int maxRange)
{

}

This can accomplish with pointer notation and array notation.

I know both of that but i want try some new techniques.

Thanks for your help.
  #2  
Old 04-Jun-2008, 09:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Alter Reference to Array


Quote:
Originally Posted by Peter_APIIT
... i have a reference to an array but i want sort it using that reference.
A reference variable can be thought of as an alternative name for another program variable. It must be initialized at the time it is declared, and its value can not be changed later. For its entire scope it is bound to that object. Period.

It is possible to have a reference variable that is a reference to an array, but its value must be assigned at the time it is declared. This makes it particularly unsuitable for use in a general-purpose sort routine.

If the sort routine itself has a parameter that is a reference to an array, it must be declared as a reference to particular sized array.

Before getting wrapped up into some sort of dead-end sorting project, you might consider something like:

CPP / C++ / C Code:
#include<iostream>

using namespace std;

//
// A function that takes a reference to an array of 5 ints.
//
void foo(int (& a)[5], int);

//
// A function that takes a pointer to int.
//
void bar(int [], int);

int main()
{
    int na1[5]  = {1, 2, 3, 4, 5};
    int na2[5]  = {5, 4, 3, 2, 1};
    int na3[6] =  {11, 22, 33, 44, 55, 66};

    int (& refn)[5] = na1; // a reference variable
    int *pnum = na1;       // a pointer variable

    cout << "Calling foo(na1,5)" << endl;
    foo(na1, 5);   // function is defined as pass by reference

    cout << "Calling foo(refn,5)" << endl;
    foo(refn, 5); // use the reference variable: same as using na1

    cout << "Calling foo(na2,5)" << endl;
    foo(na2, 5);   // pass by reference

    //
    // refn = na2;      // won't work
    // refn = na3;      // won't work
    // foo(pnum, 5); // won't work
    //

    cout << "Calling bar(na1,5)" << endl;
    bar(na1, 5);

    cout << "Calling bar(pnum,5), with pnum = na1" << endl;
    bar(pnum, 5);

    pnum = na3;
    cout << "Calling bar(pnum,6), with pnum = na3" << endl;
    bar(pnum, 6);

    return 0;
}

void foo(int (& a)[5], int size)
{
    cout << "   In foo:" << endl;
    for (int i = 0; i < size; i++) {
        cout << "      a[" << i << "] = " << a[i] << endl;
    }
    cout << endl;
}

void bar(int *a, int size)
{
    cout << "   In bar:" << endl;
    for (int i = 0; i < size; i++) {
        cout << "      a[" << i << "] = " << a[i] << endl;
    }
    cout << endl;
}


Output
Code:
Calling foo(na1,5) In foo: a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 4 a[4] = 5 Calling foo(refn,5) In foo: a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 4 a[4] = 5 Calling foo(na2,5) In foo: a[0] = 5 a[1] = 4 a[2] = 3 a[3] = 2 a[4] = 1 Calling bar(na1,5) In bar: a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 4 a[4] = 5 Calling bar(pnum,5), with pnum = na1 In bar: a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 4 a[4] = 5 Calling bar(pnum,6), with pnum = na3 In bar: a[0] = 11 a[1] = 22 a[2] = 33 a[3] = 44 a[4] = 55 a[5] = 66

Note that using a reference to an array means that the function can only be used with arguments that are arrays of that exact same (fixed) size. Not very useful for a general-purpose sort routine, I'm thinking.

The function that uses a pointer can be used with arrays of any size. (And also can be used in C programs as well as C++.)

Note that the code inside both functions uses the same notation to access array elements.


Regards,

Dave
  #3  
Old 07-Jun-2008, 22:45
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Alter Reference to Array


Thanks for your explanation.

In conclusion, pointer is the most powerful tool in Memory Address Register or Memory Data Register.


I would like to highlight one thing to all C++ programmer, array of references is illegal in VS 2005.

CPP / C++ / C Code:
#include<iostream>

using namespace std;

void bucketSort(int (&aReference)[5], int, int);


// ------------------------------------------------

int main(int argc, char *argv[])
{
	int number[] = {9, 4, 5, 3, 2, 8, 15, 13, 
		1, 6, 11, 7, 10, 12, 14};

	int &aReference[15] = number;
                
	bucketSort(aReference, 15, 15);

	return 0;
}
// ------------------------------------------------
void bucketSort(int (&aReference)[15], int size, int maxRange)
{
	int *bucket = new int[size];


}


/*

	void bucketsort(int array[], int n, int max) {
int i, j = 0;
Declare an array of size (max + 1) and initialize all values to zero. 
int *bucket = calloc(max + 1, sizeof(int));
Place each element from the unsorted array into its corresponding bucket. 
for (i = 0; i < n; i++)
bucket[array[i]]++;
 Sequentially empty each bucket back into the original array. 
for (i = 0; i < max; i++)
while(bucket[i])
array[j++] = i;
}
*/


Quote:
Error 1 error C2234: 'aReference' : arrays of references are illegal d:\c++\bucket sort\bucket sort\bucketsort.cpp 15
Error 2 error C2440: 'initializing' : cannot convert from 'int [15]' to 'int *[15]' d:\c++\bucket sort\bucket sort\bucketsort.cpp 15
Error 3 error C2664: 'bucketSort' : cannot convert parameter 1 from 'int *[15]' to 'int (&)[5]' d:\c++\bucket sort\bucket sort\bucketsort.cpp 17

  #4  
Old 08-Jun-2008, 00:51
Archer Archer is offline
Junior Member
 
Join Date: May 2008
Posts: 31
Archer is on a distinguished road

Re: Alter Reference to Array


hi
i am not getting what exactly you trying to state.i tried compiling your code by making following changes,it compiled successfully:

CPP / C++ / C Code:
#include<iostream>

using namespace std;

void bucketSort(int (&aReference)[15], int, int);


// ------------------------------------------------

int main(int argc, char *argv[])
{
	int number[] = {9, 4, 5, 3, 2, 8, 15, 13, 
		1, 6, 11, 7, 10, 12, 14};

	int (&aReference)[15] = number;
                
	bucketSort(aReference, 15, 15);

	return 0;
}
// ------------------------------------------------
void bucketSort(int (&aReference)[15], int size, int maxRange)
{
	int *bucket = new int[size];


}


/*

	void bucketsort(int array[], int n, int max) {
int i, j = 0;
Declare an array of size (max + 1) and initialize all values to zero. 
int *bucket = calloc(max + 1, sizeof(int));
Place each element from the unsorted array into its corresponding bucket. 
for (i = 0; i < n; i++)
bucket[array[i]]++;
 Sequentially empty each bucket back into the original array. 
for (i = 0; i < max; i++)
while(bucket[i])
array[j++] = i;
}
*/
i have two more questions to ask:
1. i have read in reown book that array is reference is not possible.Guess is not right.
2. Dyamic allocation of reference or array of references is possible??
Last edited by admin : 08-Jun-2008 at 21:28. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #5  
Old 08-Jun-2008, 08:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Alter Reference to Array


Quote:
Originally Posted by Peter_APIIT
In conclusion, pointer is the most powerful tool in Memory Address Register or Memory Data Register.
I didn't know that there was a competition between pointers and references to see which was "most powerful." Each has its place. Furthermore, there are two topics:

1. Reference variables.
2. Passing arguments to functions by reference.

In any case when you are writing a function that has to handle different size arrays, reference variables or pass-by-reference function arguments are of limited usefulness, compared with pointers. Maybe that is what you are getting at???
Quote:
Originally Posted by Peter_APIIT
array of references is illegal in VS 2005
Arrays of references are not possible in standard C++. Remember: a reference variable can be thought of as simply another name for a variable. Having an array of names of other variables just doesn't make sense to me. (And, apparently that's the way that the language standards folks feel about it also.)

Regards,

Dave
  #6  
Old 08-Jun-2008, 08:51
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Alter Reference to Array


Quote:
Originally Posted by Archer
...it compiled successfully:
CPP / C++ / C Code:
void bucketSort(int (&aReference)[15], int, int);

1. In your program, the first parameter is not an array of references. It is a reference to an array of 15 ints. The following statement in your program declares a variable to be a reference to an array if 15 ints:
Quote:
Originally Posted by Archer
CPP / C++ / C Code:
int (&aReference)[15] = number;

The variable aReference can be thought of as another name for the variable number, which is an array of 15 ints. Therefore that variable can be used as the first argument when calling your function. Nothing about it is illegal.
In the Original Poster's example of what wouldn't work, the following attempts to declare an array of references to int:
CPP / C++ / C Code:
int &aReference[15] = number;
The compiler correctly informs us that arrays of references are not allowed.

2. If you go to the trouble to implement the function with that argument given in your program (a reference to an array of 15 ints), it can only be used to sort arrays of 15 ints. That might be useful to you, and I invite you to complete the program. I am not personally interested in pursuing it. (And I can't think of a "good" reason to do so for this particular kind of function. Why would I want to create a sorting function that can only sort one size array?)

3. If you can think of a way to use an array of references, then you might want to contact the international standards committee and suggest that they add it to the next release of the C++ language standard. Note, please that I am not suggesting that there would never be a possible use for such things. But the fact is, that it is not possible to have an array of references according to the current Standard.

Quote:
Originally Posted by Archer
i have two more questions to ask:
1. i have read in reown book that array is reference is not possible.Guess is not right.
If you find a compiler that allows an array of references, then that compiler has not only implemented an extension to the standard language as defined by ISO/IEC 14882, "Programming Languages --- C++," it has implemented something that is expressly forbidden:

Section 8.3.2, paragraph 4:
"There shall be no references to references, no arrays of references, and no pointers to references."

Quote:
Originally Posted by Archer

2. Dyamic allocation of reference or array of references is possible??

In paragraph 3 of Section 8.3.2:
"It is unspecified whether or not a reference requires storage"
You see, since a reference can be thought of as a name of another variable, it is not possible to allocate a reference, it simply wouldn't make sense to allocate storage for a reference. (What the compiler does internally with references is up to the compiler writer. What the user does is use it as a name for another variable.)

Regards,

Dave
  #7  
Old 08-Jun-2008, 23:50
Archer Archer is offline
Junior Member
 
Join Date: May 2008
Posts: 31
Archer is on a distinguished road

Re: Alter Reference to Array


Thanks Dave!!
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 16:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 17:16
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
gnu.linkonce undefined reference newbie06 C++ Forum 4 13-Mar-2007 10:53
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:04.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.