189k views
4 votes
Assume that PrecinctReport is a structured type with these fields, address (a string), and three int fields which are counts of crimes in the given precinct: felonies, murders, and robberies. Assume that NPRECINCTS is a pre-declared int constant and that an array named allPrecincts with NPRECINCTS elements, each of type PrecinctReport has been declared and initialized Assume that an int variable murderCount has been declared. Write the necessary code that traverses the allPrecincts array and adds up all the murder counts storing the resulting total in murderCount.

1 Answer

1 vote

Answer:

Step-by-step explanation:

#include <iostream>

#include <string>

#include <cstdlib>

#include <ctime>

using namespace std;

const int NPRECINCTS = 5;

typedef struct PReport

{

string address;

int felonies, murders, robberies ;

}PrecinctReport;

int murderCount(PrecinctReport all[])

{

int sum=0;

for(int i=0;i<NPRECINCTS;i++)

sum+=all[i].felonies + all[i].murders + all[i].robberies;

return sum;

}

int main()

{

PrecinctReport allPrecincts[NPRECINCTS];

for(int i=0;i<NPRECINCTS;i++)

{

cout<<"Enter the address of the "<<i+1<<": ";

cin>>allPrecincts[i].address;

cout<<"\\Enter the felonies of the "<<i+1<<": ";

cin>>allPrecincts[i].felonies;

cout<<"\\Enter the murders of the "<<i+1<<": ";

cin>>allPrecincts[i].murders;

cout<<"\\Enter the robberies of the "<<i+1<<": ";

cin>>allPrecincts[i].robberies;

cout<<"\\ \\";

}

cout<<"Total murders : "<<murderCount(allPrecincts)<<endl;

return 0;

}

User Kurt Pfeifle
by
4.6k points