Decimal to Mixed Fraction


Is there a way that I show the hat as a mixed fraction instead of a decimal? I've seen it brought up in different places, but the questions all had a little different twist to what I'm trying to accomplish.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <cstdlib>
#include<iostream>
#include <iomanip>

using namespace std;


void get_data(int& weight,int& heightInFeet,int& heightInInches);

float Hat_Size(int weight ,int heightInFeet ,int heightInInches);

int Jacket_Size(int,int,int);

int Waist_Size(int) ;

int main(int argc, char *argv[])
{
    	//declare weight, height_in_feet, height_in_inches of type int 
	int weight;
	int height_in_feet;
	int height_in_inches;

	//declare hat result of type float
	float hatSize;
	
	//declare result of type int to hold the jacket size or waist size 
	int jacketSize;
	int waistSize;

	//declare type of operation of type char 
	char operation;

	//Count for the loop 
	int count;
	cout << "Please enter number of input sets : " ;
	cin >> count;

	cout << "Please enter the number of calculation sets you would like to perform: " << endl ;
	//for loop based on the count 
	for (int i = 0; i < count; i++ )
	{
		//	call get_data 
		get_data(weight, height_in_feet, height_in_inches);
		//	find hat size 
		hatSize = Hat_Size(weight, height_in_feet, height_in_inches);
		//	find jacket size 
		jacketSize = Jacket_Size(weight, height_in_feet, height_in_inches);
		//	find waist 
		waistSize = Waist_Size(weight);
		//    write result for all three cases 
		cout << setw(20) << "Hat Size : " << hatSize << endl;
                cout << setw(20) << "Jacket Size : " << jacketSize << endl;
                cout << setw(20) << "Waist Size : " << waistSize << endl;
                if (i < count-1) 
        {
                cout << endl << " Enter next calculation: " << endl ;
        }
	}
	
	//endfor 

	cout << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

//end of main //declare functions here, one void and three regular 

void get_data(int& weight,int& heightInFeet,int& heightInInches)
{
	//reads the weight, height in feet and height in inches
	cin >> weight;
	cin >> heightInFeet;
	cin >> heightInInches;
} 

float Hat_Size(int weight ,int heightInFeet ,int heightInInches)
{
	
	float hatSize = ((float:weight * 2.9) / heightInInches;
	return hatSize;
} 

int Jacket_Size(int weight ,int heightInFeet ,int heightInInches)
{
	
	return (weight * heightInInches / 288);
}

int Waist_Size(int weight) 
{
	
	return (weight/ 4.9);
}
Try this function.

I'm not sure if % works on floats to filter out the decimal, but I think so.

1
2
3
4
5
6
7
8
9
10
11
12
13
void (float decimal, int *whole, int *numerator, int *denominator)
{
	*whole = decimal/1; // Integer division to filter out the decimal (remainder)
	decimal -= whole; // Whole is accounted for so now remove that.
	
	for (*denominator = 1; *denominator<=100; *denominator++) // Check if any denominator up to 100 will work well
	{
		if (*denominator * decimal % 1 < 0.001f) // If a denominator is reasonably close, then this is our value
			break;
	}	//If no values exist, then we'll just use 100 as the denominator, it's close enough.  You can tune this if you like.
	
	*numerator = *denominator * decimal; //We know our denominator so let's find our numerator. 
}


Now you can use:
1
2
void (1.5, &myWhole, &myNumerator, &myDenominator);
cout << myWhole << " " << myNumerator << "/" << myDenominator;

in your code.
(float * int % int) will return an int. We are looking for the decimal portion of the value so this doesn't work. Find a way to replace that line of code (line 8 above).
Found it!:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <math.h>

void (float decimal, int *whole, int *numerator, int *denominator)
{
	int dummy;

	decimal = modf (decimal, whole); // Integer division to filter out the decimal (remainder)
	
	for (*denominator = 1; *denominator<=100; *denominator++) // Check if any denominator up to 100 will work well
	{
		if (modf(*denominator * decimal,&dummy) < 0.001f) // If a denominator is reasonably close, then this is our value
			break;
	}	//If no values exist, then we'll just use 100 as the denominator, it's close enough.  You can tune this if you like.
	
	*numerator = *denominator * decimal; //We know our denominator so let's find our numerator. 
}


http://www.cplusplus.com/reference/clibrary/cmath/modf/

modf accepts two parameters: The number we are trying to evaluate, and the address of our whole number. It will set the whole number portion of the input to the second parameter and then return the decimal portion of the number. I think it works something like this:
1
2
3
4
5
6
double modf (double param ,double *intpart)
{
	*intpart = 0;
	while (*intpart < param-1) *intpart++;
	return param-*intpart;
}
Topic archived. No new replies allowed.