me on with my code on codechef elementary [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 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;
}

Related

Having trouble creating a program that uses recursion [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 3 years ago.
Improve this question
I'm trying to write a program that calculates 1+2+3...+n with recursion. I'm just starting to learn C so I'm pretty bad...
Here's what I have right now:
#include <stdio.h>
int adder;
adder=1;
int sum(int numba) {
if (adder==numba) {
return(sum);
}
return(sum+adder);
++adder;
}
int main() {
char line[100];
int nummba;
printf("Enter in a number: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%d", nummba);
printf("The sum of numbers from 1 to %d is %d.", nummba, sum(nummba));
return(0);
}
Obviously doesn't work...
How do I use recursion to solve 1+2+3...n?
Thanks.
For starters there is no need to use global variables.
Secondly in these statements
return(sum);
and
return(sum+adder);
sum is a pointer to function. So the expressions of the return statements do not make sense.
Moreover this code snippet
int adder;
adder=1;
will not even compile because you may not use statements in the file scope. At least you should write
int adder = 1;
The function can look the following way
unsigned long long int sum( unsigned int n )
{
return n == 0 ? 0 : n + sum( n - 1 );
}
Here is a demonstrative program.
#include <stdio.h>
unsigned long long int sum( unsigned int n )
{
return n == 0 ? 0 : n + sum( n - 1 );
}
int main(void)
{
printf( "Enter in a non-negative number: " );
unsigned int n = 0;
scanf( "%u", &n );
printf( "The sum of numbers from 0 to %u is %llu.\n", n, sum( n ) );
return 0;
}
Its output might look like
Enter in a non-negative number: 10
The sum of numbers from 0 to 10 is 55.
Recursion requires that a function calls itself. You are not doing this, so you do not have a recursive function in the first place. Consider the following code:
#include <stdio.h>
int sum(int n) {
if (n == 0)
return n;
else
return n + sum(n - 1);
}
int main() {
int n;
char line[100];
printf("Enter in a number: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%d", &n);
printf("The sum of numbers from 1 to %d is %d.\n", n, sum(n));
return(0);
}
If you find recursion a bit difficult for now, you can opt for a less difficult solution using a while loop:
int sum(int n)
{
int i = 0;
int j = 1;
while (j <= n)
{
i += j++;
}
return i;
}
change your code to:
#include <stdio.h>
int sum_val=0;
void sum(int numba)
{
if(numba>=1)
{
sum_val=sum_val+numba;
sum(numba-1);
}
}
int main()
{
int nummba;
printf("Enter in a number: ");
scanf(" %d",&nummba);
sum(nummba);
printf("The sum of numbers from 1 to %d is %d.", nummba, sum_val);
return(0);
}
Did you compile your code?

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;
}

uVa100- '3n+1' ~ time limit exceeded [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 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;
}

the program is running on code blocks and after it stops [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 8 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#define N 10
float func ( int *arr, int n, int *count )
{
int *p,i;
float sum=0;
p=arr;
for (i=0;i<n;i++)
{
sum += *p;
p++;
}
sum /= n;
count=0;
p=arr;
for(i=0;i<n;i++) //לולאה שעוברת על כל המערך ומוסיפה כל תוכן של איברבמערך שיותר גדול מהממוצע
{
*count+= (*p>sum);
p++;
}
return sum;
}
void main()
{
int i, count=0, arr[N]={0}, n=N;
for(i=0;i<N;i++)
{
printf("Please enter your grade\n");
scanf("%d",&arr[i]);
}
for(i=0;i<N;i++)
printf("%d ",arr[i]);
printf("The average in class is: %f and the number of students that had the best grades(MORE) are: %d \n",func(arr,n,count),count);
}
I need some help on this program.
the code is above,
the program need to get from the users 10 grades and then print average and count how many grades is more then the average
Your main function is incorrectly declared. You should compile with all warnings and debug info (e.g. gcc -Wall -g if using GCC, which is probably used by your Codeblocks IDE...) then you should use the debugger (e.g. gdb). And you should test the result of scanf(3)
BTW,
count = 0; // better written as count = NULL;
is wrong: it is clearing the pointer. You probably want
*count = 0;
Also, your last printf is supposing some order of evaluation (since you expect the call to func to change count before printing count), so is undefined behavior. You need:
float avg = func(arr,n,&count);
// from http://stackoverflow.com/a/25382154/841108
printf("The average in class is: %f and the number"
" of students that had the best grades are: %d \n",
avg,count);
Plese show or tell your teacher that you got help on SO (he will find out anyway)
BTW, I can't understand why students are asking their homework on the web. They don't learn anything by doing that, and their teacher will notice anyway.
You are calculating sum and count in func, so you can't return two values at a time. so count the marks which are greater then average in main().
Try this-
#include <stdio.h>
#include <stdlib.h>
#define N 10
float func ( int *arr, int n)
{
int *p,i;
float sum=0;
p=arr;
for (i=0;i<n;i++)
{
sum += *p;
p++;
}
sum /= n;
printf("%lf \n",sum);
return sum;
}
int countfun(int *arr, float sum)
{
int i,count = 0;
for(i=0;i<N;i++)
{
if(arr[i] > sum)
count++;
}
return count;
}
void main()
{
int i, count=0, arr[N]={0}, n=N;
float sum=0;
for(i=0;i<N;i++)
{
printf("Please enter your grade\n");
scanf("%d",&arr[i]);
}
for(i=0;i<N;i++)
printf("%d ",arr[i]);
sum = func(arr,n);
count = countfun(arr,sum);
printf("The average in class is: %f and the number of students that had the best grades(MORE) are: %d \n",sum,count);
}
You can use a getchar() just after the last printf() to wait for a character.
You think that the execution window closes without printing. That is wrong. It does print and then exits before you can even see the output.
P:S - You have some problems with your code as mentioned in the rest of the answers. Fix them and then try this

How to end the do while loop on my demand? [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 9 years ago.
Improve this question
The following code generates Fibonacci numbers using a Do While function, instead of specifying the exact number of the exit condition while (b < 144) I want to be less precise and just make it not to show numbers that are greater that an specific value (let's say 150).
how can I do it?
please note that I'm only know basic functions.
int main()
{
int a;
int b = 0;
int c = 1;
printf("the fibonacci numbers up to 144 are:\n");
printf("%i", b);
do
{
a = c+b;
printf("\n%i", a);
c = a+b;
printf("\n%i", c);
b = c+a;
printf("\n%i", b);
}
while (b < 144);
return 0;
}
I'm not sure what do you intend for "on demand". If you mean on demand by code you could use an internal if and if condition is true then exit the loop with break command. Example extracted by linked URL:
for (i = 0; i < 5; i++) {
if (string[i] == '\0')
break;
length++;
}
If on demand means "as soon you want to stop it" then use CTRL+C sice you are in a console application.
Edit: Since your function already exit on command, you probably want to know "how to read the standard input in a console application?"
Answer: Using the scanf function (here an example):
#include <stdio.h>
int main() {
int n;
printf("Insert the value of N: ");
scanf ("%d",&n);
printf("N*N is equal to: %d\n", n*n);
getch();
}
You might want to use somthing like that :
while (a < MAX_VALUE && b < MAX_VALUE && c < MAX_VALUE)
Where MAX_VALUE is a preprocessor macro at the start of your program after the includes (http://gcc.gnu.org/onlinedocs/cpp/Macros.html#Macros), for example :
#define MAX_VALUE 100000
You can enter the limit for the number up to which do you want the series.
int n;
printf("Enter the limit: ");
scanf("%d", &n);
printf("\nthe fibonacci numbers up to %d are:\n", n);
printf("%i", b);
do
{
...
n--;
}
while (n);
Solved it: added a user specified limit and an if statement to check after each addition:
thanks for the help!
float lim;
int a;
int b = 0;
int c = 1;
printf("The following program generates Fibonacci numbers up to a specified value\n");
printf("Please enter a value of your choise:");
scanf("%f4", &lim);
printf("the fibonacci numbers up to %.2f are:\n", lim);
printf("%i", b);
do
{
a = c+b;
c = a+b;
b = c+a;
if ( a >= lim) break;
printf("\n%i", a);
if ( c >= lim) break;
printf("\n%i", c);
if ( b >= lim) break;
printf("\n%i", b);
}
while(b <= lim);

Resources