I've looked at some of the other questions like this, but it seems like the functions should be all set up properly. Everything compiles correctly when run in CodeLite, but then compiling on a Linux server using g++, I get an undefined reference to function add_fraction, subtract_fraction, multiply_fraction, divide_fraction error in 'main.cpp' How can I fix this? Thanks!
main.cpp
#include <iostream>
#include <string>
#include "header.h"
using std::cout;
using std::cin;
using std::endl;
int main()
{
int numerator;
int denominator;
int numerator2;
int denominator2;
char operation;
cout << "Input the numerator: ";
cin >> numerator;
cout << "Input the denominator: ";
cin >> denominator;
cout << "Input the numerator2: ";
cin >> numerator2;
cout << "Input the denominator: ";
cin >> denominator2;
cout << "Input the operation: ";
cin >> operation;
if (operation != '+' || '-' || '*' || '/'){
cout << "Please input a valid operator: ";
cin >> operation;
}
if (operation == '+'){
Rational addFrac;
addFrac.add_fraction(numerator, numerator2, denominator, denominator2);
}
if (operation == '-'){
Rational subFrac;
subFrac.subtract_fraction(numerator, numerator2, denominator, denominator2);
}
if (operation == '*'){
Rational multFrac;
multFrac.multiply_fraction(numerator, numerator2, denominator, denominator2);
}
if (operation == '/'){
Rational divideFrac;
divideFrac.divide_fraction(numerator, numerator2, denominator, denominator2);
}
return 0;
}
header.h
#ifndef HEADER_H
#define HEADER_H
class Rational{
public:
void reduce_fraction(int &top, int &bottom);
void add_fraction(int numerator, int denominator, int numerator2, int denominator2);
void subtract_fraction(int numerator, int denominator, int numerator2, int denominator2);
void multiply_fraction(int numerator, int denominator, int numerator2, int denominator2);
void divide_fraction(int numerator, int denominator, int numerator2, int denominator2);
};
#endif // HEADER_H
header.cpp
#include "header.h"
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
void Rational::reduce_fraction (int & top, int & bottom)
{
for (int i = top * bottom; i > 1; i--) {
if ((top % i == 0) && (bottom % i == 0)) {
bottom /= i;
top /= i;
}
}
}
void Rational::add_fraction (int numerator, int numerator2, int denominator, int denominator2)
{
int top;
int bottom;
top = numerator2 * denominator + denominator2 * numerator;
bottom = denominator2 * denominator;
cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified (Rational) Fraction -> ";
if (top == bottom){
cout << "1" << endl;
}
else {
Rational redu;
redu.reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;
}
}
void Rational::subtract_fraction (int numerator, int numerator2, int denominator, int denominator2)
{
int top;
int bottom;
top = denominator2 * numerator - denominator * numerator2;
bottom = denominator2 * denominator;
cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified (Rational) Fraction -> ";
if (top == bottom){
cout << "1" << endl;
}
else {
Rational redu;
redu.reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;
}
}
void Rational::multiply_fraction (int numerator, int numerator2, int denominator, int denominator2)
{
int top;
int bottom;
top = numerator * numerator2;
bottom = denominator * denominator2;
cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified (Rational) Fraction -> ";
if (top == bottom){
cout << "1" << endl;
}
else {
Rational redu;
redu.reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;
}
}
void Rational::divide_fraction (int numerator, int numerator2, int denominator, int denominator2)
{
int top;
int bottom;
top = denominator2 * numerator;
bottom = numerator2 * denominator;
cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified (Rational) Fraction -> ";
if (top == bottom){
cout << "1" << endl;
}
else {
Rational redu;
redu.reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;
}
}
Makefile
CXX = g++
CXXFLAGS = -g
default: main
search: main.cpp
${CXX} ${CXXFLAGS} main.cpp -o search
clean:
rm -f *.o main
You are not building header.cpp at all. It's not mentioned in your Makefile. You need to compile it and link it into your executable. Try this:
search: main.cpp header.cpp
${CXX} ${CXXFLAGS} main.cpp header.cpp -o search
Related
The program I'm writing is supposed to read the following values from a text file, then using a Fraction Class:
Store all elements individually
Return each value individually
Modify each element
Return a Fraction Object in its simplest form
Store each fraction in a vector
Output each fraction in Initial, Simplest, Mixed, Real forms to console
Perform operations on the fractions
FractionList.txt
10 4
20 3
-21 7
-99 22
32 4
-34 10
4 6
5 3
I believe I've done it using the code below, though there seems to be some issues when attempting to simplify the fractions that have had calculations performed on them where the simplified value isn't being returned, and there is still some aspects that are quite tedious and should be able to be replaced with loops.
I'd like to replace the following with the function DisplayInfo, though haven't been able to do so successfully as the simplify function doesn't seem to function when passed through to it.
List[i].DisplayInitial();
ReduceFraction(List[i]).DisplayReduced();
ReduceFraction(List[i]).DisplayMixed();
List[i].DisplayReal();
cout << endl;
}
There should also be a more straightforward way using a loop to read the specific values from the vector and write them to individual Fraction objects under the ResultingFraction function.
Would it also be possible to merge the FractionList class into Fraction?
Any help with the code is much appreciated. I've tried many solutions but they don't seem to work as expected. I'm also unsure as to whether this would be the most efficient data structure to use.
main.cpp
#include <vector>
#include <iostream>
#include <fstream>
#include "Fraction.h"
using namespace std;
int main()
{
FractionList MyFraction;
Fraction Frac;
unsigned int NbFraction;
NbFraction = MyFraction.GetListFromFile("FractionList.txt");
cout << NbFraction << " Fractions Loaded." << endl;
MyFraction.DisplayList();
system("pause");
return 0;
}
Fraction.h
#ifndef Fraction_H
#define Fraction_H
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
class Fraction {
int num;
int den;
public:
Fraction();
// Accessors
int GetNum() const { return num; };
int GetDen() const { return den; };
// Mutator
void SetNum(int n) { num = n; };
void SetDen(int d) { den = d; };
Fraction ReduceFraction(Fraction f);
// Display Member Functions
void DisplayInitial();
void DisplayReduced();
void DisplayMixed();
void DisplayReal();
void DisplayInfo();
// Overloaded Member Operators
Fraction operator+ (const Fraction& f) const {
Fraction r;
r.num = (num * f.den) + (f.num * den);
r.den = (den * f.den);
return r;
}
Fraction operator- (const Fraction& f) const {
Fraction r;
r.num = (num * f.den) - (f.num * den);
r.den = (den * f.den);
return r;
}
Fraction operator* (const Fraction& f) const {
Fraction r;
r.num = num * f.num;
r.den = den * f.den;
return r;
}
Fraction operator/ (const Fraction& f) const {
Fraction r;
if (!f.num) return *this;
r.num = num * f.den;
r.den = den * f.num;
return r;
}
Fraction operator- () {
Fraction r;
r.num = -num;
r.den = den;
return r;
}
Fraction& operator+= (const Fraction& f) {
num = num * f.den + f.num * den;
den *= f.den;
return *this;
}
};
class FractionList {
vector<Fraction> List;
public:
void DisplayList();
void SumAll();
void ResultingFraction();
unsigned int GetListFromFile(string FileName);
};
#endif#pragma once
Fraction.cpp
#include "Fraction.h"
using namespace std;
Fraction::Fraction() {
num = 0;
den = 1;
}
Fraction ReduceFraction(Fraction f) {
int num = f.GetNum();
int den = f.GetDen();
Fraction Reduced;
for (int i = den * abs(num); i > 1; i--) {
if ((num % i == 0) && (den % i == 0)) {
num /= i;
den /= i;
}
}
Reduced.SetNum(num);
Reduced.SetDen(den);
return Reduced;
}
void Fraction::DisplayInitial() {
cout << "Initial: " << num << "/" << den << endl;
}
void Fraction::DisplayReduced() {
if (den == 1) {
cout << "Reduced: " << num << endl;
}
else {
cout << "Reduced: " << num << "/" << den << endl;
}
}
void Fraction::DisplayMixed() {
int rem, remnum;
rem = num % den;
remnum = (num - rem) / den;
if (remnum == 0) {
cout << "Mixed: " << num << "/" << den << endl;
}
else if (rem < 0 || rem > 0) {
cout << "Mixed: " << remnum << " " << abs(rem) << "/" << den << endl;
}
else if (rem == 0) {
cout << "Mixed: " << remnum << endl;
}
}
void Fraction::DisplayReal() {
double realfraction = double(num) / double(den);
cout << "Real: " << realfraction << endl;
}
void Fraction::DisplayInfo() {
DisplayInitial();
DisplayReduced();
DisplayMixed();
DisplayReal();
}
void FractionList::DisplayList() {
cout << List.size() << " Fractions: " << endl << endl;
for (unsigned int i = 0; i < List.size(); i++) {
List[i].DisplayInitial();
ReduceFraction(List[i]).DisplayReduced();
ReduceFraction(List[i]).DisplayMixed();
List[i].DisplayReal();
cout << endl;
}
SumAll();
ResultingFraction();
}
void FractionList::SumAll() {
Fraction SumAll;
for (unsigned int i = 0; i < List.size(); i++) {
SumAll += List[i];
}
cout << "The Sum of All Fractions is: " << endl;
SumAll.DisplayInitial();
ReduceFraction(SumAll).DisplayReduced();
ReduceFraction(SumAll).DisplayMixed();
SumAll.DisplayReal();
cout << endl;
}
void FractionList::ResultingFraction() {
Fraction F1, F2, F3, F4, F5, F6, Result;
F1.SetNum(List[0].GetNum());
F1.SetDen(List[0].GetDen());
F2.SetNum(List[1].GetNum());
F2.SetDen(List[1].GetDen());
F3.SetNum(List[2].GetNum());
F3.SetDen(List[2].GetDen());
F4.SetNum(List[3].GetNum());
F4.SetDen(List[3].GetDen());
F5.SetNum(List[4].GetNum());
F5.SetDen(List[4].GetDen());
F6.SetNum(List[5].GetNum());
F6.SetDen(List[5].GetDen());
Result = -F6 - ((F1 + F2) * F3 - F4) / F5;
cout << "The Result using Equation [ResultingFraction= -F6-((F1 + F2)*F3-F4)/F5] is: " << endl;
Result.DisplayInitial();
ReduceFraction(Result).DisplayReduced();
ReduceFraction(Result).DisplayMixed();
Result.DisplayReal();
cout << endl;
}
unsigned int FractionList::GetListFromFile(string FileName) {
ifstream FractionFile(FileName);
if (!FractionFile.fail()) {
int num, den;
Fraction Fraction;
while (FractionFile >> num) {
FractionFile >> den;
Fraction.SetNum(num);
Fraction.SetDen(den);
List.push_back(Fraction);
}
}
return List.size();
}
I cannot seem to figure out how to make this work any help would be greatly appreciated as i have been trying for days :(
#include <iostream>
#include <math.h>
using namespace std;
int input;
int sum;
int number1;
int number2;
int number3;
void isArmstrong (int input, int sum)
{
if (input == sum)
cout << input << " is an Armstrong number" << endl;
if (input != sum)
cout << input << " is not an Armstrong number" << endl;
}
cubeOfDigits does not return input and sum to isArmstrong, (return input,sum) error is as follows: expression result unused [-Wunused-value]
int cubeOfDigits (int input, int sum, int number1, int number2, int number3)
{
cout << "Enter an integer between 0-999" << endl;
cin >> input;
number1 = input / 100;
number2 = input % 100;
number3 = number2 % 10;
sum = pow(number1, 3) + pow(number2, 3) + pow(number3, 3);
isArmstrong(input, sum);
return input,sum;
}
main calls cubeOfDigits
int main(void)
{
cout << "Welcome" << endl;
cubeOfDigits(input, sum, number1, number2, number3);
return 0;
}
I think what you intended was..
#include <iostream>
#include <math.h>
using namespace std;
int input;
int sum;
int number1;
int number2;
int number3;
void isArmstrong(int input, int sum){
if(input == sum)
cout << input << " is an Armstrong number" << endl;
if(input != sum)
cout << input << " is not an Armstrong number" << endl;
}
int cubeOfDigits (int input)
{
number1 = input/100;
number2 = input % 100;
number3 = number2 % 10;
number2 = number2/10;
sum = pow(number1,3) + pow(number2,3) + pow(number3,3);
return sum;
}
int main(void){
cout << "Welcome" << endl;
cout << "Enter an integer between 0-999" << endl;
cin >> input;
isArmstrong(input,cubeOfDigits(input));
return 0;
}
change the cubeofdigits function from (int) cubeofdigits to (void) cubeofdigits and then remove the return statement from the end of cubeofdigits function .then it must work.
I ran this codes only my zybook class, it gives me the wrong answers and tells me to use floating numbers for the functions. How can I use floating point numbers for square function.
edit:
sorry for the confusion
#include <iostream>
#include <string>
using namespace std;
float squareRoot( float s) {
float xn;
if (s == 0) {
xn = 0;
}
else {
xn = s / 2.0;
int counter = 1;
while (counter <= 10) {
xn = (xn + (s/ xn)) / 2.0;
counter = counter + 1;
}
}
return xn;
} int square(int what) {
return what* what;
}
int main() {
float sideA, sideB, sideC;
string answer = "yes";
while (answer == "yes") {
cout <<" Enter side A: ";
cin >> sideA;
cout << sideA << endl;
cout <<" Enter side B: ";
cin >> sideB;
cout << sideB << endl;
sideC = squareRoot( square( sideA) + square( sideB));
cout <<"Side C is " << sideC << endl;
cout <<"Continue? ";
cin >> answer;
cout << answer << endl;
}
return 0;
}
Your function:
int square (int what){
return what * what;
}
should instead return a float. When you pass the floating point numbers to this function, it returns them as integers so instead of 3.3 and 4.4 getting squared, 3 and 4 do.
You should return a float and change what to a float as well.
I'm trying to print the output of the function after my values are sent to the function. The cout statement needs a string but I'm not sure how I can return a string from my reduce_fraction function while keeping the math correct. In my add_fraction function, you'll see that I simply want to print the added fraction then the reduced fraction right below it. The compiler returns no errors but the output just shows the "Improper Fraction" answer.
#include <iostream>
#include <string>
using namespace std;
void reduce_fraction (int top, int bottom)
{
for (int i = top * bottom; i > 1; i--) {
if ((top % i == 0) && (bottom % i == 0)) {
bottom /= i;
top /= i;
}
}
}
void add_fraction (int numerator, int numerator2, int denominator, int
denominator2)
{
int top;
int bottom;
top = numerator2 * denominator + denominator2 * numerator;
bottom = denominator2 * denominator;
cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified Fraction -> ";
reduce_fraction(top, bottom);
}
int main()
{
int numerator;
int denominator;
int numerator2;
int denominator2;
char operation;
cout << "Input the numerator: ";
cin >> numerator;
cout << "Input the denominator: ";
cin >> denominator;
cout << "Input the numerator2: ";
cin >> numerator2;
cout << "Input the denominator: ";
cin >> denominator2;
cout << "Input the operation: ";
cin >> operation;
if (operation == '+'){
add_fraction(numerator, numerator2, denominator, denominator2);
}
return 0;
}
Use reference to reflect the changes in top and bottom
and print those in your add_fraction function after calling reduce_fraction
void reduce_fraction ( int & top, int & bottom)
{ ~~~ ~~~
//...
}
Then,
reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;
I want to simplify a fraction in my application. The fraction is like,
x/y where x and y are integers.
I want to simplify the fraction to its simplest form.
Can anyone please give me hints how to do it.
Thanks in advance.
Compute the greatest common divisor for x and y
Divide both of them by the GCD
Euclid's algorithm is an easy way to compute the GCD.
Divide both by gcd(x,y)
The Binary GCD algorithm is a fast way to compute the GCD on a computer.
#include<iostream>
using namespace std;
struct fraction
{
int n1, d1, n2, d2, s1, s2;
};
void simplification(int a,int b)
{
bool e = true;
int t; int z;
for (int i = (a*b); i > 1;i--)
{ if ((a%i==0)&&(b%i==0))
{
t = a / i;
z = b / i;
}
else
{
e = false;
}
}
cout << "simplest form=" << t << "/" << z << endl;
}
void sum(int num1, int deno1, int num2, int deno2)
{
int k,y;
k = num1* deno2 + num2*deno1;
y = deno2*deno1;
cout << "addition of given fraction = " << k << "/" << y << endl;
simplification(k, y);
}
void sub(int num1, int deno1, int num2, int deno2)
{
int k, y;
k = num1*deno2 - num2*deno1;
y = deno1*deno2;
cout << "Substraction of given fraction = " << k << "/" << y << endl;
}
void mul(int num1, int deno1, int num2, int deno2)
{
int k, y;
k = num1*num2;
y = deno1*deno2;
cout << "multiplication of given fration= " << k<< "/" <<y; cout<< endl;
simplification(k, y);
}
void div(int num1, int deno1, int num2, int deno2)
{
int k, y;
;
k = num1*deno1;
y = deno1*num2;
cout << "division of given fraction" << k << "/" << y << endl;
simplification(k, y);
}
int main()
{ fraction a;
cout << "enter numirator of f1=";cin >> a.n1;
cout << "enter denominator of f1=";cin >> a.d1;
cout << "enter numirator of f2=";cin >> a.n2;
cout << "enter denominator of f2=";cin >> a.d2;
cout << "f1= " << a.n1 << "/" << a.d1 << endl;
cout << "f2= " << a.n2 << "/" << a.d2 << endl;
mul(a.n1, a.d1, a.n2, a.d2);
div(a.n1, a.d1, a.n2, a.d2);
sub(a.n1, a.d1, a.n2, a.d2);
sum(a.n1, a.d1, a.n2, a.d2);
system("pause");
}