Execution of code stuck on running in VSCode - c

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

Related

Running programm in Dev-C++ appears as a blank screen, but not on Code::Blocks

So I have just recently started coding and I'm following a book for beginners on C language. One of the programms we learn to write is as follows below. After I compile and run with Dev-C++ it works, but on the console screen appears nothing.
I tried to run the exact same code through Code::Blocks and it works perfectly. Is there any particularity in Dev-C++ I don't know about?
I'll be adding the image of the console of both IDE's consoles.left Code::Blocks, right Dev-C++
Note: the book I'm using was wrote using Code::Blocks.
Note: all other programms I've wrote to this point in Dev-C++ work just fine. This one is the exception.
#include <stdio.h>
main()
{
// Set up the variables, as weel as define a few
char firstInitial, middleInitial;
int number_of_pencils;
int number_of_notebooks;
float pencils = 0.23;
float notebooks = 2.83;
float lunchbox = 4.99;
//The information for the first child
firstInitial = 'J';
middleInitial = 'R';
number_of_pencils = 7;
number_of_notebooks = 4;
printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n",
firstInitial, middleInitial, number_of_pencils,
number_of_notebooks);
printf("The total cost is $%.2f\n\n", number_of_pencils*pencils
+ number_of_notebooks*notebooks + lunchbox);
//The information for the second child
firstInitial = 'A';
middleInitial = 'T';
number_of_pencils = 9;
number_of_notebooks = 2;
printf("%c%c needs %d pencil, %d notebooks, and 1 lunchbox\n",
firstInitial, middleInitial, number_of_pencils,
number_of_notebooks);
printf("The total cost is $%.2f\n",
number_of_pencils*pencils + number_of_notebooks*notebooks +
lunchbox);
return 0;
}
Since I don't know much about coding yet I haven't really tried a diverse ammount of functions, because I simply don't know and understand them yet.
The code works, so I'm happy about it. But I'm still wondering why it didn't for Dev-C++, which is the IDE I was planning to keep on using.

In what situation the output could go wrong like this?

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

Problem with running and compiling c with Git Bash on Windows

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

unexpected ./ output when coding in C

I'm just starting out in learning to code in C using edX and I am sure this is a really simple mistake, which I can't spot.
I am getting an extra ./ which I can't work out where it is coming from.
This is what I get as the output
How many minutes does your shower last: 3
minutes: 3
bottles: 36./
I am using the CS50 IDE on Cloud 9 and my code is in the screenshot
Your help is appreciated.
This is my code:
int main(void)
{
int showerlength, bottle;
printf("How many minutes does your shower last: ");
scanf("%d", &showerlength);
bottle = (showerlength * 12);
printf("minutes: %d\n",showerlength );
printf("bottles: %d\n",bottle );
}
This is the output I get
~/workspace/pset1/ $ ./water
How many minutes does your shower last: 3
minutes: 3
bottles: 36./
This does not look like an issue anymore.
I tested exactly the same code in CS50 IDE, and output is fine.
I even tested it with and without return(0) in the main function. It works fine.
Screenshot
This would not be a problem anymore. But, try the same thing in the new CS50 IDE, at ide.cs50.io.

GCC performance

I am doing parallel programming with MPI on Beowulf cluster. We wrote parallel algorithm for simulated annealing. It works fine. We expect 15 time faster execution than with serial code. But we did some execution of serial C code on different architectures and operating systems just so we could have different data sets for performance measurement. We have used this Random function in our code. We use GCC on both windows and ubuntu linux. We figured out that execution takes much longer on linuxes, and we don't know why. Can someone compile this code on linux and windows with gcc and try to explain me.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (int argc, char** argv){
double Random();
int k,NUM_ITERATIONS = 10;
clock_t start_time = clock();
NUM_ITERATIONS=atoi(argv[1]);
// iniciranje random generatora
srand(time(NULL));
for(k=0; k<NUM_ITERATIONS; k++){
double raa = Random();
}
clock_t end_time = clock();
printf("Time of algorithm execution: %lf seconds\n", ((double) (end_time - start_time)) / CLOCKS_PER_SEC);
return 0;
}
// generate random number bettwen 0 and 1
double Random(){
srand(rand());
double a = rand();
return a/RAND_MAX;
}
If I execute it with 100 000 000 as argument for NUM_ITERATIONS, I get 20 times slower execution on linux than on windows. Tested on machine with same architecture with dual boot win + ubuntu linux. We need help as this Random function is bottleneck for what we want to show with our data.
On Linux gcc, the call to srand(rand()); within the Random function accounts for more than 98 % of the time.
It is not needed for the generation of random numbers, at least not within the loop. You already call srand() once, it's enough.
I would investigate other random number generators available. Many exist that have been well tested and perform better than the standard library random functions, both in terms of speed of execution and in terms of pseudo-randomness. I have also implemented my own RNG for a graduate class, but I wouldn't use it in production code. Go with something that has been vetted by the community. Random.org is a good resource for testing whatever RNG you select.

Resources