Arrays in argument/parameter. Would appreciate any help.
in
Programming Questions
•
11 months ago
Hi everyone, I'm taking a beginner's course in programming at the university and we're using processing to begin with. However, we have an assignment which I'm having a lot of issue with. The assignment asks us to make an object called Survivors, which are randomly chosen to be injured or not (health percentage), infected or not, to have ammo, etc. which I was able to do fine. However, the question goes on to ask:
"You want to know how viable your population of survivors is. Write two functions that take the array of survivors as arguments: (a) one that returns a percentage, the proportion of survivors who are completely healthy vs. being sick or injured; (b) one that returns the number of bullets in the possession of healthy survivors"
And I tried doing (a) for now and it isn't working. All I keep getting are errors, I'm getting "
syntax error insert assignmentoperator expression to complete assignment" with "survivorArray[i];" being highlighted
. I'm very lost. Any help would be greatly appreciated, here is my code for reference:
Survivor[] mySurvivor = new Survivor[10];
String names [] = new String[] {
"Jon", "Bob", "Billy", "Sander", "Jim", "Randy", "Horhay", "Madline", "Caroline", "Eddy", "Danny", "Ty", "Jack"
}; //Will be used to randomly give names to Survivors
int rad = 20; //Radius of the ellipses (the ellipse represents survivors)
void setup() {
size(1000, 600);
for (int i=0;i<10;i++) {
int index = int(random(names.length)); //This is used to give a random name from the array of strings above.
//initilize the survivor object: injured, infected, ammo, name, x position, y position
mySurvivor[i] = new Survivor(random(0, 100), (int)random(0, 2), (int)random(0, 250), index, random(rad, width-rad), random(rad, height-rad)); //Initialize the Surviver object
}
}
void draw() {
background(0);
for (int i=0;i<10;i++) {
mySurvivor[i].display(); //Operate the Survivor object
text(healthyVsSick(mySurvivor), 10, 10); //The mySurvivor is the argument for the function healthyVsSick;
}
}
float healthyVsSick(Survivor[] survivorArray) { //The function to return percentage of fine and not fine
//textSize(20);
int fine=0;
int notFine=0;
for (int i=0; i<11; i++) {
survivorArray[i]; //Takes the mySurvivor array
if (injured >50 && infected == 0) { //If their health is greater than 50, they aren't infected (so, completely fine)
fine++;
}
else //Else they are are not fine
{
notFine++;
}
}
return (fine/notFine)*100;
}
//Define the class for the survivor:
class Survivor {
//Variables for the Surviver:
color c; //To color it
float xPos;
float yPos;
float injured;
float infected;
int ammo;
int name;
boolean hurt = false; //Initilize the Survivor as not hurt
boolean sick = false; //Initilize the Survivor as not sick
Survivor(float tempInjured, float tempInfected, int tempAmmo, int tempName, float tempxPos, float tempyPos) { //The survivor constructor
//Will be passed from the arguments:
xPos = tempxPos;
yPos = tempyPos;
injured = tempInjured;
infected = tempInfected;
ammo = tempAmmo;
name = tempName;
if (injured >50) { //If their health is greater than 50, they aren't injured
hurt = false;
}
else if (injured <= 50) //Else they are.
{
hurt = true;
}
if (infected == 0) { //If their infection is 0, they aren't infected
sick = false;
}
else if (infected == 1) //If their infection is 1, they are infected
{
sick = true;
}
}
void display() {
ellipseMode(CORNER);
if (!hurt && !sick) { //If not hurt and not sick, display name, ammo and health %
fill (#00C118); //Colour green:
text(names[name], xPos + 10, yPos - 22);
text("Ammo: " + ammo, xPos + 10, yPos - 11);
text("Health: " + round(injured) + "%", xPos + 10, yPos - 1);
}
if (hurt && !sick) { //If hurt and not sick, display name, ammo and health %
fill(#F2FA23); //Colour yellow
text(names[name], xPos + 10, yPos - 22);
text("Ammo: " + ammo, xPos + 10, yPos - 11);
text("Health: " + round(injured) + "%", xPos + 10, yPos - 1);
}
if (!hurt && sick) { //If not hurt and sick, display name and "infected"
fill (#D80404); //Colour red
text(names[name], xPos + 10, yPos - 11);
text("Infected", xPos + 10, yPos - 1);
}
if (hurt && sick) { //If hurt and sick, display name and "infected"
fill(#D80404); //Colour red
text(names[name], xPos + 10, yPos - 11);
text("Infected", xPos + 10, yPos - 1);
}
textSize(9);
ellipse(xPos, yPos, rad, rad); //Will be position through the arguments
}
}
1