C :: Float Setting Decimal Places

Jun 27, 2013

I am trying to find out when using float in a calculation how to set the number of decimal places. For example my code below

Code:
#include stdio.h>
int main()
{
float x=123.0;

[Code]....

This returns an answer 8487.0000 I would like it not to show all the decimal places. However if the sum has decimal places I would like to select the number of decimal places shown.

View 3 Replies


ADVERTISEMENT

C :: User Defined Number Of Decimal Places?

Sep 2, 2013

I need the user to be able to input the number of decimal places they wish to have displayed in the output. Everything works fine as is, I just don't know how to allow for the user to input the number of decimal places they want the output to have.

Code:

#include<stdio.h>
#include<math.h>
#define PI 3.141592654
int main(void) {
//Local Declarations
int x; //desired number of decimal places
float radius; //radius of circle
float circumference; //circumference of circle

[Code] .....

View 2 Replies View Related

C++ :: Create A Converter But Results Must Be In 4 Decimal Places

Dec 1, 2013

I have to create a converter but the results must be in 4 decimal places just like, if i Entered 5000mm the result would be 500.0000cm . what should I do ? I used Float function

View 1 Replies View Related

C++ :: Getting 2 Decimal Places In Output Text File?

Jul 30, 2014

how I can make my output only have 2 decimal places because I want it to be a monetary value. In my code I used this:

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

which worked well at first because on my command prompt screen the numbers had two decimal places and looked liked this:

Premium for Pol1 is $14101.62
Premium for Pol2 is $14221.16
Premium for Pol3 is $582390.50
Premium for Pol4 is $220384.49

However, I also have the program outputting to a textfile called "output.txt" and when I opened this text file the numbers only had one or no decimal places.

Premium for Pol1 is $14101.6
Premium for Pol2 is $14221.2
Premium for Pol3 is $582391
Premium for Pol4 is $220384

How come the code for two decimal places is working for my output to command prompt but not my output to the text file?

Here is my code.

double ratesmn[86] = {
#include "MaleNonSmoker.txt"
- 1
};

[Code].....

View 2 Replies View Related

C :: Inventory Program - Entering Amounts With Two Decimal Places

Jun 7, 2013

So my inventory program runs fine as far as I can tell. Except when entering "Amounts" with more that 1-precision . ie

Amount Entered : 25.99 // Problem
Amount Entered : 25.9 // No Problem

When entering say 25.99, the rest of the file is jibberish. but 25.9, the rest of the file is fine

Code:
#include <stdio.h>
typedef struct toolRecord{
int record_num;
char tool_name[16];
int quantity;
double price;

[Code] .....

View 5 Replies View Related

C :: Program To Generate A List Of Powers Of 3 - Decimal Places?

Sep 23, 2014

So my assignment requires us to write a C program to generate a list of powers of 3 where each output line lists three numbers: k 3k 3‐k

It should look like this (just pretend all the numbers are lined up in their respective columns)

1 0 1.0
3 1 0.333333343
9 2 0.111111112
27 3 0.037037037
81 4 0.012345679
243 5 0.004115226
729 6 0.001371742
2187 7 0.000457247
6561 8 0.000152416
19683 9 0.000050805

This is what I keep getting:

1 0 1.000000000
3 1 0.333333343
9 2 0.111111112
27 3 0.037037037
81 4 0.012345679
243 5 0.004115226
729 6 0.001371742
2187 7 0.000457247
6561 8 0.000152416
19683 9 0.000050805

My input:

Code:
#include <stdio.h>
#include <stdlib.h>
void powers(void) {
int i=0;
int n=9;
int x=1;
float h=1;

[Code] .....

Notice how I'm getting 1.000000000 for my first h value in my output whereas I should be getting 1.0. What am I doing wrong?

View 6 Replies View Related

C++ :: Nested Selection Statements - Format Output Into Two Decimal Places

Apr 19, 2014

event................. red house.............. blue house........... yellow house
Chess ......................12.................... 5....................................8
TableTennis .................. 4.......................... 11.....................17
Basket Ball .................... 6......................... 5......................14
karathe ..........................5........................... 8.........................10

Write a program to input the house name, and the score (as shown in the table) of the sport event obtained by the corresponding house. Then find the total score . You should use nested selection statements.

i) If the user inputs a wrong house name, display the error message "In valid House Name".

ii) Modify the program to handle many data. After calculate and disp ay the total score of the house, the program should display a prompt "Do you need to enter nother house name?".

If the user inputs "y" or "Y", program should ask for the next house name and the scores.
If the user inputs "n" or "N" after entering all three houses, program should terminate by printing the score of the winner.
If the user inputs "n" or "N" and without entering all three houses, program should display an error message "Error in finding the winner" and terminate without printing the ranks.

Note: format the output into two decimal places.

Sample output:
Enter the house name: red house
Enter the score of Chess: 12
Enter the score of Table Tennis:4
Enter the score of Basket Ball:6
Enter the score of karathe 5
Score is: 27 Points
Do you enter another house name? y

Enter the house name: blue house
Enter the score of Chess:5
Enter the score of Table Tennis:ll
Enter the score of Basket Ball:5
Enter the score of karathe 8
Score is: 29 Points
Do you enter another house name? y

Enter the house name: yellow house
Enter the score of Chess:8
Enter the score of Table Tennis: 17
Enter the score of Basket Ball: 14
Enter the score of karathe 10
Score is: 49 Points
Do you enter another house name? n
The winner scored: 49 Points

View 3 Replies View Related

C :: Iteration Bound Determination - Get Result Correct Upto N Decimal Places

Mar 26, 2013

suppose I want to find the value of e ,e^x or sin(x) or any such function in general ,through their infinite series (maclaurin or taylor) how many iteration should I have to make to get the result correct uptu 'n' decimal places?

View 6 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 :: Possible To Print Float Values Without Decimal Point?

Apr 8, 2013

I was going through the exercises in one C programming ebook.There is this question which asks me to print a float variable in fixed decimal notation, field size of 6, right-justified, no digits after decimal point.I got printf("%6f", x );

x = float variable.

But the above code prints numbers after the decimal point, so I changed it to %d. But %d doesn't work with float variables..

View 2 Replies View Related

C++ :: Remove Decimal Point From Float Number?

Dec 16, 2013

What would be the best way to remove the decimal point from a float number? For instance if I have a float number .2546 I would like to be able to remove the dot and use just the number (int) 2546 for some calculations.

Maybe, convert to string than remove the first character from the string than convert the string back to an int?

View 2 Replies View Related

C++ :: Turning A Limited Float Into Another Float?

Nov 20, 2013

I can do the folowing:

float var1 ;
var1 = 9.12345 ;
printf("%.2f",var1) ;

the output will be 9.12. What if I wanted to save that as another separate float with displaying it on screen?

View 1 Replies View Related

C/C++ :: Cannot Convert Float To Float Assignment

Jun 8, 2014

#include <iostream>
#include <string.h>
#include <sstream>

[Code]....

View 1 Replies View Related

C/C++ :: Rotate Array By 2 Places?

Jul 25, 2012

input: array[] = {0,1,2,3,4};
output should be {3,4,0,1,2};

View 1 Replies View Related

C Sharp :: Adding 3 Decimals Places?

Apr 24, 2012

i wanted to add 3 decimals places to either string or int.

let say

scenario 1:

a = 8
to
a = 8.000
b = 11.6
to
b= 11.600
c= 34.55
to
c= 34.550

View 1 Replies View Related

C++ :: How To Write A Program To Calculate Pi To N Number Of Places

Aug 13, 2014

I assume floating point numbers in C++ have a default maximum of 5 decimal places. You can use setprecision() but is this limitless?

So how would find say write a program to calculate Pi to N number of places?

View 3 Replies View Related

C++ :: Setting Array To Int With Value

Mar 1, 2013

Lets say i have an array with the values 1, 5, 9, and 3. is there anyway to make this so i can have an int with the value 1593 based on those numbers in the array?

View 8 Replies View Related

C++ :: Setting Limit On Cin?

Feb 22, 2013

I want to set limit on cin for example

int i;
cout<<"Please enter 4 digits id: ";
cin>>i

If user enter more then 4 digits it must give an error

View 5 Replies View Related

C :: Setting Values To Pointers

Apr 5, 2014

This function below takes a pointer as an argument. What I expect to happen is, since expr++ has higher precedence than *expr, that is, the primary expression operators have higher precedence than the unary operators, pointer arithmetic should occur where we increment to the second address pointed to by dbuf, and then we should dereference the value at that address. Given that logiv, when i print dbuf[3] it should print the value pointed to at the 4th address in dbuf. However, the value it returns is 0x0 not 0x3. Why doesn't it dereference the value 0x3?

Code: void dfill(unsigned char *dbuf)
{
dbuf = (unsigned char*)malloc(4);
memset(dbuf, 0, 4);
*dbuf = 0x0;
*dbuf++ = 0x1;
*dbuf++ = 0x2;
*dbuf++ = 0x3;
printf("dbuf val: 0x%x
", dbuf[3]);
}

View 1 Replies View Related

C :: Setting Flags In 32-bit Integer?

Oct 18, 2013

I am trying to create the lparam for the WM_KEYDOWN message. I know this is the C programming forum, but my code is standard enough to apply here. According to Microsoft, the lparam must be formatted like this : lParam The repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown following. Bits Meaning 0-15 The repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent.

However, the repeat count is not cumulative. 16-23 The scan code. The value depends on the OEM. 24 Indicates whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0. 25-28 Reserved; do not use. 29 The context code. The value is always 0 for a WM_KEYDOWN message. 30 The previous key state.

The value is 1 if the key is down before the message is sent, or it is zero if the key is up. 31 The transition state. The value is always 0 for a WM_KEYDOWN message. ( WM_KEYDOWN message (Windows) )^ I'm troubled by setting it into the 32-bit value, and I get confused about what exactly happens when the logical or and the bitshift operators are used. I tried to use them below in my code by looking at Stack Overflow for setting a bit. I also don't know how to test for the endianess of my system, and how to handle it if it's big endian or little endian. Here is my code so far :

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

[Code].....

View 2 Replies View Related

C :: Setting 1D Pointer To Array

Oct 27, 2013

I have the following situation:

Code:
void myFun(float *pfMyPtr) {
float Val[] = {0.234, 0,432, 0.322, 0762, 0.984};
pfMyPtr = Val;
}
int main() {
float *pfPtr;
pfPtr = (float*) calloc (5,sizeof(float));
myFun(pfPtr);
}

I would like pfPtr to contain the values of array Val. What am I missing here?

View 5 Replies View Related

C++ ::  Setting A Function To A Variable?

Oct 29, 2014

I need to set a function to a variable of some kind. Then later in the program it needs to run the function that is set to the variable. The variable doesn't need to change after it is set to a function, it just needs to be able to be set to a function. So maybe I don't need a variable? What do I do? :3 Is this even possible? :o

Example:
if (PosRampYes == 0)
{
SomeVariableOrSomething = FirstFunction();
}
else
{
SomeVariableOrSomething = SecondFunction();
}
//later:
SomeVariableOrSomething; //so if PosRampYes is set to 0 then this line would run FirstFunction()

View 2 Replies View Related

C++ :: Setting Variables For Text

Oct 8, 2013

I am trying to find a quicker way of setting the variables for text. here is my code:

sf::Text set_text_values(sf::Text text, sf::Font font) {
text.setFont(font);
text.setCharacterSize(50);
text.setColor(sf::Color::White);
return text;

[Code] ....

And I have tried it like this

void set_text_values(sf::Text text, sf::Font font) {
text.setFont(font);
text.setCharacterSize(50);
text.setColor(sf::Color::White);

[Code] ....

The code compiles and runs but it won't show the text on the screen like it did when i did it manually like this:

int main() {
Hull.setFont(font);
Hull.setCharacterSize(50);
Hull.setColor(sf::Color::White);
}

What is wrong with it?

View 2 Replies View Related

C++ :: Setting All Elements Of Array To 0

Sep 18, 2013

I'm trying to set all the elements of my array to 0 but nothing seems to be working.

.h
#ifndef ServerGroup_h
#define ServerGroup_h
class ServerGroup {
public:
ServerGroup(const int&);

[Code] .....

I want to set each element of the array in servers to 0 based on what is passed into size by numArray.

View 9 Replies View Related

C++ :: Setting The Value Of A Pointer From Function

Feb 20, 2013

I declared a pointer in main with value 0, so I want to change its value so that it points to other variable from a function, I guess the function creates a copy of my pointer that's why whatever I do within function doesn't change the real direction of the pointer in main. I've been trying something like this:

#include <stdio.h>
void redirectionate(char *str, char *ptrCopy);
int main()
{

[Code]....

View 7 Replies View Related

C/C++ :: Setting Constants As Attribute?

Apr 3, 2015

I would like to know how can i set a constant attribute in the constructor. This attribute is an int value that cannot be changed.

For instance:

class Test {
public:
const int x;
public:
Test(const int val);

[code].....

With this code i get compile error!

View 5 Replies View Related







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