Question

USING C++: Write a function that given a 2D dynamic array of integers, will return the...

USING C++:

Write a function that given a 2D dynamic array of integers, will return the number of elements whose absolute value is smaller than a given value x. The function should take as arguments (in this order):

  • a pointer to the array (i.e., 2D dynamic array)
  • the number of rows
  • the number of columns
  • the given value

The function should return

  • the number of elements smaller in absolute value than epsilon
E.g. if A={{0.2, -0.1, 3.4},{4.4, 0, -2}} and x=0.3,  then 
smallElementCount(A, 2,3, x) will return 3 (0.2, -0.1, and 0  are all less than 0.3 in absolute value).

The function prototype is given below

int smallElementCount(double **, int, int, double);
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int smallElementCount(double **, int, int, double);

int main() {
    double **A = new double*[2];
    A[0] = new double[3];
    A[0][0] = 0.2;
    A[0][1] = -0.1;
    A[0][2] = 3.4;
    A[1] = new double[3];
    A[1][0] = 4.4;
    A[1][1] = 0;
    A[1][2] = -2;
    cout << smallElementCount(A, 2, 3, 0.3) << endl;
    return 0;
}

int smallElementCount(double **arr, int rows, int cols, double x) {
    double abs_val;
    int count = 0;
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            abs_val = arr[i][j];
            if (abs_val < 0) {
                abs_val *= -1;
            }
            if (abs_val < x)
                ++count;
        }
    }
    return count;
}

phpqiWgZR.png

Add a comment
Know the answer?
Add Answer to:
USING C++: Write a function that given a 2D dynamic array of integers, will return the...
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
  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

  • C++ Programme Write a function that receives, as arguments, a pointer to a double array, as...

    C++ Programme Write a function that receives, as arguments, a pointer to a double array, as well as the number of elements: in the array(int). The function must use pointer operations and calculate the standard deviation of the elements in the array. The function returns the standard deviation to the calling statementſ Use the function in main( to display its operation. | Example: array = (11.2, 2.4, 3.13, 16.4, 5.8, 9.22, 4.9, 10.5, 6.5, 2.99) std Dev(array) = 4.249367 ZWI...

  • C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like...

    C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like a statically allocated array (use malloc()) int **createArray(int rows, int cols) parameters to this function are the number of rows and cols in the array. The return value is the array that you have dynamically allocated void freeArray (int **array, int rows, int cols) the parameters to this function are: -the array created by createArray() -the number of rows in the array -the number...

  • C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like...

    C PROGRAM the functions create and free a dynamically allocated 2D array. It should function like a statically allocated array (use malloc()) int **createArray(int rows, int cols) parameters to this function are the number of rows and cols in the array. The return value is the array that you have dynamically allocated void freeArray (int **array, int rows, int cols) the parameters to this function are: -the array created by createArray() -the number of rows in the array -the number...

  • (C++ exercise) 5. Write a new Checkbook class that uses a dynamic array. Call the class...

    (C++ exercise) 5. Write a new Checkbook class that uses a dynamic array. Call the class a different name, since it will be different from the latest version that uses a static array. Start off with a dynamic array size of 2, made by the constructor. Every time the dynamic array becomes full and a new check has to be written, double the current size of the array. Write a doubleArray function for the class to do this, but place...

  • Given the following 2D array double nums[MAXROW][MAXCOL] = {{ 7, 12, 8, 23, 43, 16, 9,...

    Given the following 2D array double nums[MAXROW][MAXCOL] = {{ 7, 12, 8, 23, 43, 16, 9, 15}, {21, 7, 14, 48, 13, 6, 43, 29), {11, 2, 17, 91, 36, 14, 65, 43), {18, 5, 47, 38,52, 1, 18, 26}}; int numRows = 4; int numCols = 8; Write a function called totalRow that has the array, the number of rows, and the number of columns passed in. It will then print the total of the row that has 11,...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • Write a function named stats, that takes an array and the number of elements in the...

    Write a function named stats, that takes an array and the number of elements in the array as arguments. It must compute and print the minimum value in the array, maximum value in the array and the average value of all the values inside the array. Your function MUST be named stats Your function has two parameters in the following order: an array of type double The number of elements in the array, type int Your function should NOT return...

  • How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using...

    How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using namespace std; int main() {    // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic    // tic tac toe board is an array of int pointers    // each int pointer in the board points to a row    // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int)    const...

  • Please code using C++ Thank you Write a function that receives a 2d array of type...

    Please code using C++ Thank you Write a function that receives a 2d array of type double and returns the average of all elements in the 2d array

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