Is there something different with C in Hackerrank(getting different output)? - c

I've been trying to do the Love-Letter Mystery Challenge on Hackerrank.
Here are the rules: https://www.hackerrank.com/challenges/the-love-letter-mystery
And here's my solution:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX 1000
int check_palindrome(char *A)
{
int i = 0, j;
while(A[i])
i++;
i -= 1;
for(j = 0; j <= i; j++)
{
if(A[j] != A[i - j])
return 0;
}
return 1;
}
int love_letter(char *A)
{
int i = 0;
int j;
int times;
while(A[i])
i++;
i -= 1;
if(i == 0)
return 0;
if(check_palindrome(A))
return 0;
for(j = i; j >= 0; j--)
{
while(A[j] != 'a')
{
if(check_palindrome(A))
return times;
else
{
A[j] -= 1;
times += 1;
}
}
}
return times;
}
int main() {
int t, i;
char a[MAX];
scanf("%d", &t);
for(i = 0; i < t; i++)
{
scanf("%s", a);
printf("%d\n", love_letter(a));
}
return 0;
}
While testing in on my computer, I get the right output. But, when I try to run the code on Hackerrank, it tell's that my program always gives an output of:
0
0
0
0
That's wrong of course, and it fails the testcase. But why is that? Is there something different about C or something? Or is it just a problem with the site? Or with my code?

At a minimum, you seem to have forgotten to initialize the variable "times".
In terms of the actual algorithm, keep in mind that to make the letters match, you can decrement either (or both) of them. I don't think you handle all cases properly.

Related

I want to create an array of 100 size, with unique random integers from 1 to 999999 in C

I want to create an array of 100 size, which its elements are unique random integers from 1 to 999999. My code doesn't give any error message or the output that I want. What is wrong with this?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100
#define EMPTY -1
int main() {
srand(time(NULL));
int list[999999], A[N], i;
for (i = 0; i < N; i++)
A[i] = EMPTY;
for (i = 0; i < 999999; i++) {
list[i] = i + 1;
}
for (i = 0; i < 999999; i++) {
int j = rand() % 999999;
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
for (i = 0; i < N; i++) {
A[i] = list[i];
}
for (i = 0; i < N; i++) {
printf("%i\n", A[i]);
}
}
The initial loop is useless, the method is very inefficient, but the output should meet the goal...
Yet there might be an issue with the list array: it is very large and defining it as a local variable with automatic storage cause a stack overflow, depending on your target system. Try defining is as:
static int list[999999];
Here is an alternative for N small compared to 1000000:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100
int main() {
int A[N], i, j;
srand(time(NULL));
for (i = 0; i < N;) {
int n = 1 + rand() % 999999;
for (j = 0; j < i; j++) {
if (A[j] == n)
break;
}
if (i == j)
A[i++] = n;
}
for (i = 0; i < N; i++) {
printf("%i\n", A[i]);
}
return 0;
}

I do not understand why the while loop in my program is not working

Can anyone explain me why this program is not working?
It should print all the numbers, but it doesn't do that.
Why is this happening? Many thanks for any help you can offer.
#include <stdio.h>
int main(){
int i = 0;
int j = 0;
int doubleCharArray[2][7] = {{1,2,3,4,5,6},{7,8,9,10,11,12}};
while(i < 2){
while(j < 7){
printf("%d ",doubleCharArray[i][j]);
j++;
}
i++;
}
return 0;
}
You should assign j to 0 after printing each array.
#include <stdio.h>
int main(){
int i = 0;
int j = 0;
int doubleCharArray[2][7] = {{1,2,3,4,5,6},{7,8,9,10,11,12}};
while(i < 2){
while(j < 6){/* Replaced 7 to 6 */
printf("%d ",doubleCharArray[i][j]);
j++;
}
j = 0;/* Added this, re-assigning j to 0 */
i++;
}
return 0;
}
Check the comments in the code.

"file_name.exe has stopped working"

This problem is from spoj. Please see this for question.
My logic is simple as you will see. If I change array sizes in dynamic form to static ones, the code executes and asks for integers and keeps executing like indefinitely whilst it should run for the number of test cases. However if I have kept dynamic, I keep getting this persistent error: file_name.exe has stopped working.
Also plz comment on my code.
#include <stdio.h>
#include <conio.h>
int main()
{
int n, i, count, k, tcs, j, l, m;
scanf("%d", tcs);
int fact[tcs];
int arr[tcs];
for (j = 0; j < tcs; j++) {
scanf("%d", &arr[j]);
}
int calc_fact(n) {
if (n == 1)
return 1;
else
return n * calc_fact(n - 1);
}
j = 0;
m = 0;
while (m < tcs && j < tcs) {
fact[m] = calc_fact(arr[j]);
m++;
j++;
}
m = 0;
for (l = 0; l < tcs; l++) {
i = 1;
count = 0;
while (i <= fact[m]) {
if ((fact[m]) % i == 0)
count++;
i++;
}
m++;
k = (count) % ((10 ^ 9) + 7);
printf("\n%d", k);
}
return 0;
}
Assuming you are using C++, there are couple of things that you need to change in your code:
scanf("%d", tcs); should rather be scanf("%d", &tcs); This mistake is the reason your program crashes, as tcs doesn't have the correct intended value.
The function int calc_fact(n) should be outside the main() function with the prototype int calc_fact(int n). Due to this mistake, your code won't compile in the first place (are you sure when you say that your code complies when the arrays are declared statically)?
Please see the modified code below:
#include <stdio.h>
//#include <conio.h> //You actually don't need it.
int calc_fact(int n)
{
if(n==1)
return 1;
else
return n*calc_fact(n-1);
}
int main()
{
int n, i, count, k, tcs, j, l, m;
scanf("%d", &tcs);
int fact[tcs];
int arr[tcs];
for(j=0; j<tcs; j++)
{
scanf("%d", &arr[j]);
}
j=0;
m=0;
while(m<tcs && j<tcs)
{
fact[m]=calc_fact(arr[j]);
m++;
j++;
}
m=0;
for(l=0; l<tcs; l++)
{
i=1;
count=0;
while(i<=fact[m])
{
if((fact[m])%i==0)
count++;
i++;
}
m++;
k=(count)%((10^9)+7);
printf("%d\n", k); //and not printf("\n%d", k);
}
return 0;
}
Working code here.

how to generate number pattern in triangular form [duplicate]

I want to print this pattern like right angled triangle
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
I wrote the following code:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
for(j=1;j<=f;j++,k--)
{
k=i;
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
for(x=1;x<f;x++,z--)
{
z=9;
printf("%d",z);
}
printf("%d/n");
}
getch();
}
What is wrong with this code? When I check manually it seems correct but when compiled gives different pattern
Fairly simple: use two loops, one for counting up and one for counting down. Print literal "0" between the two.
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
for (int j = 10 - i; j < 10; j++)
printf("%d", j);
printf("0");
for (int j = 9; j >= 10 - i; j--)
printf("%d", j);
printf("\n");
}
return 0;
}
Like H2CO3's, but since we're only printing single digits why not use putchar():
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;
for(i = 0; i < 10; ++i)
{
// Left half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
// Center zero.
putchar('0');
// Right half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
putchar('\n');
}
return EXIT_SUCCESS;
}
Modified Code:
Check your errors:
# include<stdio.h>
# include<conio.h>
int main()
{
// clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
k=i; // K=i should be outside of loop.
for(j=1;j<=f;j++,k++)
{
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
z=9; //z=9 should be outside loop.
for(x=1;x<f;x++,z--)
{
printf("%d",z);
}
printf("\n");
}
//getch();
return 0;
}
You are defining k=i inside the for loop(loop which has j) so every time k gets value of i and thus it always get value of i and prints that value and your another condition(if(k==10)) will never be true because every time k takes value of i and i is less than 10 after first iteration of loop and z=9 inside loop so every time loop is executed it is taking value z=9 so it is printing wrong value.
Here's a C# version:
static void DrawNumberTriangle()
{
for (int line = 10; line >=1; line--)
{
for (int number = line; number < 10; number++)
{
System.Console.Write(number);
}
System.Console.Write("0");
for (int number = 9; number > line - 1; number--)
{
System.Console.Write(number);
}
System.Console.WriteLine();
}
}
I'd suggest renaming your i,j,x,z,k,f variables to ones that have meaning like the one's I used. This helps making your code easier to follow.
Rather than output the mid 0 using printf, why not print it using the loops itself.
The following short and simple code can be used:
int main()
{
int m = 10, n, p;
while(m >= 1)
{
for(n = m; n <= 10; n++)
printf("%d", n % 10);
for(p = n - 2; p >= m; p--)
printf("%d", p );
printf("\n");
m--;
}
return 1;
}
For high throughput (though of questionable merit in terms of clarity):
#include <stdio.h>
int main() {
char const digits[] = "1234567890";
char const rdigits[] = "9876543210";
for (int i = 0; i < 30; ++i) {
int k = i % 10;
fputs(digits + 9 - k, stdout);
for (int j = 9; j < i; j += 10) fputs(digits, stdout);
for (int j = 9; j < i; j += 10) fputs(rdigits, stdout);
fwrite(rdigits, 1, k, stdout);
fputs("\n", stdout);
}
}
#include <stdio.h>
void print(int i){
if(i == 10){
putchar('0');
return ;
} else {
printf("%d", i);
print(i+1);
printf("%d", i);
}
}
int main(void){
int i;
for(i = 10; i>0; --i){
print(i);
putchar('\n');
}
return 0;
}

How do I generate number pattern in triangular form

I want to print this pattern like right angled triangle
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
I wrote the following code:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
for(j=1;j<=f;j++,k--)
{
k=i;
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
for(x=1;x<f;x++,z--)
{
z=9;
printf("%d",z);
}
printf("%d/n");
}
getch();
}
What is wrong with this code? When I check manually it seems correct but when compiled gives different pattern
Fairly simple: use two loops, one for counting up and one for counting down. Print literal "0" between the two.
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
for (int j = 10 - i; j < 10; j++)
printf("%d", j);
printf("0");
for (int j = 9; j >= 10 - i; j--)
printf("%d", j);
printf("\n");
}
return 0;
}
Like H2CO3's, but since we're only printing single digits why not use putchar():
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;
for(i = 0; i < 10; ++i)
{
// Left half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
// Center zero.
putchar('0');
// Right half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
putchar('\n');
}
return EXIT_SUCCESS;
}
Modified Code:
Check your errors:
# include<stdio.h>
# include<conio.h>
int main()
{
// clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
k=i; // K=i should be outside of loop.
for(j=1;j<=f;j++,k++)
{
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
z=9; //z=9 should be outside loop.
for(x=1;x<f;x++,z--)
{
printf("%d",z);
}
printf("\n");
}
//getch();
return 0;
}
You are defining k=i inside the for loop(loop which has j) so every time k gets value of i and thus it always get value of i and prints that value and your another condition(if(k==10)) will never be true because every time k takes value of i and i is less than 10 after first iteration of loop and z=9 inside loop so every time loop is executed it is taking value z=9 so it is printing wrong value.
Here's a C# version:
static void DrawNumberTriangle()
{
for (int line = 10; line >=1; line--)
{
for (int number = line; number < 10; number++)
{
System.Console.Write(number);
}
System.Console.Write("0");
for (int number = 9; number > line - 1; number--)
{
System.Console.Write(number);
}
System.Console.WriteLine();
}
}
I'd suggest renaming your i,j,x,z,k,f variables to ones that have meaning like the one's I used. This helps making your code easier to follow.
Rather than output the mid 0 using printf, why not print it using the loops itself.
The following short and simple code can be used:
int main()
{
int m = 10, n, p;
while(m >= 1)
{
for(n = m; n <= 10; n++)
printf("%d", n % 10);
for(p = n - 2; p >= m; p--)
printf("%d", p );
printf("\n");
m--;
}
return 1;
}
For high throughput (though of questionable merit in terms of clarity):
#include <stdio.h>
int main() {
char const digits[] = "1234567890";
char const rdigits[] = "9876543210";
for (int i = 0; i < 30; ++i) {
int k = i % 10;
fputs(digits + 9 - k, stdout);
for (int j = 9; j < i; j += 10) fputs(digits, stdout);
for (int j = 9; j < i; j += 10) fputs(rdigits, stdout);
fwrite(rdigits, 1, k, stdout);
fputs("\n", stdout);
}
}
#include <stdio.h>
void print(int i){
if(i == 10){
putchar('0');
return ;
} else {
printf("%d", i);
print(i+1);
printf("%d", i);
}
}
int main(void){
int i;
for(i = 10; i>0; --i){
print(i);
putchar('\n');
}
return 0;
}

Resources