uVa100- '3n+1' ~ time limit exceeded [closed] - c

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 finished learning the basic C language recently. I started solving uVa online judge problems. I solved the 3n+1 problem, ran it in my compiler. It was doing perfectly fine. But when I submitted online it said "Time Limit Exceeded." I can't seem to place the solution.
Here's the code:
#include<stdio.h>
int execute(int x, int y)
{
int i, n, k, maxCycle=0;
for(i=x; i<=y; ++i)
{
n=1;
k=i;
while(k!=1)
{
if(k%2==1)
k=3*k+1;
else
k=k/2;
n++;
}
if(n>=maxCycle)
maxCycle=n;
}
return maxCycle;
}
int main()
{
while(1)
{
int a, b, max;
scanf("%d %d", &a, &b);
max=execute(a, b);
printf("%d %d %d\n", a, b, max);
}
return 0;
}

You are not breaking the input taking loop.
Use while ( scanf ("%d %d", &a, &b) != EOF ) when taking a,bin main().
You should break the input taking loop otherwise it waits for more input.
int main()
{
int a,b;
while(scanf ("%d %d", &a, &b) != EOF )
{
int max;
max=execute(a, b);
printf("%d %d %d\n", a, b, max);
}
return 0;
}

Related

why this recursive programis having garbage value even after assignment [closed]

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 2 years ago.
Improve this question
The values in image appear from nowhere:
What are these values and if they are garbage values then why they are still present after assigning J the value. I am also attaching the source code.
int sum(int);
void main()
{
int a, b;
int j = 0;
printf("please enter the number to find the sum\n");
scanf("%d", &a);
j = a + 1;
printf("%d\n", j);
b = sum(j);
printf("the sum is %d", b);
}
int sum(int j) {
printf("jis %d\n", j);
int f;
if (j == 0)
{
printf("if cond\n");
return f;
}
else
{
j = j - 1;
printf("f up is %d\n", f);
f = j + sum(j);
printf("f dw is %d\n", f);
return f;
}
}
In the if block of function sum, you declare int f without assigning it a value and hence it possesses a garbage value. The only time you assign it a value is after the statement printf("f up is %d\n",f);. Hence, this statement is always going to print a garbage value.
If you use an uninitialized variable, that will lead to undefined behavior . and you have used uninitialized int f in your if-elsestatement in sum function. Initialize it.
Also don't use void main use int main.

me on with my code on codechef elementary [closed]

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 4 years ago.
Improve this question
I face problems with my code when I enter 1 3 4 . although I couldn't find any error as it works perfectly with other numbers / ps. the was built as a solution to the codechef problem POTATOES
Problem summary: Write a program that inputs an integer T followed by T lines containing two space-separated positive integers. For each of these lines, output the smallest number (>1) that, when added to the sum of these two numbers, results in a sum that is a prime number.
and my code is
#include<stdio.h>
#include<math.h>
int prime(int a,int b);
int main() {
int c;
scanf("%d",&c);
int a,b,d[c];
for(a=0; a<c; a++) {
int x,y;
scanf("%d %d",&x,&y);
b=(x+y);
if(prime(x,y)-b!=0)
d[a]=prime(x,y)-b;
else d[a]=prime((x+1),y)-b;
}
for(a=0; a<c; a++)printf("%d\n",d[a]);
return 0;
}
int prime(int a,int b) {
int c,e;
for(c=2; c<(a+b); c++) {
if((a+b)%c==0) {
b++;
continue;
}
return(a+b);
}
}
Your code is wrong. In your function prime() due to the statement b++; , (a+b) is changing in (a+b)%c but the incremented c is never went back.So for bigger prime numbers your code will fail
Eg:- (89,1) (79,1) etc
Also You don't need that d[c] in your code. You don't need to store every output.When you compute one output just print it .That is ok with code-chef. Also You can divide the problem into to functions.
Try this simplified code :-
#include <stdio.h>
#include <math.h>
int prime(int n);
int make_prime(int a);
int main()
{
int c;
scanf("%d", &c);
int a, b;
for (a = 0; a < c; a++)
{
int x, y;
scanf("%d %d", &x, &y);
b = (x + y);
printf("%d\n", make_prime(b));
}
return 0;
}
int make_prime(int a)
{
int c=1;
while(prime(a+c)==0){
c++;
}
return c;
}
int prime(int n){ // simple prime function
int i,flag=1;
for (i = 2; i <= (n)/2; i++)
{
if ((n) % i == 0)
{
flag=0;
break;
}
}
return flag;
}

ERROR in GCD calculation using Function in C language [closed]

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 5 years ago.
Improve this question
this code is showing some syntactical errors I can't find out where it is, may be it is in the gcd function. here is my code.
#include<stdio.h>
#include<conio.h>
int main(void)
int gcd(int,int);
{
int a,b, temp;
printf("Enter the value of a");
scanf("%d",&a);
printf("Enter the value of b");
scanf("%d",&b);
if (b>=a)
{
int temp;
temp=b;
b=a;
a=temp;
}
elseif(b!=0)
{
gcd(a,b);
printf("The Gcd of the numbere is %d",gcd(a,b));
}
else
{
printf("The GCD of %d %d is %d:",a,b,a);
}
getch();
return 0;
}
int gcd(int a,int b)
{
int g;
while(b!=0)
{
g=a%b;
a=b;
b=g;
return g;
}
}
I would be thankful if you point out my errors and explain with the correct code.
switch the position of these two lines:
int main(void)
int gcd(int,int);
also, elseif -> else if
The gcd function uses Euclid's Algorithm. It computes a mod b, then swaps a and b with an XOR swap.
Reference
#include<stdio.h>
int gcd(int ,int );
int main(void)
{
int a,b, temp;
printf("Enter the value of a : ");
scanf("%d",&a);
printf("Enter the value of b : ");
scanf("%d",&b);
int res = gcd(a,b);
printf("The Gcd of the numbere is : %d \n",res);
return 0;
}
int gcd(int a, int b)
{
int temp;
while (b != 0)
{
temp = a % b;
a = b;
b = temp;
}
return a;
}

Unexpected end-of-file found error in C programming [closed]

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 5 years ago.
Improve this question
Hi guys i have been check for my code many many times but i still confuse where is this error come from....
This error appear while build solution...
#include<stdio.h>
#include<conio.h>
int add(int a, int b);
int main()
{
int num1, num2, ans;
printf("Please Enter the two numbers :%d %d", num1, num2);
scanf_s("%d %d", &num1, &num2);
ans = add(num1, num2);
return 0;
}
int add(int a, int b)
{
int sum = a + b;
printf("\nSummition is = %d", sum);
}
Unexpected end-of-file found error
int main() {
int num1, num2, ans;
printf("Enter the two numbers : ");
scanf("%d %d", &num1, &num2);
ans= sum(num1, num2);
printf("\nAddition of two number is : ");
return (0);
}
int sum(int num1, int num2) {
int num3;
num3 = num1 + num2;
return (num3);
}

C programming: Write a program that reads three integers from keyboard and outputs their sum [closed]

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 to write code that reads three integers from keyboard and outputs their sum. Does this mean that only integers should be entered, or should it be able to add characters as well? Here's my code:
#include <stdio.h>
int main(void) {
int a, b, c, d;
printf("\n Enter the three numbers:");
scanf("%d %d %d", &a, &b, &c);
d = a + b + c;
printf("sum of numbers is %d", d);
}
scanf("%d %d %d", &a, &b, &c); parses the input stream for 3 decimal integers optionally separated by white space (spaces, tabs, linefeeds...).
If any other characters are present (such as letters, decimal points, commas...) or if not enough input is available, scanf will return a result different than 3 and some of the output variables will not by set. Always test the return value of scanf.
Here is a corrected version:
#include <stdio.h>
int main(void) {
int a, b, c, d;
printf("\n Enter the three numbers:");
if (scanf("%d %d %d", &a, &b, &c) == 3) {
d = a + b + c;
printf("sum of numbers is %d\n", d);
}
return 0;
}

Resources