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.)
Related
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 1 year ago.
Improve this question
In below given program if I put n=0, then the program is giving right answer but if I write n instead of n=0, the program is giving me wrong answer. If I put k=10 then output is 94 but the correct answer is 55. Why it is adding additional 39?
int main(){
// program to calculate the sum of 'n' numbers
int i=1,k,n;
printf("Enter number: ");
scanf("%d",&k);
do{
n+=i;
i++;
}while(i<=k);
printf("The sum is: %d",n);
return 0;
}
Your loop is adding a value to the current value of n, but you never set the initial value of n. That means its value is indeterminate, and reading an indeterminate value (in this case when you add to it), when the variable in question never had its address taken triggers undefined behavior.
Initialize n to 0 so you have a valid starting point.
int i=1, k, n=0;
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 2 years ago.
Improve this question
The task is to calculate how many times a certain digit occurs in the entered sequence of numbers. The number of numbers to be entered and the number to be calculated are set by typing. Ask me if you have got question about code. The problem in finding a match with the number entered in the array.Can you give me hints or instructions, also i think about loop while but i don't know how to realize it please
The code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, b, n, c=0, arr[30];
printf("The count of numbers: ");
scanf("%d", &n);
printf("The number what is finding: ");
scanf("%d", &b);
for (i = 0; i < n; ++i)
{
scanf("%d", &arr[i]);
}
for(i=0;i < n;i++)
{
if(arr[i]=b)
{
c++;
printf("%d", c);
}
}
}
You should be compiling your code with at least some basic compilation flags. If you do, you will get a heads up that something is wrong before having to run it to find out. It saves a lot of time in the long run. Consult your compiler's documentation.
For instance, it would point out that your if condition is using an assignment (=) instead of an equality comparison (==). It should be:
if (arr[i] == b)
Also, you probably want to print out the total count at the end of the program - after the loop is finished. So move the printf("%d\n", c); after the loop. (You were also missing a newline which you probably wanted).
Also, scanf has a return value - you should check it. If the user enters invalid integers, you want to catch that and handle it properly.
Finally, since you declare your array to be of size 30, you should add a check that the desired length of the input array is no longer than that -- otherwise, you would get a buffer overflow.
Side note: please use more descriptive variable names. Not doing so often leads to confusion, especially for beginners. A small exception to this is for loop counters, like i in this case -- its perfectly fine to use a single letter. But consider b -- there is no obvious meaning; it should be something like target or to_find. Also, c could be count or total. As for n, perhaps size or length would be more suited.
if(arr[i]=b) it's wrong. x = y is an assignment but you want to do a check. To check if two elements are equal you should write if(arr[i] == b).
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.
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
int main(void) {
long fall, n, k, p, i, j, r;
long long x, y, a[110][110];
for(a[0][0]=scanf("%ld",&fall);fall--;printf("%lld\n",y)) {
for(i=!!scanf("%ld%ld%ld",&n,&k,&p);i<=k+1;i++)
for(j=0;++j<=i;a[i][j]=(a[i-1][j-1]+j*a[i-1][j])%p)
;
for(y=!(j=1);j<=k+1;y=(y+a[k+1][j++]*x)%p)
for(x=!((r=n%j)*!(i=-1));++i<j;x=x*(n-i)/((i==r)?j:1)%p)
;
}
return 0;
}
How does for loop work here? It doesn't follow the syntax as I see.
for loops have the following pattern:
for(initial expression; conditional expr; afterthought)
I'll break down the first loop for you, you should be able to do the rest on your own.
for(a[0][0]=scanf("%ld",&fall);fall--;printf("%lld\n",y))
The initialization part of this loop is a[0][0]=scanf("%ld",&fall).
scanf is used for reading input and returns the number of input values. In this case, it will be 1 and it gets assigned to a[0][0].
fall-- is the conditional expression. In C, positive numbers are evaluated as true. So this loop will run until fall == 0.
printf("%lld\n",y) is the afterthought. It gets run after each loop iteration. In this case, it will simply print the value.
Unraveling obfuscated code can be a good learning exercise though you must obviously never use it in practice.
This code abuses the fact that the first and third conditions of the for loop does not necessarily need to have anything to do with the loop itself. At its core, the for loop simply executes an initial expression, performs the conditional check and executes the afterthought after every iteration.