Thread: Help on array comparison

  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
    28,413
    What in the question confuses you?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    You should ask yourself if what you wrote conforms to your assignment's requirements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 01:50 AM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07: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