williamgherman / c-solutions Goto Github PK
View Code? Open in Web Editor NEWMy Solutions to K. N. King's "C Programming: A Modern Approach", second edition
License: GNU General Public License v3.0
My Solutions to K. N. King's "C Programming: A Modern Approach", second edition
License: GNU General Public License v3.0
Miss the case to do the update that a[i] is between the current *largest and *second_largest.
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.
https://github.com/williamgherman/c-solutions/tree/master/02/exercises/01
Actually, according to C Programming, A Modern Approach, 2nd edition; this exercise is called "pun.c", instead of "Hello World"
Also, versioning is 2.1, not 2.01
From the book, refer to page 9, item 2.1. (C Fundamentals)
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 1
s.
maybe changed as follow:
int sum_2_dimensional_array(const int a[][LEN], int n)
{
const int *p;
int sum =0;
for (p = *a; p < *a + n * LEN; p++)
sum += *p;
return sum;
}
https://github.com/williamgherman/c-solutions/tree/master/02/exercises/09
"Insert spaces betwee nthe tokens" should be "Insert spaces between the tokens"
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!
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
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 ].
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;
}
Answer should be:
void capitalize(char * str){
char *p = str;
while(*p != '\0')
{
if(isalpha(*p)) {
*p = toupper(*p);
}
p++;
}
}
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;
(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
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
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.
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.
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
There are 14 tokens: a, =, (, 3, *, q, -, p, *, p, ), /, 3, and ;.
when second argument is greater than 1, the function always returns 0. Adding parenthesis produces correct result if(( n /= 10) <= 0)
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
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);
}
ERROR
c4.imaginary -> c3.imaginary
1 % 3 is = 1 not zero
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 '-'.
typedef struct { Piece; Color; } Square;
Decleration doesn't declare anything
c-solutions/11/projects/04/4.c
Line 140 in f6206d2
*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".
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 ' '
.
.
enum weekdays = {MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
FRIDAY, SATURDAY, SUNDAY};
https://github.com/williamgherman/c-solutions/blob/master/02/projects/06/README.md
"is knows" should be "is known"
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)
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.
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);
}
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
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
https://github.com/williamgherman/c-solutions/blob/master/02/projects/03/README.md
"user to eter" should be "user to enter"
https://github.com/williamgherman/c-solutions/blob/master/02/projects/07/README.md
"amount by the totoal" should be "amount by the total"
(c) The answer should be 2, 8
I believe that answer should be 20 bytes
After (*s++==*t++), return *s-*t is error, it maybe return * (s--)- *( t--)
https://github.com/williamgherman/c-solutions/blob/master/02/projects/01/README.md
"prinf" should be "printf".
https://github.com/williamgherman/c-solutions/tree/master/02/exercises/10
"linebreak" should be "line break"
Answer should be :
teenager = age>=13 && age<=19?true:false
I think that line
while (c+2 != '\0')
should be
while (*(c+2) != '\0') or while (*(c+2))
Seemingly the spaces between "int main" and "int height" are also essential.
(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.
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...)
After copy pasting the answer fragment (and declaring the appropriate ints), the computer printed 20
rather than 25
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.