Tagged Questions
7
votes
2answers
418 views
C-style array class
I often use C API's in C++ that force me to use C-style arrays. I got sick of constantly using a dynamic vector with &vec[0], so I wrote this C-style array ...
2
votes
2answers
300 views
Remove repeated words from a 2D character array
This code removes repeated words from a word array (2D char array). I want to optimize this as much possible for speed.
...
1
vote
1answer
1k views
First non-repeated character in a string in c
Write an efficient function to find the first non-repeated character
in a string. For example, the first non-repeated character in "total"
is 'o' and the first non-repeated character in ...
3
votes
0answers
61 views
Dynamic array in C
It's a dynamic array, the elements can be accessed normally and it supports any type. I believe I'm relying on undefined behavior when I treat every pointer to pointer as ...
7
votes
3answers
605 views
Stack implementation using an array
I am trying a stack implementation using an array. I want to know if this approach is OK or if there is a logical problem. This program is working fine.
...
1
vote
2answers
105 views
Adding very large arrays
I am writing a stochastic simulation for a Yule process:
If you start with a certain number of bins each containing a random number of balls, you add another ball to an existing bin with a ...
10
votes
1answer
417 views
Bubble Sort in C: Array vs Linked List
A few weeks ago I posted a code review of a linked list so I decided to implement bubble sort on my journey of learning C. Here is the referenced linked list library, though this isn't really what's ...
8
votes
4answers
442 views
Dynamic array of structs in C
Based on some code on internet, I implemented a dynamic array of structures in C.
I am really interested in some feedback on this. Maybe there are some places where it can cause memory leaks or ...
10
votes
1answer
87 views
4
votes
1answer
76 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 ...
3
votes
2answers
117 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 ...
4
votes
2answers
6k views
Magic Square in C
I'm still teaching myself C out of KN King's C Programming: Modern Approach. Actually pretty excited I go this problem solved in under 2 hours. I asked this over on Stack Overflow and it was ...
3
votes
1answer
790 views
Quicksort using pointers
As an exercise, I've written quicksort algorithm in C using pointers. Please comment and help me find the cases where it breaks (if any).
...
4
votes
2answers
159 views
Array initialization from text string
I have a block of text where several arrays are printed out, with a name and values:
VAL1=10 20 30 40 50
VAL2=4 8 15 16 23 42
I have a function that looks for the value name, and sets an ...
4
votes
3answers
760 views
Reverse a string without <string.h> header function
I'm learning C from K.N. King book and just arrive at a question to reverse the words in a sentence.
Here is the output:
Enter a sentence: you can cage a swallow can't you?
Reversal of sentence: ...
67
votes
15answers
4k views
Searching an element in a sorted array
I was applying for a position, and they asked me to complete a coding problem for them. I did so and submitted it, but I later found out I was rejected from the position. Anyways, I have an eclectic ...
2
votes
2answers
1k views
Maximum Subarray Problem - Iterative O(n) algorithm
I implemented an iterative O(n) algorithm for solving the maximum sub-array problem. I would like a general review for it.
Here the ...
2
votes
2answers
372 views
Reference counted dynamic byte array
What?
I have a reference counted dynamic byte array written in C. I'm currently using this implementation in a FIFO fashion. Particularly reading data from files into the arrays, then parsing the ...
2
votes
1answer
1k views
Read multidimensional array from file in C
I want to read multidimensional double array from file into a single pointer variable. The number of rows and columns are not known and need to be determined when reading the file in order to allocate ...
1
vote
3answers
88 views
Improving function that compares two strings
I'm learning C and have made this very simple function to compare two strings. I would like to know how it can be improved:
...
2
votes
2answers
603 views
Review implementation of stack by using array in C
This question has become long after many updates. Click here to go down.
I have implemented stack using arrays in C. Please give me suggestions on how to improve it. The purpose of writing it is ...
1
vote
1answer
248 views
Missing element from array (D&C) - code design/aesthetic improvement
I am trying to implement in C/C++ a 'classical' Divide and Conquer algorithm which solves the following problem "Given an array of n-1 numbers (int) from 0 to n, find the missing number".
I am using ...
1
vote
3answers
174 views
Review: Compare two Anagrams words in C
A simple attempt by me to test whether two words are anagrams or not. Here is the code:
...
1
vote
2answers
258 views
Random Sentences code review
This program use random number generator to create sentences. It prints 20 sentences randomly.
Here is the code:
...
3
votes
1answer
129 views
Matching element of arrays on a condition
if node[i] and node[i+1] are present in the neighbor[i], then store the 'i' position of node and print the 'i' value of node.
this is also done by reversing the node array(only) and same type of ...
4
votes
2answers
143 views
Counting letters program. Notation and optimisation help
I am writing a program in C running on UNIX which counts the number of each letters in a input text file. For a file like this:
The cat sat on the green mat
The output would be like this:
...
1
vote
2answers
193 views
Please help me debug this little C program on dynamic two-dimensional array?
I am a newbie here. I have written a little C program, which is to create a two-dimensional matrix. Here is the code:
...
1
vote
1answer
217 views
C arrays and loops optimization
I am writing a program for learning purposes that takes an input of a file structured similarly to:
...
5
votes
3answers
190 views
Devise an algorithm of complexity O(N) finding No of 1's from a 2 dimensional matrix[N][N] containing 1's and 0's
Assume a 2d [n][n] Matrix of 1's and 0's/ All the 1's in any row should come before 0's/The number of 1's in any row i should be atleast the no of 1's row (i+1). find a method and write a c program to ...
2
votes
1answer
736 views
RungeKutta method in C, pointers and arrays, everything handled correctly?
Now the program works. But is it correct? Sometimes it works in C anyway... :) Could I improve it somehow(not numerically, just C-wise)? I need to allocate the array as I do right? Since I am ...
1
vote
1answer
2k views
What value the rear and front counter of an Array implementation of Queue start with? -1 or 0 or 1?
Is there anything wrong with this implementation of Queue with array?
I have started front = rear = -1.
Some references tend to start front = 0 like this:enter link description here
I think if ...
4
votes
3answers
14k views
What's the best method to initialize character array in C and why? [closed]
I have seen C strings initialized to empty string and a null character.
char str[10] = ""; or char str[10] = {'\0'}
I am just ...