Question

Question 6 Explain macro and inline functions. Write a macro and an inline function to calculate...

Question 6

  1. Explain macro and inline functions. Write a macro and an inline function to calculate the minimum of two input arguments

  1. Why should we use a pass-by-reference for a copy constructor ?   What cannot be inherited from a base class ?

  1. What is the main difference between protected and private? What is the role of inheritance type ?

  1. Give a definition of software engineering. Explain how assert( ) could enforce the correctness of your code.

  1. Assume that you have a two-dimensional array: float x[30][40]. The values of this array elements have been assigned. Write a segment of code to find the minimum and maximum of those array elements
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Codes are in Languages C++ and Java

A.1)

(Language: C++)

Macro:

It is also called preprocessors directive. The macros are defined by the '#define' keyword. Before the program compilation,

the preprocessor examines the program whenever the preprocessor detects the macros then preprocessor replaces the macro by the macro definition.

Syntax:

#define MACRO_NAME _DEFINATION

Inline:

It is a normal function that is defined by the inline keyword.

An inline function is a short function that is expanded by the compiler.its arguments are evaluated only once.

Syntax:

inline return_type _name ( parameters )

{

// CODE

}

For calculating the minimum of two input arguments

a) Using Inline:

#include <bits/stdc++.h>

using namespace std;

// Inline function

inline int Minimum(int a, int b)

{

    return (a < b) ? a : b;

}

int main()

{

    int a,b;

    cin>>a>>b; // Taking two inputs

    cout<<"Minimum of "<<a<<" and "<<b<<":"<<Minimum(a,b)<<endl;

    return 0;

}

b) Using Macro:

#include <bits/stdc++.h>

using namespace std;

// macro with parameter

#define MINIMUM(a, b) (a < b) ? a : b

int main()

{

    int a,b;

    cin>>a>>b; //Taking two inputs

    int res = MINIMUM(a,b);

    cout<<"Minimum of "<<a<<" and "<<b<<":"<<res<<endl;

    return 0;

}

A.2)

a) It is necessary to pass object as reference and not by value because if you pass it by value its copy is made using the copy constructor.This means the copy constructor would call itself to form copy.

This process will continue until the compiler runs out of memory.

b) Base class's constructors, destructor, friends can't be inherited.

A.3)

a)

Private: Only members of an equivalent class can access the function.

Protected : Same as private but derived classes also can access.

b) Role of inheritance: for reuseability of the code.

Moreover we can have common functionalities in the base class &

can extends the class by adding more new functionaity or can override the existing functionality.

A.4)

a) Software Engineering: Software engineering is the systematic application of

engineering approaches to the development of software.

b) Assert:

Like 'Assertion' allows testing the correctness of any assumptions that have been made in the program.

Assertion can be done using the assert statement(in java/C++). When any assertion fails,JVM throws an error, "AssertionError".It is especially used for testing purposes during development.

In java assertions are not enabled, bydefault.

(Language: Java)

Syntax:

assert _expression

import java.util.Scanner;

class Test

{

    public static void main(String args[])

    {

        int val=10;

        assert val >= 20 : " Hey";

        System.out.println("value is "+val);

    }

}

Output: value is 10

Assert will not be seen directly so while compiling, you have to write:

'java –ea Test' (For enable the assertion)

Then output will look like:

Exception in thread "main" java.lang.AssertionError: Hey


A.5)

(Language: C++)

float x[30][40] is an array and values are already assigned, so let's make two variables final_min and final_max, final_min defined as INT_MAX, final_max defined as INT_MIN and traverse the array to find minimum and maximum values.

Code:

#include<bits/stdc++.h>

using namespace std;

int main()

{

    float x[30][40];

    //according to question, values are already assigned

    float final_min,final_max;

    final_min=INT_MAX;

    final_max=INT_MIN;

    for(int i=0;i<30;i++)

    {

        for(int j=0;j<40;j++)

        {

            final_min=min(final_min,x[i][j]);

            final_max=max(final_max,x[i][j]);

        }

    }

    cout<<"Maximum element is:"<<final_max<<" "<<"\n"<<"Minimum element is:"<<final_min<<endl;

    return 0;

}

Happy Coding:)

Add a comment
Know the answer?
Add Answer to:
Question 6 Explain macro and inline functions. Write a macro and an inline function to calculate...
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
  • (TCO 1) The JVM executes a Java application by (Points : 6)        executing the public static...

    (TCO 1) The JVM executes a Java application by (Points : 6)        executing the public static main method of the class.        creating an object of the class and executing that object’s main method.        executing the class constructor.        compiling the Java code into byte code. (TCO 1) Which method call converts the value in variable stringVariable to a double? (Points : 6)        Double.parseDouble( stringVariable );        Convert.toDouble( stringVariable );        Convert.parseDouble( stringVariable );        Float.parseFloat( stringVariable ); (TCO 1) Which of the following can...

  • 1. (10pts) What is the value (in decimal) of X in each case below? Give the...

    1. (10pts) What is the value (in decimal) of X in each case below? Give the expression based on which you arrive at your value. Assume 4 bytes are used to store int and float, 8 for long and double. a. int X b. float X x sizeof[char)/ (float) sizeof10 c. int C- 4: 2. (20pts) Rewrite the following program using macro definitions (adefine) for all the constants and a new type definition (typedef) called Card for all the values...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • 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...

  • SHORT ANSWER QUESTIONS Part 1 Classes Abstraction: What is Abstraction in terms of representation? Specifically what...

    SHORT ANSWER QUESTIONS Part 1 Classes Abstraction: What is Abstraction in terms of representation? Specifically what choices does one make when creating a class that is an example of Abstraction? Encapsulation: What is encapsulation? What is information hiding? What does a public interface to a class consist of (not in the sense of actual java interfaces but what the client uses to manipulate objects of the class) What is an object of a class? What is the constructor? How do...

  • These are my answere to the following questions: are they right? 1. B 2. T 3....

    These are my answere to the following questions: are they right? 1. B 2. T 3. T 4. T 5. F 6. T 7. A 8. D 9. E 10. B 11. B 12. A 13. A 14. D 15. C 16. D 17. T 18. C 19. T 20. T 21. T 22. A 23. T 24. D 25. B 26. A 27. A 28. A 29. T 30. C 31. D 32. A 33. T 34. F 35....

  • code in java please:) Part I Various public transporation can be described as follows: A PublicTransportation...

    code in java please:) Part I Various public transporation can be described as follows: A PublicTransportation class with the following: ticket price (double type) and mumber of stops (int type). A CityBus is a PublicTransportation that in addition has the following: an route number (long type), an begin operation year (int type), a line name (String type), and driver(smame (String type) -A Tram is a CityBus that in addition has the following: maximum speed (int type), which indicates the maximum...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • I need this in C++. This is all one question. Introduction Your eighth assignment will consist...

    I need this in C++. This is all one question. Introduction Your eighth assignment will consist of two programs, which will involve the use of simple classes. The source code for these problems should be submitted using the naming conventions we specified in class. Please note that your computer programs should comply with the commenting and formatting rules as described in class. For example, there should be a header for the whole program that gives the author's name, class name,...

  • Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C -...

    Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C - A consecutive group of memory chunks. D - None of the choices. Question 2 How many times is the body of the loop executed? int i=1; while(true) { cout << i; if(++i==5) break; } A - Forever B - 4 C - 5 D - 6 E - 0 Question 3 What is wrong with the following piece of...

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