Question

In C++ for 2D Array:

Write a program that uses a function that manipulates a 2D array of integers as follows: 1. Fill the first row and column in

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

Code

In the below code change the array size(if required) in the highlighted area of code.

#include <iostream>
#include<cstdlib>
using namespace std;

int main()
{
int row,column;
int min = 0;
int max = 1;
int a[3][6];
row = 3;
column = 6;

int range = max - min + 1;
srand(time(0));
for(int i = 0;i<row;i++)
{
a[i][0]= rand() % range + min; //To set the first coloumn with random bits
}
for(int j = 1;j<column;j++)
{
a[0][j]= rand() % range + min; //To set the first row with random bits
}
for(int i = 1;i<row;i++)
{
for(int j = 1;j<column;j++)
{
   if(a[i-1][j]==a[i][j-1]) /*If top and left element are equal,current element is equal to sum of top and left element*/
a[i][j]= a[i-1][j] + a[i][j-1];
if(a[i-1][j]<a[i][j-1]) /* If top element is greater than left element, current element is equal to top element*/
a[i][j]= a[i][j-1];
if(a[i-1][j]>a[i][j-1]) /*If left element is greater than top element, current element is equal to left element */
a[i][j]= a[i-1][j];
  
}
}
for(int i = 0;i<row;i++) // To print the array
{
for(int j = 0;j<column;j++)
{
cout<< a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}

Screenshot of code is given for clarity:

9 1 #include <iostream> 2 #include<cstdlib> 3 using namespace std; 4 5 int main() 6 { 7 int row, column; 8 int min = 0; int m

Output

First execution

0 1 0 O

Second execution

оо о о 1

Add a comment
Know the answer?
Add Answer to:
In C++ for 2D Array: Write a program that uses a function that manipulates a 2D...
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
  • C++ program Write a program which: 1. Enters data into the 2D array of integers which...

    C++ program Write a program which: 1. Enters data into the 2D array of integers which is 5x5 The data should be entered with an assignment statement using nested for loops that is do not scan in the data. The data should be: 1 2 4 8 16 1 3 9 27 81 1 4 16 64 256 1 5 25 125 625 1 6 36 216 1296 2. Print out the following using a nested for loop Row 0:...

  • Write a Java program that does the following. a. Declare an integer 2D array with 5...

    Write a Java program that does the following. a. Declare an integer 2D array with 5 rows and 5 columns. b. Initialize the array's elements to random integers between 1 and 10 (inclusive). c. Display all the elements in the 2D array as a table of rows and columns. d. Display the row index and column index of all the even integers in the 2D array. e. Display the sum of first row's elements.

  • Done in C++ using visual studio 1. Write a program with one additional function called int[]...

    Done in C++ using visual studio 1. Write a program with one additional function called int[] reverseArray(int array). This function will receive an array from main and then reverse and should return the reversed array to the main to print out. Use a single array and a single loop, you’re are neither allowed to print out just in reverse order nor allowed to use another array variable to store the original array in reverse order. 2. Write a program which...

  • Please solve only if you know how to do it. Write the code using C++ (not...

    Please solve only if you know how to do it. Write the code using C++ (not Python or Java). Show and explain everything neatly. COMMENTS (7.5% of programming assignment grade): Your program should have at least ten (10) different detailed comments explaining the different parts of your program. Each individual comment should be, at a minimum, a sentence explaining a particular part of your code. You should make each comment as detailed as necessary to fully explain your code. You...

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

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

  • Write a program which creates a 2D array to store decimal values and passes that array...

    Write a program which creates a 2D array to store decimal values and passes that array to another function which sets each element of the array to a random value between 0.0 and 10.0.

  • Write a function called ZeroCornersSecondRow that sets the corners of a 2D array to zero and...

    Write a function called ZeroCornersSecondRow that sets the corners of a 2D array to zero and also outputs the second row. The input arguments: • inArray: A double precision 2D array of size nxn, where n is at least 3. The output arguments: outArray: A double precision 2D array of size nxn that is a copy of inArray except with the corners set to zero. • secondRow: A double precision 1D array of size n. This is the second row...

  • Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...

    Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...

  • Write a C program that uses mallloc to create a 2D array. of c columns and...

    Write a C program that uses mallloc to create a 2D array. of c columns and r rows. Elements of array must be initialized to 0 and you must use malloc and free allocated memory fo 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