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.
2
votes
2answers
70 views
OOP in C: which of these two approaches is closer to a cannonical solution? [on hold]
Here are the two approaches to writing OOP (as in "we have objects and we can instantiate any number of them, each having it's own private data"). The language is C and the platform is embedded ...
-4
votes
0answers
20 views
Serial Port Timeout Problem [on hold]
Good day,
I have problem about serial communication timeout. I have flags for outgoing and incoming data packages. I can receive data packages for some time ,but I continouosly receive timeout error ...
17
votes
8answers
3k views
Feedback on a programming practice problem in C
This is a programming practice that our teacher gave us, and I would appreciate if someone can look over my program and tell me if I did a good job.
Basically, the context is I joined a company and ...
4
votes
1answer
61 views
Automate a precedence graph with parallel programming [on hold]
I found a C/Linux exercise in a book, and I propose a code as a solution, please feel free to correct it.
Here is the exercise :
Taking into consideration the following precedence graph, which ...
-1
votes
0answers
22 views
Code to solve quadratic equations [on hold]
Please bear with me as this is probably a trivial question! I have no clue what I'm doing wrong. When trying to write a program to solve quadratic equations in a certain way I defined 3 functions: ...
-1
votes
0answers
18 views
Converting hex/binary array to string array [on hold]
In my quest to be better at programming, I've stumbled upon something that I just cannot answer.
I was tasked to convert binary/hex data (in an array) to string array. So far I've only managed to ...
9
votes
6answers
592 views
Small one time pad encryption program
This one time pad encryption program I have written (basically just an XOR "encryption" program) seems to be working fine, compiling nicely (gcc -o ./OTP.c), and doing what it's supposed to. However I ...
4
votes
1answer
43 views
Writting day_of_month and month_day with pointers instead of array indexing
Rewrite the routines day_of_year and month_day with pointers instead of indexing.
The exercise is quite simple, this is the solution:
int day_of_year(unsigned int year, int month, int day) {
...
0
votes
0answers
23 views
C program to print the prime factors of a given number [closed]
I want to print only the prime factors of a given number so I wrote the following C code:
#include<stdio.h>
#include<math.h>
int main(){
int n=15000,x;
int i,j,a;
...
6
votes
1answer
60 views
Is this an efficient way of removing duplicate words from an unsorted list?
I was using brute force to remove duplicate words since the lists were really small. But I want a solution that won't become too slow if the input grows.
This function creates a binary tree and ...
0
votes
0answers
17 views
Unable to pass all the Test Cases [closed]
Problem Statement
Given a number K, find the smallest Fibonacci number that shares a common factor( other than 1 ) with it. A number is said to be a common factor of two numbers if it exactly ...
5
votes
1answer
90 views
Optimization for add function in queue
I would prefer if more experienced users could give pointers on how I can optimize and think better when writing code.
Using Homework Questions as reference, as this is a homework related question. ...
-2
votes
0answers
25 views
Incorrect output in Caesar Cipher [closed]
I am trying to implement a Caesar cipher, but I'm not getting the expected output. Kindly help me to identify what's wrong with the code.
Key: 3
Input: Hello
Output: KNUUX
Expected Output: KHOOR
...
0
votes
0answers
35 views
Review implementation of Stack in C: push,pop then push to another stacker [closed]
I have a "Truck" and a "Stacker" both using stack. The user is able to allocate the size of the truck(i have used malloc) and the stacker size with be a fixed size of 5. I am able to push values to ...
4
votes
1answer
44 views
Making a minishell in C. How to improve error control with feoh and ferror?
I've written this minishell but I'm not sure I'm making a correct control of the errors. Could you give me some advice?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
...
9
votes
3answers
1k views
Factorial code in C
It took me a while to figure out how to write this code, but I'm glad to say that I did eventually figure it out.
Can someone tell me what they think about it? Is it well written? What could have ...
0
votes
1answer
63 views
Time Limit Exceed Problem [closed]
When I submitted below code in codepad.org, it shows "Time limit exceed" problem. Please provide alternative solution :
Problem Statement :
Given a number K, find the smallest Fibonacci number that ...
6
votes
1answer
60 views
How to improve undo & redo functionality in C?
I'm working on a small GTK+ program and I'm now implementing undo and redo capabilities. This is the code that I'm using to handle the different states.
It's just a linked list storing the allocated ...
6
votes
1answer
40 views
Functions that converts day of year to month and day and reverse with error checking
There is no error checking in day_of_year or month_day. remedy this defect.
Here is the solution:
int day_of_year(unsigned int year, unsigned int month, int day) {
int leap, i;
leap = ...
5
votes
1answer
68 views
Find whether a given number is a perfect square or not
I am trying to find whether a given number is a perfect square or not by using just addition and subtraction.
Please review my code.
#include<stdio.h>
int check(int);
main()
{
int N;
...
11
votes
3answers
864 views
4×4 Tic-Tac-Toe in C
While trying to teach myself C, I decided to take a stab at writing this Tic-Tac-Toe program on a 4x4 board. It will be great if somebody could review it and let me know of some ways of refining this.
...
2
votes
0answers
37 views
Synchronize 3 processes with fork() [closed]
I was given the following exercise in my university:
Given 3 processes:
d1 - print("I"), print("D")
d2 - print("J"), print("N")
d3 - print("M"), print("E")
Write a program ...
7
votes
1answer
57 views
Storing lines in an array
Rewrite readlines to store lines in an array supplied by main, rather than calling alloc() to maintain storage. How much faster is the program?
Here is my solution:
int readlines(char **lineptr, ...
1
vote
3answers
104 views
To check if a string C is an interleaving of A and B Code
This is my code to check if a string C is an interleaving of Strings A and B. Please suggests optimizations, and where I can improve.
#include <vector>
#include <list>
#include ...
6
votes
1answer
63 views
RC4 implementation in C
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
typedef uint8_t byte;
typedef struct
{
byte i, j;
byte S[256];
} Rc4State;
void swap(byte *a, byte *b)
{
...
5
votes
1answer
54 views
Input memory buffer exceeding situation handling
Please review the following code:
static int md5_encode(const char * input_md5 , char * output , int size)
{
// checking pre-requisits //
if( output == NULL || input_md5 == NULL || size == 0)
...
6
votes
1answer
65 views
How to make my memcpy implementation fast?
I have tried to write a function like memcpy. It copies sizeof(long) bytes at a time.
What surprised me is how inefficient it is, it's just 17% more efficient than the naivest implementation with ...
4
votes
0answers
29 views
Play some sine waves with SDL2
Runs smoothly, however valgrind is showing "possibly lost: 544 bytes in 2 blocks".
This is my first time doing a lot of things here so I might be making multiple mistakes.
Please let me know if ...
3
votes
1answer
45 views
C dynamic array and pointers
I've written an implementation of a dynamic array in C, but I'm not sure if my implementation is correct... This is what I'm worried about: if I add an element, will it remain in the collection? This ...
2
votes
1answer
39 views
Pointer version of itoa
Rewrite appropiate programs from earlier chapters and exercises with pointers instead of array indexing. Good posibilities include getline(Chapter 1 and 4), atoi, itoa, and their variants(Chapters ...
6
votes
4answers
288 views
Function to convert ISO-8859-1 to UTF-8
I wrote this function last year to convert between the two encodings and just found it. It takes a text buffer and its size, then converts to UTF-8 if there's enough space.
What should be changed to ...
7
votes
1answer
60 views
implementations of strncmp, strncat, strncpy
Write versions of the library functions strncpy, strncat and strncmp, which operate on the most n charachters of their argumen strings. For example, strncpy(s, t, n) copies at most n charachters of ...
9
votes
2answers
104 views
strend, function that checks the occurence of a pattern at the end of a string
Write a function strend(s, t) which returns 1 if the string t occurs at the end of s, and 0 otherwise.
Here is my solution:
unsigned int strend(char *source, char *pattern) {
char *saver = ...
9
votes
4answers
287 views
Pointer version of strcat
Write a pointer version of the function strcat that we showed in Chapter 2: strcat(s, t) copies the string t to the end of s.
This is the version of strcat from Chapter 2:
void strcat(char s[], ...
8
votes
3answers
521 views
Calculate the number of moves requires for a knight chess piece [closed]
It's possible to work out the number of moves required to move from (0, 0) to (A, B) for a knight on an infinite chess board in O(1) time.
This is an attempt at a solution to do it in O(n) time, ...
7
votes
1answer
68 views
getfloat, the floating point analog of getint
Write getfloat, the floating-point analog of getint. What types does getfloat return as its function value?
gefloat would also return an integer value.
Here is my solution:
int getfloat(double ...
2
votes
1answer
23 views
getint that properly handles + or - signs
As written, getint treats a + or - not followed by a digit as a valid representation of zero. Fix it to push such a charachter back on the input.
Before the for I check if the charachter after ...
11
votes
3answers
139 views
Pi Benchmarking in C
I wrote the following program to calculate n digits of Pi (where n could be anything, like 10M) in order to benchmark the CPU and it works perfectly (without OpenMP):
/*
*
* Simple PI Benchmarking ...
3
votes
1answer
63 views
Efficiently counting '1' bits in the first n bits
I have two functions to count the number of set '1' bits in the first offset bits of either a 64 bit number, or a bitmap struct.
After some profiling, particularly the latter is quite a bottleneck in ...
6
votes
2answers
78 views
Macro that interchanges 2 arguments
Define a macro swap(t, x, y) that interchanges two arguments of type t.(Block structure will help.)
The ideea is that a variable defined in a block structure exists only inside the block ...
6
votes
1answer
47 views
Correctness of calculations with struct timespec
I've noticed some discrepancies in timings in our system, and I'm trying to narrow down what could be causing them.
I'm reviewing out time abstraction, and as far as I can determine it's fine.
Am I ...
3
votes
2answers
93 views
Array manipulation exercise
While trying to learn more about arrays in C, I tried to write some code that did the following:
Read a stream of numbers from the stdin and store them in an array
Print the array in the order the ...
8
votes
2answers
60 views
VLC media player watchdog daemon
I'm looking for bugs, ways to make it more portable or standardized, improvements of any kind. Seems to do what it is supposed to on my Ubuntu 12.04 PC.
EDIT Newer version with error logging and ...
5
votes
1answer
43 views
Shorten a sorting function
I have this function:
int sort(book *data, int books, char mode, char sortBy) {
int i, j;
book aux;
aux.cat = malloc(sizeof *(aux.cat));
if(sortBy == 'n') {
for(i = 0; i < books; ...
5
votes
2answers
64 views
Handling serial port with select and local socket
I have another issue: I'm trying to handle connections made by local socket along with data from serial port. Here is my concept:
Serial port is opened and monitored by select
Local socket is ...
4
votes
2answers
75 views
Recursive reverse function
Write a recursive version of the function reverse(s), which reverses the string s in place.
Here is my solution:
void reverse(char a[], int i, int j) {
char tmp;
if(i >= j) {
...
6
votes
1answer
66 views
Are these file handling functions safe?
I'm using these 2 functions to handle file input/output and would like to know if there's anything that should be changed.
For content retrieval, enough space is allocated and the file content is ...
7
votes
3answers
99 views
Integer-to-string conversion using recursion
Adapt the ideas of printd to write recursive version of itoa; that is, convert integer into a string by calling a recursive routine
Here is the implementation of printd:
void printd(int n) {
...
6
votes
2answers
95 views
Educational “Library” project
The assignment is very open and we have only 4 things it needs to cover:
File write and read
Use a Struct
Export into HTML format
use a common sorting algorithm
so I've decided to create a little ...
3
votes
2answers
67 views
Processing student scores from the stdin
As part of learning more C, I wrote this extremely trivial C program that takes Student scores from stdin and, upon termination, prints out the minimum score/maximum score and the average score. Can ...