Write a program that prompts the user to enter the position of an object (an int
ID: 3850570 • Letter: W
Question
Write a program that prompts the user to enter the position of an object (an integer number) and the move type, the program uses the switch statement to find and print the new position according to the following:
Move Type
Number of Move Units
1, 2
3, 4
5
6, 7, 8
9, 10
No move at all
9 units to the right
12 units to the left
1 unit to the right
2 units to the left
Note (using c++)
Move Type
Number of Move Units
1, 2
3, 4
5
6, 7, 8
9, 10
No move at all
9 units to the right
12 units to the left
1 unit to the right
2 units to the left
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
cout << "Enter Position :" << endl;
int pos;
cin >> pos;
cout << "Enter Move Type (1-10) :"<<endl;
int type;
cin >> type;
int isValid=1;
switch(type){
case 1:
case 2:
break;
case 3:
case 4:
pos +=9;
break;
case 5:
pos -=12;
break;
case 6:
case 7:
case 8:
pos +=1;
break;
case 9:
case 10:
pos -=2;
break;
default :
cout << "Invalid Move Type"<<endl;
isValid=0;
break;
}
if(isValid==1)
cout << "New Position is "<<pos<<endl;
return 0;
}