Click here to Skip to main content
15,909,741 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm having trouble figuring what's missing in the code in order to convert to IEEE754 representation. when i did the input it said enter a number for IEEE754 representation and it never converted it. What's missing?

What I have tried:

#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <string>       // String managment funtions.
#include <iostream>     // For input and output
#include <cmath>        // For math functions.
#include <math.h> 
using namespace std;
////////////////////////////////////////////////////////////////////////
float getUserInput();
int wtb(int whole, int w[]);
void ftb(int fraction,int sizew,int f[]);
void etb(int e[], int exponent, int size);
void move(int e[],int w[], int f[], int s[]);
void display(int s[]);
int main()
{
int opt getuserOption();
int opt;//opt is created
int IEEE754;//IEEE754is created
switch(opt){

     
      case 1:IEEE754();
      break;
       }

// Your code starts here.
//float number;
//number = getUserInput();
//cout << truncf(number);
//string str = string("Bradley");
//cout << str.length();
//cout << str.size();
int str;// str is created
cout  ;str.at0;
//cout << str.append("Haynes");
//string str1 = "Student";
//str.swap(str1);
//cout << str;
// Create an array to store the IEEE754 of number:
int s[32];
/*
 We may need two more arrays:
 1: To store the binary representation of the whole part. Called w.
 2: To store the binary representation of the fractional part. Called f.
 Since the mantissa of the IEEE754 has 22 bits, this is a good size for the
 arrays. The array size of f will depend on how many bits are left from w.
 Ex: if the size of the whole part is 14 then the size of the fraction will be
     22 - 14. And so on.
*/
int const SIZE = 22;
// To facilitate counting; initialized w with numbers different from 0's and
// 1's. 2 is used as a marker. 
int w[SIZE]={2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
// Say the initial size of the whole is sw = 0;
int sw = 0;
// then
int f[SIZE-sw];
/*
 Let's start by checking the sign of then number and setting 
 the first bit accordingly.
 If the number is positive set w[0] = 0
    else w[0] = 1.
*/
///////////////////////////////////////////////////////////////////////////////
int number;//number is created
// Now we split the number into the whole and fraction.
 float whole = truncf(number);
 // Set fraction to the appropiated value
 float fraction = 0;

/* 
  function wtb: convert the whole number to binary. 
  Inputs: whole and w[];
  return: size of elements added to w.
*/
int sizew = wtb(whole,w);

/* 
  function ftb: convert the fraction number to binary. 
  Inputs: fraction, sizew, and f[];
  return: By reference, the binary representation of fraction.
*/
ftb(fraction,sizew,f);
/*
  Up to this point you must have the binary representation of the
  whole and the fraction in their respective arrays. It is time to
  normalize the binary representation of the whole part.
  
  Since we know the sizew and we are normalizing then the x, 
  exponent of 2, is: x = sizew - 1 = exponent -127.
  Therefore exponent = sizew -1 + 127.
  Now we need an array of 8 bits for the exponent
*/

/*
  etb is a function that convert exponent to binary.
  inputs: e,exponent,8
  outputs: By reference fill the array e with the 
  binary representation of exponent.
*/
int size = 8;
int e[size];
int exponent = 0;
etb(e,exponent,size);

/*
  Up to this point all the elements of the array IEEE754 called s
  are ready to be fill. You have:
  The sign.
  The binary representation of the exponent in e[].
  The binary representation of the whole in w[].
  The binary representation of the fraction f[].
  Now, CARUFULLY, move the elements of those arrays to s.
  If there is some markers, 2's, replaced them with 0's.
  void move(e,w,f,s)
  Call a function display.
  inputs: array s.
  outputs: Display the elements of s.
*/
move(e,w,f,s);
display(s);

//
// Your code ends here.
    return 0;
}
float getUserInput(){
    float number;
    int getuserOption;
    int opt;
    cout << "Please enter a number to convert to IEEE754: " << " ";
    cout << "1 Convert number to IEEE754" << endl;
    cout << "2 Convert IEEE754 to number" << endl;
    cout << "3 Exit" << endl;
    cin >> opt;
    cin >> number;
    return opt;
    return number;
}

int wtb(int whole, int w[]){
    int sizew = 0;
    // Fill in the w array with the binary representation of whole.
    // As you add elements to w count them or increase sizew.
    return sizew;
}

void ftb(int fraction,int sizew,int f[]){
    // Fill in the elements of f with the binary representation of 
    // fraction. Use sizew to control the number of elements on f.
}
void etb(int e[], int exponent, int size){
    //Fill in the elements of e with the binary representation
    // of exponent.
}

void move(int e[],int w[], int f[], int s[]){
    // Fill in the elements of s with the elements of e,w,f.
    // If there are markers, 2's change them to 0's.
    // Be midfu; to transition from w to s only the normalized bits.
    
}

void display(int s[]){
    // Show the elements of s in a row fashion.
}
Posted
Updated 9-Jul-21 7:24am

The function getUser Input should possibly be renamed to getuser Option ().
In the case of a function, only the first return has an effect. Subsequent returns make no sense.

You could start with this:

C++
int getuserOption() {
	int opt;
	cout << "Please enter a option to convert IEEE754 \n";
	cout << " 1 Convert number to IEEE754\n";
	cout << " 2 Convert IEEE754 to number\n";
	cout << " 3 Exit\n" << endl;
	cin >> opt;
	// cin >> number;
	return opt;
	// return number;
}

// ANSI / IEEE Std 754-2019; Standard for radix-independent floating-point arithmetic
// Convert number to IEEE754
float Num_2_IEEE754(int num) {
	float f = (float)num;  // TODO:  convert here
	return f;
}

int IEEE754_2_Num(float num) {
	int x = (int)roundf(num);  // TODO:  convert here
	return x;
}

int main() {
	int opt;				// store option
	int valint;				// local result int
	float valfloat;			// local result float
	bool dorun = true;

	do {
		opt = getuserOption(); // display menu; get opt
		switch (opt)
		{
		case 1:
			cout << "Input int: ";
			cin >> valint;
			valfloat = Num_2_IEEE754(valint);
			cout << "Result: " << valfloat << "\n";
			break;
		case 2:
			cout << "Input float: ";
			cin >> valfloat;
			valint = IEEE754_2_Num(valfloat);
			cout << "Result: " << valint << "\n";
			break;
		default:
			dorun = false;
		}

	} while (dorun);

    ...
 
Share this answer
 
Your code (it doesn't even compile) is a sequence of empty functions. You have to fill the blanks.
 
Share this answer
 
Missing is the implementation. You got the code skeleton with the TODO-list from some software architect.

You need to learn the language from some Learn C tutorial and write the code.

tip: use Visual Studio, but maybe you need some specific IDE
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900