C :: How To Use Rounding Function
Jan 16, 2014
I want to round to the 2nd digit in a float. Such as 1.85458 to 1.85. How would i go about doing this. I currently keep rounding to the closest integer so the above would round to 2.
Code:
#include <stdio.h>
#include <math.h>
int main(void){
float n;
[Code] ....
View 4 Replies
ADVERTISEMENT
Oct 12, 2013
Why the roundf function is not successfully rounding my numbers.
Code:
#include <stdio.h>
#include <math.h>
//function declarations
void CelToFah (int ctemp, int ftemp);
[Code]....
it produces a correct number when called, but it is not a rounded number, the roundf((float)cel) does nothing. I get the same number with and without it.
View 6 Replies
View Related
Feb 4, 2014
I'm having trouble with rounding to the nearest cent in this program. It's the only thing that i need. The result i need is just off by one cent. I tried multiplying the interest amount by 100 and adding .5 then dividing all that by 100 but that just made it worse. Here's the code:
Code:
#include <stdio.h>
int main()
{
int count=0;
[Code]....
View 6 Replies
View Related
Apr 23, 2013
I have an assignment for uni which requires the program to ask the user to input a number in for a variable to use in later equations. The assignment specifies that if the number that is input into the program is not an interger that it needs to be rounded UP to the nearest interger. e.g. 2.5 = 3, 5.00001 = 6 etc. i have identified this variable using "int" which i know makes it an interger however it also always rounds the number DOWN to the nearest interger. I was just wondering what the best way to approach this problem was. The only idea i have is to put + 0.99999 at the end of this variable when it is worked out so that if it is not a whole number it will be raised above the next interger and then rounded down however this will not work if there is too many decimal places.
View 6 Replies
View Related
Mar 9, 2014
How do I round to the nearest half integer?
Example;
0 to 0.2 = 0
0.3 to 0.5 = 0.5
0.6-0.9 = 1
What's the formula for C++?
I found a way to do it in math but I want to know what I'd have to put in C++. It's (x*2+.5)remove decimal numbers and divide by 2. What do I put in place of "remove decimal"?
For ex; x = 8.4 using (x*2+.5)remove decimal then divide by 2.
8.4 * 2 = 16.8
16.8 + .5 = 17.3
17.3 - decimal = 17
17/2 = 8.5
I want to do something like;
cout << " Half number: << (x*2+.5)remove decimal/2 << endl;
What would I have to put in place of "remove decimal"?
View 4 Replies
View Related
Oct 4, 2014
Ok so I'm in a programming 1 class working with c++. I have the following assignment:
Write a C++ program that:
asks for and accepts the Percentage earned with as a double (i.e. 75.45)
rounds it to an integer (>= .5 rounds up, <.5 rounds down.)
prints the original Percentage and the corresponding Grade and Points exactly as shown below.
prints an error message for any input that is less than 0 or greater than 100.
For example, if user enters 89.4, the program prints out:
Percentage: 89.4% Grade: B Points: 3.00
You must use an if-else statement to do this in your program.
Use fixed and precision output manipulators (see Ch. 3) to display the values with the exact precision shown above.
IMPORTANT:
Each if statement condition should contain only one comparison! read this again
This means code that is similar to this is NOT okay: if (Percentage >= 80.00 && Percentage <90.00)
This code is not acceptable because the if statement condition above has two comparisons.
(Hint: If you order your if-else chain statements correctly, you will only need one comparison in each.)
I have the program working, but I'm pretty sure I'm not rounding how my professor would like it to. This is my code:
#include <iostream>
#include <cmath>
#include <iomanip>
[Code].....
So my issue here is the rounding, and then theres the converting the double percetnage to an integer. In my next assignment I have to write the program with a switch statement.
View 3 Replies
View Related
Jul 18, 2013
I have made a random number generator to create a number between 1000 and 9000, but I always get numbers with many 10s and units. I want to round them 100s.
Is there any way to do this?
View 4 Replies
View Related
Jul 28, 2012
Rounding a numerical figure up to the nearest hundred. E.G.:
256 >> 300
654 >> 700
15 >> 100
I would like to know the formula to enter into Visual Basic 2008. I'm making a calculator Program, and i need a function that rounds up to the nearest 100...
View 5 Replies
View Related
Oct 28, 2013
I made this dice simulator which basically throws the dice 1 million times and outputs the frequency and percentage average for each side (1 to 6).
Everything is working fine, except my averages (floats) seem to be rounding up, causing 4% being "unassigned" at the end of the million rolls. I'm outputting with a setprecision of 2, but I only get 0's and no fractional numbers.
View 5 Replies
View Related
Jul 23, 2013
How would I be able to round a number in multiples of another...
Let's say width is 150
And multiple to be 64...
I want 150 to become 128...
if it was 160 to become 192...
The width number will change and I want to covert it in multiples of the other number example 64... The minimum value will always be the multiple number used...
View 7 Replies
View Related
Jul 6, 2014
error says "cannot convert 'int*' to 'int' in function main()
and also
type mismatch in parameter in function sort(int,int)
Heres the code:
#include<iostream.h>
#include<conio.h>
void main() {
void sort(int,int);
clrscr();
[Code] .....
View 11 Replies
View Related
Feb 11, 2013
i need a function that will work for both dynamic and static implementations of a function to get the transverse of a matrix. so far, i have this
Code:
matrix transpose(matrix m)
{
int row, col;
row = m.com_dim;
col= m.row_dim;
}
[code]....
this works well with my static implementation, but when i try it in dynamic it gives me errors. the function has to be the same for both dynamic and static implementation
View 4 Replies
View Related
Jan 19, 2013
Ok so I am working on a game and I'm in the process of developing my Player class. Anyways, what I have is a keyboard function that is called in my main function to make a shape move.
void myKeyboardFunction(unsigned char key, int x, int y) {
switch ( key ) {
[Code].....
But when I try to call it, trying to copy my previous method,
glutKeyboardFunc(Player1.playerControls);
I get an error
error C3867: 'Player::playerControls': function call missing argument list; use '&Player::playerControls' to create a pointer to member
I get an error saying it can't convert parameters. I would just like to understand why the arguments become a problem when I make the function a member of my class, when the first method I used is so easy.
View 2 Replies
View Related
Mar 19, 2013
I searched the web for error: C3867... and the discussions where murky or obscure.
My code excerpt is:
#pragma once
#include <windows.h>
#include <stdlib.h>
#include <process.h>
void PutUpfrmIO(void *);
namespace WordParsor {
[Code] .....
I get the generic message:
error C3867: 'WordParsor::Form1::PutUpfrmIO': function call missing argument list; use '&WordParsor::Form1::PutUpfrmIO' to create a pointer to memberc:userskingc++wordparsorwordparsorForm1.h... and the suggestion fix generate another error.
One person suggested the gcroot<> object wrapper... but I do not know how to modify/declair the function or its argument type.
View 2 Replies
View Related
Nov 3, 2013
I have written a function that inserts and prints a binary function correctly.
For example a tree like this [URL] ..... would print like this
Code:
node: 10
node: 7
node: 6
node: 8
node: 9
[Code] ....
Here is my print function
Code:
void prt_tree(struct node *tree) {
if (tree == NULL) {
printf("Null Tree
");
return;
[Code] .....
Could I just make some adjustments to my function to reverse it? and if so, how?
View 2 Replies
View Related
Feb 12, 2014
I'm trying to pass 2 arrays into a void funtion, and return values to one function.
this is the the program I'm working with, after I'm done I have to split it into 3 files, a header, a main, and a separate cpp file for the functions to live in.
#include <iostream>
using namespace std;
void processArrary(int numberCount[], int Numbers[], int intnumberSize, int numberCountSize);
int main() {
int Scores[26] = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189};
int numberCount[8] = { 0 };
[code]...
The goal of this program is to separate and count the groups of numbers then output the amount of numbers in each group. Near as I can tell, everthing should work, but I'm getting all zeros to be displayed in each group.
View 6 Replies
View Related
Jun 9, 2013
The function is supposed to return value from the file in my main, but I am getting empty value. I am trying to get better with pointer. Right now just teaching myself.
right now the only way for this code to show value is when in put the putchar(*ps) inside my readfile function. I would like to readfile to return value and print in the main function.
Code:
#include <stdio.h>
char *readfile(char filename[]);
int main(int argc, char *argv[] ) {
[Code].....
View 4 Replies
View Related
Nov 10, 2014
Here is a sample of my question
class Base{
public:
int getNum();
private:
int numToGet;
}
class Derived: public Base {
public:
friend ostream& operator<<(ostream& output, const Derived &B);
[Code]...
View 1 Replies
View Related
Aug 20, 2013
I want to use one median function "selectfunction" to choose one of the 2 other functions at random to pass my 2-dim array to the selected function. There is a problem in the median function
#include <iostream>
#define random(x)(rand()%x) // for random number between numbers of 0 and 1
using namespace std;
void proc1 (int iArray[][2]);
void proc2 (int iArray[][2]);
void selectfunction(int iArray[][2]);
int A[4][2] = {{1, 2} , {3, 4} , { 5, 7} , {8, 1} };
[Code]...
View 1 Replies
View Related
Mar 26, 2014
i want to use a class to print data stored as vector or array with different data types. i also want the print function two take more than one vector or array or combination of both so that they can be written to file as two columns. so i wrote the following class:
right now it has only one member function for printing two vectors. later i'll add additional functions as required.
note: there has to be template functions inside the class
i also want the object to be global so that i need not pass it as an argument to other calling functions
class printdata
{
public:
template<typename T1,typename T2>
void SaveData( vector<T1> &data1,vector<T2> &data2, std::string var)
{
[Code]....
then i want to call this template function in another ordinary function written in a seperate cpp file
these function declarations are put in a header file. so i need know whether i should put the declaration of the template function in the header to use the function in different functions
View 4 Replies
View Related
Feb 19, 2014
Is this possible?
int myfunc( int a, int b, char * c )
char a = "(int)myfunc()";
char b = "(int,int,char*)"
call(a, b, ...) // Function name and return type, params
I want to do function what registers forward what will get callback if the time is right. Basically then i dont need to edit and add extra functions into source files. I just have to include header and use register forward function. If there is anything close to this it would be perfect!
View 5 Replies
View Related
Mar 26, 2014
i want to use a class to print data stored as vector or array with different data types.
i also want the print function two take more than one vector or array or combination of both so that they can be written to file as two columns.so i wrote the following class:
right now it has only one member function for printing two vectors. later i'll add additional functions as required.
note: there has to be template functions inside the class / i also want the object to be global so that i need not pass it as an argument to other calling functions
class printdata {
public:
template<typename T1,typename T2>
void SaveData( vector<T1> &data1,vector<T2> &data2, std::string var){
std::ofstream myfile;
std::string filename;
[code].....
then i want to call this template function in another ordinary function written in a seperate cpp file these function declarations are put in a header file. so i need know whether i should put the declaration of the template function in the header to use the function in different functions.
View 1 Replies
View Related
Apr 27, 2013
I need to do it to avoid calling a function of my process from injected code.
So would like to hook this function to check whether the call is from the current module or it is from an external module, then I compare the address of the instruction who did the call with the module address range.
View 1 Replies
View Related
Jun 27, 2014
I would like to have 2 functions. (FYI, I haven't even tested these because I don't have a compiler on this PC, so don't know what they'll do. I'm also new to C++, self-teaching.)
My question is, I'm sure that oFile should be type object (of some sort), not int, but I'm not sure how to reference it correctly so that it passes from FileOpen to main to FileClose.
Code:
#include <iostream> //I/O
using namespace std;
#include <fstream> //files
using namespace ios;
int FileOpen(string fileName) {
ifstream oFile (fileName); //attempt to open file
[Code] ....
View 1 Replies
View Related
Nov 5, 2014
I need a function like strncpy; but, want to be sure the destination ends with a ASCII nul char.
So, I wonder what name is used by others in this case.
I have found strlcpy and it looks like a good name choice. [URL] ....
View 5 Replies
View Related
Dec 26, 2014
I am writing a program in which a Fucntion has to be wriiten to parse the Command Line . When I include Code for parsing in main fuction iteslf ,its run ok . But I want to make a fucntion of that code and call it from main ,than it show Segmentation error .
By using Debugging I found Some thing is mess with " -m" Parameter of Command line , But Cant Rectify it ..
Code:
int main (int argc, char *argv[]){
//get_parameter_value(argc,argv);
// buffer[packet_size+1]= char ("'");
while (argc > 1) {
if (argv[h][0] == '-')
[Code] .....
View 3 Replies
View Related