I got the following to compile and execute, but I don't understand how this works. Here is the code verbatim:
Code:
// Basic conversions in C
#include <stdio.h>
int main () {
float f1 = 123.125, f2;
int i1, i2 = -150;
char c = 'a';
[Code] .....
Okay, so first we have some variables declared as integers, floats, (and that char what it's doing). On the same lines we have values assigned to some of those variables. At first the "f2" and the "i1" confused me, but I think they're just variables whose type has been declared but have not received a value. So far I think I'm good. Then we get into the routines
I think I understand the first one. i1 didn't originally receive a value assigned to it, so when it says f1 = i1, then f1 (123.125) just becomes an integer, simple enough.
Then we get to the next routine and I'm like what dafuq??? So first we have f1 (123.125) being set to i2 (-150). wtf? So does that mean f1 is now going to have the value of -150? Vice versa? How are they becoming equal? Or does it have nothing to do with the values of the variables at all and just the type??? I'm just totally lost.
The output of that line is "-150 assigned to a float produces -150.000000." Which makes perfect sense to me written in plain English, but I don't understand how the C code works ....
So, in my platform, conversion from an unsigend integer primitive data type to any bigger integer primitive data type never extends the most significant bit of the former integer and conversion from an signed integer primitive data type to any bigger integer primitive data type always extends the most significant bit of the former integer. This is convenient to mantain the same value when converting between integer primitive data types of the same signedness (i.e, signed integers or unsigned integers).
I am trying to make sure no floating point numbers can be input at any time in my roulette program. My code is too long to put in one post so I shall piece it out
#include <iostream> #include <iomanip> #include <random> #include <cstdlib> #include <ctime> using namespace std; // structure of arrays to pass to functions
I want to count all the numbers in my text file (read.txt). Read text file consist of floating and integer number. Answer for the above file would be integer=2 and float =10.
I have a project assignment for school to write a program that does number conversions using bitwise operators. The premise is that the user enters a number with one of three letter prefixes -- Q1232, O6322, H762FA, etc. -- and the program will take that number and convert it to the other two number bases. Q is for quarternary, O is for octal, and H is for hexadecimal. The transformations should be done using bitwise operators and bit shifting. I am guessing I need to scan the number, convert it to binary, then convert it to the other two bases.
However, I am completely new to bitwise operators and bit shifting, so how to convert numbers of different bases to binary and then binary to other bases using these bit and bitwise functions. I don't have much code done yet, since I am still unsure of how to approach it, but I'll post what little I have.
Here it is:
#include <stdio.h> #include <string.h> int main() { char numType; printf(" The user will enter a number up to 32 digits in quarternary "); printf("(base 4), octal (base 8), or hexadecimal (base 16). If in ");
[Code] ....
I figure in each case I can write a function that converts the entered number to binary, then maybe two more functions that convert said binary number to the other bases. For default in the switch I will tell the user they entered an invalid number. I don't have the program looping until the user types 'EXIT' yet, but I will once I figure out anything about these bitwise operators.
2. Data represents some data object the user can pass to Processor's public methods.
3. Internally, Processor needs to use InternalData type which is based on the content of Data (I can use Data's public interface to get the required information from it, or construct a Data object using its public constructor when needed, and that's how I have done it so far).
4. To avoid repeating code and localize changes required when Data's interface would change someday, I made conversion functions from Data to InternalData and back inside Processor, as private methods.
Now here comes the kicker:
5. But I'd like these conversions to be implicit inside Processor's methods instead of explicit. And only there.
6. These conversion functions are only for Processor implementation's use. They shouldn't be visible nor accessible from the outside world.
Where the problem lays:
7. InternalData is a library type. I don't have control over it and I cannot modify its interface.
That is, I cannot just add converting constructors or conversion operator member functions to them.You can consider it to be built-in type if you wish.
8. I don't want to put those converters inside Data class either, since it's not its business and it shouldn't know that Processor converts it to something else internally.
Long story short, I'd like to teach the Processor's implementation how to make type conversions between Data and InternalData implicitly, but no one else except Processor should be affected by it. Outside world shouldn't be able to do these conversions or even know about them being done inside Processor's implementation.Is there any way to do it in C++?
The core of the problem seems to be the fact that in C++ defining implicit conversions is possible only from/to a user-defined type when defining this type. I don't know of any way to define such conversions for some other type's internal use only. (Especially when I don't have control about one of these converted types.)
Write a program that creates and displays a table of temperature conversions. Get the starting temperature from the keyboard in degrees Celsius (do not allow input of a value below absolute zero). Also get an integer value to represent the number of degrees to increment for each of a 20 row table (do not allow the increment value to be less than one. The first column will be a row number starting with one, follow by the Celsius value and then the conversions into Fahrenheit, Kelvin, and Rankine. Be sure that all columns are neatly right aligned for a variety of inputs.
Thats what i wrote so far:
#include <iostream> #include <iomanip> using namespace std; int main() { double C,F,K,R,n,a; cout <<"Enter starting temperature in Celsius: "; cin >> C; [Code] ....
Thats what the instructor looking for:
Enter starting temperature in Celsius: -500 ERROR: Temp must be >= -273.15: -273.15 Enter increments in degrees Celsius: 100
run-time check failure #0 - the value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention
when i try to run my code. It has compiled fine on another computer, but it simply will not work on this one. This is the part of code where it is receiving the error. it has to do with the stoi
Code: #include <string> // for use of string #include <fstream> //for file handling #include <iostream> // for file handling #include <cstdlib> #include <iomanip> //for the setprecision used further below using namespace std; struct MasterData //struct created named 'MasterData' to hold one line from master file
I am trying to assign the integer value to unsigned char array. But it is not storing the integer values. It prints the ascii values. Here the code snippet
The values which are stored in uc[] is ascii values.I need the integer values to be stored in uc[]. I tried to do it with sprintf. but the output is not as expected. if I print the uc[i] it should diplay the value as 0,1,2....99.
changing a 9 digit integer into a new 9 digit integer through simple mathematical operations. For example, I need to change 123456789 into the new digit 456123789. Sometimes I need to change a 9 digit integer into an 8 digit integer. An example is 789062456 into 62789456. I can ONLY use simple mathematical operations (addition, subtraction, multiplication, division and modulo).
If i do run the above program in turbo C/C++ complier, it outputs "h". But,if i change the code as i=0.6 and if (i<0.6), it outputs "w". Even if i change it to i=0.8 and if(i<0.8), then also it outputs "w".
In the above program, I am calculating the square of float number. But sometimes the number is entered as NAN and sometimes Output is NAN. What is NAN? I am entering floating point number, then y NAN is entered?
I have this homework where i am implementing a code which does error checking
so basically i have 3 variables and i assigned them as integer. so my error check is asking that if i type a value for instance 1.2 it should output "X"...
I am having trouble understanding the mantissa of a floating point number. I have divided up the floating point number into the sign bit, the exponent and the mantissa, I have found the exponent, but I am not sure what to do with the mantissa? From what I have gathered so far i divide the mantissa by ten until I get a number between 1 and 10. after that i convert the number into a decimal with everything after the decimal point (or radix) being a fractional number. But when I do that on paper I dont get my intended number. How do i put the exponent and mantissa together to make a decimal from my floating point?
ex. input is 00111010000111111111011000001000 sign is 0 exponent is 01110100 which is 64+32+16+4-127=-11 mantissa is 00111111111011000001000 which would be 1.11111111011000001
When i convert that i get 1.99756622314 i dont know what to do with the -11 exponent and the answer i want is 6.1e-4
I'm trying to convert 4 hex register into floating point value using IEEE 754 floating point format. My device will reply 4 register value. The problem is that it always reply for example 0x10 as 10 when i use getc() hence using char variable to store it is not ideal.
Write a function named "sum" that takes as its arguments the following:
(1) an array of floating point values. (2) an integer that tells how many floating point values are in the array.
The function should return as its value the sum of the floating point values. For example, if the array that's passed to the function looks like this: 0 1 2 3 4 5.8|2.6|9.0|3.4|7.1
The function should return the value as 27.9 as its value...I know I need to declare and intialize the array in the main function. I don't how to write the sum function.
I'm currently trying to learn about floating point representation in depth, so I played around a bit. While doing so, I stumbled on some strange behaviour; I can't really work out what's happening...
#include <iostream> #include <cmath> using namespace std; int main(){ float minVal = pow(2,-149); // set to smallest float possible float nextCheck = ((float)((minVal/2.0f))); // divide by two
[Code] ....
Essentially what's happening is: - I set minVal to be the smallest float that can be represented using single precision - Dividing by 2 should yield 0 -- we're at the minimum - Indeed, isZero2 does return true, but isZero returns false.
What's going on -- I would have thought them to be identical? Is the compiler trying to be clever, saying that dividing any number cannot possibly yield zero?
Suppose I wanted to check if a given floating point value is within a certain range. What would be Your recommended approach when considering simplicity, speed, portability, etc.?
Function is not returning a decimal point value. Here is my function
int meanValueFunction(vector<int> arrayValues){ int sum = 0; sum = sumFunction(arrayValues); float meanValue = sum/arrayValues.size(); cout<< meanValue << endl; return meanValue;
I want result in decimal point i.e 27.2 for the values (2 4 20 10 100) but it returns 27 instead.
I need to implement a C++ program that asks the user for four floating-point numbers. The program should then calculate the average using two different functions, one value returning and one void. The program should output the average of the four numbers. For this program I need to use float instead of int for the types of variables. Below is a proto-type code that I am able to use to do this program.
#include <iostream> using namespace std; int sum(int,int);
I'm displaying a table of floating point numbers with setprecision(5). If the number is "1.25" it will display "1.2500" which is what I want. However, if the number is "0.25" it will display "0.25000"
How can I make numbers with a base number of zero display properly?