Syntax error in my C code? [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Ok so basically, I am trying to make a program that will take a KPH of 185 and convert it to MPH all the way to 0 like so. (using prototypes)
Kilometers per hour converted to miles per hour:
Kph Mph
185 115
180 112
175 109
... ...
10 6
5 3
0 0
Unfortunately my conversion is a bit off, can someone heed some information on why that may be?
#include <stdio.h>
// Prototypes
double mph2kph(double); // convert Miles to KM
double kph2mph(double); // convert KM to Miles
int main()
{
int loop = 1;
double kph = 185; // kilometers per hour
double mph = 115; // miles per hour for computation
printf("Kilometers per hour converted to miles per hour: \n");
printf("Kph Mph\n"); // Display Header
while (loop == 1){
printf("%.2d %.2d \n", kph, kph2mph(kph));
break;
}
loop = 0;
}
//Other Functions:
double mph2kph(double x){
return x*1.61;
}
double kph2mph(double x){
return x*1.61;
}
Output =
Kilometers per hour converted to miles per hour:
Kph Mph
40325120 38090656

You are using %d to show your final result, which is used for int variables. In your case, as you are using double variables, you should go for %f or %lf.
printf("%.2lf %.2lf \n", kph, kph2mph(kph));
Also, your kilometers per hour to miles per hour conversion function is wrong. You should divide and not multiply.
double kph2mph(double x){
return x/1.61;
}
Testing your code with those corrections leads to correct results:
Kilometers per hour converted to miles per hour:
Kph Mph
185.00 114.91

I have made some changes to your program to give your desired output. Some errors in your program are already identified by some other users. Compare this with yours and try to learn. Best of luck!
#include <stdio.h>
// Prototypes
double mph2kph(double); // convert Miles to KM
double kph2mph(double); // convert KM to Miles
int main()
{
int loop = 1;
int kph = 185; // kilometers per hour
double mph = 115; // miles per hour for computation
printf("Kilometers per hour converted to miles per hour: \n");
printf("Kph Mph\n"); // Display Header
while (kph != -5){
printf("%d %.2lf \n", kph, kph2mph(kph));
getchar();
kph = kph - 5;
}
}
//Other Functions:
double mph2kph(double x){
return x*1.61;
}
double kph2mph(double x){
return x/1.61;
}

Related

Counting how many dozen in a number and also count its extra amount? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
How do i make this program counts how many dozen in a number and also count its extra amount?
This is what only I came up
#include<stdio.h>
int main()
{
float number, dozen;
printf("Please Enter any integer Value : ");
scanf("%f", &number);
dozen = number / 12;
printf("dozen of a given number %.2f is = %.2f", number, dozen);
return 0;
}
I dont know how i will get to count the dozen in a number, for example there is 45, i need to get 3 dozen and the extra will be 9.
You prompt for an integer but then use floats. You already had the correct dozen calculation and just miss the modulo operator %. Reformatted code for readability.
#include <stdio.h>
int main() {
printf("Please Enter any integer Value : ");
int number;
scanf("%d", &number);
printf("dozen of a given number %d is %d with remainder %d\n",
number,
number / 12,
number % 12
);
return 0;
}
and example execution:
Please Enter any integer Value : 14
dozen of a given number 14 is 1 with remainder 2
Now that there is an accepted answer,
here's a small lesson in arithmetic (plagiarising #Allan Wind's answer):
#include <stdio.h>
int main() {
printf("Please Enter any integer Value : ");
int number;
scanf("%d", &number);
printf("dozen of a given number %d is %d with remainder %d\n",
number,
number / 12,
number - ( number / 12 * 12)
);
return 0;
}

Why my C program can't read data from file using "<" character [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I am writing a demo C application in batch mode, which will try to read from a file as input.
The command is : metric <mydata
The C source file is:
/* Converts distances from miles to kilometers. */
#include <stdio.h> /* printf, scanf definitions */
#define KMS_PER_MILE 1.609 /* conversion constants */
int main(void)
{
double miles, /* distance in miles */
kms; /* equivalent distance in kilometers */
/* Get and echo the distance in miles. */
scanf("1f", &miles);
printf("The distance in miles is %.2f.\n", miles);
/* Convert the distance to kilometers. */
kms = KMS_PER_MILE * miles;
/* Display the distance in kilometers. */
printf("That equals %.2f kilometers.\n", kms);
return (0);
}
The file "mydata" contains simply an integer 100
When I run the command metric <mydata. The output is:
The distance in miles is 0.00.
That equals 0.00 kilometers.
Is there any idea what's wrong?
You misread the doc: instead of scanf("1f", &miles); you should write:
scanf("%lf", &miles);
Testing the return value of scanf() avoids undefined behavior when the conversion fails and would have helped detect this error. Enabling all warnings in your compiler (gcc -Wall -Wextra -Werror) is also recommended to avoid silly mistakes.
Here is a modified version:
/* Converts distances from miles to kilometers. */
#include <stdio.h> /* printf, scanf definitions */
#define KMS_PER_MILE 1.609 /* conversion constants */
int main(void) {
double miles; /* distance in miles */
double kms; /* equivalent distance in kilometers */
/* Get and echo the distance in miles. */
if (scanf("%lf", &miles) != 1) {
fprintf(stderr, "invalid input\n");
return 1;
}
printf("The distance in miles is %.2f.\n", miles);
/* Convert the distance to kilometers. */
kms = KMS_PER_MILE * miles;
/* Display the distance in kilometers. */
printf("That equals %.2f kilometers.\n", kms);
return 0;
}

The rate doesn't work, it always output 0.000 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Here is my code.
float total,rate;
rate = score / 25;
printf("Total: %f", rate);
But this doesn't work; it always outputs 0.000. Can you help?
I'm going out on a limb here and say that you have score declared as an int. int divided by int will always result in an int. You can fix this by either:
declare score as float
cast it as (float)score
multiply it with a float rate = score * 1.0f / 25
change 25 to 25.0f
Try this:
rate = score / 25.0;
printf("Total: %f", rate);
Or:
rate = (float) score / 25;
printf("Total: %f", rate);
An int divided by an int will always be an int, with everything "after the decimal" truncated.

Getting the total, average, maximum and minimum scores of a 3x4 matrix inside a 2 dimensional array in c programming [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a problem to get the total score, average score, max and minimum score. I can't find the problem on my program. I would like some help on my codes. All help is greatly appreciated. I'm using Dev-C++ as a software for my programs.
Anyway, my only problem is about the codes for the total score, maximum score and minimum score.
Here's my program:
int main(void)
{
char n[3]={'A','B','C'};
int s[3][4]={90,50,100,10,60,100,20,50,80,70,100,75};
double average=0;
int x, y, total=0, max=0, min=0;
for(x=0;x<3;x++)
{
printf("%c\t",n[x]);
for(y=0;y<4;y++)
{
printf("%d\t",s[x][y]);
}
printf("\n");
}
for(x=0;x<3;x++)
{
for(y=0;y<4;y++)
{
total+=s[x][y];
average=(double)total/n[x];
}
printf("\nThe total score of %c is %d with the average score of
%.2lf",n[x],total,average);
}
max=s[0][0];
for(x=0;x<3;x++)
{
for(y=0;y<4;y++)
{
if(s[x][y]>max)
max=s[x][y];
}
printf("\nThe maximum score of %c is %d",n[x],max);
}
min=s[0][0];
for(x=0;x<3;x++)
{
for(y=0;y<4;y++)
{
if(s[x][y]<min)
min=s[x][y];
}
printf("\nThe minimum score of %c is %d",n[x],min);
}
return 0;
}
The result is:
A 90 50 100 10
B 60 100 20 50
C 80 70 100 75
The total score of A is 250 with the average score of 3.85
The total score of B is 480 with the average score of 7.27
The total score of C is 805 with the average score of 12.01
The maximum score of A is 100
The maximum score of B is 100
The maximum score of C is 100
The minimum score of A is 10
The minimum score of B is 10
The minimum score of C is 10
You don't reset the min and max per row, only per matrix so it will reflect that.
Move the initialization inside the first for loop and it will work.
for(x=0;x<3;x++)
{
max=s[x][0];
...
Also you have to set the total to zero every row and there is no need to calculate average inside the second loop, it should be calculated outside it since that will be the correct value.

Why does program provide -143155...when I only switched from int to double? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Why does this code give a completely wrong answer if I use float or double but not int?
//C How to Program Exercises 2.33
#include <stdio.h>
#include <conio.h>
double dailyDrivingCost(double miles, double costPerGallon,double averageMilesPerGallon);
int main(void){
double a,b,c,d,e;
printf ("Please enter the number of miles daily\n");
scanf("%lf",&a);
printf ("Please enter the cost per gallon\n");
scanf("%lf",&b);
printf ("Please enter the average miles per gallon\n");
scanf("%lf",&c);
printf ("The number of miles per gallon is:%d",dailyDrivingCost(a,b,c));
getch();
return 0;
}
double dailyDrivingCost(double miles, double costPerGallon,double averageMilesPerGallon){
double overall_cost;
overall_cost= (miles/averageMilesPerGallon)+ costPerGallon;
return overall_cost;
}
Change
printf ("The number of miles per gallon is:%d",dailyDrivingCost(a,b,c));
^
to
printf ("The number of miles per gallon is:%f",dailyDrivingCost(a,b,c));
d conversion specifier is used to print an int, to print a double (or a float) you need the f conversion specifier.

Resources