I am trying to calculate the minimum coin denomination using the dynamic programming approach, but my code stops running after the 2nd query even though it should run for 10 times if I provide T = 10. Why is it stopping?
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int min(int x, int y)
{
return (x < y) ? x : y;
}
short int min_denom(short int N)
{
short int *table, i, j, x;
table = (short int*)malloc(N * sizeof(short int));
for(i = 0; i < N; i++)
table[i] = 1 + i;
for(i = 1; i <= (short int)sqrt(N); i++)
for(j = 0; j < N; j++)
if(j == i)
table[j] = min(1, table[j]);
else if(j > i)
table[j] = min(table[j - i - 1] + 1, table[j]);
x = table[N - 1];
free(table);
return x;
}
int main()
{
short int T, N, i;
scanf("%d", &T);
for(i = 1; i < T; i++)
{
scanf("%d", &N);
printf("%d\n", min_denom((short int)N));
}
scanf("%d", &N);
printf("%d\n", min_denom((short int)N));
return 0;
}
The output is:
10
100
10
500
22
Then it stops running automatically.
The code will run till the end if we just replace "%d" either with "%hd" or "%hi". The reason is since the input we are taking will be stored in a short int type of variable, so we must use the proper access specifier for that.
The changed portion is given below.
short int T, N, i;
scanf("%hi", &T);
for(i = 1; i < T; i++)
{
scanf("%hi", &N);
printf("%hi\n", min_denom((short int)N));
}
scanf("%hi", &N);
printf("%hi\n", min_denom((short int)N));
Related
This program gives me zero primes in the array after running it. This is a program that takes 2d array [n x m] then calculate how many prime numbers are there in the 2d array.
int isprime(int n)
{
int k;
if (n <= 1)
return 0;
for (k = 2; k <= (n / 2); k++){
if (n % k == 0)
return 0;
}
return 1;
}
}
int primecount(int x, int y, int a[x][y]){
int r, c, count = 0;
for(r = 0; r < x; r++) {
for(c = 0; c < y; c++) {
if(isprime(a[r][c]))
{
count++;
}
}
}
return count;
}
int main()
{
int n, m, i, j, z;
printf("Enter the Number of Rows: ");
scanf("%d", &n);
printf("\n");
printf("Enter the Number of Columns: ");
scanf("%d", &m);
printf("\n");
int a[n][m];
printf("Enter the elements of the array: \n");
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
scanf("%d", &a[i][j]);
}
z = primecount(n, m, a[n][m]);
printf("\n");
printf("The Number of Prime Numbers in the array is: %d", z);
printf("\n");
return 0;
}
For starters instead of this call
z = primecount(n, m, a[n][m]);
you need to write
z = primecount(n, m, a);
In this call of the function isprime as in the call shown above
if(isprime(r, c, a[r][c]))
the expression a[r][c] is a scalar object of the type int. However the function isprime expects a two-dimensional array instead of a scalar object of the type int.
int isprime(int p, int q, int a[p][q])
^^^^^^^^^^^
Just declare the function like
int isprime( int x );
and correspondingly change its definition.
The function will be called like
if( isprime( a[r][c] ) )
Pay attention to that the logic of the function isprime is incorrect. It returns logical true for values equal to 1 and 0 though such values are not prime numbers.
Also you need to deal with an array with elements of the type unsigned int. Otherwise the user can enter negative values.
Here is your updated program.
#include <stdio.h>
int isprime(int n)
{
int k;
if (n <= 1)
return 0;
for (k = 2; k <= (n / 2); k++)
{
if (n % k == 0)
return 0;
}
return 1;
}
int primecount(int x, int y, int a[x][y]) //function to count prime numbers
{
int r, c, count = 0;
for(r = 0; r < x; r++)
{
for(c = 0; c < y; c++)
{
if(isprime(a[r][c]))
{
count++;
}
}
}
return count;
}
int main()
{
int n, m, i, j, z;
printf("Enter the Number of Rows: ");
scanf("%d", &n);
printf("\n");
printf("Enter the Number of Columns: ");
scanf("%d", &m);
printf("\n");
int a[n][m];
printf("Enter the elements of the array: \n");
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
scanf("%d", &a[i][j]);
}
z = primecount(n, m, a);
printf("\n");
printf("The Number of Prime Numbers in the array is: %d", z);
printf("\n");
return 0;
}
Its output might look like
Enter the Number of Rows: 2
Enter the Number of Columns: 2
Enter the elements of the array: 1 2 3 4
The Number of Prime Numbers in the array is: 2
The below code shows the error, "Constant Expression Required." I tried some solutions but still I was unable to solve.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int coins( int S[], int m, int n ) {
int i, j, x, y;
int table[n+1][m];
for (i=0; i<m; i++)
table[0][i] = 1;
for (i = 1; i < n+1; i++) {
for (j = 0; j < m; j++) {
x = (i-S[j] >= 0)? table[i - S[j]][j]: 0;
y = (j >= 1)? table[i][j-1]: 0;
table[i][j] = x + y;
}
}
return table[n][m-1];
}
int main() {
int arr[20],n;
int m,i;
printf("********* MAKING CHANGE PROBLEM *********");
printf("\nEnter size of array:");
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",arr[i]);
}
m = sizeof(arr)/sizeof(arr[0]);
printf("The total number of combinations of coins that sum up to %d",n);
printf(" is %d ", coins(arr, m, n));
getch();
return 0;
}
Based on the different versions of C Language standard, following is not allowed, mostly the error is arising from here
int table[n+1][m];
instead use #define NSIZE 10 and #define MSIZE 20 and int table[NSIZE+1][MSIZE];etc
and scanf("%d",arr[i]); will cause problems use scanf("%d",&arr[i]);
I just completed a challenge in Dcoder using C. I got one correct test case out of three, and I can't seem to find the problem that prevents me from getting the other two test cases right as well. I'm still new to C, so please excuse if my code may look inexperienced. Any help is appreciated.
Here is the task:
Problem Statement
Students of Dcoder school love Mathematics. They love to read a variety of Mathematics books. To make sure they remain happy,their Mathematics teacher decided to get more books for them. A student would become happy if there are at least X Mathematics books in the class and not more than Y books because they know "All work and no play makes Jack a dull boy".The teacher wants to buy a minimum number of books to make the maximum number of students happy.
Input
The first line of input contains an integer N indicating the number of students in the class. This is followed up by N lines where every line contains two integers X and Y respectively.
Output
Output two space-separated integers that denote the minimum number of mathematics books required and the maximum number of happy students. Explanation: The teacher could buy 5 books and keep student 1, 2, 4 and 5 happy.
Constraints
1<=N<=10000 1<=X,Y<=10^9
Sample Input
5
3 6
1 6
7 11
2 15
5 8
Sample Output
5 4
And here is my code:
#include <stdio.h>
//Compiler version gcc 6.3.0
typedef struct{
int minBooks;
int maxBooks;
} Student;
typedef struct{
int books;
int happyStudents;
} Happiness;
void fillarray(Student *arr, int len){
for(int i = 0; i < len; i++){
scanf(" %d", &arr[i].minBooks);
scanf(" %d", &arr[i].maxBooks);
}
}
int getmaximum(Student *arr, int len){
int max = 0;
for(int i = 0; i < len; i++)
if(arr[i].maxBooks > max)
max = arr[i].maxBooks;
return max;
}
Happiness *calchappiness(Student *arr, int len, int max){
Happiness *re = malloc(max * sizeof(Happiness));
for(int i = 1; i <= max; i++){
re[i - 1].books = i;
for(int j = 0; j < len; j++){
if(i >= arr[j].minBooks && i <= arr[j].maxBooks)
re[i - 1].happyStudents++;
}
}
return re;
}
Happiness getmaxhappiness(Happiness *arr, int len){
Happiness re;
re.happyStudents = 0;
for(int i = 0; i < len; i++)
if(arr[i].happyStudents > re.happyStudents)
re = arr[i];
return re;
}
Happiness getminbooks(Happiness *arr, int len){
Happiness re;
re.books = 1000;
for(int i = 0; i < len; i++)
if(arr[i].books < re.books)
re = arr[i];
return re;
}
Happiness besthappiness(Happiness *arr, int len){
Happiness re = getminbooks(arr, len);
for(int i = 0; i < len; i++){
if(arr[i].happyStudents > re.happyStudents)
re = arr[i];
}
return re;
}
int main()
{
int len;
scanf(" %d", &len);
Student *students = malloc(len * sizeof(Student));
fillarray(students, len);
int happylen = getmaximum(students, len);
Happiness *happy = calchappiness(students, len, happylen);
Happiness output = besthappiness(happy, happylen);
printf("%d %d", output.books, output.happyStudents);
free(students);
free(happy);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
//Compiler version gcc 6.3.0
int main() {
int N, i, j;
long int *X, *Y, books = 0, students = 0, happy;
scanf("%i", &N);
X = (long int*)malloc(N*sizeof(long int));
Y = (long int*)malloc(N*sizeof(long int));
for(i = 0; i < N; i++)
scanf("%li %li", &X[i], &Y[i]);
for(i = 0; i < N; i++) {
happy = 0;
for(j = 0; j < N; j++)
if(X[i] >= X[j] && X[i] <= Y[j])
happy++;
if(happy > students || (happy == students && X[i] < books)) {
students = happy;
books = X[i];
}
}
printf("%li %li", books, students);
free(X);
free(Y);
return 0;
}
//Try this one hope its help
Apple and Orange problem.
Only 3 of 12 test cases are cleared. Can't think of anything else since hours.
sample input 0
7 11
5 15
3 2
-2 2 1
5 -6
sample output 0
1
1
Problem: https://www.hackerrank.com/challenges/apple-and-orange/problem
Code
int main(){
int s;
int t;
scanf("%d %d",&s,&t);
int a;
int b;
scanf("%d %d",&a,&b);
int m;
int n;
scanf("%d %d",&m,&n);
int *apple = malloc(sizeof(int) * m);
for(int apple_i = 0; apple_i < m; apple_i++){
scanf("%d",&apple[apple_i]);
}
int *orange = malloc(sizeof(int) * n);
for(int orange_i = 0; orange_i < n; orange_i++){
scanf("%d",&orange[orange_i]);
}
int fellap=0;
int fellor=0;
int d;
for(int apple_i = 0; apple_i < m; apple_i++){
d=apple[apple_i]+a;
f(d>=s && d<=t){
fellap++;
}
}
for(int orange_i = 0; orange_i < n; orange_i ++){
d=orange[orange_i]+b;
if(d>=s&&d<=t){
fellor++;
}
}
printf("%d\n", fellor);
printf("%d\n", fellap);
return 0;
}
The bug in your code is a very trivial one. You have switched the output of oranges and apples. Change
printf("%d\n", fellor);
printf("%d\n", fellap);
to
printf("%d\n", fellap);
printf("%d\n", fellor);
I don't see the need of reading all values into an array. You can simply traverse the input and do the calculations at the same time. Here is an example that passes all test cases:
Working code that passes all test cases:
int main(){
int s;
int t;
scanf("%d %d",&s,&t);
int a;
int b;
scanf("%d %d",&a,&b);
int m;
int n;
scanf("%d %d",&m,&n);
int noApples=0;
int noOranges=0;
for(int apple_i = 0; apple_i < m; apple_i++){
int apple;
scanf("%d",&apple);
if (apple+a >= s && t >= apple+a)
noApples++;
}
for(int orange_i = 0; orange_i < n; orange_i++){
int orange;
scanf("%d",&orange);
if (orange+b >= s && t >= orange+b)
noOranges++;
}
printf("%d\n%d\n", noApples, noOranges);
return 0;
}
QUESTION:
http://www.hpcodewars.org/past/cw17/problems/Prob02--CheckDigit.pdf
Here's my code:
int checkdigit(){
int n,i,j,sum1,sum2,k;
char ch;
printf("Enter the number of lines.Then enter ther the codes!");
scanf("%d",&n);
char *codes[n];
int msum[n];
int fsum[n];
for(i=0;i<n;++i){
scanf("%s",codes[i]);
}
for(i=0;i<n;++i){
for(j=0,k=3;j<21;j+=3,k+=3){
char *num;
num=codes[i];
ch=num[j];
sum1+=atoi(ch);
if(k<21)
ch=num[k];
sum2+=atoi(ch);
}
msum[i]=sum1*3;
fsum[i]=((msum[i]+sum2)%10);
if(fsum[i]!=0)
fsum[i]-=10;
}
for(k=0;k<sizeof(fsum);k++){
printf("%s %d",codes[k],fsum[k]);
}
return 0;
}
The Code now crashes after taking the first UPC code as input.
Change
fsum[i]=((msum+sum2)%10);
to
fsum[i]=((msum[i]+sum2)%10);
That is because msum is an array of integers, and msum[i] is an integer. As msum is an array, it is of type int* and is not compatible with int for the binary operator %
Here
char *codes[n];
is array of n pointers and you don't allocate memory for these pointers and try to scan values to this location so you see a crash
#include <stdio.h>
int checkdigit(int data[12]){
int i, even, odd, result;
even = odd = 0;
for(i = 0; i < 11; ++i){
if(i & 1)
even += data[i];
else
odd += data[i];
}
result = (odd * 3 + even) % 10;
if(result)
result = 10 - result;
return data[11] = result;
}
int main(){
int n;
scanf("%d", &n);
int codes[n][12];
int i, j;
for(i = 0; i < n; ++i){
for(j = 0; j < 11; ++j){
scanf("%d", &codes[i][j]);
}
checkdigit(codes[i]);
}
for(i = 0; i < n; ++i){
for(j = 0; j < 12; ++j){
if(j) putchar(' ');
printf("%d", codes[i][j]);
}
putchar('\n');
}
return 0;
}