uninitialized local variable used in C language - c

so i have been at this for a bit and i am a little stumped i am getting "uninitialized local variable gallons used". in my take_Input function on conversion(gallons);
i am aware that that means that the value for gallons is not being recognized.
why is the gallons to liters function not placing a value for gallons so that the conversion function has that value. any help appreciated thanks...
code:
double take_Input(void)
{
double a, b, c, d;
double gallons;
printf("please enter how many liters of A: ");
scanf("%lf", &a);
printf("please enter how many gallons of B: ");
scanf("%lf", &b);
printf("please enter how many liters of C: ");
scanf("%lf", &c);
printf("please enter how many gallons of D: ");
scanf("%lf", &d);
gallons_To_Liters(a,b,c,d);
conversions(gallons);
return(0);
}
double gallons_To_Liters(double a, double b, double c,double d)
{
double gallons, liters;
liters = a + c;
gallons = b + d;
gallons = (liters * 3.79) + gallons;
return(0);
}
double conversions(double gallons)
{
double totalGallons = gallons;
double quarts = totalGallons * 4;
double pints = totalGallons * 8;
double cups = totalGallons * 16;
double fluid_ounces = totalGallons * 128;
double tablespoons = totalGallons * 256;
double teaspoons = totalGallons * 768;
// output statements.
printf("the amount of gallons is: %.2f \n", totalGallons);
printf("the amount of quarts is: %.2f \n", quarts);
printf("the amount of pints is: %.2f \n", pints);
printf("the amount of cups is: %.2f \n", cups);
printf("the amount of fluid ounces is: %.2f \n", fluid_ounces);
printf("the amount of tablespoons is: %.2f \n", tablespoons);
printf("the amount of teaspoons is: %.2f \n", teaspoons);
return (0);
}

Your gallons_To_Liters function sets the local variable gallons, but does nothing with it. You need to return this value from the function.
Then in the calling function, you need to assign the return value of gallons_To_Liters to the gallons variable in that function.
double take_Input(void)
{
....
gallons = gallons_To_Liters(a,b,c,d);
....
}
double gallons_To_Liters(double a, double b, double c,double d)
{
double gallons, liters;
liters = a + c;
gallons = b + d;
gallons = (liters * 3.79) + gallons;
return gallons;
}
You need to keep in mind that variables in different functions are distinct from each other, even if their names are the same.
Also, for the take_Input and conversion functions, their return values are not used for anything, so change the functions to have a return type of void and remove the return statements from those functions.

Related

Output isn't true

Here is my code:
#include <stdio.h>
int main()
{
int inches = 0;
int yards = 0;
int feet = 0;
const int inches per foot = 12;
const int feet_per yard = 3;
printf("Enter a distance in inches: ");
scanf ("%d", &inches);
feet = inches/inches_per_foot;
yards = feet/feet_per_yard; feet %= feet_per_yard;
inches %= inches_per_foot;
printf("That is equivalent to %d yards %d feet and %d inches.\n",
printf("or %d centimeters\n", inches*2.54);
}
Input:
Enter a distance in inches: 10
Output:
That is equivalent to 0 yards 0 feet and 10 inches.
or 1717986918 centimeters.
So, what is 1717986918?
So, what is 1717986918?
It's what happens when you try to print a double as if it were an int.
printf("or %d centimeters. \n", inches *2.54);
Because inches *2.54 involves a multiplication by a floating point number, inches is implicitly cast to double and the result is a double. Use %f to print a double
printf("or %f centimeters. \n", inches *2.54);

I have question on basic c program for calculating Simple interest; program 1 works but program 2 doesn't work. why? Please help, I am new to coding

Program 1
int main() {
int p, t;
float r;
printf("please enter the principle amount\n");
scanf("%d", & p);
printf("please enter the time period of repayment\n");
scanf("%d", & t);
printf("please enter the rate of interest for lending\n");
scanf("%f", & r);
printf("The simple interest is %f", prt / 100);
return 0;
}
Program 2
#include<stdio.h>
int main() {
int p, t;
float r, interest;
interest = prt / 100;
printf("please enter the principle amount\n");
scanf("%d", & p);
printf("please enter the time period of repayment\n");
scanf("%d", & t);
printf("please enter the rate of interest for lending\n");
scanf("%f", & r);
printf("The simple interest is %f", interest);
return 0;
}
Let's break down the task of program 2.
You're trying to find out the value of "interest" where interest = ( p * r * t ) / 100.
First, you need the value of 3 variables which are 'p', 'r', 't'.
You're taking those values from user (using scanf).
You'll be able to get the value of "interest" after taking all those inputs.
So you'll have to assign values in the variable "interest" according to the equations right after you're done with taking those inputs.
That means at the end of the program.
Another thing, when you're writing an equation, you'll have to write down corresponding operators too.
#include<stdio.h>
int main() {
int p, t;
float r, interest;
printf("please enter the principle amount\n");
scanf("%d", & p);
printf("please enter the time period of repayment\n");
scanf("%d", & t);
printf("please enter the rate of interest for lending\n");
scanf("%f", & r);
interest = (p * r *t) / 100.0;
printf("The simple interest is %f", interest);
return 0;
}

Handling user's input

when I input a floating point number (eg. 48.3) the result displayed is 48.00 instead of 48.30 and whenever I try to input string with empty space the program ends immediately. I need help, how to fix this problem?
int integer;
char a[50];
float fnum;
char b[50];
printf("Please enter an integer : ");
scanf("%s",&a);
integer = atoi(a);
printf("\nPlease enter a floating-point number : ");
scanf("%s", &b);
fnum = atoi(b);
printf("Output : \n");
printf("%i + %.2f = %.2f \n", integer,fnum,(integer+fnum));
printf("%i - %.2f = %.2f \n", integer,fnum,(integer-fnum));
printf("%i * %.2f = %.2f \n", integer,fnum,(integer*fnum));
You're converting string b into an integer by calling atoi. You want to convert it to a floating point number, so use atof:
fnum = atof(b);
The atoi returns an int.
The atof returns a float.
int integer;
char a[50];
float fnum;
char b[50];
printf("Please enter an integer : ");
scanf("%s",&a);
integer = atoi(a);
printf("\nPlease enter a floating-point number : ");
scanf("%s", &b);
fnum = atof(b);
printf("Output : \n");
printf("%d + %.2f = %.2f \n", integer,fnum,(integer+fnum));
printf("%d - %.2f = %.2f \n", integer,fnum,(integer-fnum));
printf("%d * %.2f = %.2f \n", integer,fnum,(integer*fnum));

Loop with rolling total in c

I am trying to get a c program to ask me how many items i am buying than to ask the price for each item while keeping a rolling total and than ask again in a loop. I have the loop working fine but am having problems getting it to give me proper output.
here is the output i am getting when i run it.
http://i119.photobucket.com/albums/o134/halodude808/assignment2_zpsd46e84b8.jpg
#include <stdio.h>
#include <math.h>
int main(void)
{
//variables used
float penny = .01;
float nickel = .05;
float dime = .1;
float quarter = .25;
int items = 0;
float paid= 0.0;
float total = 0.0;
float price =0.0;
int counter =0.0;
for (;;)
{
printf("Please enter the number of grocery items:");
scanf("%d", &items);
for (counter = 1; counter <= items; counter++)
{
printf("Please enter the price for item #%d:", counter);
scanf("%f", &price);
total += price;
}
printf("items = %d total = %f \n", &items, &total);
getchar();
getchar();
}
}
Change
printf("items = %f total = %f \n", &items, &total);
to
printf("items = %i total = %f \n", items, total);
Also, you might want to consider checking for invalid values (zeroes, negative, characters, etc).

Calculate average miles per gallon

Are there any C language programmers on here that can help me figure this out?
I am having trouble getting the calculation for the average miles per gallon to work and my head is spinning. I would really appreciate if anyone have a solution ^_^
int x, number_of_tanks = 3;
double total_num1, total_num2;
double total_miles_per_gallon;
float division, avg;
float num1, num2;
for (x = 1; x <= 3; x++)
{
printf("Enter the number of gallons used for tank #%i: ",x);
scanf("%f", &num1);
fflush(stdin); /* clear input buffer */
printf("Enter the number of miles driven: ");
scanf("%f", &num2);
fflush(stdin); /* clear input buffer */
/*--------------------------------------------------------------*/
/* calculate and output the miles per gallon from user input. */
/* ------------------------------------------------------------ */
division = num2 / (float) num1;
printf("The miles per gallon for this tank %.1f divided by %.1f is %.1f", \
num2, num1, division);
total_num2 = total_num2 + num2;
printf("The total of miles is %f\n", total_num2);
total_num1 = total_num1 + num1;
printf("The total of gallons is %f\n", total_num1);
}
avg = (double) total_num2 / total_num1;
printf("Overall average miles per gallon for three tanks: %.1f", avg);
You don't initialise your totals, so they are undefined. When you start adding to them, you get undefined results. I bet that's what you mean by it not working.
Do this:
double total_num1 = 0;
double total_num2 = 0;

Resources