Question

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;
  
cin >> rows;
cin >> cols;
cin >> x;
  
int** arr = new int*[rows];
for(int i = 0; i < rows; ++i)
arr[i] = new int[cols];
  
// initialization, your code goes here

  
  
for(int i = 0; i < 3; i++){ // prints some values in the array
for(int j = 0; j < 3; j++)
cout << arr[i][j];
cout <<"\n";
}
  
return 0;
}

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

CodeToCopy:

Question 1:

2darray_init.cpp

#include <iostream>

using namespace std;

int main(){

    int rows = 5;

    int cols = 5;

    int x;

    /* allocating memory for rows */

    int** arr = new int*[rows];

    /* allocating memory for columns */

    for (int i = 0; i < rows; ++i ) {

        arr[i] = new int[cols];

    }

    /* taking x from user */

    cin >> x;

   

    /* place x at x'th row, x'th column */

    arr[x][x] = x;

   

    /* printing x'th row, x'th column value */

    cout << "arr[x][x] = " << arr[x][x] << endl;

    return 0;

}

Question 2:

2darray_init2.cpp

#include <iostream>

using namespace std;

int main(){

    int rows;

    int cols;

    int x;

     

    cin >> rows;

    cin >> cols;

    cin >> x;

     

    int** arr = new int*[rows];

    for(int i = 0; i < rows; ++i) {

        arr[i] = new int[cols];

       

        /* assigning x value to 2d array elements */

        for (int j = 0; j < cols; ++j) {

            arr[i][j] = x;

        }

    }

     

    for(int i = 0; i < 3; i++){ // prints some values in the array

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

            cout << arr[i][j] << " ";

        cout <<"\n";

    }

     

    return 0;

}

OutputScreenshot:


Add a comment
Know the answer?
Add Answer to:
Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...
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
  • Write loop(s) that print ALL the content of the following 2D array #include <iostream> using namespace...

    Write loop(s) that print ALL the content of the following 2D array #include <iostream> using namespace std; int main(){    int rows; int cols; int offset; cin >> rows; cin >> cols; cin >> offset;    int** arr = new int*[rows]; for(int i = 0; i < rows; ++i) arr[i] = new int[cols];    for(int i = 0; i < rows; i++) // initialization for(int j = 0; j < cols; j++) arr[i][j] = offset + i;    // printing, your code goes here   ...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • c++ #include using namespace std; int main() {    int n, x, num, j = 0;...

    c++ #include using namespace std; int main() {    int n, x, num, j = 0;    cin >> n >> x;    int*arr = new int[n];    for (int i = 0; i < n; i++)    {        cin >> num;        if (num < x)        {            arr[j] = num;            j++;        }    }    for (int i = 0; i < j; i++)    {...

  • One dimensional array What this code print #include <iostream> using namespace std; int main () {...

    One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << “Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << “ “; cout << endl; return 0; }

  • //Find two smallest integers #include <iostream> #include <string> using namespace std; // implement printArray here //...

    //Find two smallest integers #include <iostream> #include <string> using namespace std; // implement printArray here // implement findTwoSmallest here // DO NOT WRITE ANY CODE BELOW THIS LINE (EXCEPT FOR TESTING - REMOVE BEFORE SUBMITTING) int main() {     int n;     int arr[100];        cout << "Enter number of integers: ";     cin >> n;        cout << "Enter " << n << " integers: ";       for(int i = 0; i < n; i++) {        ...

  • Fix the code to print the result[] array properly: 1 #include <iostream> 2 3 using namespace...

    Fix the code to print the result[] array properly: 1 #include <iostream> 2 3 using namespace std; 4 5 void printArray(int array 7 for(inti= 0: i < 2;i++) cout くく array[i] << " ". 10 12 int mainO 13 14 int firstArray[2]10,20}; 16 int resultr21 17 int x; 18 19 II take input for second array 20 cin >>x 21 secondArray[0]-x; 22 secondarrayL1」-X+15 ; // Add elements 23 24 result0]- firstArray[0] + secondArray [0]; 25 result [1]- firstArray[1] + secondArray[1];...

  • Fix this code so only the function prototype comes before main. #include <iostream> using namespace std;...

    Fix this code so only the function prototype comes before main. #include <iostream> using namespace std; bool isMultiple(int num1, int num2) { return num1 % num2 == 0; } int main() { char ch = 'Y'; int num1, num2; while(ch =='Y') // While ch is equal to Y { cout << "Enter two numbers(largest first): "; cin >> num1; // Getting 1st number cin >> num2; // Getting 2nd number if(isMultiple(num1, num2)) cout << num2 << " " << "IS...

  • #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

    #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

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