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

  • Write a function called arrFactory that takes the address of an int** and two int values,...

    Write a function called arrFactory that takes the address of an int** and two int values, rows and cols. The function will use dynamic memory allocation such that after the function call the int** can be used as a 2D array with the specified number of rows and columns. All elements of the array must be set to 0 within the function without iterating through each element after the array is created. The function has a return type of void....

  • 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++ Write two versions of a function that returns a dynamic array consisting of all prime...

    c++ Write two versions of a function that returns a dynamic array consisting of all prime numbers less than or equal to the int parameter n. You may declare the dynamic array to have length n, even though this is a bit inefficient. In one version of the function, return the number of primes via a separate reference variable. In the other, use 0 as a sentinel value to signify the end of the prime list. See below for the...

  • 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

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

  • solve it in c+* Part II: Dynamic Arrays and Pointer Arithmetic Q5: Implement a subset function...

    solve it in c+* Part II: Dynamic Arrays and Pointer Arithmetic Q5: Implement a subset function for a dynamic array which returns a new dynamic array that is a subset of the original. (15pt) input parameters: array - (the array and any related parameters) start - index of the first element end - index of the last element This function returns a new dynamic array containing the elements from the start thru the end indices of the original array. All...

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