exam paper

hey guys, i got a question, i wrote exam and had to do nested if statements. let's say age is stored as a one dimensional array with range 1 to 50, and certain age intervals recieve a prize if a person wins within that age interval.

so say the following intervals recieves a prize, and prize is of type string, and u needed to use if statements, is it wrong if u say:

if(age[i] >= 14 && age[i] <= 25)
prize = "cell phone";
else if (age[i] >= 26 && age[i] <= 40)
prize = "voucher";
else if(age[i] > 40)
prize = "coffee machine";
Last edited on
What is the question?!

Your if statements do not check the interval [0, 13]
Last edited on
ok sorry think i explained it wrong, let me start over. i needed to use nested if statements with an one dimensional array called age and it has range 1 to 50. so say theres a competision and they give away prizes for range 14 to 25, 26 to 40, and over 40. the rest dont get a prize. so if range 14 to 25 wins a cellphone, 26 to 40 wins a voucher and over 40 wins a coffee machine, and the rest wins nothing.

now i am competing and i press my agw is 27, i would win a voucher.

my nestef ifs looked like this:

if(age[i] >= 14 && age[i] <= 25)
prize = "cell phone";
else if (age[i] >= 26 && age[i] <= 40)
prize = "voucher";
else if(age[i] > 40)
if(age[i] < 14)
prize = "nothing"
else
prize = "coffee machine";

all i really want to know is my condtions where i say age[i]..... wrong?
Last edited on
Is the range is a range of values of the array elements? What is the size of the array?

Your if statements incorrect because the condition

if(age[i] < 14)

inside

else if(age[i] > 40)

is always false.
Last edited on
Topic archived. No new replies allowed.