In: Computer Science
Please answer in C++! Let me know if you have any questions about these.
Program 1:
For this program, imagine we want to track an object and detect if it goes off the left or right side of the screen (that is, it’s X position is less than 0 and greater than the width of the screen, say, 100). Write a program that asks the user for the starting X and Y position of the object as well as the starting X and Y velocity, then prints out its position each frame until the object moves off of the screen. Design (pseudocode) and implement (source code) for this program.
Sample run 1:
Enter the starting X position: 50
Enter the starting Y position: 50
Enter the starting X velocity: 4.7
Enter the starting Y velocity: 2
X:50 Y:50
X:54.7 Y:52
X:59.4 Y:54
X:64.1 Y:56
X:68.8 Y:58
X:73.5 Y:60
X:78.2 Y:62
X:82.9 Y:64
X:87.6 Y:66
X:92.3 Y:68
X:97 Y:70
X:101.7 Y:72
Program 2:
Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack. When the player attacks, it will pick a random number between 0 and up to the maximum damage the player does, and then subtract that from the monster. The same thing happens when the monster attacks the hero, but damage is to the hero. The program should display rounds and the HP of the hero and monster each round. If the hero or monster dies, it should print that this happened and should NOT continue (i.e. no extra text). To learn how to create random numbers, see the appendix.
Sample run 1:
Enter the hero's starting hit points: 50
Enter the damage the hero’s weapon does per strike: 20
Enter the monster's starting hit points: 40
Enter the monster's damage per strike: 15
====== ROUND 1 ======
Hero attacks for: 10
Monster has 30 HP left
Monster attacks you for: 1
You have 49 HP left
====== ROUND 2 ======
Hero attacks for: 18
Monster has 12 HP left
Monster attacks you for: 7
You have 42 HP left
====== ROUND 3 ======
Hero attacks for: 0
Monster has 12 HP left
Monster attacks you for: 14
You have 28 HP left
====== ROUND 4 ======
Hero attacks for: 18
Monster has -6 HP left
The monster dies and you earn 5 XP
Battle ends...
Program 3:
Did you know that, over the last century, the stock market has returned an average of 10%? You may not care, but you’d better pay attention to this one. If you were to give a newborn baby $5000, put that money in the stock market and NOT add any additional money per year, that money would grow to over $2.9 million by the time that baby is ready for retirement (67 years)! Don’t believe us? Check out the compound interest calculator from MoneyChimp and plug in the numbers! To keep things simple, we’ll calculate interest in a simple way. You take the original amount (called the principle) and add back in a percentage rate of growth (called the interest rate) at the end of the year. For example, if we had $1,000 as our principle and had a 10% rate of growth, the next year we would have $1,100. The year after that, we would have $1,210 (or $1,100 plus 10% of $1,100). However, we usually add in additional money each year which, for simplicity, is included before calculating the interest. Your task is to design (pseudocode) and implement (source) for a program that 1) reads in the principle, additional annual money, years to grow, and interest rate from the user, and 2) print out how much money they have each year. Task 3: think about when you earn the most money! Lesson learned: whether it’s your code or your money, save early and save often…
Sample run 1:
Enter the principle: 2000
Enter the annual addition: 300
Enter the number of years to grow: 10
Enter the interest rate as a percentage: 10
Year 0: $2000
Year 1: $2530
Year 2: $3113
Year 3: $3754.3
Year 4: $4459.73
Year 5: $5235.7
Year 6: $6089.27
Year 7: $7028.2
Year 8: $8061.02
Year 9: $9197.12
Year 10: $10446.8
Program1:-
#include<stdio.h>
#include<iostream>
using namespace std;
int main(){
float xpos,ypos,xvel,yvel; //decalring float
varibles
printf("Enter the starting X position: ");
scanf("%f",&xpos); //taking values from user
printf("Enter the starting Y position: ");
scanf("%f",&ypos); //taking values from user
printf("Enter the starting X velocity: ");
scanf("%f",&xvel); //taking values from user
printf("Enter the starting Y velocity: ");
scanf("%f",&yvel); //taking values from user
if(xpos>=50){ //if xpos is greaterthan 49
while(xpos<100+xvel){ //loop
until xpos is exceeded 100
printf("\nX:%.1f
Y:%.1f",xpos,ypos); //printing xpos,ypos
xpos=xpos+xvel;
//adding xvel to xpos
ypos=ypos+yvel;
//adding yvel to ypos
}
}
else if(xpos<50){ //if xpos is less than
while(xpos>-xvel){ //loop until
xpos is less than 0
printf("\nX:%.1f
Y:%.1f",xpos,ypos); //printing xpos,ypos
xpos=xpos-xvel;
//deleting xvel from xpos
ypos=ypos+yvel;
//deleting yvel from ypos
}
}
}
Output:-
Program2:-
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<time.h> //including libraries
using namespace std;
int main(){
srand (time(NULL)); //srand to get different random
number each time
int herohp,heratt,monhp,monatt; //decalring
varaibles
cout<<"Enter the hero's starting hit points:
";
cin>>herohp; //taking input from user
cout<<"Enter the damage the hero’s weapon does
per strike: ";
cin>>heratt; //taking input from user
cout<<"Enter the monster's starting hit points:
";
cin>>monhp; //taking input from user
cout<<"Enter the damage the monster’s weapon
does per strike: ";
cin>>monatt; //taking input from user
int round=0;
while(1){ //loop until it breaks
cout<<"\n======
ROUND"<<round++<<" ======"<<endl;
int heroattack=rand()%heratt+1;
//generating random number in limit of heroatt
int monsterattack=rand()%monatt+1;
//generating random number in limit of monatt
monhp=monhp-heroattack; //deleting
heroattack from monhp
cout<<"Hero attacks for:
"<<heroattack<<endl;
cout<<"Monster has "<<
monhp<<" HP left"<<endl;
if(monhp<=0){ //if monster
health is less than 0
cout<<"The
monster dies and you earn 5 XP"<<endl<<"Battle
ends...";
break;
}
herohp=herohp-monsterattack;
cout<<"Monster attacks for:
"<<monsterattack<<endl;
cout<<"Hero has "<<
herohp<<" HP left"<<endl;
if(herohp<=0){ //if hero health
is less than 0
cout<<"The
Hero dies and Monster earn 5 XP"<<endl<<"Battle
ends...";
break;
}
}
}
Output:-
Program3:-
#include<stdio.h>
#include<iostream>//libraries
using namespace std;
int main(){
float prin,annuadd,years,interest;
cout<<"Enter the principle: ";
cin>>prin; //taking input from user
cout<<"Enter the annual addition: ";
cin>>annuadd; //taking input from user
cout<<"Enter the number of years to grow:
";
cin>>years; //taking input from user
cout<<"Enter the interest rate as a percentage:
";
cin>>interest; //taking input from user
int i;
for(i=0;i<=years;i++){
cout<<"Year
"<<i<<":";
cout<<prin<<endl;
prin=prin+annuadd;
prin=prin+((prin)/interest);
//calculating value
}
}
Output:-
#any doubts please comment
C:\Users\SATISH KOHLN Desktop\p1.exe Enter the starting X position: 50 Enter the starting Y position: 50 Enter the starting X velocity: 4.7 Enter the starting Y velocity: 2 X:50.0 Y:50.0 X:54.7 Y:52.0 X:59.4 Y:54.0 X:64.1 Y:56.0 X:68.8 Y:58.0 X:73.5 Y:60.0 X:78.2 Y:62.0 K:82.9 Y:64.0 K:87.6 Y:66.0 X:92.3 Y:68.0 X:97.0 Y:70.0 K: 101.7 Y:72.0 Process exited after 3.95 seconds with return value o Press any key to continue ...
Enter the hero's starting hit points: 50 Enter the damage the heroes weapon does per strike: 20 Enter the monster's starting hit points: 40 Enter the damage the monsters weapon does per strike: 15 ====== ROUNDO ====== Hero attacks for: 13 Monster has 27 HP left Monster attacks for: 4 Hero has 46 HP left ====== ROUND1 ====== Hero attacks for: 20 Monster has 7 HP left Monster attacks for: 10 Hero has 36 HP left ====== ROUND2 ====== Hero attacks for: 8 Monster has -1 HP left The monster dies and you earn 5 XP Battle ends... Process exited after 4.214 seconds with return value o Press any key to continue ...
Enter the principle: 2000 Enter the annual addition: 300 Enter the number of years to grow: 10 Enter the interest rate as a percentage: 10 Year @:2000 Year 1:2530 Year 2:3113 Year 3:3754.3 Year 4:4459.73 Year 5:5235.7 Year 6:6089.27 Year 7:7028.2 Year 8:8061.02 Year 9:9197.12 Year 10:10446.8 Process exited after 3.157 seconds with return value o Press any key to continue ...