This is in C programming: Assume that PrecinctReport is a structured type with t
ID: 3779121 • Letter: T
Question
This is in C programming:
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.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
/* NPRECINCTS is a pre-declared int constant */
int const NPRECINCTS =3;
/*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.
*/
struct PrecinctReport
{
char address[5];
int felonies;
int murders;
int robberies;
}allPrecincts[]; /*an array named allPrecincts with NPRECINCTS elements */
int main()
{
//each of type PrecinctReport has been declared and initialized
allPrecincts[0].felonies=3;
allPrecincts[0].murders=5;
allPrecincts[0].robberies=1;
allPrecincts[1].felonies=2;
allPrecincts[1].murders=4;
allPrecincts[1].robberies=6;
allPrecincts[2].felonies=7;
allPrecincts[2].murders=1;
allPrecincts[2].robberies=4;
//an int variable murderCount has been declared
int murderCount=0;
int i;
/*traverses the allPrecincts array and adds up all the murder counts storing the resulting total in murderCount.*/
for( i=0;i<NPRECINCTS;i++)
{
murderCount +=allPrecincts[i].felonies + allPrecincts[i].murders + allPrecincts[i].robberies;
}
printf(" ");
/*print total murder count*/
printf("total murder count is : %d",murderCount);
printf(" ");
return 0;
}
----------------------------------------------
output sample:-
total murder count is : 33
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.