In C++ Write a program that create an array of struct for 4 months (4 element ar
ID: 3706287 • Letter: I
Question
In C++ Write a program that create an array of struct for 4 months (4 element array) showing the following household budget items:
Rent (int), Gas (double), Water (double), Garbage (int), Donation (string) and Total (double which is Rent, Gas, Water and Garbage added together).
Then prompt a user to enter information for each of those 4 months. A user should enter five items with the sixth item (Total) being created for them on the basis of their input.
At the end of the fours months worth of data collection from the user, the program should output the overall total plus an average for each item (except Donation) for all four months. For Donation, you can either list out all the donations given or just not put anything as part of the overall Total
Explanation / Answer
The code is as follows :
#include <bits/stdc++.h>
using namespace std;
typedef struct household{ // declaring the data structure
int rent, garbage;
double gas, water, total;
string donation;
}household;
int main()
{
household arr[4]; // declaring the array of the structure
double total_rent=0, total_gas=0, total_water=0, total_garbage=0; // initializing the varibles
for(int i = 0; i < 4; i++) // loop to take the input
{
cout << "Enter the rent of the " << i+1 << "th month" << endl;
cin >> arr[i].rent;
cout << "Enter the gas cost of the " << i+1 << "th month" << endl;
cin >> arr[i].gas;
cout << "Enter the water cost of the " << i+1 << "th month" << endl;
cin >> arr[i].water;
cout << "Enter the garbage cost of the " << i+1 << "th month" << endl;
cin >> arr[i].garbage;
cout << "Enter the donation of the " << i+1 << "th month" << endl;
cin >> arr[i].donation;
total_rent += arr[i].rent; // calculation of the total of each commodity
total_water += arr[i].water;
total_garbage += arr[i].garbage;
total_gas += arr[i].gas;
arr[i].total = arr[i].gas + arr[i].water + arr[i].rent + arr[i].garbage; // calculating the total each month
}
double overall = 0;
double av_rent, av_water,av_gas,av_garbage;
for(int i = 0; i < 4 ; i++)
{
overall += arr[i].total; // calculating the overall total
}
av_rent = total_rent / 4; // calculating the average of each
av_water = total_water/ 4;
av_garbage = total_garbage / 4;
av_gas = total_gas / 4;
cout << "The overall totoal cost was " << overall << endl; // printing
cout << "The average rent was " << av_rent << endl;
cout << "The average water was " << av_water << endl;
cout << "The average garbage was " << av_garbage << endl;
cout << "The average gas was " << av_gas << endl;
cout << "The list of donations is as follows :" << endl;
for(int i = 0; i < 4; i++) // printing the donations
{
cout << i+1 << ") " << arr[i].donation << endl;
}
return 0;
}
Sample Output :
Enter the rent of the 1th month
12
Enter the gas cost of the 1th month
23
Enter the water cost of the 1th month
34
Enter the garbage cost of the 1th month
45
Enter the donation of the 1th month
abc
Enter the rent of the 2th month
23
Enter the gas cost of the 2th month
34
Enter the water cost of the 2th month
45
Enter the garbage cost of the 2th month
6
Enter the donation of the 2th month
54
Enter the rent of the 3th month
344
Enter the gas cost of the 3th month
34
Enter the water cost of the 3th month
54
Enter the garbage cost of the 3th month
56
Enter the donation of the 3th month
yu
Enter the rent of the 4th month
56
Enter the gas cost of the 4th month
34
Enter the water cost of the 4th month
2
Enter the garbage cost of the 4th month
3
Enter the donation of the 4th month
4
The overall totoal cost was 805
The average rent was 108.75
The average water was 33.75
The average garbage was 27.5
The average gas was 31.25
The list of donations is as follows :
1) abc
2) 54
3) yu
4) 4