Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I'm having trouble with an exercise on my homework.
I have to make a function that tells me when the next leap year is given an n (or n if it's a leap year).
I already tackled the last part, but I'm having trouble with the "next leap year" part. I assume I have to do a cycle?
Here's what I have so far
int next_leapyear(int n)
{
if(((n%4==0)&&(n%100!=0))||(n%400==0)) return n;
else
while(?){
n++;
}
return n;
}
I'm just starting to learn this language, so if you guys could keep it simple I would appreciate it
The task could be split into two parts. First, one could check whether a given n is a leap year, which coule be done with the following function.
int IsLeapYear(int n)
{
return (((n%4==0)&&(n%100!=0))||(n%400==0));
}
Next, one could use a loop, based on the function above, to increase n until it is a leap year. This could be done as follows.
int GetNextLeapYear(int n)
{
int CurrentYear = n;
while(!IsLeapYear(CurrentYear))
{
CurrentYear++;
}
return CurrentYear;
}
Your idea of increasing the year until it's a leap year will work but it's not very efficient because you have to do a lot of modulos which is a very expensive operations.
In fact to find the next normal leap year you just need to round the year to the next multiple of 4, which can be achieved with either of these
year = (year + 3) & ~0x03;
year = (year | 0x03) + 1;
In case the after rounded it's a multiple of 100 then just add 4 to get the correct year. So the implementation may be like this
int next_leapyear(int n)
{
n = (n + 3) & ~3;
if (n % 100 == 0) n += 4;
return n;
}
Increase n until your if statement condition is not true so there are 2 ways either you can put condition in while braces, which is straight forward.
2nd is that, run loop infinite times and put breaking condition inside the loop
while(1){
n++;
if(((n%4==0)&&(n%100!=0))||(n%400==0)) break;
}
Related
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 3 years ago.
Improve this question
I need an optimal algorithm for counting divisors. My algorithm takes lots of time to give me the answers.
Here is the part of my code that counts divisors.
int divisor_counter (int n) {
for (int j = 1; j <= n; j++)
{
if (n % j == 0)
{
counter++;
}
}
return counter;
}
here is a function that may help you with that.(you can find it in https://www.geeksforgeeks.org/find-divisors-natural-number-set-1/)
int divisor(n){
int counter=0;
for(int j=1;j <= sqrt(n);j++) {
if(n%j==0)//If divisor counts your number
{
if(n/j==j)
counter++;//Here because quotient is the same as divisor, one counter should be added
else
counter+=2;//Here we should add two divisors; *j*(divisor) and *n/j*(quotient)
}
}//End Of for
return counter;//print number of divisors
}
input:12
output:1,2,3,4,6,12
This algorithm is one of the optimal algorithms which you can use if you have time limit.
I suggest you this algorithm because in huge program you may face time problems and find it difficult to handle.
I will explain about this program.In fact, if one of the j counts your number, you will face two choices:
First, if the quotient is the same as the divisor, just add one counter.
Second, if the quotient is different from the divisor, add two counters;The first one for the divisor, and the second one for the quotient.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Only been coding in C for about a month, but wondered why the while loop wasn't used more often for FIZZBUZZ. Is it not as clean or some other reason? Here's what I came up with:
int main()
{
int num=1;
while(num<=100)
{
if (num%3==0 && num%5==0)
{
printf("FIZZBUZZ!!!\n");
}
else if (num%3==0)
{
printf("FIZZ!!!\n");
}
else if (num%5==0)
{
printf("BUZZ!!!\n");
}
else
{
printf("%d\n", num);
}
num++;
}
return 0;
}
Your loop can be neatly folded into a for loop:
for(int num = 1; num <= 100; ++num)
There are two advantages here:
num is scoped inside the loop, when before it was bleeding into whatever scope followed the while. Restricting variables to the minimum possible scope is a good rule of thumb, because it minimizes the amount of variables to think about at any given point.
The range over which your program will operate is now summarized in a single place: you can see the bounds (1 to 100) and the step (1) inside the for. That kind of ranges is pretty well balanced to be read and memorized quickly before reading the body of the loop. For example, if you now wanted to only check odd numbers, it would be immediately clear from reading the num += 2 in the for header, rather than stumbling upon it at the very end of the loop's body.
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 5 years ago.
Improve this question
i passed this project in and the grader gave me 50% saying i did not use function for loops. The instructions said use while loops, or does he mean otherwise? here is the code. the project is supposed to count from 10 to 0 then 0 to 10.
#include <stdio.h>
int main()
{
int Integer;
printf("Please enter an integer\n");
scanf_s("%d", &Integer);
int count = Integer;
while (count >= 1)
{
printf("%d\n", count);
count--;
}
printf("*****\n");
while (count <= Integer)
{
printf("%d \n", count);
count++;
}
return 0;
}
This may be related to a specific style the grader wanted your class to learn, or a specific conversation. I suggest asking, as your grader's response was (clearly) missing some details for you.
Meanwhile, some suggestions of what your grader might have been looking for.
Did your grader literally mean for you to use for-loops?
for ( ; count >= 1; count--) {
printf("%d\n", count);
}
Did you forget to count to 0 the first time? (The above loop will stop printing at 1, not 0.
Does your grader want you to functionalize the loop kernels?
void countDownLoopKernel ( int value ) {
printf("%d\n", value);
}
...
while ( count >= 1 ) {
countDownLoopKernel( count );
count--;
}
For a functioning program, items 1 and 3 are arbitrary. They can be crucial when fitting into a larger program's (or company's) style, for readability, for following DRY principals, or for refactoring, but for small programs like this, they make no difference. I suspect your grader is trying to get you to think about alternatives beyond "It works, so it's good enough."
They may have wanted you to use both for and while loops. To count from Integer to 1, try this:
for (count = Integer; count >= 1; count--)
printf("%d\n",count);
Also, to count from 1 to Integer, try this:
for (count = 1; count <= Integer; count++)
printf("%d \n");
I hope this helps!
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 6 years ago.
Improve this question
I am having trouble finding understanding complexity. Could someone help me understand what the complexity of the code below is and why.
for (int i = 1; i < n; i++) { // (n is a number chosen by the user)
for (int j = i - 1; j >= 0; j--) {
printf("i=%d, j=%d", i, j);
}
}
An explanation would be great.
Assuming i starts at 0, the complexity would be constant. The complexity is always expressed relative to a variable defining the number of executions, which is not the case here.
If one term should be used to describe this behavior, it is "constant". There will be a number of executions, but this number will never change
Original Question: Because i's initial value is undefined, the behavior of the code is unpredictable. There is no way to usefully answer the question other than that the complexity is undefined. There is no way to know how many operations the code will perform.
Updated Question: It's O(1). The code will always do precisely the same amount of work.
You can compute the time complexity of this code fragment by evaluating the number of operations, namely the number of calls to printf() which for simplicity's sake we shall assume to be equivalent:
Assuming i starts at 1 (you initially forgot to initialize it), the outer loop runs 99 times, for each iteration, the inner loop runs i times. Gauss was supposedly 9 years old when he computed the resulting number of iterations to be 99 * (99 + 1) / 2.
The complexity of the original piece of code was O(1) since it did not depend on any variable, but since instead you updated the code as:
void fun(int n) {
for (int i = 1; i < n; i++) {
for (int j = i - 1; j >= 0; j--) {
printf("i=%d, j=%d", i, j);
}
}
}
The time complexity would come out as O(n2).
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
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! The input begins with the number t of test cases in a single line (t <= 10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n - m<=100000) separated by a space.
I don't know how to solve the problem with advanced concepts so I solved it by using just loops.
The time limit to this problem is 6.00s
#include <stdio.h>
int main(void)
{
int a[1],b[1],j,i,test,k,flag;
scanf("%d",&test);
for(i=1;i<=test;i++)
{
for(k=0;k<1;k++)
{
scanf("%d %d",&a[k],&b[k]);
}
for(j=a[0];j<=b[0];++j)
{
flag=0;
for(k=2;k<j;++k)
{
if(j%k==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("\n%d",j);
}
}
}
return 0;
}
Couple of suggestions that will improve performance.
You don't need to check all the way to b[0]. You need to check only up to sqrt(b[0]).
Update the loop so that you check only odd numbers not all numbers.
Replace
for(j=a[0];j<=b[0];++j)
{
by
int stop = sqrt(b[0]);
// Start with an odd number and keep incrementing j by 2 to keep it that way
for(j= (a[0]/2)*2+1; j <= stop; j +=2 )
{