Cramster.com - Homework Solutions, Lecture Notes, Exams, and Free Online Homework Help
Sign Up Now! Login Customer Support Cramster Blog
McAfee Secure sites help keep you safe from identity theft, credit card fraud, spyware, spam, viruses and online scams
Problem Solved.
    Home    
    Homework Help    
   Answer Board   
    Resources (Beta)    
   
Member's Topic Headline:

help oop

Know the answer? Have a better solution? Share it.
Sign Up Now for FREE!
Join the thousands of students
getting ahead in their classes.
Member Testimonials

Question:

Advertisement:

Answer | Ask New Question | Customize Profile | Leaderboards | 
FAQ

Member's Avatar

Rookie
Karma Points: 21
Respect (35%):
Date Posted: 7/23/2008 9:23:53 AM  Status: Closed
help oop
Course Textbook Chapter Problem
N/A N/A N/A N/A
Question Details:
 

      Create a function called amax() that returns the value of the largest element in an array. The arguments to the function should be the address of the array and its size. Make this function into a template so it will work with an array of any numerical type. Write a main() program that applies this function to arrays of various types

Answers:

Member's Avatar

Expert
Karma Points: 811
Date Posted: 7/23/2008 12:32:31 PM  Status: Live
Asker's Rating: None Provided    Moderator's Rating: Helpful
Response:

#include<iostream.h>
#include<conio.h>

template <class myType>
myType amax (myType *arr, int size)
{
int i;

myType max=-9999;
 for(i=0;i<size;i++)
 {
 if(arr[i] > max)
  {
  max = arr[i];
  }
 }
 cout<<"\nMax is :"<<max;

   return max;

}
int main()
{

 int arri[5]={1,2,3,4,5};
 float arrf[5]={-11.12,-2,3,4,5.12};
 clrscr();
 amax(arri,5);
 amax(arrf,5);
    getch();
return 0;
}



Cramster Expert

Member's Avatar

(Cramster SME)
Moderator
Cramster In-House Subject Matter Expert
Date Posted: 7/24/2008 12:45:18 AM  Status: Live
Asker's Rating: None Provided    Moderator's Rating: N/A
Response:
 
Dear User
 
I just want to make a small correction to the answer provided (at the second line of the template function).

#include<iostream.h>
#include<conio.h>

template <class myType>
myType amax (myType *arr, int size)
{
int i;

myType max=-9999;
myType max = arr[ 0 ];
 for( i = 1; i < size; i++)
 {
 if(arr[i] > max)
  {
  max = arr[i];
  }
 }
 cout<<"\nMax is :"<<max;

   return max;

}
int main()
{

 int arri[5]={1,2,3,4,5};
 float arrf[5]={-11.12,-2,3,4,5.12};
 clrscr();
 amax(arri,5);
 amax(arrf,5);
    getch();
return 0;
}
 
 
It is because, normally we won't enter negative numbers as input. But, if you enter input ( all numbers ) below -9999, then you will get wrong answer for that. Please check with input
int arri[5]={ -11111, -22222, -33333, -44444, -55555}.
I hope, now, you understood why I initialized with the first element of the array. Also, to make it complete answer, we need to check the size to be greater than zero which reflects there were some valid numbers in the array.

Last, but not least, as the value of the max got printed in the function itself, I don't find any need to return the largest value to the function called. Thus, void may be expected as return type or the max value be get printed in the function called.
 
Still need any assistance / clarification ? Remember! CRAMSTER is always there to SOLVE PROBLEMS.
 



By reading or posting messages on these forums, you are agreeing to the Answer Board's Terms of Service and Conduct (TSC).


About Cramster | Terms of Use | Privacy Policy | Contact Us | Press Room | Site Map | Support | Anti-Cheating Policy

Cramster.com is not affiliated with any publisher. Book covers, title and author names appear for reference only.
Copyright © 2008 Cramster, Inc.