Saturday, May 22, 2010

How do I round money to the nearest cent in C programming?

I'm writing a program in C (NOT C ++) to convert money and I need to round U.S. currency to the nearest cent. How do I round money to the nearest cent?





Ex: $0.501 rounds to $0.50,


$0.505 rounds to $0.51,


$1.006 rounds to $1.01,


etc.

How do I round money to the nearest cent in C programming?
As far as I remember, C does not support a Currency data type and so you will have to use Float / Double variables to do this. You can use "round()" method by including %26lt;math.h%26gt; file.





A nice example at - http://www.codecogs.com/reference/math.h...
Reply:read each number as a char array.


compare last digit with five


if greater than five add 1 to previous character.





then print array n-1 element


How can i read a single digit from keyboard in C programming language?

Write a C program that reads a single digit from the keyboard and writes it out n times where n is the number read in. For example, the user enters the number 6, then the number 6 should be printed out to the screen 6 times.





Try to enter invalid input, e.g. a character instead of a number. What happens?





Amend the program to validate the input from the user and display appropriate error messages.

How can i read a single digit from keyboard in C programming language?
a = scanf();


for( i=0;i%26lt;5;i++){


print(a);


}
Reply:I'm not gonna do your homework for you.


Pusha and Pop arrays. Need crtique on stuff I have. C programming?

Write a program that implements a stack with at most 30 integer elements with operations push and pop as defined


below:


A. Declare an integer array with a size of 30 elements.


B. Write a function called push that will insert an integer into the array.


C. Write a function called pop that removes the top-most element in the array.


D. Write a main function that takes input from the user based on integers: 1 - PUSH; 2 - POP; 3 - EXIT. Whenever


the user enters 1 (for PUSH), the integer that follows it is the element to be pushed.


E. The program prints as output the elements of the stack at the end of input and prints the maximum size to


which the stack grew during the operation.





#define value 30


void push(int value)


{


stack[i]=value;


i++;


}


int pop()


{


int temp=stack[i];


stack[i]=0;


i--;


return temp;


}

Pusha and Pop arrays. Need crtique on stuff I have. C programming?
when i=0, u need to give a print statement " underflow "
Reply:What happens when i = 0 and you pop?


How do u call one exe from another exe in C programming?

for example i want to open notepad.exe from my C program


I use turboc and dev C++

How do u call one exe from another exe in C programming?
int ret = system("notepad");





I've added the ret assignment because sometime programs may result in errors.





If ret==0 then no error.





For notepad you could just use





system("notepad");
Reply:use the system() function:





#include %26lt;stdlib.h%26gt;


int main()


{


system("program.exe");


}





but this does not return the control back.





if you want the control back then try the spawn functions from


process.h.


[I don't know whether this is standard function or not. I think you can verify it by yourself. :) ]

peacock plant

What is the effect using global variables in c programming?

What can happen in a big c program when we use global variables even though we can use local variables instead of global variables?

What is the effect using global variables in c programming?
In a big program with lots of different source files it might happen that you use the same variable name for different variables.





If you are lucky your linker will warn you about this but more likely it will just merge those two variables together.





The effect is that other parts of the program mysteriously overwrite "your" variable.
Reply:You should only use Global variables if only required.


once you declare the variable as global its value persists throught the scope of the program.





But if you overwrite the same variable locally the local variable will persist.


suppose,


main()


{


int x;


x = 100;


sum(2,3);


printf("%d",x);


}


function sum(int a,int b)


{


int x;


x = a + b;


printf("%d",x);


}








When you execute the above


The x inside the function gives 5 and in the main program gives 100.


so the output will be 5 and then 100;
Reply:well, it sorta messy
Reply:They tell you to avoid using global variable as much as you can because it is GLOBAL. It is accessible by any and every portion of your code and this can lead to some sticky situations when it comes to debugging.





Note to bhiravi k:





The x declared in main() is not a global variable. Global variable in C is declared before the main().
Reply:It's just bad practice. The problem with global variables is that they can be accessed and altered by any function, method, or object in your program. You should set scope to your variables to prevent unwanted parts of the function for changing them and to make it easier to debug and track problems.


How do you make rand more random in c programming?

i have a snake program in c and i want to make the food move randomly,there is 3 food moving at the same time one step at a time.I use a call function for the movement of the food.





int movement1()


{


return ((rand () % 4));


}





and another for each of the food.the problem is that all of them moves to the same direction and does'nt change the value for the every loop made.


*I used the function in the loop...

How do you make rand more random in c programming?
it is so becuase it has got same seed for all time use time(0) as seed and you will get fully random output from rand





for help see srand(int) and rand() in your programming manual
Reply:Simple example:





#include %26lt;iostream.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;time.h%26gt;





int main()


{


int random;


time_t seconds;


time(%26amp;seconds);





srand((unsigned int) seconds);





for(int i=0; i%26lt;100; i++){


random = rand() % 4;


cout%26lt;%26lt; random %26lt;%26lt; endl;


}


return 0;


}





This loops 100 times to give random integers between 0 and 3.





Rin this program several times and you will see different numbers are always generated.
Reply:NAME


rand, srand - random number generator.





SYNOPSIS


#include %26lt;stdlib.h%26gt;





int rand(void);





void srand(unsigned int seed);





DESCRIPTION


The rand() function returns a pseudo-random integer


between 0 and RAND_MAX.





The srand() function sets its argument as the seed for a


new sequence of pseudo-random integers to be returned by


rand(). These sequences are repeatable by calling srand()


with the same seed value.





If no seed value is provided, the rand() function is auto­


matically seeded with a value of 1.


--------------------------------------...


NAME


time - get time in seconds





SYNOPSIS


#include %26lt;time.h%26gt;





time_t time(time_t *t);





DESCRIPTION


time returns the time since the Epoch (00:00:00 UTC, Jan­


uary 1, 1970), measured in seconds.





If t is non-NULL, the return value is also stored in the


memory pointed to by t.





--------------------------------------...


NAME


ftime - return date and time





SYNOPSIS


#include %26lt;sys/timeb.h%26gt;





int ftime(struct timeb *tp);





DESCRIPTION


Return current date and time in tp, which is declared as


following:





struct timeb {


time_t time;


unsigned short millitm;


short timezone;


short dstflag;


};





The structure contains the time since the epoch in sec­


onds, up to 1000 milliseconds of more-precise interval,


the local time zone (measured in minutes of time westward


from Greenwich), and a flag that, if nonzero, indicates


that Daylight Saving time applies locally during the


appropriate part of the year.





--------------------------------------...


Call the function below before generating random numbers





#include %26lt;sys/timeb.h%26gt;





void initialize_random_numbers() /* call this first */


{


struct timeb tbuf;


ftime(%26amp;tbuf);


/* add milliseconds to make it more random */


srand(tbuf.time + tbuf.millitm);


}





then call your movement1() function.


Is it possible to get data from Excel when programming in C?

I need to come up with a C program that will use data that have been input in an Excel file and the program must output the results to the Excel file. Is this possible? How? Where can I get more information?

Is it possible to get data from Excel when programming in C?
The easiest way to communicate with an Excel data file is to save it as a .csv file. This is a comma-separated, text version of your Excel data. In an Excel file, try a 'save-as' and choose .csv (comma delimited) file. Then (after closing the file) take a look at what you see by opening that file with wordpad or any text editor. Now you can decide how to label your rows/columns and create your 'c' program appropriately. In 'C' you'll need to be comfortable with file i/o and working with strings to do this sort of thing.





Note that you can have a 'C' program output in this format and open the file with Excel. If you need more info on csv files, check the wikipedia link below. Good luck.
Reply:i have no idea for accessing the excel file from the c program. i think that it will be impossible since excel might have its own method of creating and accessing the file values that we dont know and Microsoft will not share outside unlike opensource community. u can better create a database and access it through linking the program to database and getting the values through querying it. this will make ur program simple and make ur productivity easy since u can concentrate ur attention on program development rather than managing data.