I want to create a program that solve quadratic equation
(Ax²+Bx+C=0) using 2 void functions: one to insert the values of A,B,C, and the second for solving the equation. This is what I did:
#include <iostream>
#include <math.h>
using namespace std;
void add_nmbr(int a, int b, int c){
int *pa,*pb,*pc;
cout << "Entrer le nombre A " <<endl;
cin >> a;
cout << "Entrer le nombre B " <<endl;
cin >> b;
cout << "Entrer le nombre C " <<endl;
cin >> c;
pa = &a;
pb = &b;
pc = &c;
cout << a <<"x2 + "<<b<<"x + "<<"c = 0"<<endl;
}
void resoudre(int a,int b, int c){
double delta;
double x1,x2;
delta= b*b-4*a*c ;
if (delta<0){
cout << "Pas de solution !"<<endl;
}else{
x1=(-b-(sqrt(delta)))/(2*a);
x2=(-b+(sqrt(delta)))/(2*a);
}
cout << a <<"x2 + "<<b<<"x + "<<"c = 0"<<endl;
cout << "la solution est : " << x1 << endl;
cout << "la solution est : " << x2 << endl;
}
int main()
{
int a,b,c;
add_nmbr(a,b,c);
resoudre(a,b,c);
return 0;
}
When you declare a function like void add_nmbr(int a, int b, int c) you are passing parameters by value which means you pass a copy of value into the function. You can change the value inside add_nmbr for a but that value stays inside the function. In you case, the variable a in function main stays un-initialized.
The same thing for resoudre. To fix it, you can use reference, like this
void add_nmbr(int &a, int &b, int &c) {...}
Look at this;
void add_nmbr(int& a, int& b, int& c){
cout << "Entrer le nombre A " <<endl;
cin >> a;
cin.ignore(); //Use it after cin because of you hitting enter after getting the value.
cout << "Entrer le nombre B " <<endl;
cin >> b;
cin.ignore();
cout << "Entrer le nombre C " <<endl;
cin >> c;
cin.ignore();
cout << a <<"x2 + "<<b<<"x + "<<"c = 0"<<endl;
}
But yeah, you should try to read up on call by refence and call by value.
Why you dont use reference ?
Like that
void add_nmbr(int& a, int& b, int& c)
{
cout << "Entrer le nombre A " << endl;
cin >> a;
cout << "Entrer le nombre B " << endl;
cin >> b;
cout << "Entrer le nombre C " << endl;
cin >> c;
cout << a << "x2 + " << b << "x + "<<"c = 0"<< endl;
}
void resoudre(const int &a,const int &b, const int &c)
{
double delta;
double x1,x2;
delta= b*b-4*a*c ;
if (delta<0){
cout << "Pas de solution !"<< endl;
}else{
x1=(-b-(sqrt(delta)))/(2*a);
x2=(-b+(sqrt(delta)))/(2*a);
}
cout << a <<"x2 + "<< b << "x + "<< "c = 0"<< endl;
cout << "la solution est : " << x1 << endl;
cout << "la solution est : " << x2 << endl;
}
Do attention, you need a test in a because you divided by 0.
Related
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
#include "CourseGrade.h"
using namespace std;
CourseGrade* maximumGrade(CourseGrade* course0, CourseGrade* course1)
{
}
int main()
{
cout << "Course Grade App!" << endl;
cout << "--------------------------" << endl;
cout << endl;
//Prompting and creating CourseGrade objects and pointer values from inputs
int c1;
float g1;
cout << "Please enter the first course and its grade: ";
cin >> c1 >> g1;
CourseGrade Course0(c1, g1);
CourseGrade* ptrCourse0;
ptrCourse0 = &Course0;
int c2;
float g2;
cout << "Please enter the second course and its grade: ";
cin >> c2 >> g2;
CourseGrade Course1(c2, g2);
CourseGrade* ptrCourse1;
ptrCourse1 = &Course1;
int c3;
float g3;
cout << "Please enter the third course and its grade: ";
cin >> c3 >> g3;
CourseGrade Course2(c3, g3);
CourseGrade* ptrCourse2;
ptrCourse2 = &Course2;
cout << "-----------------------------------" << endl;
cout << "Course" << setw(10) << "Grade" << endl;
cout << "-----------------------------------" << endl;
cout << ptrCourse0->getCourse() << setw(10) << ptrCourse0->getGrade() << endl;
cout << ptrCourse1->getCourse() << setw(10) << ptrCourse1->getGrade() << endl;
cout << ptrCourse2->getCourse() << setw(10) << ptrCourse2->getGrade() << endl;
cout << "-----------------------------------" << endl;
cout << "The course with the maximum grade is: " << maximumGrade(ptrCourse0, ptrCourse1) << endl;
cout << "The average grade is: " << (ptrCourse0->getGrade() + ptrCourse1->getGrade() + ptrCourse2->getGrade()) / 3 << endl;
}
// End of main.cpp
void CourseGrade::setCourse(int c)
{
if (c >= 1000 && c <= 9999)
{
course = c;
}
}
void CourseGrade::setGrade(float g)
{
if (g >= 0.00 && g <= 100.00)
{
grade = g;
}
}
int CourseGrade::getCourse() const
{
return course;
}
float CourseGrade::getGrade() const
{
return grade;
}
CourseGrade::CourseGrade(int c, float g)
{
if (c >= 1000 && c <= 9999)
{
course = c;
}
else
{
course = 1000;
}
if (g >= 0.00 && g <= 100.00)
{
grade = g;
}
else
{
grade = 0.00;
}
}
Can you guys please help me out? I have three objects due to the prompt, but it only asks for two pointers? I am completely unaware of how to get the maximumGrade to show the course with the largest grade. I have tried using if statements to compare the grades of the two pointers showing the values of the grades. It HAS to use pointers to compare the course grades. Thank you guys!
I have three objects due to the prompt, but it only asks for two pointers?
The only way to make sense of two pointers passed to maximumGrade is to assume that these are the beginning and end of an array which contains the three objects.
CourseGrade courses[3] = { Course0, Course1, Course2 };
cout << "The course with the maximum grade is: "
<< maximumGrade(courses, courses+3)->getCourse() << endl;
The body of maximumGrade can then be e. g.
CourseGrade *c = NULL;
float g = 0; // current maximum
for (; course0 < course1; ++course0) if (g <= course0->getGrade())
g = (c = course0)->getGrade();
return c;
I'm currently writing a code that sums together the coefficient of two polynomials.
This all works fine as long as the grade of the first polynomial is smaller or equal to the second. "if (d1 <= d2)"
However, when "d1 > d2", the code seems to have some kind of issue I don't understand.
I'm sitting on this for a while now but don't seem to find the problem. Does anybody have any hints?
Thanks in advance.
#include <iostream>
#include <math.h>
using namespace std;
double * poli_sum (double * p1, int d1, double * p2, int d2, int & dr);
double * read_poly(int &n){
cout << "Grade of function: ";
cin >> n;
double * c=new double[n+1];
for(int i=0;i<=n;i++){
cout << "Coefficient of degree " << i << ": ";
cin >> c[i];
}
return c;
}
double * read_second_poly(int &m){
cout << "Grade of second function: ";
cin >> m;
double * g=new double[m+1];
for(int i=0;i<=m;i++){
cout << "Coefficient of degree " << i << ": ";
cin >> g[i];
}
return g;
}
double * poli_sum (double * p1, int d1, double * p2, int d2, int & dr){
dr = 0;
if (d1 <= d2){
cout << "You're in first, d1 = " << d1 << endl;
cout << "d2 = " << d2 << endl <<endl;
for (int i = 0; i <= d2; i++){
if (i >d1) p1[i] = 0; // setting array[i] to zero when it gets outside the defined grade
cout << (p1[i]+p2[i])<< endl;
cout << "pi1( "<< i << "): "<< p1[i] << endl;
cout << "pi2( "<< i << "): "<< p2[i] << endl << endl;
dr++;
}
}
else if (d1 > d2){
cout << "You're in second, d1 = " << d1 << endl;
cout << "d2 = " << d2 << endl <<endl;
for (int i = 0; i <= d1; i++){
if (i > d2) p2[i] = 0; // setting array[i] to zero when it gets outside the defined grade
cout << (p1[i]+p2[i])<< endl;
cout << "pi1( "<< i << "): "<< p1[i] << endl;
cout << "pi2( "<< i << "): "<< p2[i] << endl << endl;
dr++;
}
}
cout << "dr: "<< dr << endl;
return 0;
}
int main()
{
double *p1;
double *p2;
int g1, g2, g3;
p1=read_poly(g1);
p2 = read_second_poly(g2);
poli_sum(p1,g1, p2, g2, g3);
delete[] p1;
delete[] p2;
return 0;
}
Below is some code that I am trying to write for my class. I have the dot product worked out. Cross product is giving me some trouble. Having trouble understanding the error I get:
invalid conversion from 'int*' to 'int'
#include <iostream>
#include <string>
using namespace std;
int A[3];
int B[3];
int length = 3;
int cross[3];
int dotProduct (int A[], int B[]){
int product = 0;
for (int i = 0; i < length; i++)
{
product = product + A[i] * B[i];
}
return product;
}
int crossProduct(int A[], int B[], int cross[]){
cross[0]=A[1] * B[2]-A[2] * B[1];
cross[1]=A[2] * B[0]-A[0] * B[2];
cross[2]=A[0] * B[1]-A[1] * B[0];
return cross;
}
int main (){
cout << "Please enter the coordinates for vector A: " << endl;
cout << "X: ";
cin >> A[0];
cout << endl << "Y: ";
cin >> A[1];
cout << endl << "Z: ";
cin >> A[2];
cout << endl;
cout << "Please enter the coordinates for vector B: " << endl;
cout << "X: ";
cin >> B[0];
cout << endl << "Y: ";
cin >> B[1];
cout << endl << "Z: ";
cin >> B[2];
cout << endl;
cout << "Dot Product is: " << dotProduct (A, B) << endl;
crossProduct (A, B, cross);
cout << "Cross Product is: ";
for (int i=0;i<length;i++){
cout << crossProduct[i]<<" ";
}
return 0;
}
For the dot product, I would write:
int
dotProduct(const int A[], const int B[])
{
return std::inner_product(A, A + length, B, 0);
}
For the cross product, you should rather write:
...
crossProduct (A, B, cross);
cout << "Cross Product is: ";
for (int i=0;i<length;i++){
cout << cross[i]<<" ";
}
...
I am fairly new to programming and I wanted to see if this was an efficient way to write a program to solve for lowest number out of 3 using a function. I could not think of more efficient way with what I have learned thus far.
#include <iostream>
using namespace std;
float SmallestNum( float a, float b, float c)
{
if ((a <= c) && (a <= b))
{
float min;
min = a;
return min;
}
else if ((b <= c) && (b <= a))
{
float min;
min = b;
return min;
}
else if ((c <= b) && (c <= a))
{
float min;
min = c;
return min;
}
}
int main()
{
float a, b, c, z;
cout << "Please enter 3 seperate numbers : " << endl << endl;
cout << "1st: ";
cin >> a;
cout << "2nd: ";
cin >> b;
cout << "3rd: ";
cin >> c;
cout << endl << endl;
z = SmallestNum(a,b,c);
cout << z << " is your lowest number" << endl;
cout << endl;
system("pause");
return 0;
}
In my opinion the most readable way is to use standard algorithms. For example
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
int main()
{
float a, b, c, z;
cout << "Please enter 3 seperate numbers : " << endl << endl;
cout << "1st: ";
cin >> a;
cout << "2nd: ";
cin >> b;
cout << "3rd: ";
cin >> c;
cout << endl << endl;
z = std::min( { a, b, c } );
cout << z << " is your lowest number" << endl;
cout << endl;
system("pause");
return 0;
}
If you want to write the function yourself then the code can look like
#include <iostream>
#include <cstdlib>
using namespace std;
float SmallestNum( float a, float b, float c)
{
float min = a;
if ( b < min ) min = b;
if ( c < min ) min = c;
return min;
}
int main()
{
float a, b, c, z;
cout << "Please enter 3 seperate numbers : " << endl << endl;
cout << "1st: ";
cin >> a;
cout << "2nd: ";
cin >> b;
cout << "3rd: ";
cin >> c;
cout << endl << endl;
z = SmallestNum( a, b, c );
cout << z << " is your lowest number" << endl;
cout << endl;
system("pause");
return 0;
}
Hello I just started learning C++ and Im trying to make a calculator, right now having a fue problems that I simply dont know how to fix in C++.
#include <iostream>
using namespace std;
int main() {
int a, b, c, d;
cout << "1. Saskaitiissana(+)" << endl;
cout << "2. atnnemssana(-)" << endl;
cout << "3. daliissana(/)" << endl;
cout << "4. reizinaassana(*)" << endl;
cin >> d;
switch(d){
case 1 :
cout << "ievadiet a un b lai saskaitiitu(+)" << endl;
cin >> a;
cin >> b;
c = a + b;
cout << "The sum of number 1 and number 2 is " << c << "\n" <<endl;
break;
case 2 :
cout << "ievadiet a un b lai atnnemtu(-)" << endl;
cin >> a;
cin >> b;
c = a - b;
cout << c << endl;
break;
case 3 :
cout << "ievadiet a un b lai reizinaatu(*)" << endl;
cin >> a;
cin >> b;
c = a * b;
cout << c << endl;
break;
case 4 :
cout << "ievadiet a un b lai dal'itu(/)" << endl;
cin >> a;
cin >> b;
if(b==0)
{
cout<<"Nulle neder! start over."<<endl;
}
c = a/b;
cout << c << endl;
break;
}
return 0;
}
The things I still have to do.
Find the most easy way for the program to use numbers only. Also when I type in a number it can not be "empty space".
Also how can I make the case after it finish and gives you the result, go back to the begining of start menu? and if I want to exit a program I press esc or 5?
Also with the exit option I was thinking of useing do while "5" is pressed, can that work in c++?
Right now Im most interested on how to check program to use numbers only and have no empty space when adding numbers.
Thank you for your time :)
For ignoring non-numeric input you can this piece of code:
std::cin >> d;
while(std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::cout << "Bad entry. Enter a NUMBER: ";
std::cin >> d;
}
or in C-style:
while(scanf("%i",&d)!=1)
{
fseek(stdin,0,SEEK_END);
}
You can also put your whole bunch of code in a while statement to re-run the calculator after one operation.
Taking into account the safe input:
//----------------------------------------------------------------------------
#include <iostream>
using namespace std;
//----------------------------------------------------------------------------
void SafeDouble (double &d)
{
while (!(cin >> d))
{ cin.clear();
while (cin.get() != '\n');
cout << "\tIncorrect. Try again\n\t";
}
cin.sync();
}
//----------------------------------------------------------------------------
int main()
{
cout << "The simpliest calculator\n";
double a = 0.,b = 0.;
cout << "\na = ";
SafeDouble (a);
cout << "b = ";
SafeDouble (b);
cout << "\nEnter operation sign: +, -, * or /\n";
char op;
cin >> op;
cin.sync();
switch (op)
{
case '+': cout << a << " + " << b << " = " << a + b;
break;
case '-': cout << a << " - " << b << " = " << a - b;
break;
case '*': cout << a << " - " << b << " = " << a * b;
break;
case '/': if (b == 0.0)
cout << "Division by zero";
else
cout << a << " / " << b << " = " << a / b;
break;
default: cout << "Incorrect operation sign";
}
cin.get();
return 0;
}
//-----------------------------------------------------------------------------