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 6 years ago.
Improve this question
I want to display the progress of my C program in percentages while it is running. The actual work in the program whose progress should be measured is confined in a loop. Here is what I tried:
int i;
int to = 100000000;
while (i++ < to) {
printf("\rPercent done: %d", (100 * i)/to);
}
Might be a dumb question, but how does one display a progress while
the program is running?
Not like this.
You have multiple issues:
Your i is uninitialized, so the program will print garbage. (Fix: -> int i = 0; instead)
Your "progress counter" will only count the progress while in the loop. As soon as Percent done: 100 will be printed, only the loop will be over.
You're printing 100 million lines to the console. Maybe think that through again.
With (i*100)/to you're hitting integer overflow about half way through, so use i / (to / 100) instead. Notice depending on the compiler the compiler could optimize that out by itself.
A little less obnoxious way would be:
#include <stdio.h>
int main (void) {
int i = 0;
int to = 100000000;
while (i++ < to) {
if (i % 1000000 == 0) {
printf("\rPercent done: %d", i / (to / 100));
}
dostuff();
}
printf("\rLoop finished");
return 0;
}
Note that this will only accurately reflect how far the program has come executing the loop. Any work before/after the loop will not be measured by this.
This only prints a console message for every full percent. Still obnoxious that you're getting 100 console messages, but nowhere near as bad as 100.000.000 (!) calls to printf. Still though, that's still a performance impact.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I was solving the problem... But I don't know why it does when a number greater than 200 is entered, it becomes an infinite loop. I used the recursive function. And I also only can use this function. What should I do?
#include <stdio.h>
int f(int n){
if (n<=0) return 0;
else if(n==1 || n==2) return 1;
else {
return f(n-1)+f(n-2);
}
}
int main()
{
int n;
scanf("%d", &n);
printf("%d", f(n));
return 0;
}
… it becomes an infinite loop.
If you are judging whether the program is in an infinite loop by waiting for it to finish, you cannot distinguish between it being in a very long loop and it being in an infinite loop unless you wait forever, which I doubt you have done.
When you ask for f(200), the call to f evaluates f(199) and f(198). Then the first of those calls to f evaluates f(198) and f(197), and the second evaluates f(197) and f(196). When you follow this tree of calls, there are very roughly 1041 calls involved. So your program never terminates while you are waiting because evaluating that many calls would take eons.
(The number of calls for a given input n is going to be very similar to the Fibonacci sequence for n, and the ratios between numbers approaches (1+sqrt(5))/2, so the number of calls will be around (1+sqrt(5)/2)n times some constant depending on the initial terms.)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
The following C code takes so long to compile and execute.
#include <stdio.h>
int main()
{
int max;
printf("Enter a value for max: ");
scanf("%d", &max);
for (int i=0; i<max; i++)
{
printf("%d\t", i);
}
return 0;
}
But, If I initialize the max variable with some number like
int max = 0;
The Compilation and execution is almost instantaneous. Can someone explain why?
Edit:
When I printed the value of the variable max before my input, it showed 2203648 (some garbage value). Instead of "int max = 0", if i assign
int max = 2203648;
the compilation and execution takes the same long time. But, as mentioned earlier, if i assign max say
int max = 200;
the compilation and execution is instantaneous. Does it have to do anything with the pre-assigned garbage value?
Also, this problem occurs only in windows computers, I tested with ubuntu, and the compilation and execution is instantaneous in both version of the code.
In Windows 10:
compilation and execution, as of "Enter a value for max: " appears on screen:
without variable initialization = around 8 seconds
with variable initialization = instantaneous
compiler - gcc
The scanf is failing. Check the return value.
i.e.
if (scanf("%d", &max) != 1) {
fprintf(stderr, "Unable to read max");
exit(1);
}
max is probably some large value hence the large amount of time
EDIT
The delay to see the prompt is that the printf is in a buffer and will not be displayed until the loop completes
I think variable initialization of max=0 doesn't make that great deal of a difference, but god knows why in ypur case its taking 8seconds. I think you should reinstall your GCC Compiler set your path variables correctly once more, and try the above code on a different IDE.
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 4 years ago.
Improve this question
1.
int main() { int a; a = 0; }
//OR
2.
#define ZERO 0 int main() { int a; a = ZERO; }
I think that the second one takes less execution time because it is preprocesssed.
The one liner - compilation time and execution time are different metrics altogether!!
Any program, written in mid to high level language, is not executed as-is by the system. For C, it is the compiler, which takes the source file as input and produces the binary (machine-executable code). In the process, it incorporates a lot of optimization to the code.
In this case, it's most likely, that the compiler will optimize out the statements altogether, as they are not used meaningfully. Both the snippets are likely to generate the same binary (unless you forcefully turn off the optimization). If at all, you have to check the generated binary and perform the time-measurement to check the final result.
They are equal. The compiler's preprocessor just replaces ZERO before doing anything.
Assuming it is written like this:
#define ZERO 0
int main() { int a; a = ZERO; }
The program will be transformed to
int main() { int a; a = 0; }
before it is compiled and it will not reach optimization step.
Update:
Just to clarify, sometimes you see codes like this:
#define ERROR_MEMORY_ALLOCATION_FAILED -1
char *buf = malloc(10);
if (!buf)
return ERROR_MEMORY_ALLOCATION_FAILED;
This can help code readability a lot, especially when you reuse these definitions.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have for loop which makes falling effect in matrix. I am generating numbers in the first row of matrix and the fall.
#include <stdio.h>
#include <stdbool.h>
#include <Windows.h>
void hra(){
.....
do {
for(int i = V; i > 0; i--) {
for(int j = 0; j < S; j++) {
mat1[i][j] = mat1[i - 1][j];
}
}....}
}
But now its too quick. When i use Sleep() it will slow down everything (user input etc..)
Is there a way to slow down only this loop (and gradually make it quicker)?
//
I am sorry I should have mentioned that the user sees the falling numbers and have to interact with them (the numbers fall at the bottom of the matrix where they get stored or they get erased by user). So I want them to "fall" slowly so the user can see them and decide which ones he want or dont. And V is 10 and S 4.
The solution you are looking for is multi-threading. Have the main thread do the calculations, have another thread do the graphics and a third thread handle to user input.
usleep() takes a time in microseconds as parameter, you could put a variable as value and then decrement it. Per example if you want to slow down this for:
for(int i = V; i > 0; i--)
you could add:
usleep(x * i);
in it. (Where x is whatever you want.) Feel free to use any equation you want to choose the way it will slow down.
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 am starting to learn C. Today I was trying a little program that just do a average point starting from 3 input.
After all I wanted to print the number of the averages done in the session, so I insert a simple
counter=counter+1;
into the main while loop and a
printf("you done the average %d times", counter);
before the return 0.
The problem is: if I do the average for just 1 or 2 times, the counter show
every time a different number, never the right, but ever around the int maximum. I tried everything, but it don't work. Where is my mistakes?
This is my first post on this site, i read the rules but i'm sorry if i'm breaking just one. The variable "counter" is declared.
int main()
{
int vote1, vote2, vote3, tot, media, contatore, err;
char opz;
do{
after this, i start an while loop, and this is its end:
contatore=contatore+1;
} while(opz!='n');
printf("hai eseguito la media %d volte", contatore);
return 0;
obviously the code is in italian, where counter=contatore
You have to initialise the variable:
int contatore = 0;