Giter Site home page Giter Site logo

williamgherman / c-solutions Goto Github PK

View Code? Open in Web Editor NEW
486.0 11.0 199.0 467 KB

My Solutions to K. N. King's "C Programming: A Modern Approach", second edition

License: GNU General Public License v3.0

C 96.48% Makefile 2.91% C++ 0.61%
c education solutions exercice exercise king

c-solutions's People

Contributors

dragi-ns avatar jack9221 avatar jll32 avatar sprc86 avatar williamgherman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

c-solutions's Issues

Typos

Minor errors, but:
7.10:
Your sentence continas %d vowels.

should read:
Your sentence contains %d vowels.

7.14 (readme):
floating-poing

should read:
floating-point

10.02 (readme):
(b) a (parameter), b and c (local to g) (c) a (declared in block), b, c (local to g) and d

should read:

(b) a (parameter), b and c (local to g) 
(c) a (declared in block), b, c (local to g) and d

Exercise 11.06

Miss the case to do the update that a[i] is between the current *largest and *second_largest.

Exercise 14.09 (b)

#define MEDIAN(x,y,z) (((x)>=(y)&&(x)<=(z))?(x):((y)>=(x)&&(y)<=(z))?(y):(z))

The original answer is wrong when x>y>z

The answer may be
#define MEDIAN(x,y,z) (((x)>(y)&&(x)<(z))||((x)<(y)&&(x)>(z)))?(x):(((y)>(x)&&(y)<(z))||((y<x)&&(y>z)))?(y):(z)

Exercise 13.6

I think that line

while (c+2 != '\0')
should be

while (*(c+2) != '\0') or while (*(c+2))

Chapter 13, exercise 5, b

Answer should be:

void capitalize(char * str){
  char *p = str;
  while(*p != '\0')
	{
	  if(isalpha(*p)) {
	    *p = toupper(*p);
	  }
	  p++;
	}
}

Programming project 8, exercise 10

I don't understand why this programs closest departure time is one under current time. For example 13:15 (1:15pm) departure time is 12:47pm. Also when inputting 22:00 (10:00pm) the closest departure time is 9:45pm. Even the book is totally wrong on this exercises. If user enters 22:00 and above. Closest departure time would be 8:00 am. next day. Since the last departure is at 9:45pm

Exercise 13.11

Awesome job on the repo, but there is an issue in this one, I think:
*string1 - *string2 doesn't compare the last two elements, the K&R version in the book does though. The solution would be to use a counter, like so:

int compare_strings(char *s1, char *s2)
{
int counter = 0;
char *ptr1 = s1, *ptr2 = s2;

while (*ptr1++ == *ptr2++)
{
    counter++;
    if (*ptr1 == '\0')
        return 0;
}
//The line in question:
return *(s1 + counter) - *(s2 + counter); 

}

.

.

Exercise 7.4 Solution

The code should allow for nonalphabetic inputs as well. By adding cases to reflect this should fix the problem.

For example there is no case for 0.

All the alphabet cases need an additional case to allow for inputs of the numeric values.

An additional case for the hyphen '-'.

Exercise 8.08

I think the solution is:

int temperature_readings[30][30];

the problem is to make an array to show months and hourly reading of each day. So to give hours of each day of the month, the column size should also be 30.
In short: 30 hourly readings for 30 days.

Exercise 16.15

enum weekdays = {MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
FRIDAY, SATURDAY, SUNDAY};

Exercise 13.16

I think that ++ operator should be inside while loop, not in its condition:

    while (*s)
        if (*s++ == ' ') count++;

otherwise, first value of array is never tested against ' '.

Chapter 17 Exercise 6

Function delete_from_list is not working correctly.

It should delete a certain node of value n in a linked list only but the implemented one skips all nodes before the node to be deleted and then the list starts from the next node.

example:
1->2->3->4->5

list = delete_from_list(list, 3)
Expected Result: 1->2->4->5
Actual Result: 4->5

Chapter 9, Exercises 6.

when second argument is greater than 1, the function always returns 0. Adding parenthesis produces correct result if(( n /= 10) <= 0)

Exercises 3.3

(b) "%d-%d-%d" and "%d -%d -%d" are not equivalent.

// For input "1 -2 -3"
scanf("%d-%d-%d", &a, &b, &c);     // a=1, b and c are not assigned
scanf("%d -%d -%d", &a, &b, &c);   // a=1, b=2, c=3

postfix increment operator with pointer, CH11, proj 11.4

if (num_in_rank[rank] == 2) *pairs++;

*pairs++; might be changed to (*pairs)++;.
Given postfix increment operators have higher precedence than indirection operators, the expression *pairs++; is equivalent to *(pairs++);, accessing the object stored in the next address in memory to pairs, which leads to the compiler issuing a warning "expression result unused", and to the program failing to recognize pairs in the example of "8c/as/7c/ad/3h".

chapter 13 exercise 15

(a)return 3
(b)return 0
(c)The length of the longest prefix of the string s that consists entirely of characters from the string t. Or, equivalently, the position of the first character in s that is not also in t.

Exercise 16.18

typedef struct { Piece; Color; } Square;
Decleration doesn't declare anything

Chapter 19 Exercise 7

The function resize_contents use the original array pointer for reallocation which is not safe. The data will be lost forever if reallocation failed. The requirements are that a new dynamic array will be created with the size doubled and if reallocation is a success, the original array will be freed and re-points to the reallocated memory (which is not the case in your implementation).

Suggested solution;

static void resize_contents(Stack s)
{
    Item *new_contents = realloc(s->contents, (s->size *= 2) * sizeof(Item));
    if(new_contents == NULL)
         terminate("Error in resize_contents: stack is full.");
    
   free(s->contents);
   s->contents = new_contents;
}

Exercise 4.09

c) is incorrect.
You have stated:
0 -1 3

When in reality it is:
2 -1 3

Per code (any standard):

#include <stdio.h>

int main(void)
{
int i, j, k;

i = 1; j = 2; k = 3;
i -= j -= k;
printf("%d %d %d\n", i, j, k);

return 0;
}

More specifically, per KN Kings site:
http://knking.com/books/c2/answers/c4.html

Exercise 4.10 part (d)

Thanks for you solutions it has really helped me a lot.
I think the output would be 6 8 instead of 6 9.
Thank you!

Programming Project 8.9 - Grid directions mixed up, and initial grid fill per textbook

Love your elegant solution overall and using the boolean idea for the direction variables!

(1) Note: The names of the direction variables (up, down, left, right) don't match those same directions actually being tested in the "if" statements nor the actual placement of the next letter in the grid ("switch-case" statements). (The i, j (row, column) are mixed around, relative to your naming convention.) For example:

"up": tests and places next letter to the RIGHT.
"down": tests and places next letter to the LEFT.
"right": tests and places next letter BELOW.
"left": tests and places next letter ABOVE.

(2) The problem instructs to fill the grid initially with a character (all '.' initially), instead of going through at the end and replacing '0' with '.' characters. (Very minor difference, the tests are the same, and the end result is obviously the same...)

Exercise 17.13

Solution produces incomplete list
Last lines of function read:

    *pp = new_node;
    new_node = list;
}

but should possibly read?

  *pp = new_node;
   new_node->next = list;
}

also either declared return type should be void or something should be returned to avoid warning.

exercise 2.10

Seemingly the spaces between "int main" and "int height" are also essential.

Chapter 9 Exercise 10 small corrections required

a) The wrong name of the array is used in the function; instead of n[ i ] it should be a[ i ].

b) I think the type of function of double would work better for computing the average instead of type int; double average() instead of int average(). Correct me if I am wrong.

c) On line 6, it should be a[ i ] instead of a[ n ].

Exercise 14/5

b, solution is 2 not 3
The program shortcircuits after the first condition of AND hasn't been met so i get's incremented only twice

Chapter 8 project 14

while (i >=0) should be while (i > 0)
i is 0 after the last word is printed, but it keeps looping so i becomes -1

Issue with Chapter 4 Exercise 5

You seem to have the wrong solutions for the last three parts of this question.
Part (a) is okay, since it is obvious that
8 % 5 = 3
regardless of the C standard being used.

However, on the next three parts, you seem to be doing something wrong.
-8 % 5 = -3 or -8 % 5 = +2
The first solution is obvious and the second one is obtained by adding 5 to it (which is allowed and will not change anything).
You seem to be adding 8 however, which is definitely not a good idea.

Exercise 6.08 is slightly incorrect

The solution to 6.08 reads:

Solution

The program will print 10 1 then print an endless stream of 1 s. The postfix increment operator in the print statement will always cause the conditional in the for loop to return true.

However, the program will actually print 10 5 3 2 followed by an endless stream of 1s.

Exercise 16.05

First if's condition should be d1day < d2day, cause then d1 is an earlier date, but
This doesn't include years at all so that has to checked as well to:
if(d1day < d2day && d1.year <= d2.year) return -1;

Programming Project 5.7 has 5 if statements

I suggest the following:

#include <stdio.h>

int main() {
int a1, a2, a3, a4, min1, max1, min2, max2, min, max;
printf("Enter four integers: ");
scanf("%d %d %d %d", &a1, &a2, &a3, &a4);

if (a1 < a2) {
min1 = a1;
max1 = a2;
} else {
min1 = a2;
max1 = a1;
}

if (a3 < a4) {
min2 = a3;
max2 = a4;
} else {
min2 = a4;
max2 = a3;
}

if (min1 < min2) {
min = min1;
} else {
min = min2;
}

if (max1 < max2) {
max = max2;
} else {
max = max1;
}

printf("Min is %d, max is %d\n", min, max);
}

chapter 13 Project 6

Program wont compile.
Line 15 is the Ptoblem
if (strcmp(toupper(argv[i]), toupper(planets[j])) == 0) {

toupper return a int value, strcmp expects type char* not an type int.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.