C Program to reverse a Linked List.

Data structures using C,C Program to reverse a Linked List. Linked list is a data structure in which the objects are arranged in a linear order. Linked List contains group of nodes, in which each node contains two fields, one is data field and another one is the reference field which contains the address of …

C Program to implement Integration.

C Program to Implement Numerical Integration (Trapezoidal / Rectangle Rule) Numerical integration approximates a definite integral by summing the areas of thin vertical slices under the curve. The rectangle rule divides the interval [a, b] into N equal strips and sums f(x) * width for each strip. Integration is used in engineering and science to …

K&R C Exercise 3-3: expand — Expand a-z Shorthand into Full Range

Exercise 3-3. Write a function expand(s1,s2) that expands shorthand notations like a-z in the string s1 into the equivalent complete list abc…xyz in s2. Allow for letters or digits on either end of a range, and take care of cases like a-b-c and a-z0-9 and -a. Arrange that a leading or trailing – is taken …

K&R C Exercise 3-2: escape — Convert Special Characters to Escape Sequences

Exercise 3-2. Write a function escape(s,t) that converts characters like newline and tab into visible escape sequences like \n and \t as it copies the string t to s. Use a switch. Write a function for the other direction as well, converting escape sequences into the real characters. Two functions walking their input strings character …

K&R C Exercise 3-1: Binary Search with One Comparison Per Loop

Exercise 3-1. Our binary search makes two tests inside the loop, when one would suffice (at the cost of more tests outside). Write a version with only one test inside the loop and measure the difference in run-time. The standard K&R binsearch performs two comparisons per iteration — it checks x < v[mid], then x …

K&R C Exercise 2-10: lower — Convert to Lowercase with Ternary

Exercise 2-10. Rewrite the function lower, which converts upper case letters to lower case, with a conditional expression instead of if-else. This exercise looks trivial on the surface — it is a one-liner. The real lesson is about the conditional (ternary) operator and what it means to choose it over if-else. K&R introduce the ternary …