Stone codechef: What is wrong with the code? [closed] - c

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 7 years ago.
Improve this question
I am new to coding and finding my way!! I am solving a problem at codechef http://www.codechef.com/problems/RRSTONE.
All tests cases are passed at my side but the answer is still wrong. Any Help???
//codechef stones
#include<stdio.h>
int main(int argc, char *argv[])
{
unsigned int size; //N=size
unsigned long int k; //k = K
scanf("%u %lu",&size,&k);
long int a[size], max;
int i,j;
for(i=0;i<size;i++)
scanf("%lu",&a[i]);
i=j=0;
if(k==0)
goto printing;
if(k%2==1)
k=1;
else
k=2;
for(j=0;j<k;j++)
{
max=a[0];
for(i=0;i<size-1;i++)
{
if(max<a[i])
max=a[i];
}
for(i=0;i<size;i++)
a[i]=max-a[i];
}
printing: //removed this comment
for(i=0;i<size-1;i++)
printf("%d ",a[i]);
printf("%d",a[i]);
return 0;
}

I suggest you should look at the criteria properly.
Constraints
1 <= N <= 105
0 <= K <= 109
Ai does not exceed 2 * 10^9 by it's absolute value.
Here is modified code try with this.
And one more thing: Since you are learning understand the code first.
#include<stdio.h>
int main(int argc, char *argv[])
{
unsigned int size; //N=size
unsigned long int k; //k = K
scanf("%u %lu",&size,&k);
long long int a[size], max; //CHANGE HERE!!!! and so forth for long long
int i,j;
for(i=0;i<size;i++)
scanf("%lld",&a[i]);
i=j=0;
if(k!=0)
{
if(k%2==1)
k=1;
else
k=2;
for(j=0;j<k;j++)
{
max=a[0];
for(i=1;i<size;i++)
{
if(max<a[i])
max=a[i];
}
for(i=0;i<size;i++)
a[i]=max-a[i];
}
}
for(i=0;i<size;i++)
printf("%lld ",a[i]);
return 0;
}
EDIT: Sorry I forgot to put this note. There is issue sometimes with goto when you are using online compilers, so better to try with a clean approach and make sure you are selecting the correct option for source language (C-gcc or C99 whatever you're using) . Earlier I have experienced the same.

Related

Void function usage issue, I'm new to C [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 months ago.
Improve this question
the task is to print ascending order numbers using only void function. Any insight on what's wrong with the code? Explanation too would be appreciated
#include<stdio.h>
void listNumbersAsc(int start, int end);
int main()
{
int x,y;
printf("Enter the range of numbers maintaining start at first and end at second:");
scanf("%d%d",&x,&y);
listNumbersAsc(x, y);
}
void listNumbersAsc(int start, int end){
int i,s,e;
if(start>=end){
s=start;
e=end;
}
else{
s=end;
e=start;
}
for(i=s; i<=e; i++){
printf("%d",i);
}
}
This should work:
#include<stdio.h>
void listNumbersAsc(int start, int end);
int main()
{
int x,y;
printf("Enter the range of numbers maintaining start at first and end at second:");
scanf("%d %d",&x,&y);
listNumbersAsc(x, y);
}
void listNumbersAsc(int start, int end){
int i,s,e;
if(start<=end){
s=start;
e=end;
}
else{
s=end;
e=start;
}
for(i=s; i<=e; i++){
printf("%d ",i);
}
}
You if statement in listNumbersAsc function wasn't correct.
Also added a space between two %d in scanf("%d %d",&x,&y) to separate two numbers.
The problem of the function is that this for loop
for(i=s; i<=e; i++){
printf("%d",i);
}
can be executed inly once if initially start is specified equal to end due to this if statement
if(start>=end){
s=start;
e=end;
}
else{
s=end;
e=start;
}
That is due to this if statement start can not be less than end.
Also there are redundant variables in the function
The function can be defined the following way
void listNumbersAsc( int start, int end )
{
if ( end < start )
{
int tmp = start;
start = end;
end = tmp;
}
do
{
printf( "%d ", start );
} while ( start++ != end );
putchar( '\n' );
}
Pay attention to that this loop
for(i=s; i<=e; i++){
shown in your question and in the other answer is in general wrong. If the user will specify the value INT_MAX for the parameter end then the loop can be infinite.

Why array doesn't print correct numbers? [closed]

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 4 years ago.
Improve this question
Following code:
#include <stdio.h>
int izbaciSveProste(int n, int x[], int y[])
{
int i;
int flag=0;
for(i=2; i<n/2; i++)
{
if(n%i ==0)
{
flag =1;
break;
}
}
if(flag==1)
return 0;
else
return 1;
}
int main()
{
int i,j,n,x[100],y[100];
printf("Koliko elemenata zelite u polju?\n");
scanf("%d", &n);
printf("Enter elements in array:- ");
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
int len = sizeof(x)/sizeof(x[0]);
for(i=0; i<len; i++)
{
if(izbaciSveProste(x[i]))
{
for(j=i; j<len; j++)
{
x[j] = x[j+1];
}
i--;
len--;
}
}
printf("Elementi nakon brisanja su:\n");
for(i=0; i<len; i++)
printf("%d\n",y[i]);
printf("\n");
return 0;
}
Purpose of this program should be to delete all prime numbers from array x[] with n elements, remaining elements should be rewritten in array y[] and show count of elements in array y[] in the end.I believe that function is okay and error is in main() specifically in storing y[].
Your function int izbaciSveProste(int n, int x[], int y[]) requires three arguments. Your code izbaciSveProste(x[i] passes one argument. That's not much enough. The compiler tells you that fact with the error message:
error: too few arguments to function 'izbaciSveProste'
Your function prototype has 3 parameters:
int izbaciSveProste(int n, int x[], int y[])
When you call the function, you only provide 1:
if(izbaciSveProste(x[i]))
The compiler wants to get all 3.
As your function doesn't even touch the 2 array parameters, you might simply remove them from the function definition and only take 1 integer.
Another problem:
You print y[i] in your loop but you never assign any value to that array.

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

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

Spoj LastDigit Wrong answer [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
Link to the problem
Spoj LastDigit problem
I have tested my code on my machine but in SPOJ it is showing wrong answer.
Here is my code-
#include<stdio.h>
int main()
{
int num=0;
scanf("%d",&num);
while(num--)
{
int a=0;
unsigned long int b;
scanf("%d\t%lu",&a,&b);
a%=10;
b%=100;
if(a==0||a==1)
printf("%d\n",a);
else if(b==0)
printf("1\n");
else if(a==5)
printf("5\n");
else
{
int d=b%4,e=1;
while(d--)
e*=a;
printf("%d\n",e%10);
}
}
return 0;
}
Your program is buggy. You are assuming cycle of length 4 for all digits except 0,1,5. That's incorrect
For instance, consider the input1
2 4
Your program outputs 1 whereas the answer should be last digit of Power(2,4) = last digit of 16 = 6
In SPOJ you get a WA because your program is used to calculate the last digit of not so big number. In the problem, it is clearly mentioned that the value of 'b' lies between '0' and '2,147,483,000' inclusive.
The correct way to solve the problem is by using Modular Exponentiation.
# Wasim Thabraze I used the modular approach but my solution is not accepted because
my solution is taking more than than 700bytes
#include<iostream>
using namespace std;
int lastdigit(int a,long int b);
int main()
{
int t,a;
long int b;
cin>>t;
while(t--)
{
cin>>a>>b;
cout<<lastdigit(a,b)<<"\n";
}
return 0;
}
int lastdigit(int a,long int b)
{ if(b==0){return 1;}
if(a%10==0){return 0;}
if(a%10==1){return 1;}
if(a%10==5){return 5;}
if(a%10==6){return 6;}
int ret,mod=b%4;
if(a%10==2||a%10==8||a%10==4)
{
if(a%10==8){if(mod==1){mod=3;}else if(mod==3){mod=1;}}
if(a%10==4){if(mod==1||mod==3){mod=2;}else if(mod==2){mod=0;}}
if(mod==1){ret= 2;}
else if(mod==2){ret =4;}
else if(mod==3){ret =8;}
else if(mod==0){ret= 6;}
}
else if(a%10==3||a%10==7||a%10==9)
{
if(a%10==7){if(mod==1){mod=3;}else if(mod==3){mod=1;}}
if(a%10==9){if(mod==1||mod==3){mod=2;}else if(mod==2){mod=0;}}
if(mod==1){ret= 3;}
else if(mod==2){ret= 9;}
else if(mod==3){ret= 7;}
else if(mod==0){ret= 1;}
}
return ret;
}

Resources