C is a general-purpose computer programming language used for operating systems, games and other high performance work and is clearly distinct from C++. It was developed in 1972 by Dennis Ritchie for use with the Unix operating system.
6
votes
2answers
517 views
Simple calculator
I created my first program in C: a simple calculator. I want to know if it is possible to make this code more effective. Is it, for example, possible to change the calculator function so that I don't ...
9
votes
1answer
37 views
Interprocess Communication in a Farmer-Worker setup
The following code is my first C program I have made for an university assignment about Interprocess Communication. This involved creating a parent process, the farmer, and then creating a certain ...
4
votes
2answers
107 views
C library that converts integers to string and vice-versa
I've created this little library to help ease myself (and for others, hopefully) with the pain of having to convert integers to string and vice-versa. It's written in C, and for maximum portability I ...
0
votes
0answers
14 views
Validating constant assignments in C at compile time [on hold]
This is a way to check constant variable assignments for meeting conditions, e.g. exceeding limits at compile time. That is, anything the compiler can optimize to be constant. The Program should emit ...
7
votes
2answers
221 views
Simple key-value store in C, take 2
Followup to Simple key-value store in C.
Response to previous reviews
You may want to hide the KVSstore structure
Done.
Add a comparison function pointer to KVSstore. If this pointer is ...
-4
votes
0answers
22 views
Project Euler problem 8, I don't know what is going wrong [closed]
I did a course on C programming 2 years ago and I was trying to get back into it with the project Euler problems. My code is quite sloppy, sorry for that.
Problem 8 has you calculate the biggest ...
4
votes
2answers
64 views
Simple key-value store in C
I needed a simple key-value store. Here's what I came up with.
Usage
Use kvs_create and kvs_destroy to create stores and clean ...
6
votes
2answers
68 views
OpenGL text rendering library for Lua built on freetype-gl
FTGL wasn't making me happy, so I decided to try something else. This isn't completely finished; I plan to work more on layouts and alignment, and add a few other things. It should be far enough along ...
0
votes
0answers
25 views
Implementation of subtyping in C [closed]
I'm trying to figure out a good way to do subtyping in C:
statemachine.h
...
5
votes
4answers
105 views
Dynamically-sized stack - follow-up 3
Follow up of - Dynamically-sized stack - follow-up 2
I've took the tips given to me, and what I did now is:
Use same case type for type stack and it's functions
Returning 1 if push failed due to an ...
8
votes
1answer
39 views
Lua bindings for FTGL (FreeType font rendering in OpenGL)
I wrote some Lua bindings for FTGL's C API. This works well enough, but I ended up with lots of macros, one for each Lua function signature. For example, LUD_NUMBER_NUMBER_TO_NUMBER creates a Lua ...
5
votes
4answers
153 views
Creating nodes for linked list in a while loop in C
I am trying to create nodes for a linked list in a while loop. The code works, but it is not very pretty. How would I get the same results the proper way?
...
6
votes
1answer
56 views
Dynamically-sized stack - follow-up 2
Follow up of - Dynamically-sized stack - follow up
I've took the tips given to me, and what I did now is:
changing the function names to have a prefix
making the ...
4
votes
2answers
91 views
Translating warning codes as messages to be printed
I am writing an X application with Xlib in C, and this code is for error handling and error printing.
How to make it simpler and clearer without using a large amount of ...
4
votes
2answers
118 views
Dynamically-sized stack - follow up
Follow up of - Which is a better implementation of a stack data structure (based on an array)?
Please review my stack implementation from before. I've made it dynamically sized if needed and made a ...
2
votes
1answer
57 views
Which is a better implementation of a stack data structure (based on an array)?
This is a version I made:
...
7
votes
4answers
496 views
Three functions for computing simple arithmetic
Here is my code where I have 3 functions that do simple arithmetic:
...
2
votes
1answer
42 views
Sorting an int[] array using insertion sort algorithm - follow-up
Previous questions:
Is my implementation of insertion sort is correct?
Sorting an int[] array with an Insertion Sort
I'm currently learning about different sorting algorithms and after reading on ...
5
votes
1answer
50 views
Sorting an int[] array with an Insertion Sort
I'm currently learning about different sorting algorithms and after reading on the concept of the insertion sort of how it's done I've tried to implement it by myself before seeing how it was ...
2
votes
1answer
41 views
Is my implementation of insertion sort is correct? [closed]
I'm currently learning about different sorting algorithms and after reading on the concept of the insertion sort of how it's done I've tried to implement it by myself before seeing how it was ...
3
votes
2answers
66 views
Merge sorting linked lists
This works as expected and I want to get this code reviewed so that I can improve my coding ability.
I am concerned about readability and the quality of the code
...
2
votes
2answers
58 views
Comparing two bubble sort algorithms
I'm learning C and I've got to a point learning about different sorting algorithms. Before seeing how it was done, I wanted to try doing it myself based on what I read on how it's working.
...
12
votes
0answers
58 views
Tiny Lua library to get char pointer from string
Background
I'm using Lua with luaglut to do some OpenGL stuff. The luaglut API is almost identical to the gl/glut C APIs. Sometimes, gl functions want a pointer to some data, for example:
...
10
votes
4answers
463 views
Prime Factorization
I'm doing an Objective-C project and I need to do some prime factorization and so I came up with the following algorithm which is 99% C, except for the array part which was easiest to implement in ...
7
votes
4answers
117 views
Simple compression algorithm
An implementation of a simple compression algorithm that's featured in a programming practice book.
My goals:
Robust: All error conditions must be handled properly. The specification indicated in ...
5
votes
3answers
66 views
Bubble sorting in C
I've been into algorithms lately, so I wanted to apply a simple algorithm using a language of my choice, which was C in this case.
I've implemented the bubblesort algorithm (for strings) in a simple ...
2
votes
0answers
20 views
POSIX: Synchronous event library
I have implemented a small library that handles synchronous events with POSIX compliant threads. I oriented me on the already existing POSIX thread API. Here are the files I created:
...
0
votes
1answer
38 views
Possible memory issue in linked list program [closed]
I am trying to write a program which will count the occurrence of words in a paragraph.
The logic I am following :
I am using a linked list for the purpose. And I am searching sequentially - if new ...
9
votes
3answers
444 views
Checking for palindromes using dynamic memory allocation
This is a homework assignment, but it's already done and it's not for a grade; it's meant as a refresher for the rest of the course. I've never done anything with dynamic memory allocation in C ...
3
votes
1answer
36 views
Iterative Flood Fill implementation in C [closed]
I am trying to implement an iterative version of flood fill in C:
...
3
votes
0answers
46 views
Synchronous events library
I have implemented a small library that handles synchronous events with POSIX compliant threads. I oriented me on the already existing POSIX thread API. Here are the files I created:
...
3
votes
2answers
91 views
Game of Life generation procession algorithm
I've tried to make Game of Life in console and this is the algorithm I used to generate new generation:
...
10
votes
2answers
312 views
Minimalistic rmdir
In my eyes most of the GNU stuff is bloated and doesn't really fit my view of how a Linux environment should look like. I have come up with my own minimalist implementation of the GNU Coreutils, but ...
5
votes
2answers
63 views
DateTime internal representation to FILETIME
Here is a routine I put together to convert SQL Server's internal DateTime representation (BINARY(8)) to a ...
6
votes
1answer
62 views
Implementation of Tower of Hanoi iterative procedure
I have been working last night on implementing Tower of Hanoi without using recursion. I have found an algorithm on Wikipedia about the same topic here. I have implemented it and it's working fine ...
0
votes
1answer
5 views
Longest collatz sequence using dynamic programming
I am trying to solve longest collatz sequence problem under 1000000 with the below code. Can anyone suggest a faster way to approach this problem? I was thinking of dynamic programming, but I'm ...
11
votes
6answers
493 views
Efficiency with strcpy, strcat and malloc
I'm still learning C, but I'm trying to make sure I've got a decent grasp on working with "strings" and data structures.
If possible, I'd like a little input on how I'm handling this and see if
it ...
5
votes
4answers
127 views
NULL-eating / elimination version of fgets() in C
I've come up with the following routine to read data from a file stream, but I don't want any NULLs in the stream to terminate the string early. This function will ...
-4
votes
2answers
82 views
“Magic Fraction” finder
A Magic Fraction for N is one that has the following properties:
It is a proper fraction (the value is < 1)
It cannot be reduced further (the GCD of the numerator and the denominator ...
1
vote
2answers
80 views
Using brute force to find an energy optimal path for a given amount of time
The code below implements a brute force method to find an energy optimal path for a given amount of time t_aim using recursion (the recursion function is ...
10
votes
3answers
104 views
Add color to terminal output
I have the following header included in some of my projects so that I can add a little color to the terminal output. Here is how it would be used:
...
6
votes
2answers
84 views
Diagonal gradient optimization
With the SDL library, I'm creating an SDL_Surface* at w and h dimensions.
...
7
votes
4answers
551 views
A simple command line linear interpolator in C
I'm trying to become more familiar with C (been working through the MIT Practical Programming in C course and K&R) so decided to write a simple command-line linear interpolator. You can feed it a ...
7
votes
1answer
80 views
Queens placement
The code below is intended to be a learning material for high school intermediate programming class, as an introduction to recursion and backtracking. I was thinking of rook placement and found it too ...
-1
votes
1answer
2k views
Stone Game One Four [closed]
Alice and Bob are playing a game called "Stone Game". Stone game is a two-player game. Let N be the total number of stones. In each turn, a player can remove either one stone or four stones. The ...
2
votes
1answer
53 views
Find the nearest (small) number x where x can be represented by sum of cubes
I recently came across a problem where a number \$x\$ is given and I have to find \$y\$:
\$y < x > 1\$
\$y\$ can be expressed as \$a^3 + b^3\$ (more than one combination)
Example:
If 4105 ...
8
votes
1answer
344 views
Project Euler #2
Problem:
Each new term in the Fibonacci sequence is generated by adding the
previous two terms. By starting with 1 and 2, the first 10 terms will
be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
20
votes
4answers
2k views
Project Euler #1
If we list all the natural numbers below 10 that are multiples of 3 or
5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
My ...
8
votes
2answers
112 views
Reasonable way to implement “safe” math operations using _Generic?
I've been thinking about a way to make it easier to safely use math operations with C's basic datatypes (e.g. using the CERT C coding standard). So far, I've come up with something like this:
...
19
votes
5answers
2k views
Aliens at the train
I solved this problem on SPOJ:
The aliens have arrived to Earth and everything is in harmony, the two
races can live together. However, one specific Female Alien does not
want to see humans ...