C/C++ :: Converting A Fraction To Decimal?

Apr 28, 2015

i am having issues converting a fraction to a decimal, i think the code would go in the istream because it is working with input data but i just cant figure it out.

#include <cstdio>
#include <string>
#include <iostream>
using namespace std;

[Code].....

View 2 Replies


ADVERTISEMENT

C/C++ :: Fraction Calculator - Converting From Fraction To String

May 27, 2014

I'm having a problem when i convert from fraction to string. When I run my program it runs fine I'm supposed to get an output of the fraction ex (2/5) and the decimal 0.4 the problem is that it does not output the fraction all I get it / and 0.4

the code for converting from fraction to string is the following
std::string string;
char numerator[100]/* = {0}*/;
char denominator[100]/* = {0}*/;
_itoa_s(numerator_, numerator, 10);
_itoa_s(denominator_, denominator, 10);

[Code] .....

View 5 Replies View Related

C++ :: Get Fraction For Any Decimal Number In Variable?

Jun 9, 2013

How do i get the fraction for any decimal number in a variable?

Code: Code: #include<iostream>
using namespace std;
int main()
{
double x = 1/6;
cout << x;
cin.get();
}

View 1 Replies View Related

C++ :: Fraction Class - Warning Converting To Int From Double

Jul 28, 2013

I am trying to write a Fraction class and getting the following warning when compiling my code :

Fraction.cpp: In constructor 'Fraction::Fraction(double)':
Fraction.cpp:8: warning :converting to 'int' from 'double'

My Fraction.cpp class looks like :

#include "Fraction.h"
Fraction::Fraction(int n, int d):num(n),den(d) {
cout << This is double param constructor <<endl;
}
Fraction::Fraction(double d):num(d),den(0)

[Code] ....

How can I get rid of the warning ?

View 8 Replies View Related

C/C++ :: Converting From Float To Decimal?

Sep 11, 2014

Question: A computer uses 10 bits to store integers with 1 bit for a sign. It stores an approximation of real numbers in 10 bits. The first bit of the first five is the sign of the mantissa and the other four bits are the mantissa. The first bit of the second five is the sign of the exponent and the other four the exponent.

1)What is the range of integers?

2)what is the range of real numbers(Float Type)

The first question was simple. I just found the smallest 10 digit binary number 1000000000 = -512 and then found the largest 10 digit binary number which would have to be 0111111111 = 511, therefore the range of integers is from -512 to 511.

For the second question - I am either making this harder than it is, or it really is a challenging question. So I followed the steps and first I was thinking I would take the number 1000000000 and convert this to a decimal (assuming its a 10 bit float)...But, can you even do this with a 10 bit float??

I ended up getting 1000000000 (after denormalizing) = .000100000 = 0.625.. would that be the minimum range? If so, then I know what I need to do to find the maximum , but if not - then I am really lost.

My Process was:

1.00000 X 2^(-4) = my final result of 0.625 after converting.

View 9 Replies View Related

C# :: Converting Decimal To Hexadecimal

Feb 20, 2015

I am new at programming and I have some questions about converting decimal to hexadecimal WITHOUT using .net library. The problem is, that I don't know how to do vice versa. (if you type 1254, program returns 6,14,4. I want programm to return 4,14,6- this is almost hexadecimal number (14 is not converted to "E")). Also the task is, that program has to return value in string form.

static void Main(string[] args) {
int a = 0;
int result = 0;
int n=1000000;
int[] array = new int[n];
Console.WriteLine("Insert numbers");
a = int.Parse(Console.ReadLine());

[Code] ....

View 12 Replies View Related

C++ ::  Converting Feet To Inches As A Decimal

Oct 21, 2014

I have my program all but done, but I can't get past this last part. I need to have the user input their height as a decimal. For example someone who is 5 foot 11 inches would need to enter it as 5.11, then I need to display there height in inches. Since i'm 5 foot 11, I would need to enter 5.11, then have it display 71 inches.

View 4 Replies View Related

C++ :: Converting Decimal To Binary Using Recursion

Feb 25, 2013

I'm trying to write a program that converts a decimal number to a binary one. I have most of the program written, but I am having a little bit of trouble. Whenever I enter a decimal number, the program will convert it correctly to binary, however, the last number is not included in the conversion. EX: Converting 37 into binary (0100101) yields 010010 when entered into the program. BTW the program must utilize recursion to achieve this goal.

#include <iostream>
using namespace std;
void decToBinary(int num1);
int main() {
int num1;

[code]....

View 4 Replies View Related

C++ :: Converting Binary Numbers Into Decimal

Apr 25, 2014

This is the question: [URL] .....

Now I have the binary numbers printed out in my code, but I don't know how I can covert them into to decimal.

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main() {
int numberOfDigits;
int numberOfRows;
char flag;

[Code] ....

View 1 Replies View Related

Visual C++ :: Converting Decimal Number Given By User To Binary?

Dec 19, 2014

Write a C++ program that adds three binary numbers of 8-bit each. Every number is stored into an array of 8 elements.

Example:

Array A
0 1 2 3 4 5 6 7
0 1 1 0 1 1 1 0
+
Array B
0 1 2 3 4 5 6 7
0 1 1 0 1 1 1 0
+
Array C
0 1 2 3 4 5 6 7
0 1 1 0 1 1 1 0

Store the result into an array D of 8 elements. Your program should show the decimal numbers for every binary number. Print screen of 6 answers. This means you should try your program six times with different numbers in every run and show the printed screen result.

View 5 Replies View Related

C :: Converting Decimal Number To Any Base Then Find Ones And Twos Complement

Oct 10, 2014

I have the code working on converting a decimal number to base 1 through 16. I need getting the code to output the one's and two's complement.

Code:
#include <stdio.h>
int main(void) {
char base_digits[16] =
{'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

int converted_number[64];
long int number_to_convert;
int next_digit, base, index=0, a;

/* get the number and base */

[Code] ....

View 8 Replies View Related

C :: Finding Written Numbers In A String And Converting Them To Decimal Numbers

Jun 20, 2013

User enters sentence "The Smiths have two daughters, three sons, two cats and one dog." (The numbers may change depending on what the user chooses to enter. He told us the range would be from zero to nine.) and we have to convert the written numbers within the sentence into actual decimal numbers and print out the new sentence. Ex. The Smiths have 2 daughters, 3 sons...etc.

I have written the following bit of code which reads the string and finds all the "written numbers" but I am not sure how to proceed from there. I am stuck on how to print out the new sentence with the converted numbers as my professor mentioned something about creating the new string using dynamic memory allocation.

Code:
#include <stdio.h>#include <string.h>
int main () {
char A[100];
int length = 0;
int i;

[Code] .....

View 7 Replies View Related

C++ :: Writing A Function That Compares Two Fraction?

Dec 25, 2014

I'm writing a function that compares two fraction. if the fractions are equal it returns 0. If the fraction in the first parameter is less than the fraction in the second parameter it returns a negative number. Otherwise it returns a positive number. in doing so convert the fraction to a floating point number.

completing the function to run

typedef struct fracion
{
int numo;
int denom;
}fraction;

[Code].....

View 2 Replies View Related

C++ :: Static Constant Datatype For Fraction Class

Aug 4, 2013

My Fraction.h class looks like :

class Fraction {
int num;
unsigned int den;
public:
Fraction(int = 1,int =1);
//Constants of Datatype

[Code] ....

The implementation Fraction.cpp is as follows :

#include "Fraction.h"
Fraction::Fraction(int n, int d):num(n),den(d){
cout << This is double param constructor <<endl;
}

And the application main.cpp is

int main(){
Fraction f1(3,9);
f1 = Fraction::sc_fUnity; // how to implement this ?
}

How can I write the Fraction.cpp for the constant static member ?

View 6 Replies View Related

C++ :: How To Give Fraction Object In S String Via Template

May 9, 2014

I am trying to give fraction object in s string via template.

struct Fr // simple Fraction {
int num;
int denom;
Fr(int i,int j) {

[Code] ....

View 10 Replies View Related

C++ :: Create A Program That Calculates Fraction Of Time?

Dec 16, 2013

I am trying to create a program that calculates fraction of time remaining when a student leaves during the middle of the semester. I have to use function files here. I am having trouble with the division in function long double tuitionfrac(int, int, int). No matter what numbers are entered, fractime gives an output of 0. I don't understand that.

long double tuitionfrac(int opening, int leaving, int semend)
{
int lefttime = semend - leaving;
cout << "Left time " << lefttime << endl; //for error checking
int tottime = semend - opening + 1;
cout << "Total time " << tottime << endl;
long double fractime = lefttime/tottime;
cout << "fractional time " << fractime << endl; //always returns as 0.
return fractime;
}

View 4 Replies View Related

Visual C++ :: Class Fraction - Overloading Operators

Sep 25, 2013

the question am having problems with..

1.Write a class function that defines adding, subtracting, multiplying and dividing fractions by overloading standard operators for the operations.

2. Write a function member for reducing factors and overload I/O operators to input and output fractions. how would i set this up?

View 5 Replies View Related

Visual C++ :: Structures Function - Comparing Two Fraction

Nov 5, 2014

I'm writing a function that compares two fraction. if the fractions are equal it returns 0. If the fraction in the first parameter is less than the fraction in the second parameter it returns a negative number. Otherwise it returns a positive number. in doing so convert the fraction to a floating point number.

typedef struct fracion {
int numo;
int denom;
}fraction;
int compareFractions (fracion, fraction);

[Code] ....

View 2 Replies View Related

C++ :: Implementation Of Array Index Operator For Fraction Class

Jan 7, 2014

I need an implementation for the array index operator overloading of my Fraction class . The Fraction.h looks like :

#include <iostream>
using namespace std;
class Fraction {
public:
Fraction(int = 1, int = 1);

[Code] .....

I am not able write the Array index operator overloading functions.

View 3 Replies View Related

C++ :: Display Fraction In Proper From Based On 2 Arguments Passed To Class Member Function

Mar 15, 2015

We're assigned a project working with classes and fractions. My goal is to display a fraction in proper from based on 2 arguments passed to a class member function proper();

My strategy was to utilize the greatest common factor between the 2 arguements, then divide both the numerator and denominator by that number and then it would display.

The program actually runs, but only seems to divide the numerator and not the denominator. This in return makes my other class member functions have incorrect comparisons and sums.

Code:
#include<iostream>
#include<conio.h>
class Fraction {
friend void compare(Fraction a, Fraction b);
friend void sum(Fraction a, Fraction b);

[Code] ....

View 14 Replies View Related

C++ :: Write A Class Definition For A Fraction Class

Nov 25, 2013

Write a class definition for a Fraction class. Its member fields are num and den, both of type int. The constructor builds the default fraction 1/1. It has the following operations:

void plusEquals(Fraction second);
//Adds the second fraction to this fraction like the operator += void minusEquals (Fraction second);
//Subtracts the second fraction from this fraction void timesEquals (Fraction second);
//Divides this fraction by the second fraction void dividesEquals (Fraction second);
// Divides this fraction by the second fraction void reduce();
// Reduces this fraction to lowest terms double todecimal();
//returns the decimal value of this fraction void scan(istream&);
//scans a fraction written with a slash as in ¾ void print(ostream&);
//prints a fraction using a slash Fraction();
//constructs a default fraction with denominator 1, numerator 0 Fraction(int n, int d); //constructs a fraction given value for num and den

2. Write a menu-driven driver program designed to allow thorough testing of your Fraction class.

View 2 Replies View Related

C/C++ :: How To Convert A Decimal Value To Hex

Mar 27, 2014

How would you go about converting a decimal value to hex and then do math? Every example of converting decimal to hex that I have seen creates an array and I wouldn't be able to do math if I did that. Something like this.

15 decimal to hex F
17 decimal to hex 11
F hex + 11 hex = 20

View 8 Replies View Related

C :: Binary Number To Decimal

Mar 30, 2014

I am very new to programming and have been working on a program that can receive decimals or binary numbers and convert them. The decimal --> binary works fine. For some reason I cannot figure out I cannot get the "BinaryToDecimal" function to perform. By putting a "printf" into the for-loop.

Code:

#include <stdio.h>#include <string.h>
#include <math.h>
char* ReverseString (char _result[]) {
int start, end, length = strlen(_result);
char swap;
for (start = 0, end = length-1; start < end; start++, end--)

[code]....

View 2 Replies View Related

C :: Decimal To Binary Using Bitwise

Nov 15, 2013

How to do this program i can easily do it in a simple for loop but i have to do this program with the following directions:

1. Write a function called bitN() that returns the value of bit N in number, where number is the first parameter, and N is the second. Assume N of the least significant bit is zero and that both parameters are unsigned int's. (A simple one-liner will suffice)

2. Write a main() function that uses bitN() to convert a decimal integer into its binary equivalent. Obtain the integer to convert from the first command-line argument.

3. Use the expression
unsigned int numBits = sizeof(unsigned int)*CHAR_BIT;
to get the number of bits in an unsigned int. (Include limits.h to get the definition for CHAR_BIT.)

View 2 Replies View Related

C :: How To Truncate And Round A Decimal

Jan 14, 2014

To one decimal place?

Code:

#include <stdio.h>
int main() {
double num, trunc, round;
printf("
Enter a Decimal:
}

[code]...

That's my code currently, I've gotten it to round as I want, but I want it to truncate to one decimal place as well.If i enter 4.157, It should truncate to 4.1, and round to 4.2.

View 1 Replies View Related

C :: Binary To Decimal Converter

Feb 26, 2013

I'm trying to make a program that takes up to a seven digit binary number and converts it to its decimal equivalent. I'm still a beginner, so this might be a simple question but how do I stop the user from entering anything but 1s and 0s? This means no decimals or numbers other than 1 or 0.I already made it so it won't accept anything below 0 or above 1111111.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved