Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is an entry first year class, so these should be easy. Program I 1. Write a

ID: 3621648 • Letter: T

Question

This is an entry first year class, so these should be easy.

Program I
1. Write a program that has default values passed to the function
2. This function will calculate and return the volume of a box
3. The volume = width x height x length
4. Set it so the default value for width is 15;
5. Set it so the default value for height is 8;
6. Set it so the default value for length is 6;
7. Demonstrate each by being called from main()

Program II
1. Overload a function named AREA()
2. If one argument is passed then the function assumes that it is a square box and the area is calculated by the side2.
3. If two arguments are passed then the function assumes that it is a rectangle and the area = width x height.
4. Demonstrate each from being called in main()

Program III:

#include <iostream>
using namespace std;

double fallingDistance(int);

int main()
{
int i;
double distance;
cout<<" Time(s)          Distance(m) ";
for(i=1;i<=10;i++)
   {distance=fallingDistance(i);
      cout<<" "<<i<<"                  "<<distance<<endl;
   }
cin.get();
cin.get();
return 0;
}
double fallingDistance(int time)
{return .5*(9.8)*(time*time);
}

Explanation / Answer

Program 1:

When making the function parameters, just initialize the ones that you want default values for. Default values must start from the rigtht side of the parameters going left, you can't have void function(default paramenter, non-default) for example in a fucntion.

#include <iostream>

using namespace std;

int calculateVol( int width = 15, int height = 8, int length = 6 )
{
    int volume;
    volume = width*height*length;
    return volume;
}//calculateVol

int main()
{
    int w = 2, h = 2, l = 2;
    cout << "Volume is " << calculateVol(w,h,l) << endl;
    cout << "Default volume is " << calculateVol() << endl;

    system( "pause" );
    return 0;
}//main()

Program 2:

I just used a default parameter of 0 for second parameter, and an if statement. Doesn't matter what the actual value of the default parameter is for this setup.

#include <iostream>

using namespace std;

int AREA( int width, int length = 0 )
{
    int area;
    if (length == 0)
        area = width*width;
    else
        area = width*length;
    return area;
}//calculateVol

int main()
{
    int w = 4, l = 2;
    cout << "Area of square of L 4 is " << AREA(w) << endl;
    cout << "Area of rect. is " << AREA(w,l) << endl;

    system( "pause" );
    return 0;
}//main()

Program 3:

Just have to add "inline" to the function header, although I'm not 100% sure.

#include <iostream>
using namespace std;

double fallingDistance(int);

int main()
{
    int i;
    double distance;
    cout<< " Time(s)          Distance(m) " ;

    for(i=1;i<=10;i++)
    {
           distance=fallingDistance(i);
        cout<<" "<<i<<"                  "<<distance<<endl;
    }

    cin.get();
    cin.get();
    return 0;

}

inline double fallingDistance(int time)
{
    return .5*(9.8)*(time*time);
}

Hope this helps! Don't just copy the answers, try to understand the reasoning behind it, or else you won't benefit from it!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote