Question

Write a C++ program using function that takes a Two Dimensional array, number of rows and...

Write a C++ program using function that takes a Two Dimensional array, number of rows and number of columns in
that array as argument, and returns another 2D-array containing binary representation of those
numbers.

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

Refer to screenshots for Indentation and Comments

CODE :

#include <iostream>
#include <math.h>
using namespace std;    
long long** binary_array(int** int_array,int n, int m);          // Declaration of function
int main () {
    int n,m;
    cout<<"Enter number of rows : ";                      
    cin>>n;                                                      // Number of rows
    cout<<"Enter number of columns : ";
    cin>>m;                                                      // Number of columns
    int** int_array=new int*[n];                                 // Dynamic 2-D array with memory allocation using new
    cout<<"Enter the respective values of 2-D array : "<<endl;
    for(int i=0;i<n;i++)
    {
        int_array[i]=new int[m];                                    // Allocating memory for i'th row
        for(int j=0;j<m;j++)
            {
                int t;
                cin>>t;
                int_array[i][j]=t;                                  // input value of ith row and jth columns
            }  
    }
    long long** bin_array = binary_array(int_array,n,m);            // function call
    cout<<"Binary representation of each number is : "<<endl;
    for (int i = 0; i < n; i++)
    {
        for(int j=0;j<m;j++)
            cout<<bin_array[i][j]<<"\t";                            // printing the result
        cout<<endl;
    }
    return 0;
}

long long** binary_array(int** int_array,int n, int m)              // function definition
{
    long long** bin;
    bin = new long long*[n];                                        // 2-D dynamic array
    for(int i=0;i<n;i++)
    {
        bin[i] = new long long[m];                                  // Allocating memory for i'th row
        for(int j=0;j<m;j++)
        {
            int repres[20];
            int k=0;
            long long temp = 0;
            int val = int_array[i][j];
            while(val)
            {
                repres[k]=val%2;                                     //Calculating and storing the binary representation
                val=val/2;
                k++;
            }
            while(k--)
            {
                if(repres[k]==1)
                temp+=pow((long long)10,(long long)k);                  // Loop to store binary value of each value in our integer array
            }
            bin[i][j]=temp;
        }
    }
    return bin;                                                         // return the binary array
}

Snapshots of CODE

SAMPLE OUTPUT :

Add a comment
Know the answer?
Add Answer to:
Write a C++ program using function that takes a Two Dimensional array, number of rows and...
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
  • Use C: 3. In function main, declare a two-dimensional integer array with 5 rows and 4...

    Use C: 3. In function main, declare a two-dimensional integer array with 5 rows and 4 columns. Call the following functions from function main. Call a function createArray to seed the random number generator and to fill the two-dimensional array with random integers between -20 and +20. Parameters for this function should be the array and the number of rows and columns). Call a function signs to count the number of positive values, number of negative values and number of...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • C++.Write the body of a function that returns the sum of all elements in a two-dimensional...

    C++.Write the body of a function that returns the sum of all elements in a two-dimensional array passed to it as an argument. Declare and initialize all needed variables. int arraySum(const int numbers[][COLS], int rows) { }

  • Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of...

    Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of integers in which each row has three columns the number of rows in the array function  f returns the sum of the odd integers in the given array of integers and returns zero if there are no odd integers in the array COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None COMPILER COMMAND LINE ARGUMENTS -lm INPUT OF THE TEST CASE 1 #define NUM_ROWS 5...

  • Write a C++ function that takes a two-dimensional array and a row index. The function should...

    Write a C++ function that takes a two-dimensional array and a row index. The function should return the multiplication of all the values in the row. Write a main () program to test the function. Student Note: Please solve the question as simple as you can.

  • Using Java. Write a program that creates a “triangular” two-dimensional array A of 10 rows. The...

    Using Java. Write a program that creates a “triangular” two-dimensional array A of 10 rows. The first row has length 1, the second row has length 2, the third row has length 3, and so on. Then initialize the array using nested for loops so that the value of A[i][j] is i+j. Finally, print out the array in a nice triangular form.

  • Write a C++ function printArray(A, m, n) that prints a mxn two dimensional array A of...

    Write a C++ function printArray(A, m, n) that prints a mxn two dimensional array A of integers, declared to be "int** A," to the standard output. Each of the rows should appear on a separate line. Your input will be as follows with the first two values being the number of rows and columns respectively. HINT: This is best done with one for-loop nested within another Sample 1/0 1: Input: 3 2 1 2 3 4 Ол 6 Output: 12...

  • Write a piece of code that constructs a two-dimensional array of integers with 5 rows and...

    Write a piece of code that constructs a two-dimensional array of integers with 5 rows and 10 columns. Fill the array with a multiplication table, so that array element [i][j] contains the value i * j. Use nested for loops to build the array. java Program

  • Write a program that uses a two-dimensional array to store the highest and lowest temperatures for...

    Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. You should also have a parallel array to store the names of the 12 months. The program should read the Month's names, and the temperatures from a given input file called temperature.txt. The program should output the highest and lowest temperatures for the year, and the months that correspond to those temperatures. The program must use the following functions:...

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