"file_name.exe has stopped working" - c

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.

Related

Unable to deduce why a segmentation fault occurs in my recursive function

I have experimented with my code a bit by commenting out specific parts of my code. I have found out that no segmentation fault occurs when i comment out the for loops with the variable j( shown in code). Also, when i comment out the recursive part in the function ( the lines where the function calls itself), no seg fault occurs even when the for loops are present. So clearly the for loops are causing problems in the second or higher iterations of the function, but I have no idea why. One possible reason i could think of is that infinite recursion occurs, but as far as i know no infinite recursion occurs here.
I have been trying to solve the following problem: https://www.codechef.com/problems/H1
#include<stdio.h>
int prime1[]={3,5,7,11,13,17};
int min=1000000000; //just some large number
void check(int *, int);
void swap(int*, int, int);
int prime(int);
int main()
{
int T;
scanf("%d", &T);
for(int i=0; i<T; i++)
{
printf("\n");
int arr[9];
for(int j=0; j<9; j++)
scanf("%d", &arr[j]);
check(arr, 0);
if(min==1000000000)
min=-1;
printf("%d\n", min);
}
}
void check(int arr[],int step) //step indicates the level of
//iteration
{
int k, a[9];
for(k=0; k<9; k++)
{
if(arr[k]!=k+1)
break;
}
if(k==9)
{
if(step<min)
min=step;
}
else
{
for(int i=0; i<8; i++)
{
if(i%3!=2)
{
if(prime(arr[i]+arr[i+1]))
{
for(int j=0; j<9; j++) // segmentation fault
a[j]=arr[j];
swap(a, i, i+1);
check(a, step+1);
}
}
if(i<=5)
{
if(prime(arr[i]+arr[i+3]))
{
for(int j=0; j<9; j++) // segmentation fault
a[j]=arr[j];
swap(a, i, i+3);
check(a, step+1);
}
}
}
}
}
int prime(int a)
{
for(int i=0; i<6; i++)
{
if(a==prime1[i])
return 1;
}
return 0;
}
void swap( int a[], int b, int c)
{
int temp;
temp=a[b];
a[b]=a[c];
a[c]=temp;
}
Your algorithm runs forever switching the first two tiles.
I removed some loops, i=0, k=0 and arr[] = {4,3,} we are left with:
void check(int arr[], int step)
{
int k, a[9];
// arr[0] != 1, so k != 9, we fall in else { ... }
// for (k = 0; k < 9; k++) {
// if (arr[k] != k+1)
// break;
// }
// printf("\n");
// if (k == 9) {
// if (step < min)
// min = step;
// } else {
int i = 0;
// for (int i = 0; i < 8; i++) {
// if (i%3 != 2) {
// if (prime(arr[i] + arr[i+1])) {
// i = 0, so i%3 != 2, so we fall in the first if
for (int j = 0; j < 9; j++)
a[j] = arr[j];
// you swap arr[0] with arr[1]
swap(a, i, i+1);
// and then you check again
check(a, step+1);
// and it runs so forever, never reaching this point
// }
// }
// if (i <= 5) {
// if (prime(arr[i] + arr[i+3])) {
// for (int j = 0; j < 9; j++)
// a[j] = arr[j];
// swap(a, i, i+3);
// check(a, step+1);
// }
// }
}
}
}
This algorithm needs rethinking. You need to differentiate the algorithm when run with different step number. The easiest is to switch a random title on each check, the better is to build a proper decision tree, where you remember which titles were switched.
You may interest yourself in some proper indentation,

Error in selection sort with function in C

I am trying to do a selection sort using function called min().
This is my code:
#include <stdio.h>
#include <conio.h>
void main() {
int i, temp, arr[20], n, loc;
int min(int [], int, int);
printf("Enter a range of the array");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter elements");
scanf("%d", arr[i]);
}
for (i = 0; i < n; i++) {
loc = min(arr, i, n);
temp = arr[i];
arr[i] = arr[loc];
arr[loc] = temp;
}
min(int arr[], int i, int n) {
int j, loc, temp;
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = j;
}
}
return (temp);
}
getch();
}
the compiler is giving one error when compiling.
it saying:
Error SELECTIONSORT.C 22: Expression Syntax.
my line number 22 is min(int arr[],int i, int n) according to my compiler Turbo C++.
Please guide me where I am going wrong.
Thanks for any help.
There are multiple problems in your code:
The function min must be defined outside the body of the main() function.
Note that it is considered bad style to declare function prototypes in a local scope. Either define the function before the main() function or put the prototype before the main() function.
Also the prototype for main() without arguments should be int main(void).
In function min, you must initialize temp to i, or use i directly.
You should print the array contents after the sort, otherwise the program has no effect.
Here is a corrected version:
#include <stdio.h>
#include <conio.h>
int min(int [], int, int);
int main(void) {
int i, temp, arr[20], n, loc;
printf("Enter a range of the array: ");
if (scanf("%d", &n) == 1) {
for (i = 0; i < n && i < 20; i++) {
printf("Enter element %d: ", i);
if (scanf("%d", &arr[i]) != 1)
break;
}
n = i; // n is the actual number of inputs
for (i = 0; i < n; i++) {
loc = min(arr, i, n);
temp = arr[i];
arr[i] = arr[loc];
arr[loc] = temp;
}
for (i = 0; i < n; i++) {
printf("%d\n" array[i]);
}
}
getch();
return 0;
}
int min(int arr[], int i, int n) {
int j;
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
i = j;
}
}
return i;
}

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

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.

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