Question

In: Computer Science

(In C++ (very quick question)) 1. Write a complete program. Declare a 2-Dimensional (size 20 x...

(In C++ (very quick question))

1. Write a complete program. Declare a 2-Dimensional (size 20 x 30) integer array and use random numbers from 0 to 99 to initialize it. Define the prototype and the definition for a function called Find99(). The function takes three parameters: a 2-Dimensional array of integers (size 20 x 3), and 2 integers to specify the sizes of row and column of the array. (These two parameters should be used in your loops for checking the sizes of row and column.)

The function must check each element of the array. If the element contains the integer 99, the function should print the position of the number 99. (Hint: there may be multiple 99s or no 99 at all.)

Sample output: 99 is located at row 0 and column 0.

Solutions

Expert Solution

Please find below the code for the required problem, having comment for each line section to make the code understandable:

#include <iostream> //for input and output

#include<stdlib.h>// to use srand numbers function

#include<time.h> //to use time in strand so that everytime new sequence is generated

using namespace std;

int main()

{

    int i,j;          //declare the loop counters

    const int row = 20, col = 30; //set a constant column and row value to make a 20*30 2d array

    int arrayInput[row][col];        //2darray named arrayInput with size 20*30 having type int

    // function declaration with void retun type and passing array, number of columns and number of rows

    void Find99(int arrayInput[20][30], int row, int col);

    srand(time(0));   //helps to generate differnt sequence everytime.It is called once to generate random number

    //loops to generate random numbers and put in the arrayInput array

    for (i = 0; i<row; i++)

    {

        for(j=0;j<col;j++)

        {

             arrayInput[i][j]=rand()%100; //get random number and store in the 2darray

           

        }

    }

    Find99(arrayInput, row, col); //call the function Fin99 with array and cout of row and columns in it

    return 0;

}

void Find99(int arrayInput[20][30], int row, int col){//function definition

    int i, j; //counters to be used in the function

    //loop counter to traverse each element in the array

    for (i = 0; i<row; i++)

    {

        for(j=0;j<col;j++)

        {

            if(arrayInput[i][j]==99) //if the located array element is 99?

            {

            //print the details when 99 is located

             cout<< "99 is located at row "<<i<<" and column " <<j<<endl;

            }

        }

    }

    return;

}

Please find below the screenshot for the code orientation:

Description: The program will have an array named arrayInput with size 20*30 as per the problem requirements. rand()%100 will generate a random number between 0 to 99 and store the number in array. Static variable col and row will be passed to the function to define the number of rows and columns in the 2 dimensional array.

Please be noted that as per the sample output provided in the question, the row and column number will start from 0

Please find below the output for the program:

The numbers generated everytime will differ from the last execution. Thus the result will differ from one each other.

Run1:

Run 2:


Related Solutions

Question 2. Write a complete C++ program that uses a 2-dimensional array with 4 rows and...
Question 2. Write a complete C++ program that uses a 2-dimensional array with 4 rows and 30 columns. Row represents sections of a course and column represents the students, value inside each position of the array is the final exam grade for each students. Fill the array with random numbers between 40 and 100. Calculate the total, average, maximum, minimum for each section. Please do it simple. code
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
C++ Write a program to declare an array of double of size 25, ask the user...
C++ Write a program to declare an array of double of size 25, ask the user to input all array elements from the keyboard, then write a function named out_of_order that will test this array for the condition a[0] >= a[1] >= a[2] >= ... > a[24] The function returns a -1 if the elements are not out of order, otherwise it returns the index of the first element that is out of order. Explain what you do to avoid...
Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
In C++ Declare an integer array of size 20 and assign the array with 20 randomly...
In C++ Declare an integer array of size 20 and assign the array with 20 randomly generated integers in the range [1, 100]. Shuffle the data in the array so that all numbers smaller than 50 are put before the numbers greater or equal to 50. Note you are not allowed to create any other arrays in the program. Finally, print out the shuffled array. (25 points) #include<iostream> using namespace std; const int NUM = 20; int main() {      ...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an integer pointer p. 3. Let p pointing to the array a[ ]. 4. Use p (you have to use p) to put 0 into the first element of this array, 2 into the second element, 4 into the 3rd element, 6 into the 4th element, ... 198 into the 100th element of this array. 5. Use a (you have to use a) to display...
Write a program (in Q0.c) to do the following: In main(), declare an integer x. Print...
Write a program (in Q0.c) to do the following: In main(), declare an integer x. Print the address of x (using the address-of operator). Pass x as an argument to a function void fooA(int* iptr). (Hint: can you pass x itself directly?) In fooA(int* iptr), print the value of the integer pointed to by iptr, the address pointed to by iptr, and the address of iptr itself. In the main function, following the call to fooA(...) , print the value...
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write Matrix Addition 2 D (dimensional) Array program in c++.
Complete the "dumb" 8 queens program that Use the 1 dimensional array representation. C++ This is...
Complete the "dumb" 8 queens program that Use the 1 dimensional array representation. C++ This is the solution to the  question. i want this program to written in different WAY. nothing fancy. #include<cmath> #include<fstream> #include<iostream> using namespace std; bool ok(int b[][8]){ int rQueens=0, dQueens=0; for(int row=0; row<8; row++){ for(int column=0; column<8; column++){ //Rows test if(b[row][column]==1) rQueens++; if(rQueens>1) return false; //Diagonals test    for(int j=1; ((column-j)>=0)&&((row-j)>=0); j++){ if(b[row-j][column-j]==1&&b[row][column]==1) return false; } for(int k=1; ((column-k)>=0)&&((row+k)<8); k++){ if(b[row+k][column-k]==1&&b[row][column]==1) return false; } } rQueens=0; }...
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2)...
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2) Write a program to implement the merge sort algorithm in a one dimensional array?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT