Trouble dereferencing pointer in c [closed] - c

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 3 years ago.
Improve this question
I am a bit of a sports better, and I am finding myself manually computing the decimal odds from the moneyline odds commonly found on many sportsbooks. I am trying to write a program that will at least remove the burden of calculating this over and over again.
My program is one that will take the inputs "team-one moneyline-teamone team-two moneyline-teamtwo" and output the team name with the decimal odds for a back and lay bet as below:
Input: Rams +230 Bucs -280
I have tried messing around with all of the permutations of dereferencing but can't seem to stop it from giving a segmentation fault.
#include <stdio.h>
#include <stdlib.h>
void calcML(int a, float *b, float *c);
int main (int argc, char* argv[])
{
int oneML, twoML;
float oneDF, oneDA, twoDF, twoDA;
oneML = atoi(argv[2]);
twoML = atoi(argv[4]);
printf("Team one is %s with a ML of %i.\nTeam two is %s with a moneyline of %i.\n", argv[1], oneML, argv[3], twoML);
calcML(oneML, &oneDF, &oneDA);
calcML(twoML, &twoDF, &twoDA);
printf("%s %f %f\n", argv[1], oneDF, twoDA);
printf("%s %f %f\n", argv[3], twoDF, oneDA);
}
void calcML (int a, float *b, float *c)
{
if (a < 0){
int tmp = -a;
*b = 1 + tmp/100;
*c = 100/tmp;
}
else {
*b = 1 + a/100;
*c = 100/a;
}
}
As above, I would expect the output to be:
Team one is Rams with a ML of +230.
Team two is Bucs with a ML of -280.
Rams 3.30 3.80
Bucs 1.36 1.43
Right now the output is:
Team one is Rams with a ML of +230.
Team two is Bucs with a ML of -280.
Segmentation fault
Like I said, I know it is a pointer arithmetic issue, but I have tried a million different permutations and can't figure it out. Thank you to any kind soul that doesn't mind pointing out my dumb mistake!

I was unable to reproduce the segfault, but as someone else has mentioned you appear to be accidentally truncating any floating point values. In calcML, when you execute 1 + tmp/100; the compiler is "helping you" by coercing tmp to an integer. This was using clang 10.0.1 on a MacBook. I would imagine that changing 1 to 1.0 and 100 to 100.0 will help clarify some of the math. Your pointers at least all look correct from what I can tell.

This is the completed code after fixing the float and some of the arithmetic.
#include <stdio.h>
#include <stdlib.h>
void calcML(int a, float *b, float *c);
int main (int argc, char* argv[])
{
int oneML, twoML;
float oneDF, oneDA, twoDF, twoDA;
oneML = atoi(argv[2]);
twoML = atoi(argv[4]);
printf("Team one is %s with a moneyline of %i.\nTeam two is %s with a moneyline of %i.\n", argv[1], oneML, argv[3], twoML);
calcML(oneML, &oneDF, &twoDA);
calcML(twoML, &twoDF, &oneDA);
printf("%s %f %f\n", argv[1], oneDF, oneDA);
printf("%s %f %f\n", argv[3], twoDF, twoDA);
}
void calcML (int a, float *b, float *c)
{
if (a < 100){
float tmp = -a;
*b = 1.0 + 100.0/tmp;
*c = tmp/100.0 + 1;
}
else {
*b = 1.0 + a/100.0;
*c = 100.0/a + 1;
}
}

Related

How to store numbers in malloc array? [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 11 days ago.
Improve this question
I want to use the count variable to store numbers in the W array using malloc, but the output keeps repeating the same number entered.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int *w = malloc( sizeof(int) * 10000);
int x; int count = 0;
while (scanf("%d", &x) == 1) {
printf("%d", &x]);
*(w + x) = count;
count++;
}
free(w); w = NULL;
return 0;
}
argc, argv are not used so leave them out.
Prefer sizeof a variable (*w) opposed to a type (int). If the type changes you only need to make the change in one place.
Use constants instead of magic values.
You don't need the temporary variable x.
Prefer unsigned (size_t) types to (unsigned) int for values that should not be negative. It eliminates a possible error class.
Prefer a for to while-loop when counting things. It might be more readable like this:
for(size_t count = 0;; count++) {
if(scanf("%d", &w[count]) != 1)
break;
printf("%d\n", w[count]);
}
printf("%d", ...) takes a value not an address. This appears to be the main issue.
For readability by user add a new newline when printing the value.
#include <stdio.h>
#include <stdlib.h>
#define LEN 10000
int main() {
int *w = malloc(LEN * sizeof *w);
for(size_t count = 0; scanf("%d", &w[count]) == 1; count++)
printf("%d\n", w[count]);
free(w);
}
example session:
$ ./a.out
1
1
2
2
4
4

Casting float to int doesn't work properly in C

I tried to solve a task on codewars and happened something strange, in one case casting worked as usual and in the second o got strange behavior. Here is the code:
#include <stdio.h>
#include <string.h>
#include <math.h>
int digital_root(int n) {
char tmp[30];
char *ptr;
while(n > 9){
sprintf(tmp, "%d", n);
int len = strlen(tmp);
float n_tmp = n / (pow(10, len));
n = 0;
for(int i = 1; i <=len; i++){
float t = n_tmp * (pow(10, i));
printf(" vars = [%f , %d] ", t, (int)t); //this line
n += (int) t;
n_tmp -= (int)t/pow(10,i);
}
}
return n;
}
int main()
{
digital_root(16);
return 0;
}
And the line printf(" vars = [%f , %d] ", t, (int)t); outputs: vars = [1.600000 , 1] vars = [6.000000 , 5], what is so strange. Can someone explain this behavior?
Every casting float to int is a rounding, so every time you do it, stop for a second and think: what kind of rounding do I need this time? Dropping the fractional part? floor()? ceil()? Or rounding positive float towards nearest int? If 5.999999999999 should be 6, use (int)(t+.5), not (int)t.
Or you can mimic the printf behavior and round only the smallest float fraction, which can vary (depending on the biggest value you use). (int)(t+.000001) will round both 1.9 to 1 (as it probably should), and 3.9999999999 to 4 (because it is 4 with some tiny number representation errors).
You should however know the size of a fraction which can be an actual, meaningful part of your data. Everything smaller than this fraction must be rounded. It can be either .00000001 or .5, that depends on your needs and the algorithm itself.

how to pass pointers as parameters in a function? [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 3 years ago.
Improve this question
I am trying to print the sum and diffrence of numbers using pointers but i am getting only sum as output.
#include <stdio.h>
#include<math.h>
void update(int *a,int *b)
{
int sum,sub;
sum = *a + *b;
printf("",sum);
sub = abs(*a - *b);
printf("",sub);
}
int main()
{
int a, b;
int *pa = &a, *pb = &b;
scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}
If I have understood correctly (taking into account the function name) you mean something like the following
#include <stdio.h>
#include <stdlib.h>
void update( int *a, int *b )
{
*a += *b;
*b = abs( *a - *b - *b );
}
int main(void)
{
int a, b;
scanf( "%d%d", &a, &b );
update( &a, &b );
printf( "a = %d, b = %d\n", a, b );
return 0;
}
If for example to enter two values 20 and 10 then the output will look like
a = 30, b = 10
Pay attention to that these calls of printf in your function
printf("",sum);
printf("",sub);
do not make sense.

Value assigned to s variable when uses %d instead %f [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 5 years ago.
Improve this question
I assume output to be 8 but the line s=a+b assign value 5 to s variable not sum of a+b. I know that I am using %d instead of %f.
#include <stdio.h>
void summ(int, int);
int main(){
int a = 3, b = 5;
summ(a, b);
return 0;
}
void summ(int a, int b){
float s;
s = a + b;
printf("%d", s);
}
You can't use any old format specifier in a printf(). It always has to match the type of the variable you are passing in. If you want to print a value as another type, you have to convert it first, then print the converted value. So either this has to be:
void summ(int a, int b)
{
float s;
s = a + b;
printf( "%f\n", s );
}
or
void summ(int a, int b)
{
int s;
s = a + b;
printf( "%d\n", s );
}
In the case of your sum function, you won't see a noticeable difference (except that %d does not print a ".0000" after the number, but you could have suppressed that by adding a length of 0 to the fraction of your %f by writing it as %.0f).
OTOH, if you have a division:
void divv(int a, int b)
{
float s;
s = a / b;
printf( "%f\n", s );
}
the CPU will perform a (often faster) integer division. That means that 3 / 5 (which would be 0.6) just gives you 0 because it didn't bother calculating the fraction. The compiler doesn't look at the type of variable an expression is assigned to, so it doesn't see that it could actually store the fraction. It just looks at the operands of the operator, which are both int, calculates an int, and expands it into a float. So if you wanted a precise result, you'd have to do:
void divv(int a, int b)
{
float s;
s = (float)a / (float)b;
printf( "%f\n", s );
}
because as soon as one of the arguments is a float, it will perform a floating point division and also calculate the fraction.
here is a possible version of the code that cleanly compiles and performs the desired functionality.
#include <stdio.h>
void summ(int, int);
int main( void )
{
int a = 3;
int b = 5;
summ(a, b);
return 0;
} // end function: main
void summ(int a, int b)
{
float s;
s = (float)(a + b);
//printf("%d", s);
printf( "%f\n", s );
} // end function: summ
The output from the above code is:
8.000000

Composite Simpson's Rule in C [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 9 years ago.
Improve this question
I'm trying to program Composite Simpson's Rule in C. The formula is:
where x_j=a+jh for j=0, 1, ..., n-1, n with h=(b-a)/n; in particular, x_0=a and x_n=b.
For some reason, the first and second loop have the same value. I checked over this a lot of times, but I can't seem to find my mistake.
#include <stdio.h>
#include <math.h>
float f(float);
float a;
float b;
float x;
float h;
int n;
int j;
a=0;
b=2;
n=8;
h = (n - j) / b;
float first;
float second;
int main() {
sum = (h / 3.0f) * (f(h) + f(n));
printf("%f\n", sum);
second = (4.0f) * h * f(a);
}
printf("second sum: %f\n",second );
sum = sum + first + second;
printf("%f\n", sum);
return 0;
}
The answer should be around 3.1 (The value of the final sum)
Your divisions won't probably do what you expect:
(2 / 3) == 0
Dividing int with int will result in int.
Use float constants (2.0f / 3.0f)
Edit:
You still have the same problem with the other n / 2.
And you should use %f when printing floats: printf("first sum: %f\n",first);
The value of the integral is: 3.109337

Resources