Please give me the solutions for 1-3 Name: Worksheet: 2D Arrays and Loops 2D arr
ID: 3807109 • Letter: P
Question
Please give me the solutions for 1-3
Name: Worksheet: 2D Arrays and Loops 2D arrays are helpful in storing information about data collected when a location needs to be stored. In archeology, a dig site is typically divided into a grid of one foot squares that are marked off with string. The following array, grid, represents the number of artifacts found in each square of a dig site: o 8 8 5 3 1. Write the code to print out the total number of square feet at the site. 2. Write the code to find the total number of artifacts found at this dig site. 3. write a method to count how many locations on the grid have a certain number of artifacts. So if 7 is passed in, it would return 5. (back)Explanation / Answer
Let grid[5][6] represents the given grid. Also value of grid[i][j] represents the number of artifacts found in the ith row and jth colomn of the grid.
Let variable row and colomn contains the number of rows n colomns in the grid.
Also assume grid, row and colomn are global variables.
1)
int number_of_square_feet = 0;
for(int i=0;i<row;i++)
for(int j=0;j<colomn;j++)
number_of_square_feet++;
cout<<"number_of_square_feet = "<<number_of_square_feet;
2)
int total_number_of_artifacts = 0;
for(int i=0;i<row;i++)
for(int j=0;j<colomn;j++)
total_number_of_artifacts = total_number_of_artifacts + grid[i][j];
cout<<"total_number_of_artifacts = "<<total_number_of_artifacts;
3)
int count_location(int count){
int location = 0;
for(int i=0;i<row;i++)
for(int j=0;j<colomn;j++)
if(grid[i][j] == count)
location++;
return location;
}