Is it possible to compare arrays using a switch statement? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is it possible to compare arrays using a switch statement?

I wanted sth like multiple: if (arr[x]<arr2[x]){statements;} but simpler. Is there a way to use switch or other loops for it?

28th Nov 2017, 3:47 PM
N8W1nD
N8W1nD - avatar
3 Answers
+ 3
assuming that x is random length of arr and both arr and arr2 are the same length this works becuase you will be comparing for example arr2[23]< arr[44]{true} but it might be best to show more of your code so we can help
28th Nov 2017, 4:21 PM
D_Stark
D_Stark - avatar
+ 2
What's your exact problem you're trying to resolve? Throw more info my way and I'll help you solve it.
28th Nov 2017, 3:51 PM
AgentSmith
0
This code is for my first(textbased) game. You shoot gummibears at the boss to stop him while he is moving towards you. The user inputs the color of the gummibear he wants to shoot(4 colors available, stored in char shoot). This part of the code is to evaluate which ammo type was used and change variables according to the effect. (ammo is stored in the ammo[4] array) The switch loop lowers the ammo in the array ammoR[x] so I can compare it with ammo[x]. In the if-statements I evaluate which ammo was used and the effects of it. This System is split in two so I can reuse it for the second and third boss where the same ammo will have different effects. Thats why I dont use one switch funktion for everything Can I exchange the if part with a switch part or make it more effective in another way? cin>>shoot; //ammo reduction: switch (shoot) { case 'R'||'r'://red ammo ammoR[0]=ammo[0]--; break; case 'G'||'g'://green ammo ammoR[1]=ammo[1]--; break; case 'Y'||'y'://yellow ammo ammoR[2]=ammo[2]--; break; case 'B'||'b'://blue gummibear ammoR[3]=ammo[3]--; break; } //effects of different ammo: if (ammoR[0]<ammo[0])//absorbed damage {dround--;} if (ammoR[1]<ammo[1])//effective damage {dround+=4;} if (ammoR[2]<ammo[2])//ncrit=true-next shot crits {dround++; ncrit=true;} if (ammoR[3]<ammo[3])//stun {dist++; dround+=2;}
28th Nov 2017, 7:28 PM
N8W1nD
N8W1nD - avatar