CODE in JAVA or C. emester Programming Projec This program should illustrate and
ID: 3851126 • Letter: C
Question
CODE in JAVA or C.
emester Programming Projec This program should illustrate and simulate the ethical issue of the tragedy of the commons. In essence it is a software toy that permits users to examine the balance between choices that benefit the individual but cost society versus those that benefit society at a cost to the individual The program can be written in either Java or C. Students have broad discretion with regards to implementation Quick overview. Please consider creating an array (or linked list) of objects. Each object might have an integer ID, a Boolean switch (selfish or not-selfish), and a happiness score (integer). Then use the object in part A Finally, implement part B representing the society Part A. The first part of the program will create a series of individuals (objects) who make decision that are either selfish or altruistic. Each object will make its decisions independently. Some objects may choose to always be selfish. Others will always be altruistic. And some might be random. Each object will have an identity (an integer is sufficient), and can generate its choice. Each object will also have a score for happiness, which can change The program should be able to create from 1 to 20 (or more, if you prefer) individuals according to the user's preference. The program should be able to run multiple iterations (each with the same individuals), and be capable of up to 20 (or more, if you prefer) iterations, with output for each iteration When an individual is selfish, they gain 5 happiness points. When they are altruistic, they lose 2 happiness points When Part B is done, the government score will be added to the functionality of Part A. Part B. The second part will incorporate an entity called "government which will represent the aggregate effects of individual decisions by the individual objects If all individuals choose to be altruistic, society will benefit, but all of the individuals will be poor. If all individuals choose to be selfish, the individuals may do well, but society will be miserable. And if some number of individuals are altruistic and others are selfish, a balance might be achieved. The balance could be optimized, providing results for the number of objects with various characteristics If there are more selfish individuals than altruistic individuals, the government score is a negative number equal to the net number of selfish individuals (selfish -altruistic) If there are an equal number of selfish and altruistic individuals, the government score is a zero If there are more altruistic individuals than selfish individuals, the government s is a positive number equal to the net number of altruistic individuals. (Altruistic selfish)Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct individual{
int id;
bool selfish;
int happiness_score;
}arr[20];
int n,government_score;
//PART A
void part_a(){
printf("Enter no. of individuals ");
scanf("%d", &n);
int i;
for(i=0;i<n;i++){
arr[i].id=i; // to assign id to the individual
arr[i].selfish=rand()%2;
//assuming initial happiness score of all the individuals is 0
if(arr[i].selfish)
arr[i].happiness_score=5;
else
arr[i].happiness_score=-2;
//display information
printf("Individual Id - %d ",arr[i].id);
if(arr[i].selfish)
printf("Selfish ");
else
printf("Altruistic ");
printf("Happiness Socore - %d ",arr[i].happiness_score);
}
}
void part_b(){
int count_selfish=0,i,count_altruistic=0,punishment;
//to count no. of selfish indviduals
for(i=0;i<n;i++){
if(arr[i].selfish)
count_selfish++;
}
//no. of altruistic individuals
count_altruistic=n-count_selfish;
if(count_altruistic==count_selfish)
government_score=0;
else
government_score=count_altruistic-count_selfish; // positive score for more altruistic, negative score for more selfish individuals
//add the government score in individual happiness
for(i=0;i<n;i++)
arr[i].happiness_score+=government_score;
printf(" Government score is %d ",government_score);
//punishment
if(government_score>0){
for(i=0;i<n;i++){
if(arr[i].selfish)
{
punishment=rand()%100;
if(punishment<9)
printf("Individual %d will be charged a penalty of 50 points ", i);
}
}
}
if(government_score<0){
for(i=0;i<n;i++){
if(arr[i].selfish)
{
punishment=rand()%100;
if(punishment<50)
printf("Individual %d will be charged a penalty of 75 points ", i);
}
}
}
}
int main(){
part_a();
part_b();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.