Please help me find the solutions for 1-3. This is for a high school AP computer
ID: 3807597 • Letter: P
Question
Please help me find the solutions for 1-3. This is for a high school AP computer science class, so please don't use too hard of things in the code.
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
// C++ code
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int grid[][6] = {{0,4,1,8,6,1},{4,7,7,0,7,1},{0,8,8,5,3,1},{4,0,3,4,4,7},{6,1,7,5,6,0}};
int totalArtifacts = 0;
int squarefeet = 0;
int number;
// part1
int n = sizeof grid / sizeof grid[0]; // 2 rows
int m = sizeof grid[0] / sizeof(int); // 5 cols
squarefeet = n*m;
cout << "Total squarefeet: " << squarefeet << endl;
// part 2
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
totalArtifacts = totalArtifacts + grid[i][j];
}
}
cout << "Total number of artifacts: " << totalArtifacts << endl << endl;
// part 3
cout << "Enter a number: ";
cin >> number;
int count = 0;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if(grid[i][j] == number)
count++;
}
}
cout << "There are " << count << " locations for " << number << " artifacts " << endl;
return 0;
}
/*
output:
Total squarefeet: 30
Total number of artifacts: 118
Enter a number: 4
There are 5 locations for 4 artifacts
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.