Optimization with a float in c - c

I am currently doing exercices on Kattis and I meet a problem with I think the float.
I must compare the size of matchstick with the size of box
Sibice problem on Kattis.com
Here is a picture of the exercise
I can make examples but when I submit I past only the first two...
Here is a picture of my submission
I don't have a trace or explication for know my errors... I tried to change the type of my variables but there is no change... I think the problems is float but I need it.
Here is my code
#include <stdio.h>
void sibice(float n, float w, float h)
{
float v = 0;
for(float i = 0; i != n; i += 1) {
scanf("%f", &v);
if(v < w + h / 2)
printf("DA\n");
if(v == w + h / 2)
printf("DA\n");
if(v > w + h / 2) {
printf("NE\n");
}
}
}
int main(void)
{
float n = 0;
float w = 0;
float h = 0;
scanf("%f %f %f", &n, &w, &h);
sibice(n, w, h);
return (0);
}
Do you think that I can optimize my code ?

Incorrect test
If a match can fit is a more like v*v <= h*h + w*w than v < w + h / 2. Can it diagonally fit?
See Pythagorean theorem
#include <math.h>
...
float hyp = hypotf(h,w); // sqrt(h*h + w*w)
for(float i = 0; i != n; i += 1) {
scanf("%f", &v);
// if(v < w + h / 2)
if(v < hyp)
No need for 3 tests
One test is sufficient.
if(v <= hyp) {
printf("DA\n");
} else {
printf("NE\n");
}
Integer math
As all calculations are whole numbers, code could use int only math.
void sibice(int n, int w, int h) {
int hyp2 = h*h + w*w;
for (int i = 0; i != n; i += 1) {
int v;
scanf("%d", &v);
if (v*v <= hyp2)
...
Other issues may exist

Why do you need to use float? With these changes the code passes the tests:
#include <stdio.h>
#include <stdlib.h>
void sibice(int n, int w, int h)
{
int v;
int result;
for (int i = 0; i != n; i += 1) {
result = scanf("%d", &v);
if (1 != result)
exit(1);
if (v * v <= w * w + h * h)
printf("DA\n");
else
printf("NE\n");
}
}
int main(void)
{
int n = 0;
int w = 0;
int h = 0;
int result = 0;
result = scanf("%d %d %d", &n, &w, &h);
if (3 != result)
return 1;
sibice(n, w, h);
return (0);
}

Related

This solution would not accept any floats

Currently on one of the cs50x problem sets 'Cash', which is a simple 'ask for how much change is owed, then calculate how many coins are required' task, so not here asking for a solution but, I don't understand why this won't work.
While it does ask for an input, when I type in a float such as 5.96, it simply hangs. No returns, no errors whatsoever. I have to force it shut. The other thing is the while loop doing the same when set to 0, which is the intended way of doing things in order to get the exact number of coins.
I know how inefficient this code is and there are simpler ways of doing things. I just wish to understand the whys in order to avoid making the same mistakes moving on. Thanks.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
// changes and other containers
int p = 1;
int n = 5;
int d = 10;
int q = 25;
int x = 0;
float c;
// get how much change is owed in float
do
{
c = get_float("Change owed: ");
}
while (c < 0);
// int conversion to avoid imprecision
int a = round(c * 100);
// 1 because 0 spits out an unknown error
while (a >= 1)
{
// if the converted amount is bigger than a quarter
if (a >= q)
{
// x = number of coins, a = amount left
x = a / q;
a = a % q;
}
else if (a >= d)
{
x = x + a / d;
a = a - a % d;
}
else if (a >= n)
{
x = x + a / n;
a = a - a % n;
}
else
{
x = x + a / p;
a = a - a % p;
}
}
printf("%i\n", x);
printf("%i\n", a);
}
Thanks to WhozCraig, I figured out that my logic was at fault.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
// changes and other containers
int p = 1;
int n = 5;
int d = 10;
int q = 25;
int x = 0;
float c;
// get how much change is owed in float
do
{
c = get_float("Change owed: ");
}
while (c < 0);
// int conversion to avoid imprecision
int a = round(c * 100);
// 1 because 0 spits out an unknown error
while (a >= 1)
{
// if the converted amount is bigger than a quarter
if (a >= q)
{
// x = number of coins, a = amount left
x = a / q;
a = a % q;
}
else if (a >= d)
{
x = x + a / d;
a = a % d;
}
else if (a >= n)
{
x = x + a / n;
a = a % n;
}
else
{
x = x + a / p;
a = a % p;
}
}
printf("%i\n", x);
printf("%i\n", a);
}

Need help finding the mistake in the C programme

I am trying to calculate value of sin(x) in C but I am getting black screen in code:block after execution, its taking long for compilation and execution.
#include<stdio.h>
float mult(float x, int m, int i) {
float a = x;
if (i == m) {
return x;
} else {
i++;
a = a * mult(x, m, i);
return a;
}
}
int fact(int m) {
printf("%d! ", m); fflush(stdout);
int b;
if (m == 1) {
return 1;
} else {
b = m * fact(m - 1);
return b;
}
}
float term(float x, int m) {
float a = 0, b = 0, c = 0;
int i = 0;
a = mult(x, m, i);
b = fact(m);
c = a / (1.0 * b);
return c;
}
float sinof(float x, int m, int n) {
float b = 0;
if (m >= 10) {
return (0);
} else {
printf("......%d ", m); fflush(stdout);
b = term(x, m);
m = m + 2;
n = -n;
b = b + (n * sinof(x, m, n));
return b;
}
}
int main() {
float x = 0, sin = 0;
int m = 1, n = 1;
printf("Enter the angle in radians:");
scanf("%f", &x);
sin = sinof(x, m, n);
printf("%f", sin);
}
I hope the logic is correct.
m is odd. Below fails to stop recursion.
if(m==10){ // Never true
return(0);
} else{
b=term(x,m);
m=m+2;n=-n; // ***********
b=b+(n*sinof(x,m,n));
return b;
}
I recommend OP get own code working first. There are various other issues.
For a simplified recursive sine(), mouse over to see.
static double my_sin_helper(double xx, double term, unsigned n) {
if (term + 1.0 == 1.0) {
return term;
}
return term - my_sin_helper(xx, xx *term / ((n + 1) * (n + 2)), n + 2);
}
// valid for [-pi/2 + pi/2]
double my_sin_primary(double x) {
return x * my_sin_helper(x * x, 1.0, 1);
}

Feige Fiat Shamir Scheme not working

I'm trying to implement the Feige Fiat Shamir Identification Scheme in C (Arduino) and it works, but only when e = 0. When e = 1 it doesn't work.
How can I make it work?
#include <Wire.h>
int getGCD(int a, int b)
{
int c;
while (a != 0)
{
c = a;
a = b % a;
b = c;
}
return b;
}
int getCoprime(int n)
{
int coprime;
do
{
coprime = random(1, n);
}
while (getGCD(n, coprime) != 1);
return coprime;
}
//Preparation
int n = 7 * 3;
int s = getCoprime(n);
int v = (s * s) % n;
void loop ()
{
e = random(0, 2);
r = random(1, n);
int y = (r * (int)pow(s, e)) % n;
int x = (r * r) % n;
int ysqmodn = y * y % n;
int test = (x * (int)pow(v, e)) % n;
if(ysqmodn == test)
{
Serial.print("The current ICC matches. \n");
}
else
{
Serial.print(String(e));
Serial.print("\n");
}
delay(500);
}
It does work when e==1. When e==0 the computation is trivial, since s and v fall out due to power of 0 always being 1. This is the code copied and altered only enough to get it to compile.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
int random (int low, int high) {
return low + rand() % (high - low);
}
int getGCD(int a, int b) {
int c;
while (a != 0)
{
c = a;
a = b % a;
b = c;
}
return b;
}
int getCoprime(int n) {
int coprime;
do
{
coprime = random(1, n);
}
while (getGCD(n, coprime) != 1);
return coprime;
}
int main(void) {
int e, x, y, r, n, s, v, test, ysqmodn;
srand((unsigned)time(NULL));
n = 7 * 3;
s = getCoprime(n);
v = (s * s) % n;
e = random(0, 2);
r = random(1, n);
printf("n=%d, s=%d, e=%d, r=%d\n", n,s,e,r);
y = (r * (int)pow(s, e)) % n;
x = (r * r) % n;
ysqmodn = y * y % n;
test = (x * (int)pow(v, e)) % n;
if(ysqmodn == test)
printf("The current ICC matches. \n");
else
printf("%d\n", e);
return 0;
}
Sample results:
n=21, s=2, e=1, r=2
The current ICC matches.
n=21, s=11, e=0, r=12
The current ICC matches.
n=21, s=8, e=1, r=14
The current ICC matches.
n=21, s=17, e=1, r=13
The current ICC matches.
n=21, s=1, e=0, r=9
The current ICC matches.
n=21, s=4, e=0, r=13
The current ICC matches.

FIR filter in C?

I have a homework to implement an FIR filter in C and I wonder whether you think I understood the assignment correctly. The program I wrote that I think solves the problem is:
#include <stdio.h>
float FIRfloats[5];
void floatFIR(float newsample)
{
int i;
float sum=0;
FIRfloats[0]=newsample*0.0299;
FIRfloats[1]=FIRfloats[2]*0.4701;
FIRfloats[2]=FIRfloats[3]*0.4701;
FIRfloats[3]=FIRfloats[4]*0.0299;
/* sum */
for(i=0;i<5;i++)
{
sum=sum+FIRfloats[i];
}
printf("Sum: %f\n", sum);
}
int main ()
{
float n=0.0f;
while (scanf("%f", &n) > 0)
{
floatFIR(n);
}
return 0;
}
And the specification is
Before a new sample xk arrives the old samples are shifted to the
right and then each sample is scaled with a coefficient before the
result yk, the total sum of all scaled samples, is calculated
Coefficients should be c0=0.0299, c1=0.4701, c2=0.4701, c3=0.0299.
Do you think that I solved the assignment correctly? I think it seemed too easy and therefore I wonder.
I'm afraid the implementation provided in the question will not provide the correct results.
In FIR (Finite Impulse Response) filter with 4 coefficients the output series (y) for input series (x) is:
y[t] = c0*x[t] + c1*x[t-1] + c2*x[t-2] + c3*x[t-3]
Therefore the implementation should be similar to:
/* add includes (stdio.h and whatever else you'll need...) */
float floatFIR(float inVal, float* x, float* coef, int len)
{
float y = 0.0;
for (int i = (len-1) ; i > 0 ; i--)
{
x[i] = x[i-1];
y = y + (coef[i] * x[i]);
}
x[0] = inVal;
y = y + (coef[0] * x[0]);
return y;
}
main(int argc, char** argv)
{
float coef[4] = {0.0299, 0.4701, 0.4701, 0.0299};
float x[4] = {0, 0, 0, 0}; /* or any other initial condition*/
float y;
float inVal;
while (scanf("%f", &inVal) > 0)
{
y = floatFIR(inVal, x, coef, 4);
}
return 0;
}
This does the shift and multiplication at the same loop (which does not affect results - only is more efficient.)
If you want to follow the spec exactly, you can change floatFir like this:
float floatFIR(float inVal, float* x, float* coef, int len)
{
float y = 0.0;
for (int i = (len-1) ; i > 0 ; i--)
{
x[i] = x[i-1];
}
x[0] = inVal;
for (int i = 0 ; i < len ; i++)
{
y = y + (coef[i] * x[i]);
}
return y;
}

Howto compute the factorial of x

how to get the value of an integer x, indicated by x!, it is the product of the numbers 1 to x.
Example: 5! 1x2x3x4x5 = 120.
int a , b = 1, c = 1, d = 1;
printf("geheel getal x = ");
scanf("%d", &a);
printf("%d! = ", a);
for(b = 1; b <= a; b++)
{
printf("%d x ", c);
c++;
d = d*a;
}
printf(" = %d", d);
how to get the som of an integer x, indicated by x!, is the product of the numbers 1 to x.
Did you mean factorial of x ?
Change d = d*a; to d = d*b inside the loop
You can simply do:
for(b = 1; b <= a; b++) {
d *= b;
}
// d now has a!
This is the optimal implementation in size and speed:
int factorial(int x)
{
static const int f[13] = { 1, 1, 2, 6, 24, 120, /* ... */ };
if ((unsigned)x < (sizeof f/sizeof f[0])) return f[x];
else return INT_MAX+1; /* or your favorite undefined behavior */
}
Hint: x! (x factorial) does not fit in an int except for very very small values of x.
Try
d = d * b;
instead of
d = d * a
and it should work fine
You actually have a lot of redundant code there, that might be why you did not spot the error yourself.
To calculate the factorial, you only need the accumulator (d in the above code) and the input (a). Why?
My code is not good as other but it works for me:
#include <iostream>
using namespace std;
unsigned int fattoriale (int n){
if (n == 1){
return 1;
}
else {
return n * fattoriale(n-1);
}
}
int main() {
int tmp, num;
cin >> num;
tmp = fattoriale(num);
cout << "Stampo il fattoriale del numero inserito: " << tmp << endl;
}
int factorial(int x)
{
int f;
if (x == 0)
{
f = 1;
}
else if (x > 0)
{
f = x*factorial(x-1);
}
return f;
}
int main()
{
int n = 0;
cout << factorial(n);
return 0;
}

Resources