Thread: using arrays of pointers, soo close

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    here is my code:
    quick note, can't edit the function prototypes (this is for a lab, prototypes must be those ones))

    Code:
    #include <iostream>
    #include <string>
    #include <ctime>
    using namespace std;
    void header(int lab,int question,string date); 
    void printArrayA( int * , int size, int high, int low); 
    void printArrayB( int *, int size); 
    void printArrayC( int *, int size); 
    void printArrayD( int &, int size); 
    void printArrayE( int * , int size);
    char menu();
    int main()
    {
    	int max(0), min(0),n;
    	int* myArray=NULL;
    	header(6,1,"March 10");
    	char selection;
    	cout<<"Please enter the size of the Array: ";
    	cin>>n;
    	myArray=new int [n];
    	do
    	{
    		selection=menu();
    		switch(selection)
    	{
    		case 'a':
    		case 'A':
    			{
    				cout<<"Enter a minimum value for the array : ";cin>>min;
    				cout<<"Enter a maximum value for the array : ";cin>>max;
    				while (min>=max)
    				{
    					cout<<"Invalid number : ";cin>>max;
    				}
    				printArrayA(myArray,n,max,min);
    				break;
    			}
    		
    		case 'b':
    		case 'B':
    			{
    				printArrayB(myArray,n);
    				break;
    			}
    
    		case 'c':
    		case 'C':
    			{
    				printArrayC(myArray,n);
    				break;
    			}
    		case 'd':
    		case 'D':
    			{
    				printArrayD(*myArray,n);
    				break;
    			}			
    		case 'e':
    		case 'E':
    			{
    				printArrayE(myArray,n);
    				break;
    			}	
    		default :
    			{
    				cout<<"Invalid Entry"<<endl;
    				break;
    			}
    	}
    	}
    	
    	while(!(selection=='f'||selection=='F'));
    }
    void header(int x, int y, string z)
    { 
    	 cout<<"	++	Dave Tripp	++\n";
    	 cout<<"        ++	Lab "<<x<<"		++\n";
    	 cout<<"	++	Question "<<y<<"	++\n";
    	 cout<<"	++	"<<z<<"	++"<<endl;
    }
     char menu()
     {
    		cout<<"     MENU \n";
    		cout<< "A. Populate the Array with Random Numbers. \n";
    		cout<< "B. Sort the Array from least to greatest. \n";
    		cout<< "C. Sort the Array from greatest to lest. \n";
    		cout<< "D. Reverse the Array. \n"; 
    		cout<< "E. Print the Array. \n";
    		cout<< "F. Exit. \n";
    		char letter; cin>>letter;
    					return letter;
     }
     void printArrayE( int inputarray[], int n)
     {
     for (int i = 0; i<n; i++)
     {
    		cout<<inputarray[i]<<" ";
     }
    	cout<<endl;
    	cout<<endl;
     }
     void printArrayA( int inputarray[], int n, int max,int min)
     {	
    	int i;
    	srand((unsigned int)time(NULL));
    	for(i = 0; i <n; i++)
    	{
    		inputarray[i]=min+rand()%(max-1);
    	}
    	cout<<endl;
    	cout<<endl;
     }
     void printArrayB( int inputarray[],int n)
     {
    	int i,j;
    	int temporary;
    	for(i=0;i<n;i++)
        {
            for(j=0;j<i;j++)
            {
                if(inputarray[i]<inputarray[j])
    			{
    				temporary=inputarray[i]; 
                    inputarray[i]=inputarray[j];
                    inputarray[j]=temporary;
                }
    
            }
    
        }
    }
     void printArrayC( int inputarray[],int n)
     {
    	int i,j;
    	int temporary;
    	for(i=0;i<n;i++)
        {
            for(j=0;j<i;j++)
            {
                if(inputarray[i]>inputarray[j])
    			{
    				temporary=inputarray[i]; 
                    inputarray[i]=inputarray[j];
                    inputarray[j]=temporary;
                }
    
            }
    
        }
     }
     void printArrayD( int *inputarray[],int n)
     {
    	 int* k;
    	 *inputarray=k;
    	 int i;
    	 for (i = 0; i < n/2; i++)
    	 { 
    		 swap ( k[i], k[n-1-i]); 
    	 } 
    
    }
    i know the problem is in the printArrayD function somewhere, i think i need to switch soemthing around with the *inputarray=k line, i've tried for a while but can't figure it out.
    this is the error message i get:

    1>------ Rebuild All started: Project: Lab 03, Configuration: Debug Win32 ------
    1>Build started 3/24/2011 12:44:54 AM.
    1>_PrepareForClean:
    1> Deleting file "Debug\Lab 03.lastbuildstate".
    1>InitializeBuildStatus:
    1> Touching "Debug\Lab 03.unsuccessfulbuild".
    1>ClCompile:
    1> dtripp3_Lab08_q3.cpp
    1>c:\users\dave tripp\documents\visual studio 2008\projects\es1036\lab 03\dtripp3_lab08_q3.cpp(154): warning C4700: uninitialized local variable 'k' used
    1>dtripp3_Lab08_q3.obj : error LNK2019: unresolved external symbol "void __cdecl printArrayD(int &,int)" (?printArrayD@@YAXAAHH@Z) referenced in function _main
    1>C:\Users\Dave Tripp\Documents\Visual Studio 2008\Projects\ES1036\Debug\Lab 03.exe : fatal error LNK1120: 1 unresolved externals
    1>
    1>Build FAILED.
    1>
    1>Time Elapsed 00:00:01.29
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped =========

    Someone help me please and thanks!
    Last edited by davebptripp; 03-23-2011 at 11:37 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    printArrayD() is declared as a function that accepts a reference to int as the first argument.

    It is later implemented as a function that accepts a pointer to pointer (or array of pointers) to int.

    The two do not match.

    The call of printArrayD() in main() attempts to call the one you have declared, which has not been defined. Hence the linker error.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    1. In your prototype, it seems you will pass some integer value by reference.
    2. In your call to printArrayD, you are doing *myArray, which according to me is not valid. You should just pass your array as myArray.
    3. In defintion, try like;
    Code:
    void printArrayD( int & inputarray[],int n)
     {
    	 int* k;
    	 //*inputarray=k;
    	 int i;
    	 for (i = 0; i < n/2; i++)
    	 { 
                                    cout<<inputArray[i]<<endl;
    		 swap ( k[i], k[n-1-i]); 
    	 } 
    
    }
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Mr.777 View Post
    2. In your call to printArrayD, you are doing *myArray, which according to me is not valid.
    It is valid, although it does not pass an array to the function. *myArray is equivalent, in this case, to myArray[0].

    The declaration of printArrayD() is accepting a reference to int, not a reference to an array.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by grumpy View Post
    It is valid, although it does not pass an array to the function. *myArray is equivalent, in this case, to myArray[0].

    The declaration of printArrayD() is accepting a reference to int, not a reference to an array.
    So, yes, he/she should pass a single instance of array as protoype is asking for reference to a variable...
    Correction.... And thanks grumpy
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    thanks

    thanks everyone for the help. got it working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers As 2D arrays
    By TieFighter in forum C Programming
    Replies: 29
    Last Post: 03-22-2010, 06:46 AM
  2. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  3. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  4. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM

Tags for this Thread