October 22, 2024
Chicago 12, Melborne City, USA
C#

Why printf show false value of an hex number

Code char a; a = 0xf1; printf("%x\n", a); Output fffffff1 printf() show 4 bytes, that exactly we have one byte in a. What is the reason of this misbehavior? How can I correct it? You need to sign in to view this answers

Read More
C#

C, Calculating the distance between two GPS locations?

If I have two GPS locations, say 51.507222, -0.1275 and 48.856667, 2.350833, what formula could I use to calculate the distance between the two? I’ve heard a lot about a haversine formula, but can’t find any information about it, or how to apply it to C. I’ve written the following code, however, it’s very innacurate.

Read More
C#

Anonymous union within struct not in c99?

here is very simplified code of problem I have: enum node_type { t_int, t_double }; struct int_node { int value; }; struct double_node { double value; }; struct node { enum node_type type; union { struct int_node int_n; struct double_node double_n; }; }; int main(void) { struct int_node i; i.value = 10; struct node n;

Read More
C#

C calculator refuses to work when more than 1 “if” statements are added

I made a small C calculator earlier today, and I noticed it worked fine when the only operation it was able to do was addition, however when I tried to add subtraction, multiplication or division, it would simply not give an output, and I don’t know the reason for it as another calculator I had

Read More
C#

Reinterpret cast in C – cannot take the address of an rvalue

I have two structs struct A and struct B with the same memory layout, and I want to cast a return value of type struct A to type struct B, for example, in C++, I can achieve this goal in single line (without any temporary variable) like this: #include <utility> struct Temp { int a;

Read More
C#

c code for solving an augmented matrix of size [N][N+1]

#include <stdio.h> #include <stdlib.h> #include <math.h> void pivot(double **matrix, int n, int row) { double maxElement = fabs(matrix[row][row]); int maxRow = row; for (int i = row + 1; i < n; i++) { if (fabs(matrix[i][row]) > maxElement) { maxElement = fabs(matrix[i][row]); maxRow = i; } } if (maxRow != row) { double *temp =

Read More
C#

How to debug a recursive Sokoban solver, which occasionally runs with wrong data

I am writing a program to solve Sokoban levels – because why not. The initial trigger for writing the program was to show a colleague how a program can be written using the MVC pattern. From there, it evolved. The good: for some warehouses, the solver finds the proper solutions. I know because I created

Read More
C#

Can't get the right output for perfect number algo

trying to write a perfect number algorithm but can’t get right output even thought 28 is a perfect number. #include <stdio.h> int main() { int x = 0; int sum; printf("enter a number: "); scanf("%d", &x); for(int i = 1;i < x;i++) { if(x % i==0) { printf("%d ", i); sum = sum + i;

Read More
C#

Remove characters from a string in C

I only have access to ‘C’ and need to replace characters within a character array. I have not come up with any clean solutions for this relatively simple procedure. I pass in a character array, for example: char strBuffer[] = "/html/scorm12/course/course_index.jsp?user_id=100000232&amp;course_id=100000879&amp;course_prefix=ACQ&amp;version=2&amp;scorm_version=3&amp;roster_id=100011365&amp;course_name=Test%20Course%201.2&amp;mode=browse&amp;course_number=0000&amp;mode_id=1"; I need to modify this buffer to replace all the &amp; with &. The

Read More
C#

Division by zero not causing runtime exception on Nvidia Jetson

I’m not very familiar with the internal details of ARM processors, but I do not understand the following behaviour on my Nvidia Jetson Nano dev board. C code sample … //main.c #include <stdio.h> int main() { int fred = 123; int i; for(i = -10 ; i <= 10 ; i++) printf("%d / %d ==

Read More