Skip to content

Commit c7ca8fd

Browse files
committed
Clean up code
1 parent e102ec1 commit c7ca8fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+289
-211
lines changed

chapter01/1-15.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
#include <stdio.h>
88

9+
/* functions */
10+
float fahrToCelsius(float);
11+
912
float fahrToCelsius(float fahr)
1013
{
1114
float celsius;
12-
1315
celsius = (5.0 / 9.0) * (fahr-32.0);
14-
1516
return celsius;
1617
}
1718

@@ -27,9 +28,7 @@ int main(void)
2728
step = 20; /* step size */
2829

2930
fahr = lower;
30-
3131
printf("Fahrenheit\tCelsius\n");
32-
3332
while (fahr <= upper) {
3433
celsius = fahrToCelsius(fahr);
3534
printf("%10.0f\t%7.1f\n", fahr, celsius);

chapter01/1-16.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
#define MAXLINE 1000 /* maximum input line length */
1212

13-
int getLine(char s[], int lim);
14-
void copy(char to[], char from[]);
13+
/* functionst */
14+
int getLine(char [], int);
15+
void copy(char [], char []);
1516

1617
/* getLine function: read a line into s, return length */
1718
int getLine(char s[], int lim)

chapter01/1-17.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#define NCHARS 82 /* number of chars per line, including
1111
the newline and null characters */
1212

13-
int getLine(char s[], int lim);
13+
/* functions */
14+
int getLine(char [], int);
1415

1516
/* getLine function: read a line into s, return length */
1617
int getLine(char s[], int lim)

chapter01/1-18.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
#define YES 1
1111
#define NO 0
1212

13-
int getLine(char s[], int lim);
14-
int delTrailingWS(char s[], int len);
15-
void delBlankLns(char s[], int len);
13+
/* functions */
14+
int getLine(char [], int);
15+
int delTrailingWS(char [], int);
16+
void delBlankLns(char [], int);
1617

1718
/* getLine function: read a line into s, return length */
1819
int getLine(char s[], int lim)

chapter01/1-19.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
#define YES 1
1212
#define NO 0
1313

14-
int getLine(char s[], int lim);
15-
int delTrailingWS(char s[], int len);
16-
void reverse(char s1[], char s2[], int len);
14+
/* functions */
15+
int getLine(char [], int);
16+
int delTrailingWS(char [], int);
17+
void reverse(char [], char [], int);
1718

1819
/* getLine function: read a line into s, return length */
1920
int getLine(char s[], int lim)

chapter01/1-20.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#define MAXLINE 1000
2020
#define N 4 /* tabstop for every n columns */
2121

22-
int getLine(char s[], int lim);
22+
/* functions */
23+
int getLine(char [], int);
2324
void detab(void);
2425

2526
char line[MAXLINE]; /* currently read line */

chapter01/1-21.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
#define YES 1
1919
#define NO 0
2020

21-
/* functions declarations */
22-
int getLine(char s[], int lim);
23-
int count(char s[], char c, int p);
24-
void entab(char line[], char modLine[]);
21+
/* functions */
22+
int getLine(char [], int);
23+
int count(char [], char, int);
24+
void entab(char [], char []);
2525

2626
/* getLine function: read a line into s, return length */
2727
int getLine(char s[], int lim)

chapter01/1-22.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
#define MAXLINE 1000 /* maximum input line length */
1212
#define LINE_LENGTH 81 /* maximum output line length */
1313

14-
int getLine(char s[], int lim);
15-
void foldLine(char line[], char fldLine[], int lineLen);
14+
/* functions */
15+
int getLine(char [], int);
16+
void foldLine(char [], char [], int);
1617

1718
/* getLine function: read a line into s, return length */
1819
int getLine(char s[], int lim)

chapter01/1-23.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
#define SLASH_ASTERISK 1
1616
#define ASTERISK_SLASH 0
1717

18-
int getLine(char s[], int lim);
19-
int findComment(char line[], int notation);
20-
int delComment(char line[], char modLine[], int start, int end);
21-
int delBlankLine(char s[]);
18+
/* functions */
19+
int getLine(char [], int);
20+
int findComment(char [], int );
21+
int delComment(char [], char [], int , int);
22+
int delBlankLine(char []);
2223

2324
/* getLine function: read a line into s, return length */
2425
int getLine(char s[], int lim)

chapter01/1-24.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
#define IN 1
1717
#define OUT 0
1818

19-
int getLine(char s[], int lim);
20-
int findComment(char line[], int notation);
21-
int delComment(char line[], char modLine[], int start, int end);
19+
/* functions */
20+
int getLine(char [], int);
21+
int findComment(char [], int);
22+
int delComment(char [], char [], int , int);
2223

2324
/* getLine function: read a line into s, return length */
2425
int getLine(char s[], int lim)

chapter02/2-1.c

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <float.h>
1212
#include <math.h>
1313

14+
/* functions */
15+
void computeRanges(void);
16+
void stdLibraryMacros(void);
17+
1418
void computeRanges(void)
1519
{
1620
/* characters */

chapter02/2-10.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
#define MAXLINE 1000 /* maximum input line length */
1010

11-
int getLine(char s[], int lim);
12-
void toLowercase(char line[]);
11+
/* functions */
12+
int getLine(char [], int);
13+
void toLowercase(char []);
1314

1415
/* getLine function: read a line into s, return length */
1516
int getLine(char s[], int lim)

chapter02/2-2.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
#define MAXLINE 1000 /* maximum input line length */
1010

11-
int getLine(char s[], int lim);
11+
/* functions */
12+
int getLine(char [], int);
1213

1314
/* getLine function: read a line into s, return length */
1415
int getLine(char s[], int lim)

chapter02/2-3.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
#define MAXLINE 1000
1515

16-
int htoi(char s[], int len);
16+
/* functions */
17+
int htoi(char [], int);
1718

1819
int htoi(char s[], int len)
1920
{

chapter02/2-4.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
#define MAXCHAR 100
1010

11-
void squeeze(char s1[], char s2[]);
11+
/* functions */
12+
void squeeze(char [], char []);
1213

1314
void squeeze(char s1[], char s2[])
1415
{

chapter02/2-5.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
#define MAXCHAR 100
1212

13-
int any(char s1[], char s2[]);
13+
/* functions */
14+
int any(char [], char []);
1415

1516
int any(char s1[], char s2[])
1617
{

chapter02/2-6.c

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <stdio.h>
99

10+
/* functions */
1011
unsigned int setbits(unsigned int, int, int, unsigned);
1112

1213
unsigned int setbits(unsigned int x, int p, int n, unsigned y)

chapter02/2-7.c

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <stdio.h>
99

10+
/* functions */
1011
unsigned int invert(unsigned int, int, int);
1112

1213
unsigned int invert(unsigned int x, int p, int n)

chapter02/2-8.c

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <stdio.h>
88

9+
/* functions */
910
unsigned int rightrot(unsigned int, int);
1011

1112
unsigned int rightrot(unsigned int x, int n)

chapter02/2-9.c

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <stdio.h>
1919

20+
/* functions */
2021
int bitcount(unsigned);
2122

2223
int bitcount(unsigned x)

chapter03/3-1.c

+23-13
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22
* Exercise 3-1. Our binary search makes two tests inside the loop, when one
33
* would suffice (at the price of more tests outside.) Write a version with
44
* only one test inside the loop and measure the difference in run-time.
5+
*
6+
* Answer: binSearch_alt algorithm is faster because it has one less
7+
* comparison in the loop; However it doesn't stop when the number is found in
8+
* the loop.
59
* By Faisal Saadatmand
610
*/
711

812
#include <stdio.h>
913

10-
int binSearchOld(int x, int v[], int n);
11-
int binSearch(int x, int value[], int n);
14+
#define SIZE 12
15+
16+
/* functions */
17+
int binSearch(int, int [], int);
18+
int binSearch_alt(int, int [], int);
1219

1320
/* binSearch: find x in value[0] <= value[1] <= ... <= value[n - 1] */
1421
int binSearch(int x, int v[], int n)
@@ -30,31 +37,34 @@ int binSearch(int x, int v[], int n)
3037
return -1; /* no match */
3138
}
3239

33-
int binSearch_new(int x, int value[], int n)
40+
int binSearch_alt(int x, int v[], int n)
3441
{
3542
int low, high, mid;
3643

3744
low = 0;
3845
high = n - 1;
3946

40-
mid = (low + high) / 2;
41-
while (low <= high | x != value[mid]) {
47+
while (low < high) {
4248
mid = (low + high) / 2;
43-
if (x < value[mid])
44-
high = mid - 1;
45-
else
49+
if (v[mid] < x)
4650
low = mid + 1;
51+
else
52+
high = mid;
4753
}
48-
if (x == value[mid])
49-
return mid;
54+
if (v[low] == x)
55+
return low;
5056
return -1;
5157
}
5258

5359
int main(void)
5460
{
55-
int array[12] = { 2, 4, 5, 6, 8, 9, 12, 16, 20, 32, 40, 78 };
56-
57-
printf("%i\n", binSearch_new(2, array, 12));
61+
int array[SIZE] = { 2, 4, 5, 6, 8, 9, 12, 16, 20, 32, 40, 78 };
62+
int number = 0;
5863

64+
printf("Enter number to search in the array: ");
65+
while (scanf("%i", &number) != EOF) {
66+
printf("%i\n", binSearch_alt(number, array, SIZE));
67+
printf("Enter number to search in the array: ");
68+
}
5969
return 0;
6070
}

chapter03/3-2.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010

1111
#define MAXLINE 10000 /* maximum input line length */
1212

13-
int getLine(char s[], int lim);
14-
void escape(char line[], char modLine[]);
15-
void escapeRev(char line[], char modLine[]);
13+
/* functions */
14+
int getLine(char [], int);
15+
void escape(char [], char []);
16+
void escapeRev(char [], char []);
1617

1718
/* getLine function: read a line into s, return length */
1819
int getLine(char s[], int lim)

chapter03/3-3.c

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#define YES 1
1717
#define NO 0
1818

19+
/* funstions */
20+
int getLine(char [], int);
21+
int expand(char [], char []);
22+
1923
/* getLine function: read a line into s, return length */
2024
int getLine(char s[], int lim)
2125
{

chapter03/3-4.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
#include <string.h>
1111
#include <limits.h>
1212

13-
void reverse(char s[]);
14-
void itoa(int n, char s[]);
13+
/* functions */
14+
void reverse(char []);
15+
void itoa(int, char []);
1516

1617
/* reverse function: reverse string s in place */
1718
void reverse(char s[])

chapter03/3-5.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
#include <stdio.h>
99
#include <string.h>
1010

11-
void reverse(char s[]);
12-
void itob(int number, char convertedNumber[], int base);
11+
/* functions */
12+
void reverse(char []);
13+
void itob(int, char [], int);
1314

1415
/* reverse function: reverse string s in place */
1516
void reverse(char s[])

chapter03/3-6.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
#include <string.h>
1010
#include <limits.h>
1111

12-
void reverse(char s[]);
13-
void itoa(int n, char s[], int w);
12+
/* functions */
13+
void reverse(char []);
14+
void itoa(int, char [], int);
1415

1516
/* reverse function: reverse string s in place */
1617
void reverse(char s[])

chapter04/4-1.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010
#define MAXLINE 1000 /* maximum input line length */
1111

12-
int getLine(char line[], int lim);
13-
int strindex(char source[], char searchfor[]);
12+
/* functions */
13+
int getLine(char [], int);
14+
int strindex(char [], char []);
1415

16+
/* globals */
1517
char pattern[] = "ould"; /* pattern to search for */
1618

1719
/* getLine: get line into s, return */

0 commit comments

Comments
 (0)