hey guys i have to write a function
that returns the max number in the collatz sequence(not the length),
for example
if n==7
the output should be 52 because its the largest number ;
int collatz_max(int n)
{
int max=0;
if(collatz_max(n)>max)
{
max=n;
return max;
}
if(n%2==0)
{
return collatz_max(n=n/2);
}
else
return collatz_max(n=n*3+1);
}
}
The statement
if(collatz_max(n)>max)
Is just calling itself without changing n or doing anything else. So it would repeatedly just keep calling itself until it runs out of stack. You need to rethink the algorithm
Related
So my program reads numbers until the value 0 then it calculates the sum of numbers. Then I have to write a function which displays the sum. Also, my program read a number "y" from keyboard and I need to find the result of the sum/y.For example if the result of the sum is 10 and I enter y=3 the function result should return the result of 10/3.
So after the program display the result of the sum it asks me to enter again values until 0 value then it closes.
#include <stdio.h>
// Shows a message with what the program is doing.
void ShowIntroduction(void)
{
printf("My program finds a sum etc");
}
// find the sum of the numbers enter until 0 value
int sum(void)
{
int s=0,n;
do
{
scanf("%d",&n);
if (n > 0)
s=s+n;
}
while(n != 0);
return s;
}
// show the result
void sumResult(int a)
{
printf("The sum is %d", a);
}
// find the result of sum/y
double result(int s,int y)
{
double res;
res=s/(double)(y);
return res;
}
int main()
{
int y;
scanf("%d",&y);
ShowIntroduction();
sumResult(sum());
result(sum(),y);
return 0;
}
I am pretty sure I don't call a function in a correct way.
The program displays the sum but not the result of the sum / y
You are calling sum() twice, one for displaying the sum and one for displaying the average result. But because you let the users enter their values inside of sum() you see the behavior you describe.
Just run your program as is and enter two times the same sequence of numbers. Then you should see both the sum and the average. This is just to make you understand your program.
The solution is to store the sum returned by sum() in a variable and give its value to sumResult() and result().
https://codeforces.com/contest/4/problem/A
This is a summary of the problem.
I initially started out with a function which checks whether the input numbers or even or not, then by using a for loop which scrolls through numbers from 1 to the given number.
I've used another variable to store the complement(i.e 8=1+7,2+6,3+5 and so on)
#include<stdio.h>
#include<stdlib.h>
int check_even(int,int);
int main()
{
int n,i,m;
int a;
scanf("%d", &n);
if(n<=100&&n>0)//checking the weight conditions for the watermelon
{
for(i=1;i<=n;i++)
{
m=n-i;
a=check_even(m,i);//checking whether both no are even
if (a==0)
break;
else
continue;
}
if(a==0)
printf("YES");
else if(a==1)
printf("NO");
}
return 0;
}
int check_even(int m,int i)
{
if(m%2==0 && i%2==0)//checking for even no.
return 0;
else
return 1;
}
The case where I'm getting stuck is n=2
2=1+1, both are odd hence output should be "NO", but I repeatedly keep getting "YES".
In the for loop. first you check_even(1,1) which returns 1, so a is 1, therefore the loop continues, check_even(0,0) and returns 0, and a is 0 now so it print YES. Actually you should set for(i=1;i<n;i++).
This code is to print the Fibonacci series using recursion. So I thought to devise a recursion instead of using iteration but as soon as I just execute the code and as soon as the value provider function is executed it is showing some error "segmentation error". I want to do it this way only... Can anyone help? I'm just a beginner so please help and encourage me...
#include<stdio.h>
int fibonacci(int n)
{
int res;
if(n==0)
return 0;
if(n==1)
return 1;
else
res = fibonacci(n-1)+fibonacci(n-2);
return res;
}
int value_provider(int n)
{
int choice1;
if(n>=0)
{
choice1 = fibonacci(n-1);
n -- ;
}
printf("%d",choice1);
if(n>=0)
{
value_provider(n);
}
}
void main()
{
int n;
printf("enter the number");
scanf("%d",&n);
value_provider(n);
}
This code is showing segmentation fault...
What can I do to remove it rather than changing the code?
I want to do it only this way; please help!
I think your value_provider function has poor terminating conditions.
Don't try to calculate fibonacci of -1 so n must be >=1
Also once n is zero you need to finish the recursion, don't
call value_provider again.
Try something more like this;
int value_provider(int n)
{
int choice1=-1;
if(n>=1)
{
choice1 = fibonacci(n-1);
n -- ;
}
printf("%d ",choice1);
if(n>0)
{
value_provider(n);
}
}
I've been at this project for hours and hours trying to figure this out but I'm to the point of brain dead where everything I read leaves me confused.
The idea is to enter a number and the program will tell me whether it is right or wrong. Every single time, the end response after I enter a number is that the number is too low.
Also, the final answer states that the answer is too low and that it's correct at the same time.
Finally, this thing is suppose to ask again if the number entered is incorrect, yet I have no knowledge of how to do this.
Literally, the tiniest advice is much appreciated at this point. It's been a long, groaning night.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int number;
//new function
void welcomeMessage(){
printf("Welcome to my new guessing game!\n");
printf("Let's get started!\n");
}
//new function
int randomNumber(){
int range;
srand(time(NULL));
range = (20 - 1) + 1;
number = rand() % range + 1;
return 0;
}
//new function
int guessInput(){
int guess, range;
printf("I'm thinking of a number between 1 and 20\n");
printf("Care to give it a guess? Be careful! You only get 4 tries!\n");
scanf("%d", &guess);
return 0;
}
//new function
int wrongAnswer(){
int guess, number;
if(guess < number)
{
printf("Try again, your guess is too low\n");
return 0;
}
else if(guess > number)
{
printf("Give it another try, your guess was a bit to high\n");
return 0;
}
return 0;
}
//new function
int correctAnswer(){
int guess, number;
if(guess == number)
printf("Great job! That time you got it right!\n");
return 0;
}
int main(){
welcomeMessage();
randomNumber();
guessInput();
wrongAnswer();
correctAnswer();
}
You're not actually passing the value of guess to wrongAnswer() or correctAnswer(). guess in those two functions is uninitialized and doesn't contain the value stored in guessInput(). This is why wrongAnswer tells you that the guess is too low and correctAnswer tells you that it's correct.
You'll also want to remove the number declaration within those functions. You have a global number right now that stores the random number, but the new number variable declared within your functions will take precedence -- it's uninitialized and doesn't contain the random number like you think it does.
You may want to adjust your wrongAnswer() and correctAnswer() functions to take guess as an integer argument, and remove the guess and number declarations within those two functions. Something like
int wrongAnswer(int guess);
int correctAnswer(int guess);
You may also want to consider having your guessInput() function return the value of guess. Try something like
int guessInput()
{
int guess;
printf("I'm thinking of a number between 1 and 20\n");
printf("Care to give it a guess? Be careful! You only get 4 tries!\n");
scanf("%d", &guess);
return guess;
}
int main()
{
...
int guess = guessInput();
wrongAnswer(guess);
correctAnswer(guess);
...
}
This way you're passing the value of guess to your two functions so that they can actually evaluate whether the number is correct or incorrect.
You'll also want to look at the value of your return functions. Right now they aren't really telling you anything, and they return 0 regardless. Consider changing them to return 0 if the guess was correct and return 1 if the guess was incorrect.
int correctAnswer(int guess)
{
if(guess == number) {
printf("Great job! That time you got it right!\n");
return 0;
} else {
return 1;
}
}
With this information you can create a while loop to continually ask the user for input until they input the correct answer. Something like
int main()
{
...
int is_correct = 1, is_wrong = 1;
int guess;
while (is_correct == 1) {
guess = guess_input();
is_wrong = wrongAnswer(guess);
is_correct = correctAnswer(guess);
}
...
}
The while loop above will call each of the three functions, forever, until the user guesses the correct input. It evaluates is_correct == 1, constantly checking the value of is_correct, and repeating itself. When is_correct == 0 the loop will break and your program will terminate. This is where the return values I mentioned above come in -- a return value of 0 indicates a correct answer and will allow your program to stop. A return value of 1 will repeat the loop. There are other ways to do this, but it may help while you're starting out.
Hopefully this helps you out. I'd also consider redesigning your wrongAnswer() and correctAnswer() functions -- do you really need two? Could you reduce that to one function?
The Most basic issue that i see with the program is that you are not passing values to the functions. Each function is just working in itself and the value or should i say the 'number' it has to work with is not being passed into them.
You can use global variables or pass the values directly. This is what i would do:
The input function:
int guessInput(){
int guess, range;
printf("I'm thinking of a number between 1 and 20\n");
printf("Care to give it a guess? Be careful! You only get 4 tries!\n");
scanf("%d", &guess);
return guess;}
The Random Number Generator Function:
int randomNumber(){
int range;
srand(time(NULL));
range = (20 - 1) + 1;
number = rand() % range + 1;
return number;}
The Answer Function: ( you really don't need 2 functions for this )
int Answer(int guess, int number){
int counter=0;
if(guess < number)
{
printf("Try again, your guess is too low\n");
counter=1;
}
else if(guess > number)
{
printf("Give it another try, your guess was a bit to high\n");
counter=1;
}
else if(guess == number)
{
printf("Great job! That time you got it right!\n");
counter=2;
}
return counter;}
Now that all your functions can accept variables, Modify the Main function
int main(){
int number=0;
int guess=0;
int answr=0; // This does not have to exist but since your doing a return.
welcomeMessage();
number=randomNumber();
guess=guessInput();
Do {
answr=Answer(guess,number);
}(while answr<2)
}
So when the counter reaches 2, which means that the answer is right, the while loop will stop when the correct answer is guessed by the user.
PS: You may need to polish my code a bit since im also in a brain dead mode atm. :D
I wrote a code to get first 1000 prime palindromes ,though my logic is correct ,I dont seem to be getting first 1000 prime palindromes ,I am getting some 113 Prime Palindromes and after that I don't get any . I think this is because my logic is not efficient enough ,that is why it is taking so much time to compile ,but I already tried three different methods and everytime the Runtime is getting stuck after the 113th Prime Palindrome Number .
Can anyone explain why exactly I am getting this problem ,is it because the code is not efficient?
/* Program to find the first 1000 prime palindromes */
#include<stdio.h>
#include<math.h>
int prime(long int n)
{
int i,check=0;
if(n!=2 && n%2==0)
return 0;
if(n==2 || n==3)
return 1;
for(i=3;i<n/2;i=i+2)
if(n%i==0)
check++;
if(check==0)
return 1;
else
return 0;
}
/*long int reverse_number(long int n,long int partial)
{
if(n==0)
return partial;
else
return reverse_number(n/10,partial*10 + n%10);
}*/
int palindrome(long int n)
{
long int reverse = 0;
long int n_copy = n;
int rem;
while(n_copy!=0)
{
rem = n_copy%10;
reverse = reverse*10;
reverse = reverse + rem;
n_copy = n_copy/10;
}
if(reverse==n)
return 1;
else
return 0;
}
int main()
{
long int i;
int count=5,digits;
printf("The 1000 prime palindromes are: \n");
printf("1. 2\n2. 3\n3. 5\n4. 7\n");
for(i=11;;i=i+2)
{
if(prime(i))
{
if(palindrome(i))
{
printf("%d. %ld\n",count,i);
count++;
}
/*if(reverse_number(i,0)==i)
{
printf("%d. %ld\n",count,i);
count++;
}*/
}
if(count==50)
break;
}
printf("\n\n");
return 0;
}
Let me quote my imperative programming professor here: "You can check whether the number is a prime and a palindrome, or check whether it's a palindrome and a prime..."
Also, you're breaking the loop when count is 50, which I suppose you want to do when it's 1000.
Without editing the prime and palindrome functions, besides the order in which they're called, my PC stops finding more after 781 9989899.