WRITE AN INTERACTIVE PROGRAM that allows the user to enter (at least two)....UP-
ID: 3798711 • Letter: W
Question
WRITE AN INTERACTIVE PROGRAM
that allows the user to enter (at least two)....UP-TO 15 vectors that are to be added........display the resultant on the screen
study in your text the if statement
[use the if statement to make sure the user only enteres between two and 15 vectors] **Comments all the line
use <iostream>, <fstream>, <cmath>, <math.h> as a header file, do not use any other
one of my friend did it like below, i think all patterns are good here, but professor asked to use if statement to make sure the user only enteres between two and 15 vectors...so need to fix it...
/*
Dweep Dey Program 6
02/27/17
This program add up to fifteen vectors from a text file
*/
using namespace std;
#include <iostream>
#include <cmath>
#include <math.h>
#include <fstream>
//global variables
ifstream Infile;
float rdir, rlength, a, endtheta, magnitude, resdir, len, the,sum;
//functiony goodness
float calcvector(float length, float theta);
float length [15], theta [15];
void getdata();
int main()
{
float length, theta; //local variables
cout<<"I wrote this to show the addition of vectors in a text file...''"<<endl; // user message
getdata(); // calls getdata
calcvector(length, theta); // calls calcvector
system;"pause"; //so we can see the equation printed on the screen above
return 0; // ends program
}
void getdata()
{
int i, numberofdatas;
Infile.open("thedata.txt");
i = 0;
while(!Infile.eof()) //while not the end of the file
{
Infile>>length[i];
// cout<<length[i]<<" "<<endl;
Infile>>theta[i];
//cout<<theta[i]<<" "<<endl;
i++;
}
numberofdatas=i;
cout<<"number of data points "<<numberofdatas<<endl; // shows # of data points in file
Infile.close(); // closes file
system;"pause";
}
float calcvector(float len, float the) // starts calcvector
{
int i=0; // local variables
float valuex=0, valuey=0, valuexx=0, valueyy=0; // local variables
// len =length[i]; the = theta[i]; // sets arrays into usable format
for(i=0; i<=(a-1); i++); //separate x value loop
{
len =length[i]; the = theta[i]; // sets arrays into usable format
rdir=(3.14159/180.0)*the; // turns degress into radians
valuex=valuex+cos(rdir)*len; // calculates x value
cout<<"x="<<valuex<<endl; //shows user x value
valuexx = valuex+valuex; // sum of x values
}
cout<<"sum x="<<valuexx<<endl; // shows user sum of all x values
for(i=0; i<=(a-1); i++); // separate y value loop
{
len =length[i]; the = theta[i]; // sets arrays into usable format
valuey=sin(rdir)*len; // calculates y value
cout<<"y="<<valuey<<endl; // shows user y values
valueyy = valuey+valuey; // sum of y values
}
cout<<"sum y="<<valueyy<<endl; // shows user sum of all y values
magnitude=((valuexx*valuexx)+(valueyy*valueyy)); //adding sum of x and y squared
cout<<magnitude<<endl; // shows user value of x and y squared and added
rlength=sqrt(magnitude); // finding root of the sums
cout<<"magnitude is..."<<rlength<<endl; // displays magnitude
resdir=atan(valuey/valuex); // finds angle of new vector
endtheta=(180.0/3.14159)*resdir; // turns radians to degress
cout<<"direction is..."<<endtheta; // displays final angle for user
system;"pause";
}
***We cannot use void get data under int main(), however he did its good
Thank you.
Explanation / Answer
#include <iostream>
#include <random>
#include <string>
#include <ctime>
#include <sstream>
using namespace std;
void tossOutput(int faceA, int faceB,int limit);
int getLimit();
string getStatus(int facea,int faceb);
string getResult(int winsa,int winsb,int suma,int sumb);
default_random_engine e(time(nullptr)); // gives you difference sequences of random number.
//default_random_engine e; // gives you same sequences of random number.
uniform_int_distribution<int> u(1, 6);
string nameA = "Alla";
int winsA = 0;
int sumA = 0;
int faceA = 0;
int countA =0;
string nameB = "Bella";
int winsB = 0;
int sumB = 0;
int faceB = 0;
int main()
{
int limit= getLimit();
tossOutput(faceA, faceB,limit);
//who wins the game?
cout<<getResult(winsA,winsB,sumA,sumB);
system("pause");
return 0;
}
void tossOutput(int faceA, int faceB,int limit)
{
for( int n=1; n<=limit; n++)
{
faceA = u(e);
faceB = u(e);
sumA += faceA;
sumB += faceB;
cout << nameA << " tosses " << faceA << endl;
cout << nameB << " tosses " << faceB << endl;
cout<<getStatus(faceA,faceB);
}
}
int getLimit()
{
int limit;
cout<<"Enter the limt";
cin>>limit;
return limit;
}
string getStatus(int facea,int faceb)
{
if(facea > faceb)
{
return nameA+" Won the Game ";
}
else if(facea < faceb)
{
return nameB+" Won the Game ";
}
else
{
return "tie ";
}
}
string getResult(int winsa,int winsb,int suma,int sumb)
{
if(winsa > winsb)
{
cout << nameA << " won the game" << endl;
}
else if(winsb > winsa)
{
cout << nameB << " won the game" << endl;
}
else if(suma > sumb)
{
cout << nameA << " won the gmae" << endl;
}
else if( sumb > suma)
{
cout << nameB << " won the game" << endl;
}
else
{
cout << " gmae is tie" << endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.