Question

1. The following global function takes as input parameters an array of bird objects as defined...

1. The following global function takes as input parameters an array of bird objects as defined in Bird.h and the length of the array. It returns the bird with the largest wing span. There are multiple errors with the code. Please clearly identify them, including style issues.

Bird.h

class Bird

{ public:

Bird();

~Bird(); int WingSpan;

};

Main.cpp

const int MAXBIRDS 14;

int main()

{

Bird biggest, b[MAXBIRDS];

...some code assume is ok …

biggest = LargestWingSpan(b, MAXBIRDS);

return 0;

}

Bird& LargestWingSpan(Bird *birds, int &Length) {

int i; Bird bigBird(“name”);

bigBird = birds[i]; for (i = 1; i <= Length; i++) {

if (birds[i].WingSpan > bigBird.WingSpan) bigBird = birds[i];

} return bigBird;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. Indentation need to be corrected in both the files. The indented code is given below

2. In Main.cpp, the Bird.h header file need to be included using #include
3. There is no constructor defined in the Bird class. A constructor with int argument should be defined to initialize wingSpan. The corrected code shows the constructor)
4. The for loop should start at index 0 and go to less than length like this
   for (i = 0; i < Length; i++) {
       ....
   }
  
5. The return type should be modified to Bird and remove the & symbol since we can return a reference to local variable. The correct function signature should be
Bird LargestWingSpan(Bird *birds, int &Length) {
   ....
}

6. The constant should be assigned using = symbol like this
const int MAXBIRDS=14;

7. The function prototype for LargestWingSpan() should be added before main() function. Also the signature should be modified to using const int& instead of int &.

Bird LargestWingSpan(Bird *birds, const int &Length);


8. The constructors and destructors of Bird class need to implemented in a file named Bird.cpp


============================================
All correct files are given below

Bird.h
----

class Bird
{
   public:
       Bird();
       Bird(int w);
       ~Bird();
      
       int WingSpan;

};

Bird.cpp
-----

#include "Bird.h"
Bird::Bird(){ WingSpan = 0;}
Bird::Bird(int w) {WingSpan = w; }
Bird::~Bird(){}

Main.cpp
-----
#include "Bird.h"
const int MAXBIRDS=14;
Bird LargestWingSpan(Bird *birds, const int &Length);

int main()

{

   Bird biggest, b[MAXBIRDS];

   //...some code assume is ok …
   biggest = LargestWingSpan(b, MAXBIRDS);

   return 0;
}

Bird LargestWingSpan(Bird *birds, const int &Length){
   int i;
   Bird bigBird(0);
   bigBird = birds[i];
   for (i = 0; i < Length; i++) {
       if (birds[i].WingSpan > bigBird.WingSpan)
           bigBird = birds[i];
   }
   return bigBird;
}

Add a comment
Know the answer?
Add Answer to:
1. The following global function takes as input parameters an array of bird objects as defined...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose you define a file format to describe an array of java.awt.Rectangle objects. You start with...

    Suppose you define a file format to describe an array of java.awt.Rectangle objects. You start with an integer saying the length of the array, then a sequence of integers describing the x, y, width, and height parameters that can feed into the constructor for java.awt.Rectangle (according to the documentation). Let's define a method that will return an array of Rectangles given a String representing a filename. You could test the method by creating a file such as this: 2 0...

  • a. Draw a memory diagram to illustrate the passing of parameters with respect to the method...

    a. Draw a memory diagram to illustrate the passing of parameters with respect to the method call r.compareTo(s) in the following code. More specifically, illustrate how the calling object and parameter object are referenced during the execution of the method r.compareTo(s). b. For the same definition for class Rectangle as part a., draw the memory diagram that illustrates the storage of array and objects when the following code executes: class Rectangle { private double length, breadth; public Rectangle(double 1, double...

  • Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array...

    Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

  • An array of class objects is similar to an array of some other data type. To...

    An array of class objects is similar to an array of some other data type. To create an array of Points, we write Point parray [4]; To access the object at position i of the array, we write parray [i] and to call a method on that object method, we write parray [i]. methodName (arg1 , arg2 , ...) ; To initialize an array of objects whose values are known at compile time, we can write Point parray [4] =...

  • C++: Find Time Analysis Worst case O() of each member function with explanation: 1) /** insert function:    parameters:...

    C++: Find Time Analysis Worst case O() of each member function with explanation: 1) /** insert function:    parameters: obj    Description: inserts new elements into the correct position in the    sorted array, sliding elements over the position to right, as needed **/ template <typename Object> SortedArray::void insert(const Object &obj) {    if (theSize >= theCapacity)    {        cout << "Error: there is no enough memory space. " << endl;        return;    }    else    {        for (int i = ((2 * theSize) + 1); (i...

  • please answer correctly. Language/Type Java Inheritance polymorphism Assume that the following classes have been defined: public...

    please answer correctly. Language/Type Java Inheritance polymorphism Assume that the following classes have been defined: public class George extends Elaine ( public void method1() { print("George 1 "); public class Jerry { public void method10) { print("Jerry 1 "); public void method20 { print("Jerry 2 "); public String toString() { return "Jerry": public class Elaine extends Kramer public String toString() { return "Elaine " + super.toString(); public class Kramer extends Jerry public void method1() { super.method1(); print ("Kramer 1"); public...

  • C++ Write the code for the following procSale function. Its purpose is to take the array...

    C++ Write the code for the following procSale function. Its purpose is to take the array of Vendltem structures (first argument), the selection (2nd argument, which is also the array item to purchase), and the cash (3rd argument, which is the amount offered to pay for the item) and produce the following results: .If the cash is more than $1.00, output the message "Cash amount too large!" and return. If the cash is less than the price of the item...

  • 2 Class Vec Message1 2.1 Introduction • Write a class Vec Message1 with the following declaration....

    2 Class Vec Message1 2.1 Introduction • Write a class Vec Message1 with the following declaration. class Vec_Message1 { public: Vec_Message1(); Vec_Message1(int n); Vec_Message1(int n, const Message1 &a); Vec_Message1(const Vec_Message1 &orig); Vec_Message1& operator= (const Vec_Message1 &rhs); ~Vec_Message1(); int capacity() const; int size() const; Message1 front() const; Message1 back() const; void clear(); void pop_back(); void push_back(const Message1 &a); Message1& at(int n); private: void allocate(); void release(); int _capacity; int _size; Message1 * _vec; }; 2.2 allocate() and release() • Examine the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT