Illegal use of floating point - c

I'm trying to write a code that calculates monthly pay for a project.
This is the formula I was given:
(Rate + Rate/((1+Rate)^Months)-1) * Principle
Rate according to this formula is Rate/1200 so as an example if the rate is 7% it would be 7/1200 which is 0.00583333333. I'm trying to get the exact number 0.00583333333 into my program but then I get the error "illegal use of floating point".
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
float r;
int m, y;
int p;
//int mp;
printf("Enter Rate: ");
scanf("%d", &r);
r = r%1200;
printf("Enter number of years: ");
scanf("%d", &y);
m = y*12;
printf("%.10lf\n",r);
printf("%d",m);
return 0;
}
How do I get 0.00583333333 to be a part of my calculation in the program?

try to change scanf("%d", &r); by scanf("%f", &r); and r = r%1200 by r = r/1200

Related

During compilation it is not asking for user input of rate and time

// Q. Find out the simple interest.
#include <stdio.h>
#include <math.h>
int main()
{
int principal, rate, time;
printf("principal\n");
scanf("% d", &principal);
printf("rate\n");
scanf("% d", &rate);
printf("time\n");
scanf("% d", &time);
printf("Simple interest is %d", principal * rate * time / 100);
return 0;
}
For scanf to work, you need to remove the space between the % and the d in the format strings: % d.
#include <stdio.h>
#include <math.h>
int main()
{
int principal, rate, time;
printf("principal\n");
scanf("%d", &principal);
printf("rate\n");
scanf("%d", &rate);
printf("time\n");
scanf("%d", &time);
printf("Simple interest is %d", principal * rate * time / 100);
return 0;
}
Also, the program cannot ask for input during compilation. It will only ask for input when it is run.

Why is this code not producing an output?

Im writing a simple program to show the distance/time between two converging trains. I wanted to test the program and return output value through the function float converge, and then apply that to the main function, but it seems that the converge function does not work.
#include <stdio.h>
float converge(int x, int y, int z) {
return x / (y + z);
}
int main(void) {
int dist, speed1, speed2;
printf("Enter distance in miles");
scanf("%d\n", &dist);
printf("Enter train 1 speed and train 2 speed in mph");
scanf("%d%d\n", &speed1, &speed2);
converge(dist, speed1, speed2);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
float converge (float x, float y, float z)
{
int time=x/(y+z);
return time;
}
int main ()
{
float dist, speed1, speed2;
printf("Enter distance in miles:\t");
scanf("%f", &dist);
printf("Enter speed of first train in mph:\t");
scanf("%f", &speed1);
printf("Enter speed of second train in mph:\t");
scanf("%f", &speed2);
printf("Time between this two trains is %f",converge(dist, speed1, speed2));
}
There are multiple problems in your code:
converge performs integer arithmetic and converts the result to float only for the return value. If you want to compute a fractional number, you should change it to: double converge(int x, int y, int z) { return (double)x / ((double)y + z); } or better use double for the input values and the argument types:
double converge(double x, double y, double z) { return x / (y + z); }
There are trailing newlines in the scanf() conversion formats: this will cause scanf() to consume any trailing white space typed after the numbers, including any number of newlines typed at the prompts. You will not get the second prompt as long as you enter empty lines. Remove these \n from the format strings.
The result of the computation is not printed.
Here is a modified version:
#include <stdio.h>
double converge(double x, double y, double z) {
return x / (y + z);
}
int main(void) {
double dist = 0, speed1 = 0, speed2 = 0;
printf("Enter distance in miles: ");
scanf("%lf", &dist);
printf("Enter train 1 speed and train 2 speeds in mph: ");
scanf("%lf%lf", &speed1, &speed2);
if (speed1 + speed2 <= 0)
printf("No collision\n");
else
printf("Time until collision: %f seconds\", 3600 * converge(dist, speed1, speed2));
return 0;
}
Why is this code not producing an output?
It produces no output for the expected output from the result of converge() because there is no statement in the provided code, which could cause this output.
You need for example one printf() statement after the call to converge() in order to print the result of converge():
#include <stdio.h>
float converge (int x, int y, int z)
{
return x/(y+z);
}
int main (void)
{
int dist, speed1, speed2;
float converge_result;
printf("Enter the distance between the two trains in miles:\n");
scanf("%d", &dist);
printf("\n");
printf("Enter the speed of train 1 and the speed of train 2 in mph:\n");
scanf("%d %d", &speed1,&speed2);
printf("\n");
converge_result = converge(dist, speed1, speed2);
printf("The time until the two trains encounter each other is:\n %f",converge_result);
return 0;
}
or alternatively:
#include <stdio.h>
float converge (int x, int y, int z)
{
return x/(y+z);
}
int main (void)
{
int dist, speed1, speed2;
printf("Enter the distance between the two trains in miles:\n");
scanf("%d", &dist);
printf("\n");
printf("Enter the speed of train 1 and the speed of train 2 in mph:\n");
scanf("%d %d", &speed1,&speed2);
printf("\n");
printf("The time until the two trains encounter each other is: \n%f ",
converge(dist,speed1,speed2);
return 0;
}
By the way, the calculation of the distance in time seems incorrect or at least incomplete.

Turbo C gives Output as 0.000000

I have set the correct directory paths for Turbo C. But yet it gives the output as 0.000000
Following is the program:
#include <conio.h>
#include <math.h>
#include <stdio.h>
void main() {
int n;
float r, si, ci, p;
clrscr();
printf("enter principle amount\n");
scanf("%f", &p);
printf("enter rate of interest\n");
scanf("%d", &r);
printf("enter number of years\n");
scanf("%f", &n);
si = p * n * r / 100;
ci = p * (pow((1 + (r / 100)), n) - 1);
printf("simple interest=%f\n", si);
printf("compound interest=%f", ci);
getch();
}
It is supposed to give numbers instead of 0.000000
Any help?
Change:
scanf("%f",&n);
to:
scanf("%d",&n);
since n is an integer, not a float, as suggested in the comments already.
For r, which is of type float, you should use scanf("%f",&r);.
PS: Consider using a modern compiler, such as GCC.

Problems with a program [C arrays]

My friend and I are trying to build a program together, but it just doesn't seem to be working. Neither of us have much experience with C, so we just can't spot the issue... Any advice or help would be much appreciated!
Apologies for the slightly awkward lyrics?
[Edit] The problem is that when we input values, we get ridiculous figures like 4586368.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
void main()
{
int room[20] = {};
int i;
int rooms = 0;
char option = 0;
int lights = 0;
int hrsUsed = 0;
int Telly = 0;
int TVWatt =0;
int sumTV;
int TVuse = 0;
int Computer = 0;
int compWatt = 0;
int compUsed = 0;
int compTotal;
int kwH_lights;
int fridge = 0;
int washLoad = 0;
int dryerLoad = 0, dishLoad = 0, cookLoad = 0;
int showeruse = 0;
int total_kWh;
printf("Enter number of rooms");
scanf_s("%d", &rooms);
for(i=0;i<rooms;i++)
{
printf("input average wattage of lights");
scanf_s("%d", &lights);
lights=lights/1000;
printf("input number of hours use/day (average)");
scanf_s("%d", &hrsUsed);
kwH_lights=((lights*hrsUsed)*365);
printf("input number of TVs");
scanf_s("%d", &Telly);
printf("input average wattage");
scanf_s("%d", &TVWatt);
printf("input average use a day");
scanf_s("%d", &TVuse);
sumTV=((Telly*(TVWatt/1000))*TVuse)*365;
}
printf("Input number of fridge/freezer");
scanf_s("%d",&fridge);
fridge=(fridge*2)*365;
printf("input number of Computers and/or video game consoles in the house");
scanf_s("%d", &Computer);
for(i=0;i<Computer;i++) {
printf("input wattage");
scanf_s("%d", &compWatt);
printf("input average hrs used/day");
scanf_s("%d", &compUsed);
compTotal=((compWatt/1000)*compUsed)*365;
}
printf("Input average number of washing machine loads /day");
scanf_s("%d",&washLoad);
washLoad=washLoad*365;
printf("Input average number of clothes dryer loads/day");
scanf_s("%d",&dryerLoad);
dryerLoad=(dryerLoad*3)*365;
printf("Input average number of dishwasher loads/day");
scanf_s("%d",&dishLoad);
dishLoad=(dishLoad*1.5)*365;
printf("Input average cooking load/day");
scanf_s("%d",&cookLoad);
cookLoad=(cookLoad*7)*365;
printf("Input average hrs/day of shower usage");
scanf_s("%d",&showeruse);
showeruse=(showeruse*7)*365;
total_kWh=((kwH_lights)+(sumTV)+(fridge)+(compTotal)+(dryerLoad)+(dishLoad)+(cookLoad)+(showeruse));
printf("Total= %d", &total_kWh);
}
You should change this:
printf("Total= %d", &total_kWh);
to that:
printf("Total= %d", total_kWh);
Same is true for all your other integer variables.
There were quite a few mistakes in your code:
you printed the memory-address instead of result value (don't use & with printf if your variable is a plain int)
the computer for-loop had no curly brackets (so only the printf statement was looped)
results were not summed up (in all loops you've just overwritten your inputs from the last loop)
the rooms[] Array was never used - a few other variables also (possible error source, if you wanted to use them and just forgot it)
the result from a multiplication with 1.5 will hold a double value - you should cast that back to int (dishLoad)
The bold mistake is probably that one, why your values were wrong...
Also notice: The 'average number of washing machine loads/clothes dryer loads/ dishwasher loads' should better be asked by week or month... Or should hold Floating Point values: Because everyone I know don't use the washing machine and clothes dryer every day multiple times. So now you can't enter something like once a week (which would be an factor of 0.14, but is not enterable cause all values are stored as int).
Here Comes the code with everything fixed, I could found:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
int main(int argc, char** argv){
int i = 0;
int rooms = 0;
int lights = 0;
int hrsUsed = 0;
int Telly = 0;
int TVWatt =0;
int sumTV = 0;
int TVuse = 0;
int Computer = 0;
int compWatt = 0;
int compUsed = 0;
int compTotal= 0;
int kwH_lights = 0;
int fridge = 0;
int washLoad = 0;
int dryerLoad = 0, dishLoad = 0, cookLoad = 0;
int showeruse = 0;
int total_kWh = 0;
printf("Enter number of rooms: ");
scanf_s("%d", &rooms);
for(i=0;i<rooms;i++){
printf("A few questions about room %d\n", i+1);
printf("input average wattage of lights: ");
scanf_s("%d", &lights);
lights+=lights/1000;
printf("input number of hours use/day (average): ");
scanf_s("%d", &hrsUsed);
kwH_lights+=((lights*hrsUsed)*365);
printf("input number of TVs: ");
scanf_s("%d", &Telly);
printf("input average wattage: ");
scanf_s("%d", &TVWatt);
printf("input average use a day: ");
scanf_s("%d", &TVuse);
sumTV+=((Telly*(TVWatt/1000))*TVuse)*365;
}
printf("Input number of fridge/freezer: ");
scanf_s("%d",&fridge);
fridge=(fridge*2)*365;
printf("input number of Computers and/or video game consoles in the house: ");
scanf_s("%d", &Computer);
for(i=0;i<Computer;i++){
printf("A few questions about computer %d\n", i+1);
printf("input wattage: ");
scanf_s("%d", &compWatt);
printf("input average hrs used/day: ");
scanf_s("%d", &compUsed);
compTotal += ((compWatt/1000)*compUsed)*365;
}
printf("Input average number of washing machine loads/day: ");
scanf_s("%d",&washLoad);
washLoad=washLoad*365;
printf("Input average number of clothes dryer loads/day: ");
scanf_s("%d",&dryerLoad);
dryerLoad=(dryerLoad*3)*365;
printf("Input average number of dishwasher loads/day: ");
scanf_s("%d",&dishLoad);
dishLoad=(int)((dishLoad*1.5)*365);
printf("Input average cooking load/day: ");
scanf_s("%d",&cookLoad);
cookLoad=(cookLoad*7)*365;
printf("Input average hrs/day of shower usage: ");
scanf_s("%d",&showeruse);
showeruse=(showeruse*7)*365;
total_kWh=((kwH_lights)+(sumTV)+(fridge)+(compTotal)+(dryerLoad)+(dishLoad)+(cookLoad)+(showeruse));
printf("Total= %d\n", total_kWh);
system("Pause");
return 0;
}
I hope it helps you out - if you got any questions left, feel free to ask.
My first step would be to correct the second for loop { } ... fix this and ask again.
[EDIT]
your calculations with usages of int values divided by other ints (compwatt / 1000) ... are you sure your idea of using int is correct?
or:
cookLoad=(cookLoad*7)*365;
why multiplying with 7 AND 365? should not the average / day be multiplied by 365 only?
For more readability of your code, you can employ compound assignment operators as below,
Operator Name Syntax Meaning
-------------------------------------------------
Addition assignment a += b a = a + b
Subtraction assignment a -= b a = a - b
Multiplication assignment a *= b a = a * b
Division assignment a /= b a = a / b

Normal Distribution/Standard Deviation

My Instructions: Write a program that starts out asking the user for the mean u and standard deviation s for the normal distribution (see the wiki article )
The program then asks for an N, and then asks for N values x. For each x it writes out f(x) to the screen. Note that the program asks the user for u, s, and N just once. After that it asks for N values for x, one by one. After each value x it writes out the corresponding value of the function.
What I am confused about is what the N is supposed to stand for. I assumed it was number of x's but can anyone clarify this for me?
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdlib.h>
int main()
{
double u,s, N, x1,math1, math2, math3,n, v, x;
printf("Enter Mean: ");
scanf("%lf", &u);
printf("Enter Standard Deviation: ");
scanf("%lf", &s);
printf("Enter number of x's: ");
scanf("%lf", &N);
for (v=1; v<=N; v++)
{
printf("Enter Value: ");
scanf("%lf", &x);
n=(-1/2);
printf("f(x)= ");
math1 =1/(u*sqrt(2*M_PI));
math2= (x-u)/s * (x-u)/s;
math3= M_E * exp(n);
x1 = math1 * exp(math3)*exp(math2);
printf("%lf \n", x1);
}
system("Pause");
}
N stands for number of inputs
pretty much clear from this part:
for (v=1; v<=N; v++)
If your instructions are as given, then N does indeed stand for the number of x values required.
Your program does just that, asking for N values of x.
First, it declares a variable N at the start of the program:
double u,s, N, x1,math1, math2, math3,n, v, x;
Then it prompts for input as an integer:
printf("Enter number of x's: ");
scanf("%lf", &N);
...and finally uses that integer to read in N values for x.
for (v=1; v<=N; v++)
{

Resources