How can we rectify nan while using trigonometric functions in c++ - c++

cout << "Please enter the A vector's magnitude \n";
cin >> A_v;
cout << "Please enter the B vector's magnitude \n";
cin >> B_v;
cout << "The general form to find the direction of the resultant vector = \n" << "tan^-1 X " << UL << " [B X sin] \n" << UL_
<< " [A + B X cos]\n";
cout << "Please enter the angle value \n";
cin >> ct;
cost = cos(ct);
sint = sin(ct);
itan = atan(ct);
al = itan*((B*sint)/(A+(B*cost)));
ald = al*(180/3.141562);
cout << "The direction of the vector is inclined at " << ald << " degrees \n";
This is a program to find the inclination/direction of a vector(in physics)

Related

Is there any way I can store multiple int's from a user input into a vector?

I am trying to store multiple int into a vector by having the user input the grade they have received. However I do believe there is a more simplified version of trying to do this. Would it be better to have separate functions? Is there anyone who can guide me in the right direction?
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main() {
cout << "Welcome to the Grade Calculator! \n"
<< "What is the student's full name? ";
string student_name;
getline(cin,student_name);
cout << "\nPlease input the points earned for each assignment.";
vector<int> student_grades(8);
cout << "\nLab 2: ";
cin >> student_grades[0];
cout << "Lab 3: ";
cin >> student_grades[1];
cout << "Lab 4: ";
cin >> student_grades[2];
cout << "Lab 5: ";
cin >> student_grades[3];
cout << "Lab 6: ";
cin >> student_grades[4];
cout << "Lab 7: ";
cin >> student_grades[5];
cout << "Lab 8: ";
cin >> student_grades[6];
cout << "Lab 9: ";
cin >> student_grades[7];
cout << "Lab 10: ";
cin >> student_grades[8];
//Finding sum of the grades
int sum_of_grades = accumulate(student_grades.begin(),student_grades.end(),0);
//Console Output
cout << '\n'<< student_name << " has earned " << sum_of_grades << " points in the course, so far." << endl;
cout << "\nThere are 2000 points possible in this course." << endl;
cout << "\nInput the anticpated score for the final project (200 points possible): ";
int anticipated_score;
cin >> anticipated_score;
int total_score = sum_of_grades+anticipated_score;
cout << "Total Projected Assignment Points: " << total_score << endl;
cout << "\nFinal Exam Score Needed for Final Course Grade (1000 Points Possible)"
<< "\nTo Earn an A, need at least: " << 1800 - total_score << " points, for a total of: 1800"
<< "\nTo Earn a B, need at least: " << 1600 - total_score << " points, for a total of: 1600"
<< "\nTo Earn a C, need at least: " << 1400 - total_score << " points, for a total of: 1400"
<< "\n To Earn a D, need at least: " << 1200 - total_score << " points, for a total of: 1200" << endl;
return 0;
}
You can use a for-loop:
for(int i = 0; i < 8; ++i){
cout << "\nLab " << i+2 << ": ";
cin >> student_grades[i];
}
This code is essentially equivalent to what you wrote in your program; it loops through all possible indices, prompting the user to input numbers into the vector.

I don't know how to write the average grade with variables

I'm trying to automatically set the number of the given variables, for example:
char subject1[30];
char subject2[30];
char subject3[30];
float grade1;
float grade2;
float grade3;
cout << "Type in your first subject: " ;
cin >> subject1;
cout << "Type in your second subject: ";
cin >> subject2;
cout << "Type in your third subject: ";
cin >> subject3;
cout << "Type in your grade for: " << subject1 << " :";
cin >> grade1;
cout << "Type in your grade for: " << subject2 << " :";
cin >> grade2;
cout << "Type in your grade for: " << subject3 << " :";
cin >> grade3;
float sum = grade1 + grade2 + grade3;
float average = (sum / 3);
cout << "AVERAGE GRADE";
cout << "************************************" << endl;
cout << subject1 << grade1 << endl;
cout << subject2 << grade2 << endl;
cout << subject3 << grade3 << endl;
cout << "====================================" << endl;
cout << "Average: " << average << endl;
return 0;
The code that calculates it works but I was wondering as how do I put the 3 grades that user inputed. So I don't have to go edit the calculation part every time I add another subject. I'm not sure if I explained well as to what I meant but I hope you understand.
A simple solution would be to store everything in a vector (that's preferred most of the time over the char array you used) an then just loop for the amount of subjects you have.
#include <vector> // need to inlcude this to be able to use vector
#include <iostream>
const int numSubjects = 3;
std::vector<std::string> prefix{"first", "second", "third"};
std::vector<std::string> subjects(numSubjects);
std::vector<float> grades(numSubjects);
for(int i = 0; i < numSubjects; i++) {
std::cout << "Type in your " << prefix[i] << " subject: ";
std::cin >> subjects[i];
std::cout << "Type in your grade for " << subjects[i] << ": ";
std::cin >> grades[i];
}
//afterwards do the calculations
Note that I initialized the vectors with a size of numSubjects that way you can access and write to indices of the vector with the [] operator. If you don't initialize vector with a size then you can use push_back() to insert elements.

Math results in zero. New to coding

I'm trying to complete an assignment but I'm having difficulty with the math expressions and variables in general. I'm trying to make a program that takes user info on groceries and then outputs a receipt. Here is my code.
#include <iostream>
#include <string>
using namespace std;
int main()
{
//user input
string firstItem, secondItem;
float firstPrice, secondPrice;
int firstCount, secondCount;
double salesTax = 0.08675;
double firstExt = firstPrice * firstCount;
double secondExt = secondPrice * secondCount;
double subTotal = firstExt + secondExt;
double tax = subTotal * salesTax;
double total = tax + subTotal;
//user input
cout << "What is the first item you are buying?" << endl;
getline(cin, firstItem);
cout << "What is the price of the " << firstItem << "?" << endl;
cin >> firstPrice;
cout << "How many " << firstItem << "s?" <<endl;
cin >> firstCount;
cin.ignore();
cout << "What is the second item you are buying?" << endl;
getline(cin, secondItem);
cout << "what is the price of the " << secondItem << "?" << endl;
cin >> secondPrice;
cout << "How many " << secondItem << "s?" << endl;
cin >> secondCount;
// receipt output
cout << "1st extended price: " << firstExt << endl;
cout << "2nd extended price: " << secondExt << endl;
cout << "subtotal: " << subTotal << endl;
cout << "tax: " << tax << endl;
cout << "total: " << total << endl;
return 0;
}
The program output either 0 for all or negatives.
Your calculations must go after you read in the values, not before. You're making your calculations based on uninitialized variables.
A declaration and initialisation like
double firstExt = firstPrice * firstCount;
initialises firstExt to be the product of the current values AT THAT POINT of firstPrice and firstCount.
It doesn't set up some magic so that the value of firstExt is recalculated whenever the values of firstPrice or firstCount are changed.
In your case, firstPrice and firstCount are uninitialised variables when you do this. Accessing values of uninitialised variables of type int gives undefined behaviour.
What you need to do is something like
cout << "What is the price of the " << firstItem << "?" << endl;
cin >> firstPrice;
cout << "How many " << firstItem << "s?" <<endl;
cin >> firstCount;
firstExt = firstPrice*firstCount; // do the calculation here
If the value of firstExt is not needed until this point, you can declare it here instead;
double firstExt = firstPrice*firstCount; // do the calculation here
which means any earlier use of firstExt will give a compiler diagnostic.

inf output computing line slopes

I am very new at C++.
I wrote this code below which is supposed to tell me if 2 lines have an intersection point, so I figured two lines with equal "M" in the y=Mx+B equation should not intersect and all others would.
The program seems to be understanding this, but unless the slope of the inputted line segment is 0 it outputs inf or -inf.
why is this happening?
#include <iostream>
using namespace std;
int main ()
{
typedef double vector2d[2];
vector2d pointA, pointB, pointC, pointD;
double LineSeg1, LineSeg2;
double yes, no;
cout << "Enter x for point A: ";
cin >> pointA[0];
cout << "Enter y for point A: ";
cin >> pointA[1];
cout << "Point A = (" << pointA[0] << "," << pointA[1] << ")" << endl;
cout << "Enter x for point B: ";
cin >> pointB[0];
cout << "Enter y for point B: ";
cin >> pointB[1];
cout << "Point B = (" << pointB[0] << "," << pointB[1] << ")" << endl;
cout << "Enter x for point C: ";
cin >> pointC[0];
cout << "Enter y for point C: ";
cin >> pointC[1];
cout << "Point C = (" << pointC[0] << "," << pointC[1] << ")" << endl;
cout << "Enter x for point D: ";
cin >> pointD[0];
cout << "Enter y for point D: ";
cin >> pointD[1];
cout << "Point D = (" << pointD[0] << "," << pointD[1] << ")" << endl;
LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointB[0]));
cout << "slope segment 1 = (" << LineSeg1 << endl;
LineSeg2 = ((pointD[1]-pointC[1])/(pointD[0]-pointC[0]));
cout << "slope segment 2 = (" << LineSeg2 << endl;
if ( LineSeg1 == LineSeg2 ) {
cout << "no\n";
}
else ( LineSeg1 != LineSeg2 ) ;{
cout << "yes\n";
}
return 0;
}
This line:
LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointB[0]));
has a divide by zero error.
I believe the equation should be:
LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointA[0]));

addition operator has no affect

I am trying to add two floats in a for loop and its telling me '+' has no affect. I am attempting to make it parse through each incrememnt (.25) of the two ranges (begrate and endrate) (1 and 2) and 1+.25 is not working correctly and I get an infinite loop
float begrate,endrate,inc,year=0;
cout << "Monthly Payment Factors used in Compute Monthly Payments!" << endl;
cout << "Enter Interest Rate Range and Increment" << endl;
cout << "Enter the Beginning of the Interest Range: ";
cin >> begrate;
cout << "Enter the Ending of the Interest Range: ";
cin >> endrate;
cout << "Enter the Increment of the Interest Range: ";
cin >> inc;
cout << "Enter the Year Range in Years: ";
cin >> year;
cout << endl;
for (float i=1;i<year;i++){
cout << "Year: " << " ";
for(begrate;begrate<endrate;begrate+inc){
cout << "Test " << begrate << endl;
}
}
system("pause");
return 0;
That's because begrate+inc has no effect on the value of begrate. The + operator is not like the ++ operator. You must assign the results to something to have an effect. What you wanted is this:
begrate = begrate + inc
Or
begrate += inc
You could use += instead of +, as this will set begrate to begrate+inc. The better solution would be to have a temporary loop variable that starts equal to begrate then increment it.
for (float i=1;i<year;i++){
cout << "Year: " << " ";
for(float j = begrate;j<endrate;j+=inc){
cout << "Test " << j << endl;
}
}
Just replace the following line
for(begrate;begrate<endrate;begrate+inc){
with
for(begrate;begrate<endrate;begrate+=inc){
notice the begrate*+=*inc here