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.

learn more… | top users | synonyms

4
votes
1answer
33 views

Diagonal gradient optimization

With the SDL library, I'm creating an SDL_Surface* at w and h dimensions. SDL_Color is a struct with 4 ...
7
votes
4answers
529 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
64 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
33 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
330 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, ...
18
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 ...
3
votes
0answers
48 views
6
votes
2answers
52 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 ...
10
votes
5answers
829 views

Caesar cipher cracker

How can I refactor this with less code? This is homework and is cracking a Caesar cipher-text using frequency distribution. I have completed the assignment but would like it to be cleaner. ...
6
votes
2answers
291 views

Project Euler #8: Maximum product of 13 consecutive digits

I've solved problem #8 in Project Euler, which asks to find the 13 consecutive digits with the greatest product from a particular 1000-digit number. I would like to hear any suggestions regarding my ...
16
votes
4answers
2k views

Generating 1000 random numbers

I have got the following C code and I feel it is crazy stupid and there must be a better way to do this, but just can't think of one and have yet to learn algorithms. ...
11
votes
7answers
3k views

Is this a fast implementation of reversing a string?

I thought of this algorithm today. It seems fast, but how can I know? ...
3
votes
2answers
122 views

Shuffling array elements in C

This is an answer to this problem. Basically, given an array, swap the given number of elements in the array. The solution that came to my mind: since we always swap at least two elements, pick two ...
0
votes
3answers
24 views

Determining if integers are sorted

I have this task to find whether 5 integers are ordered from the smallest to the largest or not. Depending on the results, the program output should be "yes" or "no". No loops or arrays supposed to ...
3
votes
3answers
82 views

List primes within an interval

I'm trying to solve the SPOJ primes problem: Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! ...
3
votes
5answers
331 views

Loading dictionary using trie in C very slow with large data set

The code below works for very small dictionary files I used to test (two words). However when I run this using a more typical dictionary file, my machine slows to the point of near crash. I would ...
0
votes
1answer
48 views

Multiple assignment operations in one statement [closed]

As I was writing a pretty straightforward loop to map the boundaries of a box, I found that I was doing this a lot: ...
6
votes
1answer
45 views

Unix 'sleep' clone - unsure of safety

I decided to try making some simple clones of the various user-space Unix utilities in an effort to learn the system-specific headers and improve my C skills. The code that follows is a clone of ...
4
votes
3answers
158 views

Dictionary load function using hash table

This loads a dictionary text file into memory to be used as part of a spell checker. It's part of a larger program, but I wanted general comments so I can clean it up further. ...
5
votes
2answers
84 views

Finding prime numbers in a range

Finding the prime numbers between two given integers in the minimum number of comparisons: ...
10
votes
3answers
348 views

Parsing ARP cache in C

How can I make this better? ...
10
votes
1answer
78 views

Bitcoin address validator in C

Here is a Bitcoin address validator I am looking to have reviewed in C. Normally I would have the enum and function prototypes declared in a header file, but I ...
7
votes
3answers
342 views

Hierarchical k-ary tree in C without relying on RAM

I created a k-ary tree in C to be used as an easy and efficient way to organize "UML-like" data in embedded devices. The left node is at the lower logical level (a child) while the right node is at ...
9
votes
3answers
366 views

Finding the largest of a series of numbers being XORed

I'm working on solving a problem involving finding the largest of a series of numbers being XORed. This is the actual problem: Xorq has invented an encryption algorithm which uses bitwise XOR ...
9
votes
2answers
94 views

Mapping enum to function - request / response library encapsulation

I'm writing code for an embedded device - specifically, a camera. The idea is different camera manufacturers can implement the code so it properly works with their camera. Here, I'm encapsulating the ...
8
votes
3answers
166 views

Output int from string input

This takes a string input like 1,2,3,4,5, splits it using , delimiter, converts each char to int and output it in a single line ...
3
votes
1answer
47 views

Simplifying linked list “search and delete” function

The function asks the user for a value to search for, then deletes all matching values in the list. The program runs flawlessly, but I can't figure out a simple/elegant way of separating cases for ...
2
votes
1answer
41 views

Implementation involving POSIX, semaphores, and locks

I am trying to get over my fear of multithreading programming and teaching myself to code using POSIX. I wrote a small version of the consumer producer problem. I am hoping I can get some feedback if ...
2
votes
0answers
28 views

C code for hash-consing: how could its performance be increased?

I've isolated this function, cons, for hash-consing in C. It is where the bottleneck of my program is. How could its performance be improved? ...
2
votes
0answers
37 views

Deleting nodes having greater value on right side

Problem Summary: Given a singly linked list, remove all the nodes which have a greater value on right side. Example: The list 12->15->10->11->5->6->2->3->NULL should be changed to 15->11->6->3->NULL. ...
2
votes
0answers
26 views

NGON (many polygons) on SPOJ - Time Limit Exceeded

I am trying to solve NGON problem. I am using bottom up dynamic programming here. Recurrence function is: ...
3
votes
0answers
57 views

Generating prime numbers using Sieve of Eratosthenes with CUDA

I'm learning CUDA and wrote a little program which generates prime numbers using the Sieve of Eratosthenes. (I know the limitations of CUDA, specially with memory sizes and limits, but this program is ...
4
votes
2answers
71 views

Static variable representing program state accessed with multiple functions

I have this method that performs certain functions based on the state that the algorithm is currently at: ...
3
votes
1answer
57 views

SPOJ is giving TLE for this solution of ROCK

I am solving ROCK problem on SPOJ. I have used bottom up dynamic programming approach for this problem. Time complexity for this is \$O(n^3)\$. ...
5
votes
4answers
378 views
5
votes
4answers
87 views

Stack implementation using only one queue in C

We start with an empty queue. For the push operation we simply insert the value to be pushed into the queue. The pop operation needs some manipulation. When we need to pop from the stack (simulated ...
2
votes
1answer
102 views

Sum the Multiples of 3 and 5

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 N. ...
0
votes
1answer
38 views

Implementing a bubble sort on a linked list [closed]

I'm using the following prototype: Data* bubble_sort(Data *list); I'm trying to implement a bubble sort given a linked list of names. Note: this returns ...
7
votes
1answer
155 views

Type-length-value (TLV) encode/decode

I wrote these methods to encode data as array of TLV objects, and also to serialize and deserialize them. Any feedback on improvements, etc. would be appreciated. Please note that I ignored ...
5
votes
1answer
81 views

Consumer-Producer Problem: POSIX Thread

I have implemented a producer consumer problem, following the resources below: Oracle doc CSEE I have used mutex_t and ...
5
votes
1answer
113 views

Spell Check and Trie implementation

I have written this code for an Edx course called CS50x (a beginner course). This problem set required me to write a program which: loaded a dictionary into some sort of data structure (I choose to ...
4
votes
1answer
33 views

Basic Reverse Polish CL Parser

K and R Exercise 5-10 Write the program expr, which evaluates a reverse Polish expression from the command line, where each operator or operand is a separate argument. For example, ...
7
votes
2answers
171 views

Can I make this simulation written in C more “C++-y”?

I am learning C++ from a C background, and I was wondering if I could convert this old C program I'd written into C++ as a personal exercise. The original C program is a simple Monte Carlo simulation ...
5
votes
3answers
139 views

Go Aaaaaaaaaaah

The problem is described in full here - Aaah!. Input The input consists of two lines. The first line is the “aaah” Jon Marius is able to say that day. The second line is the “aah” the ...
1
vote
2answers
53 views

Skiena's Programming Challenges: The Trip problem

I have the following solution for the Trip problem from Skiena's and Revilla's Programming Challenges (full problem description here). ...
4
votes
2answers
67 views

C Development Utilities Library

I have been working on a small library of development utilities for some time now, incrementally improving and expanding it as I make use of it in other projects. I feel that it is fairly mature now; ...
2
votes
1answer
42 views

Priority checker for infix-to-postfix converter

I am trying to implement an infix to postfix conversion program using stacks. I have actually have done it successfully, but I'm uncertain about the complexity of the logic that I used for the ...
1
vote
1answer
37 views

How am I supposed to write a binary tree (S-Expression) pretty printer in C?

OK, I feel like I am doing something really wrong here. I'm just trying to create a binary tree, and a pretty printer for it. My approach is looking so bad I can't even... ...