Question

QUESTION 5: Write a C++ function called layers which takes an NxN integer array (for some...

QUESTION 5: Write a C++ function called layers which takes an NxN integer array (for some constant N) and populates the entries 0..N2-1 as follows:

  • The upper left entry (row-0, col-0) is 0.
  • Subsequent numbers are assigned along Southwest-to-Northeast diagonals, layer-by-layer.

For example, if N=4, your function would result in the following assignment to entries in the array:

0

2

5

9

1

4

8

12

3

7

11

14

6

10

13

15

(the individual “layers” are shown by alternating shading).

#define N 10     // or some other number...

// your function here

void layers(int a[][N]) {

}

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//Code

#include<iostream>

#include<iomanip>

#define N 10     // or some other number...

using namespace std;

// your function here

void layers(int a[][N]) {

              int n=0; //current number to assign

              int row=0; //current row

              int col=0; //current column

              int total=N*N; //total number of cells/locations

              //looping until all locations have been filled

              while(n<total){

                           //copying value of col and row to two temporary variables

                           int c=col;

                           int r=row;

                           //as long as r and c are valid, looping.

                           while(r>=0 && c<N){

                                         //adding n to location pointed by row r and column c

                                         a[r][c]=n;

                                         //increasing n to assign next number

                                         n++;

                                         //moving up

                                         r--;

                                         //moving right

                                         c++;

                           }

                           //if there is a row to complete, incrementing row

                           if(row<N-1){

                                         row++;

                           }else{

                                         //if row is last row, increasing col

                                         col++;

                           }

                          

              }

}

int main(){

              //creating an N*N array, layering it and displaying it

              int array[N][N];

              layers(array);

             

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

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

                                         cout<<setw(3)<<array[i][j]<<" ";

                           }

                           cout<<endl;

              }

             

              return 0;

}

/*OUTPUT for N=10*/


Add a comment
Know the answer?
Add Answer to:
QUESTION 5: Write a C++ function called layers which takes an NxN integer array (for some...
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
  • Given an array and a starting position write a function replaceFromN, that takes an integer array...

    Given an array and a starting position write a function replaceFromN, that takes an integer array 'array', the size of the array size and a starting positions 'n' as parameters and replaces the elements starting from that index onward with the sequence 1,2,3,... The function returns nothing. void replaceFromN(int array[], int size, int n) For example, given array= {15,12,4,9,2,3} n =2 the function should modify array to be {15,12,1,2,3,4}

  • C++ ProgrammingWrite a function that takes an integer array, the array size and a number....

    C++ ProgrammingWrite a function that takes an integer array, the array size and a number. And it determines how many times that number appears in the array. Just copy the following code and paste it to your answer. And fill the corresponding part. #include < iostream >using namespace atd; int howmany (int array lint , int number){ //Your code comes here} int main() {const int N =10; int array[N] = [11,3,2,1,3,4,6,9,1.3); int count = howlany(array,N,3);cout << 3 < "appesa" << cout << "times!"; return 0;

  • Java Write a function/method called CheckHash: takes two arguments, an array and an integer count n....

    Java Write a function/method called CheckHash: takes two arguments, an array and an integer count n. The function computes the exclusive OR of the hashcodes (https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#hashCode--) of the first n strings in the array (result type int).

  • 1.) Write a function called canMakeSum() that takes a sorted array of n integers and determines...

    1.) Write a function called canMakeSum() that takes a sorted array of n integers and determines whether two distinct elements of the array add up to some specified value, "key". If so, return 1. Otherwise, return 0. The function signature is: int canMakeSum(int *array, int n, int key);

  • Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e....

    Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e. a two-dimensional array), number of rows and number of columns as input and prints the matrix after rotating it 90 degrees clockwise. Example: void rotate-matrix (int* matrix, int row, int column) Inputs: row 4, column-6 and the pointer to the matrix below: 2 3 6 5 8 0 Output:

  • c++ help please! Create a 2D character array in your main function and use nested for...

    c++ help please! Create a 2D character array in your main function and use nested for loops to fill the array with the letter ‘e’ to represent empty spaces. Create a function to print the board on the screen using a nested for loop. The function header is: void printBoard (char board [][3]) Create a function that checks whether a particular space has already been filled. If the space is filled it returns a boolean value of true, otherwise false....

  • Homework Question Write a void function called transformArray that takes two parameters - a reference to...

    Homework Question Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array.  The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was...

  • ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given....

    ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given. Perform various matrix processing activities according to the algorithms below. Store the results into the output vector (one-dimensional array) with appropriate size. For Grade 7) Count the number of odd values (n mod 2 <> 0) for each row. For Grade 9) Calculate the sum of positive values for each column. To obtain inputs and return the results, define appropriate type C/C++ functions. Please...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • I have to write a C program to assign seats on each flight of the airline’s...

    I have to write a C program to assign seats on each flight of the airline’s only plane (capacity: 40 seats, in 10 rows). For the sake of simplicity, assume that each row has 4 seats labeled A, B, C, D. Your program should display the following menu of alternatives: Please type 1 for "first class" Please type 2 for "business class" Please type 3 for “economy class”. If the person types 1, then your program should assign a seat...

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