Posts

Showing posts with the label exercises

C++ Crash Course: Solution to Exercises in Chapter 4.

Image
My solutions to the exercises at the end of Chapter 4 in Part 1 of C++ Crash Course. In this series of exercises, you will make use of a Copy Constructor, Copy Assignment Operator, Move Constructor, and a Move Assignment Operator. Pay close attention to the differences in the elapsed times of Timer 1 and Timer 2. Pay close attention to the fact that when Timer 2 is destroyed, its name will be Timer 1 because of the Copy Constructor, where you will copy Timer 1's name to Timer 2, as well as the time stamp. You will also notice that there is never an indication that Timer 3 is destroyed, but keep in mind we are moving Timer 4 to Timer 3. Therefore, when Timer 4 is destroyed, that is really Timer 3, but after having Timer 4's name moved into its name field. A little bit confusing if you try to breeze through it, but pay close attention to each line of the program, and you will understand exactly what is happening in this exercise. Github: https://github.com/pereiradaniel/CPP_CRASH...

C++ Crash Course: Solution to Exercise 3-4.

Image
My solution to Exercise 3-4 from Part 1, Chapter 3 of C++ Crash Course. This one was fairly straightforward, but it illustrates a very important thing on line 13. When you assign new_value to the reference original_ref , it assigns the value to the int that original_ref is pointing to. The result is that new_value and original both have the same value, 200. Github: https://github.com/pereiradaniel/CPP_CRASH_COURSE/blob/master/P1C3/EXERCISES/3_4.cpp Youtube: C++ Crash Course: Solution to Exercise 3-4.

C++ Crash Course: Solution to Exercise 3-3.

Image
My solution to Exercise 3-3 from Part 1, Chapter 3 of  C++ Crash Course. Github: https://github.com/pereiradaniel/CPP_CRASH_COURSE/blob/master/P1C3/EXERCISES/3_3.cpp Youtube: C++ Crash Course: Solution to Exercise 3-3.

C++ Crash Course: Solution to Exercise 3-2.

Image
My solution to Exercise 3-2 from Part 1, Chapter 3 of C++ Crash Course. Github: https://github.com/pereiradaniel/CPP_CRASH_COURSE/blob/master/P1C3/EXERCISES/3_2.cpp Youtube: C++ Crash Course: Solution to Exercise 3-2.

C++ Crash Course: Solutions to Exercises in Part 1, Chapter 2.

Image
My solutions to the Exercises in Part 1, Chapter 2. Github: https://github.com/pereiradaniel/CPP_CRASH_COURSE/tree/master/P1C2/EXERCISES Video: C++ Crash Course: Solutions to Exercises in Part 1, Chapter 2.

C++ Crash Course: Solutions to Exercises in Part 1, Chapter 1.

For the last while, I have been busy with ESP32 and Arduino programming, but now I finally have time to ram through this book and the exercises. There are a few repositories on Github where people have offered solutions to some of these exercises, but I find that they are often not complete. Therefore, I am endeavouring to make a more complete listing, or at least fill in some of the holes. Here is a video and a link to the repository for the solutions to Part 1, Chapter 1. The exercises can be found at the end of each chapter. I am back-tracking a bit from where I am in Part 1 Chapter 5, which is where I am currently studying. Github: https://github.com/pereiradaniel/CPP_CRASH_COURSE/tree/master/P1C1/EXERCISES Video: C++ Crash Course: Solutions to Exercises in Part 1, Chapter 1.

C: Exercise 4-3 improved by splitting ascending/descending loop into two...

Image
Improvement to exercise 4-3 using a different type of switchable for loop, with compiler examination.  Repl: https://replit.com/@pereiradaniel/Improved-asc-desc-for-loop-added-to-existing-project Git repo: https://github.com/pereiradaniel/beginning_c/blob/master/ch4/exercises/4_3/refactor5.c  

C: Improve exercise 4-3 with improved ascending/descending for loop.

Image
Improvement to exercise 4-3 using a different type of switchable for loop, with compiler examination.  Repl: https://replit.com/@pereiradaniel/Improved-asc-desc-for-loop-added-to-existing-project Git repo: https://github.com/pereiradaniel/beginning_c/blob/master/ch4/exercises/4_3/refactor5.c  

C: Exercise 4-3, sort by ascending or descending order.

Image
Added sorting by ascending or descending order. Repl: https://replit.com/@pereiradaniel/Exercise-4-3-refactor-4 Git repo: https://github.com/pereiradaniel/beginning_c/blob/master/ch4/exercises/4_3/refactor4.c

C: Exercise 4-3 improvements of refactor 3.

Image
Improvement of this program: https://youtu.be/1_ER9Gzsglw Repl: https://replit.com/@pereiradaniel/Exercise-4-3-refactor-3 Git repo (this project):  https://github.com/pereiradaniel/Exercise-4-3-refactor-3 Git repo (archive of projects):  https://github.com/pereiradaniel/beginning_c  

repl: Exercise 4-3, refactor 3.

 

C: Print number of non-printable chars, printable chars, total char code...

Image
Refactor of previous program: https://youtu.be/pILnYM-qYB0 Display number of non-printable, printable, and total chars output by the program. Git repo: https://github.com/pereiradaniel/beginning_c/blob/master/ch4/exercises/4_3/refactor3.c

C: Refactored character code table printer.

Image
Refactored version of previous program: https://youtu.be/W8me8An2fA4 Extend the previous program to output the appropriate name, such as “newline”, “space”, “tab”, and so on, for each whitespace character. Git repo: https://github.com/pereiradaniel/beginning_c/blob/master/ch4/exercises/4_3/exercise4_3.c

C: Print character codes in a table.

Image
A program that will output the printable characters for character code values from 0 to 127. Each character code is printed along with its symbol. Make sure the columns are aligned. UPGRADES to original exericse!      - User selects between 1-8 columns to display.      - CHAR_MAX to control upper limit of char codes. Git repo: https://github.com/pereiradaniel/beginning_c/blob/master/ch4/exercises/4_2/exercise4_2.c

C: Print a multiplication table.

Image
Write a program that will generate a multiplication table of a size entered by the user. Git repo: https://github.com/pereiradaniel/beginning_c/blob/master/ch4/exercises/4_1/exercise4_1.c

C: Calculate discount price based on quantity input by user.

Image
Write a program that will calculate the price for a quantity entered from the keyboard, given that the unit price is $5 and there is a discount of 10 percent for quantities over 30 and a 15 percent discount for quantities over 50. Git repo: https://github.com/pereiradaniel/beginning_c/blob/master/ch3/exercises/3_3/exercise3_3.c

C: Program That Calculates Pay Based On Salary And Hours.

Image
 https://github.com/pereiradaniel/beginning_c/blob/master/ch2/exercises/2_4/exercise2_4.c // Exercise 2-4 // Write a program that prompts for the user’s weekly pay in dollars and the // hours worked to be entered through the keyboard as floating-point values. The program // should then calculate and output the average pay per hour in the following form: // Your average hourly pay rate is 7 dollars and 54 cents. #include <stdio.h> double checkInput(double input, double min, double max); int main(int argc, char* argv[]) { double hours = 0, rate = 0; // Get input for hours: printf("Enter number of hours worked: "); while(hours == 0) { scanf(" %lf", &hours); hours = checkInput(hours, 1, 10000); } // Get input for salary: printf("Enter hourly rate: "); while(rate == 0) { scanf(" %lf", &rate); rate = checkInput(rate, 1, 100); } // Calculate pay:...

C: Convert Inches To Yards, Feet, And Inches.

Image
This exercise is taken from Chapter 2 of the book,  "Beginning C", 5th edition . This is my solution to the problem. https://github.com/pereiradaniel/beginning_c/blob/master/ch2/exercises/2_1/exercise2_1.c // Exercise 2-1 // Write a program that prompts the user to enter a distance in inches and // then outputs that distance in yards, feet, and inches. // There are 12 inches in a foot and 3 feet in a yard. #include <stdio.h> int main(int argc, char* argv[]) { // Constants for conversion process: const int inches_per_foot = 12; const int feet_per_yard = 3; // Variables for user input and calculations: int input = 0, inches = 0, yards = 0, feet = 0; // Prompt user for input in inches: printf("Enter number of inches for conversion to yards, feet, inches: "); scanf(" %d", &input); inches = input; // Convert inches into yards, feet, inches: feet = inches / inches_per_foot; yards = feet /...