I am trying to do the problem 200B on codeforces. I tested my code, and the output was all right. But when I uploaded it to the online judge system, I failed on the very first test case. It said my output was -0.000000000000, instead of 66.666666666667.
But I have compiled and run on Visual Studio C++ 2010, MacOS clang 13.0.0, and Linux GCC 6.3.0, the outputs were all the same as mine, 66.666666666667. I am very curious and want to figure out in what situation the output could be -0.000000000000.
On my computer,
Input:
3
50 50 100
Output:
66.666666666667
On the online judge system,
Input:
3
50 50 100
Participant's output
-0.000000000000
Jury's answer
66.666666666667
Checker comment
wrong answer 1st numbers differ - expected: '66.66667', found: '-0.00000', error = '1.00000'
#include <stdio.h>
int main(void)
{
int n;
double sumOrange = 0;
double sumDrink = 0;
scanf ("%d", &n);
while (n-- > 0) {
int m;
scanf("%d", &m);
sumOrange += m / 100.0;
sumDrink++;
}
printf("%.12lf\n", (sumOrange / sumDrink) * 100.0);
return 0;
}
I just don't understand why my output could be -0.000000000000. Please help, thanks.
Update: Tested on different versions of GCC (4.9, 5.1, 6.3), the wrong output does not appear. Guess the cause might lie in the specific implementation of printf.
The problem is because printf function in GNU gcc C11 does not support %.12lf format. It should be changed to %.12f For more information, you can read the article below:
Correct format specifier for double in printf
Related
So I have preinstalled extension like 'Code Runner' and 'C/C++' in Vscode.
The problem I am facing is in running a code which gets stuck on running :
#include <stdio.h>
#include <stdlib.h>
int main()
{
//program to convert minutes into years and days
int min = 0;
double yrs = 0.0;
double days = 0.0;
double min_in_yr = 0;
printf("Enter the nmuber of minutes: ");
//get input from the user
scanf("%d", &min);
//calculation
min_in_yr = (60 * 24 * 365);
yrs = (min / min_in_yr);
days = (yrs * 365);
printf("%d minutes is approx %f years and %f days\n", min, yrs, days);
return 0;
}
There is no problem with the code as it seems to be running fine on CodeBlocks.
Also I tried executing a simple hello world program which seemed to be executed perfectly on Vscode in 1.8 s.
What could be the problem?
So I looked up and found a solution for this problem.May it helps other beginners who get stuck doing this on Vscode.
So the problem is actually with the scanf function which seems to be not compatible with OUTPUT section of the output panel.I don't know the reason for this but I found a alternate way to do this.
Just make your code run in the terminal instead.
To do this:
1.Make sure you have code runner extension installed
2.Goto File>Preferences>Settings>Extensions>Run Code configuration and under that scroll down to search for Code Runner:Run in Terminal and check that option.If you are not able to find that search in settings for text in bold and you find
3.That's it.Run your code and it will automatically be executed in the terminal
screenshot
I'm fairly new to all of this, so if it is possible could help be really dumbed down for me thank you :).
So I've had about 1 semester of coding at Uni, and we were learning C. Wanting to practice during my holiday I've done some research and opted to download Atom, installed the Gpp-compiler packages, MinGw and created a path, installed git bash etc. according to all the online instructions I could find.
However when I finally thought I was done with all the set up, a problem occured. As I was testing things out, I realised I couldn't run things properly like I was doing on the uni terminal and gedit.
Shown in my screenshot, shouldn't "hello world" be printed first after I try to run it, then I could enter a value and it should print the value or something.
However after running it would come up blank, until I enter something random and then it would print all at once.
Not sure what's happening here or what I did wrong, any guidance would be great thank you.
Edit: this only seems to be a problem, when I use scanf. Without it, everything prints out in order.
My code:
#include <stdio.h>
int main (void) {
int value;
printf("hello world\n");
scanf("%d", &value);
printf("%d", value);
return 0;
}
when I try to call, it comes out blank until I input 45, then it prints out "hello world" "45"
FNATIC P1#PC MINGW64 ~/OneDrive/Documents/CPP
$ gcc -o main main.c
FNATIC P1#PC MINGW64 ~/OneDrive/Documents/CPP
$ ./main
45
hello world
45
Another example:
#include <stdio.h>
#include <math.h>
int main(void) {
double sideA, sideB, sideC;
double s, area;
printf("Please enter 3 sides of your triangle: \n");
scanf("%lf %lf %lf", &sideA, &sideB, &sideC);
s = (sideA + sideB + sideC) / 2;
area = sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
printf("%lf\n", area);
return 0;
}
Output: nothing came out until I entered (2 3 4) and (5.31 4.2 7.77)
FNATIC P1#PC MINGW64 ~/OneDrive/Documents/CPP
$ ./heron
2 3 4
Please enter 3 sides of your triangle:
2.904738
FNATIC P1#PC MINGW64 ~/OneDrive/Documents/CPP
$ ./heron
5.31 4.2 7.77
Please enter 3 sides of your triangle:
10.542172
Here is my code that I am having issues with. The goal of the program is to scan in a bunch of doubles and perform some simple statistical operations on them. The line I am having the issue with is the fgets(). I have included the stdio.h, it's just not showing up in the code. My actual question is where are the stdin and NULL giving me issues when I though they were part of the language? The exact error I am getting is that both Symbol stdin and NULL could not be resolved.
/*
* simpleStats.c
*
* Created on: Sep 17, 2018
* Author: David Liotta
*/
#include <stdio.h>
#define BUFSIZE 256
int main(){
double n, max, min, sum, mean;
char line[BUFSIZE];
int numsRead = 0;
int numOfItems = 1;
n = -1;
max = n;
min = n;
sum = n;
while(n != 0 && fgets(line, BUFSIZE, stdin ) != NULL){
numsRead = sscanf(line, "%f", &n);
if(numsRead == 1 && n != 0){
numOfItems++;
if(n > max)
max = n;
if(n < min)
min = n;
sum = sum + n;
}
if(numsRead == 0)
printf("Bad input\n");
}
mean = sum / numOfItems;
printf("# of items: %i", numOfItems);
printf("\nSum: %f.3", sum);
printf("\nMax: %f.3", max);
printf("\nMin: %f.3", min);
printf("\nMean: %f.3", mean);
}
This code should compile. I suspect something might be wrong with your development environment.
Since you're running Eclipse, I'm assuming that your compiler is GCC. I may be wrong though.
Try to locate your compiler executable, and run the compilation by hand:
gcc -Wall -o simpleStats simpleStats.c
or, if you're on Windows:
gcc.exe -Wall -o simpleStats.exe simpleStats.c
You may have to specify the full path to gcc.exe, (depending on your environment, it might even be called something else; you may be able to retrieve the full path from the console window in Eclipse).
Pay close attention to the output. Copy/paste the full output verbatim in your original post if you can (do not rephrase the warnings / error messages).
I seldom use Eclipse, but with most IDEs you get to chose what kind of project you want to create. Make sure you selected something like "console application", the error you're referring to (stdin not being resolved) may suggest a linker error. Again, it's hard to tell without the exact GCC output.
A couple more things to check:
make sure your compiler and its dependencies are properly installed,
make sure that this compiler is targeted at Windows (or whatever OS you use), not at some exotic embedded platform,
most development environments come with a bunch of sample projects, see if you can build one.
The problem I was having ended up being the compiler not correctly reading the code. I used a different compiler, and with some minor syntax changes, the code worked fine.
I am new to C and trying to write a program using Xcode that takes the temperature in Fahrenheit and converts it to Celsius, and vise versa. My code so far is below.
#include <stdio.h>
#include "hw2.h"
void convert_temp(int degree1, char scale1, int* degree2, char* scale2){
if (scale1 == 'F') {
*degree2 = ((degree1 - 32) * 5) / 9;
*scale2 = 'C';
}
else {
*degree2 = ((degree1 * 9) / 5) + 32;
*scale2 = 'F';
}
}
int main() {
int degree1, degree2;
char scale1, scale2;
printf("Enter a temperature and a scale\n");
scanf("%d %c", °ree1, &scale1);
convert_temp(degree1, scale1, °ree2, &scale2);
printf("%d %c = %d %c\n", degree1, scale1, degree2, scale2);
return 0;
}
Here is an example of correct i/o:
Enter a temperature and a scale
32 F
32 F = 0 C
However, when I run the code, this is what I get:
Enter a temperature and a scale
32 F
hw2 was compiled with optimization - stepping may behave oddly; variables may not be available.
(lldb)
I cannot understand the output I am getting. Can anybody tell me why I do not get 32 F = 0 C on my output? Everything in my code seems fine to me.
Assuming hw2 is the name of your program, then the debugger is complaining that it was compiled with optimisations turned on, which isn't normal during development, as the optimizer does all sorts of clever things to get the program running faster.
You need to do the following in Xcode:
Ensure you are debugging using the Debug build configuration (Check your Schemes).
Ensure you haven't turned on Optimizations for the Debug build configuration (Check your Build Settings).
i am enthusiast and new in programming trying this simple c language code and compile it with gnu compiler. here is my code:
/*simple program:trying to printf string and get a letter*/
#include "stdio.h"
int main()
{
int i=0;
char c;
while(i++<100){
if(i % 2)
printf("%C this is even number",i);
if(i==50){
c=getchar();
printf("you enter %c letter",c);
}
}
return 0;
}
however when i compile it with gcc and run in terminal, it doesnt show anything,no error and no warning. i tried to figure it out by changing the way my code behave but still not work. is it the code that wrong or gcc have bug or may be i miss about something?
Try to put a newline at the end of printf text:
printf("%C this is even number\n",i);
Also i is an int so use the %d format:
printf("%d this is even number\n",i);
Edit: I just tested this with the changes and it works for me.
You should call fflush(stdout) after your display.
It ran for me using cygwin under Windows Vista.
I pasted your code into a file called test.c and ran gcc test.c that output a file called a.exe which I then ran.
johnma#johnma-PC ~
$ ./a.exe
this is even number♥ this is even number♣ this is even number this is even numb
this is even numbern this is even number◄ this is even number this is even numb
er this is even number this is even number↓ this is even numberthis is even numb
er this is even number this is even number! this is even number# this is even nu
mber% this is even number' this is even number) this is even number+ this is eve
n number- this is even number/ this is even number1 this is even number
you enter
letter3 this is even number5 this is even number7 this is even number9 this is
even number; this is even number= this is even number? this is even numberA this
is even numberC this is even numberE this is even numberG this is even numberI
this is even numberK this is even numberM this is even numberO this is even numb
erQ this is even numberS this is even numberU this is even numberW this is even
numberY this is even number[ this is even number] this is even number_ this is e
ven numbera this is even numberc this is even number
johnma#johnma-PC ~