Question

In: Computer Science

Can someone explain the code for each line?(how do they run,what do they mean) void printArray2D(int...

Can someone explain the code for each line?(how do they run,what do they mean)

void printArray2D(int arr[M][N]){
int i, j;
for(int i =0; i < M; i++){
for(int j=0; j<N; j++){
printf(" %d ", arr[i][j]);//print all the array elements
}
printf("\n");
}
}

void populateRandom2D(int arr[M][N]){
int i, j;
int min, max;
printf("Enter min number in the array:\n");
scanf("%d", &min);
printf("Enter max number in the array:\n");
scanf("%d", &max);
for(int i =0; i < M; i++){
for(int j=0; j< N; j++){
arr[i][j] = rand() % (max+1-min) + min;//generate the random numbers between the range of min and max
}
}
}

void linearSearch2D(int arr[M][N]){
int i, j, r;
printf("Please enter a value to search for:");
scanf("%d", &r);
for(int i =0; i < M; i++){
for(int j=0; j< N; j++){
if(r == arr[i][j]){//loop the array and print out the location of the element when it catch the element
printf("value %d was found at (%d, %d)\n", r, i+1, j+1);
}
}
}
}

void rightShift2D(int arr[M][N]){
int i,j, temp;
int a[M][N];

temp = arr[3][2];
for(i=0; i<M; i++){//row
for(j=0; j< N; j++){//column
a[i][j] = arr[i][j];//avoid to use the changed element.
if(j==0){
arr[i][j] = a[i-1][j+2];
}
else{
arr[i][j] = a[i][j-1];
}
}
}
arr[0][0] = temp; // the first element is the last element in the array
}

Solutions

Expert Solution

In printArray2D(prints a 2-D array)

void printArray2D(int arr[M][N]){ /*defines a function named printArray2D of return value void (means it returns nothing and receives a 2-D array named arr with M rows and N columns*/

int i, j;//declares two integers i and j

for(int i =0; i < M; i++){ /*runs a for loop from i=0 to i<m to iterate through the rows, M is number of columns in a 2-D array passed as parameter to the function( named arr)*/

for(int j=0; j<N; j++){ /*runs a for loop from j=0 to i<n to iterate through the columns,N  is number of columns in a 2-D array passed as parameter to the function( named arr)*/

printf(" %d ", arr[i][j]); /*prints all the array elements %d specifies the type of output as integer and printf takes to argunments here first one displays the string as it is without %d,which gets replaced by arr[i][j] whien iterated through array using for loop*/

}
printf("\n");/*prints the output on next line->'\n' basically means change the line
}
}

_______________________________________________________________________________________

In populateRandom2D(populates a 2-D array by random elements within min and max range)

void populateRandom2D(int arr[M][N]){

/*defines a function named populateRandom2D of return value void (means it returns nothing and receives a 2-D array named arr with M rows and N columns*/

int i, j;//declares two integers i and j

int min, max;//declares two integers min and max

printf("Enter min number in the array:\n"); /*prints the value between "" on the screen as it is and changes line for next event ,input or output(due to \n at the end)*/

scanf("%d", &min); /*receives the value from the console of type(integer , %d signifies type as integer) and store the value in variable named min as defined on the 4th line*/

printf("Enter max number in the array:\n"); /*prints the value between "" on the screen as it is and changes line for next event ,input or output(due to \n at the end)*/

scanf("%d", &max); /*receives the value from the console of type(integer , %d signifies type as integer) and store the value in variable named max as defined on the 4th line*/

for(int i =0; i < M; i++){ /*runs a for loop from i=0 to i<m to iterate through the rows, M is number of columns in a 2-D array passed as parameter to the function( named arr)*/

for(int j=0; j<N; j++){ /*runs a for loop from j=0 to i<n to iterate through the columns,N  is number of columns in a 2-D array passed as parameter to the function( named arr)*/

arr[i][j] = rand() % (max+1-min) + min;/*generate the random numbers between the range of min and max using predefined rand() function that generates random numbers,to make sure the limit of random number generated between min and max,it is further manipulated by taking modulo and adding min.*/
}
}
}

_______________________________________________________________________________________

In linearSearch2D(searches for an element in a 2-D array using linear search method)

void linearSearch2D(int arr[M][N]){ /*defines a function named linearSearch2D of return value void (means it returns nothing and receives a 2-D array named arr with M rows and N columns*/

int i, j, r; //declares three integers i, j and r

printf("Please enter a value to search for:"); /*prints the value between "" on the screen as it is */

scanf("%d", &r); /*receives the value from the console of type(integer , %d signifies type as integer) and store the value in variable named r as defined on the 2nd line*/

for(int i =0; i < M; i++){ /*runs a for loop from i=0 to i<m to iterate through the rows, M is number of columns in a 2-D array passed as parameter to the function( named arr)*/

for(int j=0; j<N; j++){ /*runs a for loop from j=0 to i<n to iterate through the columns,N  is number of columns in a 2-D array passed as parameter to the function( named arr)*/

if(r == arr[i][j]){/*loop the array and print out the location of the element when it catches the element entered to be present in the array,since whole statement is in for loop , it checks the same for each element of the array and prints the below statement if match occurs*/
printf("value %d was found at (%d, %d)\n", r, i+1, j+1);/*prints the value r, alongwith the position it was found at(in the array) signified using i+1 and j+1(i+1 & j+1 because of 0-indexing and we need to display position not index) */
}
}
}
}

_______________________________________________________________________________________

In rightShift2D(right shifts the content of an array)

void rightShift2D(int arr[M][N]){ /*defines a function named rightShift2D of return value void (means it returns nothing and receives a 2-D array named arr with M rows and N columns*/

int i,j, temp;//declares three integers i, j and temp

int a[M][N];//declare nother array a[M][N] as the same size of the arr[M][N]

temp = arr[3][2]; //save last value of arr into temp variable

for(int i =0; i < M; i++){ /*runs a for loop from i=0 to i<m to iterate through the rows, M is number of columns in a 2-D array passed as parameter to the function( named arr)*/

for(int j=0; j<N; j++){ /*runs a for loop from j=0 to i<n to iterate through the columns,N  is number of columns in a 2-D array passed as parameter to the function( named arr)*/

a[i][j] = arr[i][j];//store the original arrray contents to the new array a[M][N]

if(j==0){//check for the first column of the array j is for iterating the columns (2nd for loop).

arr[i][j] = a[i-1][j+2];//store the temp array a[i-1][j+2]'s content to the location specified as arr[i][j],it is basically moving last element of a row to the first column of next row but only for first element of a column checked in if condition as j==0*/

else{//beginning of else of the if

arr[i][j] = a[i][j-1];//simply shift elements to the right,ie,to the next column in else case

}
}
}
arr[0][0] = temp;
/*assign the first element(previously stored in temp) as the last element in the array */
}


Related Solutions

Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)...
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)       {        for(int k=0;k<j;k++)        cout<<A[k]<<" + ";        cout<<rem<<"\n";        return;       }     for(int i=0;i<=rem;i++)    {          if(i<=rem)          A[j]=i;          printperm(A,n-1,rem-i,j+1);    } }
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
Hello sir can you explain me this code in details. void claimProcess() { int id; bool...
Hello sir can you explain me this code in details. void claimProcess() { int id; bool found = false; system("cls"); printf("Enter patient ID for which you want to claim insurrence: "); scanf("%d", & id); int i; for (i = 0; i < patientCount; i++) { if (patients[i].id == id) { found = true; break; } } if (found == false) { printf("subscriber not found\n"); return; } int numOfDaysHospitalized, suppliesCost, surgicalFee, otherCharges; bool ICU; printf("How many days were you haspitalized: ");...
Can someone explain how to manipulate the clock in verilog code? For example, I do not...
Can someone explain how to manipulate the clock in verilog code? For example, I do not understand how to go from 100MHz to 68027Hz.
Can someone please write clear and concise comments explaining what each line of code is doing...
Can someone please write clear and concise comments explaining what each line of code is doing for this program in C. I just need help tracing the program and understand what its doing. Thanks #include <stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/wait.h> int join(char *com1[], char *com2[]) {    int p[2], status;    switch (fork()) {        case -1:            perror("1st fork call in join");            exit(3);        case 0:            break;        default:...
explain the code for a beginner in c what each line do Question 3. In the...
explain the code for a beginner in c what each line do Question 3. In the following code, answer these questions: Analyze the code and how it works? How can we know if this code has been overwritten? Justify how? #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int changed = 0; char buff[8]; while (changed == 0){ gets(buff); if (changed !=0){ break;} else{     printf("Enter again: ");     continue; } }      printf("the 'changed' variable...
Python 3 can someone explain in very simple terms what EVERY line of code does, including...
Python 3 can someone explain in very simple terms what EVERY line of code does, including what j and i do def longest(string): start=0;end=1;i=0; while i<len(string): j=i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: end=j start=i i=j; avg=0 for i in string[start:end]: avg+=int(i) print('The longest string in ascending order is',string[start:end]) print('The average is',avg/(end-start)) s=input('Enter a string ') longest(s)
Can you please explain in detail what each line of code stands for in the Main...
Can you please explain in detail what each line of code stands for in the Main method import java.util.Scanner; public class CashRegister { private static Scanner scanner = new Scanner(System.in); private static int dollarBills[] = {1, 2, 5, 10, 20, 50, 100}; private static int cents[] = {25, 10, 5, 1}; public static void main(String[] args) { double totalAmount = 0; int count = 0; for (int i = 0; i < dollarBills.length; i++) { count = getBillCount("How many $"...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q =...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); cout << *p; return 0; } Question 31 options: 10 20 compiler error Runtime error Question 32 A union’s members are exactly like the members of a Structure. Question 32 options: True False Question 33 Given the code below, what are the errors. #include <iostream> using namespace...
Given the root C++ code: void sort() {    const int N = 10;    int...
Given the root C++ code: void sort() {    const int N = 10;    int x[N];    for(int i = 0; i < N; i++)    {        x[i] = 1 + gRandom-> Rndm() * 10;        cout<<x[i]<<" "; }    cout<<endl;    int t;       for(int i = 0; i < N; i++)    {    for(int j = i+1; j < N; j++)    {        if(x[j] < x[i])        {   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT