Skip to content

Instantly share code, notes, and snippets.

@paulghaddad
Created March 2, 2021 13:33
Show Gist options
  • Save paulghaddad/8fc0efafcba023f803dc6325ebc70b34 to your computer and use it in GitHub Desktop.
Save paulghaddad/8fc0efafcba023f803dc6325ebc70b34 to your computer and use it in GitHub Desktop.
Practice of Programming
Exercise 1-1: Comment on the choice of names and values in the following code:
#define TRUE 0
#define FALSE 1
if ((ch = getchar()) == EOF)
not_eof = FALSE;
- Answer:
- You'd expect TRUE to be 1 and FALSE to be 0.
- Inconsistent snake case (`getchar` and `not_eof` )
Exercise 1-2: Improve this function
int smaller(char *s, char *t) {
if (strcmp(s, t) < 1)
return 1;
else
return 0;
}
// improved
#define FALSE 0
#define TRUE 1
int isGreaterLexigraphically(char *s, char *t) {
if (strcmp(s, t) == 1)
return TRUE
else
return FALSE
}
Exercise 1-4: Improve each of these fragments:
if ( !(c == 'y' || c == 'Y') )
return;
// Solution:
if ((c != 'y') && (c != 'Y'))
return;
length = (length < BUFSIZE) ? length : BUFSIZE;
// Solution:
if (length >= BUFSIZE) {
length = BUFSIZE
}
flag = flag ? 0 : 1;
// Solution:
if (flag)
flag = 1
else
flag = 0
quote = (*line == '"') ? 1 : 0;
// Solution
if (*line == '"')
quote = 1
else
quote = 0
if (val & 1)
bit = 1
else
bit = 0
// Solution
if (val & 1 > 0)
bit = 1
else
bit = 0
Exercise 1-5: What is wrong with this excerpt?
int read(int *ip) {
scanf("%d", ip);
return *ip;
}
insert(&graph[vert], read(&val); read(&ch));
// Which read will take place first? This matters because
// different values will be populated in val and ch
// These two read calls should be placed on separate lines.
Exercise 1-6: List all the different outputs this could produce with various orders of evaluation
n = 1
printf("%d %d\n", n++, n++)
// "1 2"
// "2 1"
// "2 3"
// "3 2"
Exercise 1-7: Rewrite the C excepts more clearly:
if (istty(stdin)) ;
else if (istty(stdout)) ;
else if (istty(stderr)) ;
else return(0);
if (retval != SUCCESS)
{
return (retval);
}
// All went well!
return SUCCESS;
for (k = 0; k++ <5; x += dx)
scanf("%lf", &dx);
// Solutions
if (!istty(stdin) && !istty(stdout) && !istty(stderr)) ;
else return 0;
if (retval == SUCCESS)
return SUCCESS;
else
return retval;
for (i = 0; i < 5; , i++) {
scanf("%lf", &dx);
x += dx;
}
Exercise 1-8: Identify the errors in this Java fragment and repair it by rewriting with an idiomatic loop:
int count = 0;
while (count < total) {
count++;
if (this.getName(count) == nametable.userName()) {
return (true);
}
}
// Solution
for (int count = 0; count < total; count++) {
if (this.getName(count) == nametable.userName()) {
return true;
}
}
Exercise 1-9: Identify the problems with this macro definition:
#define ISDIGIT(c) ((c >= '0') && (c <= '9')) ? 1 : 0
// Since the parameter c is used twice in the expression,
// side effects could result depending on how this macro
// is called.
Exercise 1-10: How would you rewrite these definitions to minimize potential errors?
define FT2METER 0.3048
define METER2FT 3.28084
define MI2FT 5280.0
define MI2KM 1.609344
define SQMI2SQKM 2.589988
// Solution: Write them in terms of each other when you
// can. ie,
// define METER2FT 1 / FT2METER
Exercise 1-11: Comment on these comments.
void dict::insert(string& w)
// returns 1 if w in dictionary, otherwise returns 0
// => contradicts code which returns nothing
if (n > MAX || n % 2 == 0) // test for an even number
// better if this was placed in a well-named function
// Write a message
// Add to line counter for each line written
void write_message()
{
// increment line counter
line_number = line_number + 1
fprintf(fout, "%d %s\n%d %s\n%d %s\n"),
line_number, HEADER,
line_number+1, BODY,
line_number+2, TRAILER);
// increment line counter
line_number = line_number + 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment