Question

Write a program that randomly generates a 20 3 20 two-dimensional array, board, of type int....

Write a program that randomly generates a 20 3 20 two-dimensional array, board, of type int. An element board[i][j] is a peak (either a maximum or a minimum) if all its neighbors (there should be either 3, 5, or 8 neighbors for any cell) are less than board[i][j], or greater than board[i][j]. The program should output all elements in board, with their indices, which are peak. It should also output if a peak is a maximum or a minimum.

c++ programing

code as text

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

If you have any doubts please comment below.

It implements all the functions mentioned above and also implents an extra function isValid(int i, int j) which checks if i,j are valid indexes for the 2d array.
fillBoard fills the array with random numbers between [0,99].
In case of any doubts just comment down.

Cpp Code:

#include <iostream>

#include <cstdlib>

using namespace std;

const int ROWS = 20;

const int COLS = 20;

void fillBoard(int b[][COLS]);

void showBoard(int b[][COLS]);

void findPeaks(int b[][COLS], int rows);

bool isValid(int i, int j);

int main(){

int board[ROWS][COLS];

fillBoard(board);

showBoard(board);

findPeaks(board,ROWS);

// system("pause");

return 0;

}

void fillBoard(int b[][COLS]){

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

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

b[i][j] = rand()%100;

}

}

}

void showBoard(int b[][COLS]){

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

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

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

}

cout << endl;

}

}

bool isValid(int i, int j){

if(i>=0 && i<=ROWS && j>=0 && j<=COLS){

return true;

}

return false;

}

void findPeaks(int b[][COLS], int rows){

for(int r=0;r<ROWS;r++){

for(int c=0;c<COLS;c++){

bool isMax = true, isMin=true;

for(int i=r-1;i<=r+1;i++){

for(int j=c-1;j<=c+1;j++){

if(!(i==r && j==c) && isValid(i,j)){

if(b[i][j]>b[r][c])

isMax = false;

if(b[i][j]<b[r][c])

isMin = false;

}

}

}

if(isMax)

cout << b[r][c] << "\t(" << r <<","<<c<<") is a MaxPeak" <<endl;

if(isMin)

cout << b[r][c] << "\t(" << r <<","<<c<<") is a MinPeak" <<endl;

}

}

}

Add a comment
Know the answer?
Add Answer to:
Write a program that randomly generates a 20 3 20 two-dimensional array, board, of type int....
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
  • ugi square Square. 1,000,000 times and see the number Ul ll dimensional board, of type int....

    ugi square Square. 1,000,000 times and see the number Ul ll dimensional board, of type int. An element board[i][j] is a peak (either aay, or a minimum) if all its neighbors (there should be either 3, 5, or 8 ncihitlh) for any cell) are less than board[ i][j], or greater than boa program should output all elements in board, with their indices, wh peak. It should also output if a peak is a maximum or a minimum. amaximum 21. Write...

  • Write a program that uses an int one-dimensional array list to input 10 integers representing amounts...

    Write a program that uses an int one-dimensional array list to input 10 integers representing amounts of money. Find the highest (maximum) amount and the lowest (minimum) amount. Output all the amounts and their difference to the highest amount side by side as shown in the sample output. c++ Chapter #8 (Arrays) and 6 (Value Returning and void Functions) Exercise #1: One-dimensional array manipulation Write a program that uses an int one-dimensional array list to input 10 integers representing amounts...

  • Array processing Create a program that processes two-dimensional array according to task variant Additional requirements 1....

    Array processing Create a program that processes two-dimensional array according to task variant Additional requirements 1. 2. 3. 4. Input dimensions of array form keyboard (use dynamic memory allocation) Fill array with random numbers in range R. Processing of array should be implemented as function (using indices). Displaying the content of array should be implemented as function (using pointers) 5 12 8 2 8 3 3 16 1 22 215 34 0 11 88 13 22 6 5 81 76...

  • Write a C program to do the following...in this EXACT order: a. Declare an integer array...

    Write a C program to do the following...in this EXACT order: a. Declare an integer array named alpha of 50 elements. b. Use a for loopt to initialize each element of alpha to its index value (e.g. alpha[0] = 0, alpha[1]=1, etc.). c. Output the value of the first element of the array alpha. d. Output the value of the last element of alpha. e. Set the value of the 25th element of the array alpha to 62 and output...

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

  • What is the run-time of this recursive code: int maximum(int array[], int index, int len); //define...

    What is the run-time of this recursive code: int maximum(int array[], int index, int len); //define maximum function. int minimum(int array[], int index, int len);//define minimum function. int main() { //Main method int array[MAX_SIZE], N, max, min; //Defining all the variable. int i; printf("Enter size of the array: ");//Taking input from user as a array size scanf("%d", &N); printf("Enter %d elements in array: ", N);//Taking input from user for(i=0; i<N; i++) { scanf("%d", &array[i]);//storing all the element in array. }...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • this is advance java Monster ARray Summary: Write a program that shows the path of an imaginary monster moving through a two dimensional array trying to "eat&#3...

    this is advance java Monster ARray Summary: Write a program that shows the path of an imaginary monster moving through a two dimensional array trying to "eat" the highest values in the array. As the monster goes, it leaves a trail behind it. Imagine a 40x40 array, displayed like this, where Os are printed as periods (dots), and non-zero valués are printed as normal. Then, place the values 2 through 9 randomly on the array. The rest of the document...

  • Write a C++ program to scale and average the values in a two-dimensional array. Write the...

    Write a C++ program to scale and average the values in a two-dimensional array. Write the following functions:           void randomize2DArray(int arr[ROW_SIZE][COL_SIZE]) – places random values between 1 and 100 in a two-dimensional array .           void scale2DArray(int arr[ROW_SIZE][COL_SIZE], double scale) – multiplies every value in the two-dimensional array by scale.           double average2DArray(int arr[ROW_SIZE][COL_SIZE]) – calculates the average value over all elements in the two-dimensional array. Randomize and print the values in the two-dimensional array of size 7 x...

  • 4. Write a program that finds the smallest element in a one-dimensional array containing 20 integer...

    4. Write a program that finds the smallest element in a one-dimensional array containing 20 integer values in the range 0 to N Hint: Consider following fragment of code Program in C language: for(c=0;c<size;c++) {     if(array[c] < minimum)     {         minimum = array[c];         location = c;     } } this c code is not running and missing some code . please add the missing code so when i paste it will run and provide flowchart

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