Help on array comparison

This is a discussion on Help on array comparison within the C++ Programming forums, part of the General Programming Boards category; Ok.. trust me, this is the final question from me.. Write a function template called printArray that takes an arbitrary ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    33

    Help on array comparison

    Ok.. trust me, this is the final question from me..

    Write a function template called printArray that takes an arbitrary array type and and two additional integer arguments, lowSubscript, highSubscript. A call to this function will print only the designated portion of the array. Validate lowSubscript and highSubcript if either is out of range or if highSubscript is less than or equal to lowSubscript, printArray should return 0; otherwise printArray should return the number of elements printed

    really confused with this... any guidance on this?? pls

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    What in the question confuses you?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    the template..

    i did normal main program by doing a function and initialized two arrays.. but only later realized we need to use the template for it..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    Well, since you need to validate that the subscripts are within range, you presumably cannot just have a pointer to the first element of the array passed. You need to obtain the array size. Here is an example:
    Code:
    #include <cstddef>
    #include <iostream>
    
    template<typename T, std::size_t N>
    void printSize(T (&arr)[N])
    {
        std::cout << N << std::endl;
    }
    
    int main()
    {
        int numbers[] = {1, 2, 3, 4, 5};
        printSize(numbers);
    }
    From this, you should be able to work out how to modify your current code to become a function template.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    oh..

    thanks sir.. will try to work on this template (pardon the pun)

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    Code:
    #include<iostream>
    using namespace std;
    
    template < class T >
    T printArray(const T *array, const int count,int lowsub,int highsub)
    {
    	if(highsub>count || highsub<=lowsub)
    		return 0;
    	else
    	{
    		for (int i=lowsub; i<=highsub; i++)
    			cout<<array[i]<<" ";
    		cout<<endl;
    		return array[i];
    	}
    	
    }
    
    template < class T >
    void printArray(const T *array, const int count)
    {
    	for (int i=0; i<count; i++)
    		cout<<array[i]<<" ";
    	cout<<endl;
    }
    
    void main()
    {
    	const int icount = 5,dcount = 6,ccount = 4; 
    	int i[icount] = {2,5,3,7,4};
    	double d[dcount] = {2.4,3.7,1.5,6.5,8.2,4.3};
    	char c[ccount] = "RAY";
    
    	cout<<"Array i contains : \n";
    	printArray(i,icount);
    	printArray(i,icount,2,4);
    	cout<<"Array d contains : \n";
     	printArray(d,dcount);
    	printArray(d,dcount,2,5);
    	cout<<"Array c contains : \n";
    	printArray(c,ccount);
    	printArray(c,ccount,0,1);
    }

    my effort...

    is this right?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    You should ask yourself if what you wrote conforms to your assignment's requirements.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 12:50 AM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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