I'm sure there's a very very simple answer to this, but I've looked for hours and can't figure it out. As the Title says I get an error which I commented in the code. Everything seems to be in the correct syntax, so I have no idea what it could be.
#include <iostream>
#include <math.h>
using namespace std;
float x, y, k, q, base, exponent;
/*float atan(q)
{
//I had made my own functions for all the different math functions that
would have normally been included in the math.h file, but I had no idea
how to tackle making a function for an arc tangent as all the information
I could find online just had you kinda guess what the end result might be,
so I used the math.h file for this and only this function.
}*/
float sqrot(k)
{ // <----- the error is here
float guess, divide, average, z;
guess=rand()%k;
for(z;z<500;z++)
{
divide=k/guess;
average=(guess+divide)/2;
guess=average;
}
}
float cosine(y)
{
y=1-(((y*y)/2)+((y*y*y*y)/24)-((y*y*y*y*y*y)/720));
return y;
}
float sine(x)
{
x=x-(((x*x*x)/6)+((x*x*x*x*x)/120)-((x*x*x*x*x*x*x)/5040));
return x;
}
float power(base, exponent)
{
while(exponent>1)
{
base=base*base;
exponent-1;
}
return base;
}
float haversine(lat1,long1,lat2,long2)
{
float degree_to_rad, pi=3.14159;
int d_lat, d_long, a, c, mi;
degree_to_rad=pi/180;
d_lat=(lat2-lat1)*degree_to_rad;
d_long=(long2-long1)*degree_to_rad;
a=power(sine(d_lat/2),2)+cosine(lat*degree_to_rad)*cosine(lat2*degree_to_rad)*power(sine(d_long/2),2);
c=2*atan((sqrot(a))/(sqrot(1-a)));
mi=3956*c;
return mi;
}
int main()
{
int answer;
cout<<"Enter the Latitude of your starting location: ";
cin>>lat1;
cout<<"Enter the Longitude of your starting location: ";
cin>>long1;
cout<<"Enter the Latitude of your ending location: ";
cin>>lat2;
cout<<"Enter the Longitude of your ending location: ";
cin>>long2;
answer=haversine(lat1, long1, lat2, long2);
cout<<"The distance between the two points is: "<< answer;
return 0;
}
Formal arguments must have a type
float sqrot(k)
should have been
float sqrot(type k)
// ^^^^
and similarly with all other functions
Related
I have this simple C++ program where I have to input the marks of 5 subjects and calculate it's percentage. But I have no idea why in the output it shows "0" and if I remove those return 0 statements under input() and percentage() functions then it shows "nan" in the output.
Here is the code:
#include<iostream>
using namespace std;
class Marks
{
float s1,s2,s3,s4,s5;
float p;
public:
float input();
float percentage();
};
float Marks::input()
{
cout<<endl<<"Enter the marks of your 5 subjects."<<endl;
cin>>s1>>s2>>s3>>s4>>s5;
cout<<"Subject 1="<<s1<<endl<<"Subject 2="<<s2<<endl<<"Subject 3="<<s3<<endl<<"Subject 4="<<s4<<endl<<"Subject 5="<<s5<<endl;
//return 0;
}
float Marks::percentage()
{
p=s1+s2+s3+s4+s5;
p=p/5;
cout<<p<<endl;
//return 0;
}
int main()
{
Marks student1;
cout<<"Marks of student 1 are as follow:"<<endl<<student1.input()<<endl;
cout<<"Student 1: Percentage = "<<student1.percentage()<<endl;
return 0;
}
And this is the output:
Your functions are promising to return a float, but they don't. That is undefined behaviour. And once your program contains UB anywhere, it becomes meaningless in its entirety.
#include<iostream>
#include<conio.h>
using namespace std;
//function declaration:
double circleArea(double);
int main(){
double rad1;
double rad2;
double ringArea;
cout<<"plz enter number";
cin>>rad1;
cout<<"plz enter number";
cin>>rad2;
ringArea = circleArea(rad1)-circleArea(rad2);
cout<<ringArea<<endl;
system("pause");
}
// function definition:
double circleArea(double rah){
return(3.1415926*rah*rah);
}
In above code if I enter 5 then it return value 2.66454e-015 but if I make changes in function definition like
double circleArea(double rah){
int confuse;
confuse =(3.1415926*rah*rah);
return confuse;
}
and enter value of 5,5 or 6,6 or 7,7 then it return value of 0.but when I enter 5,6 or 6,7 other than same values then it return some other value which is not matching with part in which I have not declared variable confuse.
Your variable declaration is wrong
double circleArea(double rah){
double confuse;
confuse =(3.1415926*rah*rah);
return confuse;
}
try that
double circleArea(double rah) {
double confuse;
confuse = (3.1415926*rah*rah);
return confuse;
}
This is the task i was given
Your task is to create a class named equation which will have the data members a, b and c which are the coefficients
of the quadratic equation. The class will have two more data members namely proot and nroot which stand for the
positive root and negative root of the equation. Suppose that variables a, b and c are integers. Where proot and nroot
are floats.
- Construct the class objects by using a nullary constructor.
- Then design a friend function which will determine the proot and nroot of the equation.
- Create another friend function which will display the values of proot and nroot.
I have a couple of questions
I tried to declare "a" as an integer and take its square root it was giving an error saying that "More than one instances of sqrt match the argument list". Same thing worked when i declared "a" as double and type-casted into integer. Why is is so?
the output should be
-1
-1.5
but my output is entirely different. What am i doing Wrong?
My professor told me that to make a function friend of a class we have to write its prototype in the class. Prototype does not include "&" but if i dont write it the program does not work
enter code here
#include <iostream>
#include <math.h>
using namespace std;
class Equation
{
friend void Roots (Equation & );
friend void Display (Equation &);
int a;
int b;
int c;
float proot;
float nroot;
public:
Equation ()
{
a=0;
b=0;
c=0;
proot=0;
nroot=0;
}
Equation (int _a, int _b, int _c)
{
a=_a;
b=_b;
c=_c;
}
};
void Roots (Equation &obj1)
{
double a;
int determinant;
a=(obj1.b^2)-(4*obj1.a * obj1.c);
if (a>-1)
determinant=int(sqrt(a));
else
{
cout<<"Determinant returns an imaginary number; solution not possible\n";
exit (0);
}
obj1.proot= (-obj1.b + determinant)/2*obj1.a;
obj1.nroot= (-obj1.b - determinant)/2*obj1.a;
}
void Display (Equation &obj1)
{
cout<<"Value of positive root : "<<obj1.proot<<endl;
cout<<"Value of negative root : "<<obj1.nroot<<endl;
}
void main ()
{
int a,b,c;
cout<<"Calculate Quadratic Equation"<<endl<<"Enter A : ";
cin>>a;
cout<<"Enter B : ";
cin>>b;
cout<<"Enter C ";
cin>>c;
Equation obj(a,b,c);
Display (obj);
Display (obj);
}
a=(obj1.b^2)-(4*obj1.a * obj1.c);
The ^ operator in C++ is a bitwise XOR, so obj1.b^2 part calculates the XOR of obj1.b and the bit pattern 000...10. That is definitely not what you want here.
The power function in C++ is pow, so you square that by doing pow(obj1.b, 2), also if you're working with C++ it would be better to include the header as cmath and not math.h.
EDIT: You also never call Roots() to calculate anything:
Equation obj(a,b,c);
Display (obj);
Here you construct your equation and immediately try to show its result, without first calling Roots(obj). That will at least calculate an answer but is still wrong because there seems to be a mistake in your calculation.
You also need parenthesis around 2 * obj1.a in your calculation. Try with and without them and see the difference!
Calculate Quadratic Equation
Enter A : 10
Enter B : 10
Enter C 2
Value of positive root : -0.276393
Value of negative root : -0.723607
And this is correct. Although you apparently expect the two roots to have different signs, that is not necessarily going to be the case.
Instead of
obj1.b^2
try
pow(obj1.b, 2)
^ is doing a XOR operation - probably not what you had in mind.
You also never seem to call your Roots function. You need something like:
Equation obj(a,b,c);
Roots(obj);
Display (obj);
You are getting big numbers returned, because when you call the Equation (int _a, int _b, int _c) constructor, proot and nroot are left uninitialized. You are then returning them at the end, because you never call the Roots function.
As an example of how to create useful friend functions:
#include <iostream>
#include <cmath>
class Equation
{
public:
Equation () : m_a(0), m_b(0), m_c(0), m_proot(0), m_nroot(0)
{
}
Equation (int a, int b, int c) : m_a(a), m_b(b), m_c(c), m_proot(0, false), m_nroot(0, false)
{
}
private:
// these would be better as floats or doubles, but your requirement appears to want them to be ints. You'll need to cast them when doing division operations.
int m_a;
int m_b;
int m_c;
// std::optional would be more useful, but it was removed from the last C++14 draft
std::pair<float, bool> m_proot;
std::pair<float, bool> m_nroot;
void Calculate()
{
// do your actual calculations here
// note that you will need to set the m_proot.second and m_nroot.second values to true if they are valid
// also note that ^ is not a power operation; you need to use std::pow for that
}
// this friend function is useful
friend std::ostream& operator<<(std::ostream&, const Equation&);
// this one is created just to meet the requirements of the assignment
friend void CalculateRoots(Equation&);
};
std::ostream& operator<<(std::ostream& os, const Equation& e)
{
std::cout << "Roots of (" << e.m_a << ")x^2 + (" << e.m_b << ")x + " << e.m_c << ": "
if (m_nroot.second || m_proot.second)
{
std::cout << "(";
if (m_nroot.second)
{
std::cout << m_nroot.first << ", ";
}
if (m_proot.second)
{
std::cout << m_proot.first;
}
std::cout << ")" << std::endl;
}
else
{
std::cout << "No real roots" << std::endl;
}
return os;
}
// must be friend function to call private member function Calculate
void CalculateRoots(Equation &obj1)
{
obj1.Calculate();
}
// does not need to be a friend function - using operator<< overload (which is a friend function itself)
void DisplayRoots(Equation &obj1)
{
std::cout << obj1;
}
int main ()
{
int a,b,c;
cout<<"Calculate Quadratic Equation"<<endl<<"Enter A : ";
cin>>a;
cout<<"Enter B : ";
cin>>b;
cout<<"Enter C ";
cin>>c;
Equation obj(a,b,c);
CalculateRoots(obj);
DisplayRoots(obj);
}
This gives you the required friend functions from the assignment's description while at least pushing you closer to a better design.
Ok guys so i changed the data type of variables from
int a;
int b;
int c;
to
float a;
float b;
float c;
because it was giving warning about conversions and data loss and also changed the statement where proot and nroot are calculated from
obj1.proot= (-obj1.b + determinant)/2*obj1.a;
obj1.nroot= (-obj1.b - determinant)/2*obj1.a;
to
obj1.proot= (-obj1.b + determinant)/(2*obj1.a);
obj1.nroot= (-obj1.b - determinant)/(2*obj1.a);
Now it is working as expected...Producing correct reusult! :)
I have wrote a simple average calculation program trying to calculate a semester average. When I compile the code I get an error telling me my 'inputExam' function was not declared in this scope. I've researched the error message and I can't figure out what to do to fix it.
I also get this error for the other functions, but once I understand my error I think I can fix the others.
#include <iostream>
using namespace std;
int main()
{
double finalExam=0.0;
double midterm = 0.0;
double quizzes = 0.0;
double labs = 0.0;
double semGrade=0.0;
midterm=inputExam("Midterm");
finalExam=inputExam("Final");
quizzes=inputAndAvgQuizzes();
labs=inputAndAvgLabs();
semGrade=(midterm*.2)+(finalExam*.2)+(labs*.5)+(quizzes*.1);
cout<<"Your End of Semester Grade is: " semGrade;
return 0;
}
double inputExam(string examType)
{
double grade;
cout<< "Enter the " examType " Score: ";
cin>>grade;
return (grade);
}
double inputAndAvgLabs()
{
double num [4];
double sum;
double avg;
if (int a=0, a<3,a++)
{
cout<<"What is the grade?"<<endl;
cin>>num[a]>>endl;
}
if (int a=0, a<3, a++)
{
sum=sum+num[a];
}
avg=sum/4;
return avg;
}
double inputAndAvgQuizzes()
{
double num[3];
double sum;
double avg;
double lowest = num[0];
if (int a=0, a<2,a++)
{
cout<<"What is the grade?"<<endl;
cin>>num[a]>>endl;
}
if (lowest>num[1])
{
lowest=num[1];
}
if (lowest>num[2])
{
lowest=num[2];
}
sum=num[1]+num[2]+num[3]-lowest;
avg=sum/2;
return avg;
}
You need to let it be known that those functions exist so add prototypes for your functions above main or define your functions there. Like so:
...
double inputExam(string examType);
double inputAndAvgLabs();
double inputAndAvgQuizzes();
int main() { ... }
//definitions after main
..or copy paste all those definitions above the call to main like so:
...
// Function defs here
// Prototypes no longer needed
...
int main() {...}
// Defs no longer needed here
Alternatively you can put all the definitions in an external file and compile it into the project via a make file or better yet, and as you progress, create classes in header and implementation files and include them in your file the same way(sort of) that you do #include <iostream>.
Another small nugget of advice would be to avoid using namespace std;. If not only in theory it's bad practice and can lead to namespace clashing in larger projects. If you, like me, hate typing std::string ... then add using std::string; to your code for the same ease of use.
In C/C++ you need to declare the function before you use it. In this case, it simply means declaring function prototypes before your main function and then implementing them after the main function.
Example:
// declare a prototype
double Function(int variable);
int main()
{
Function(5);
return 0;
}
// Implement the function
double Function(int variable)
{
/* Do Something */
}
Alternatively, you could change your code to the form:
// Implement the function first
double Function(int variable)
{
/* Do Something */
}
int main()
{
Function("Testing");
return 0;
}
I'm having an issue with passing parameters for this simple program. I am getting a "cannot convert 'float*' to 'float' argument '2' to 'void getData(std::string, float, float, float, float)'" error when trying to compile this program. Can anyone figure out what I'm doing wrong? I've been trying forever. Note: Please ignore deprecated stuff like system("PAUSE") and a few other things. This is simple the way my teacher has thought me to code and this is what he wants me using for this program. I am aware of getchar() and I use it for practice and final work. Plus this shouldn't affect the program as I have been using it without issues before on small programs for my C++ class.
Here's the code:
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
void getData(string,float,float,float,float);
void getCalc(int,float,float,float,float,float,float,float,float,float,float);
void getPrint(float,float,float);
int main()
{
int const acres=1000;
string crop;
float cpa[4];
float yield[4];
float per[4];
float increase[4];
float cost[4];
float grossmin[4];
float grossmax[4];
float netmin[4];
float netmax[4];
float netave[4];
getData(crop,cpa,yield,per,increase);
getCalc(acres,cpa,yield,per,increase,cost,grossmin,grossmax,netmin,netmax,netave);
getPrint(netmin,netmax,netave);
system("PAUSE");
return 0;
}
void getData(string fcrop,float fcpa[],float fyield[],float fper[],float fincrease[])
{
for (int i=0;i<4;i++)
{
cout<<"Enter the crop: ";
getline(cin,fcrop);
cout<<"Enter the cost per acre:$ ";
cin>>fcpa[i];
cout<<"Enter the yield: ";
cin>>fyield[i];
cout<<"Enter $/bishell: ";
cin>>fper[i];
cout<<"Enter the percentage increase: ";
cin>>fincrease[i];
cin.ignore(80,'\n');
}
}
void getCalc(int acres,float fcpa[],float fyield[],float fper[],float fincrease[],float fcost[],float fgrossmin[],float fgrossmax[],float fnetmin[],float fnetmax[],float fnetave[])
{
for (int i=0;i<4;i++)
{
int acres=1000;
fcost[i]=acres*fcpa[i];
fgrossmin[i]=acres*fyield[i]*fper[i];
fgrossmax[i]=fgrossmin[i]+(fgrossmin[i]*fincrease[i]/100);
fnetmin[i]=fgrossmin[i]-fcost[i];
fnetmax[i]=fgrossmax[i]-fcost[i];
fnetave[i]=(fnetmin[i]+fnetmax[i])/2;
}
}
void getPrint(float fnetmin[],float fnetmax[],float fnetave[])
{
for (int i=0;i<4;i++)
{
cout<<"The minumum profit is:$ "<<fnetmin[i]<<endl;
cout<<"The maximum profit is:$ "<<fnetmax[i]<<endl;
cout<<"The average profit is:$ "<<fnetave[i]<<endl;
}
}
In the prototype you have written at the start of the program, this is written.
void getData(string,float,float,float,float) ;
It should be this instead identical to the one in its definition.
void getData(string,float[],float[],float[],float[]);
The function prototype should be same in its declaration and implementation.