Posts

C: Temperature Conversion With Main Repeat

Image
 https://github.com/pereiradaniel/beginning_c/blob/master/ch3/exercises/3_1/exercise3_1.c // Exercise 3-1 // Write a program that will first allow a user to choose one of two options: // 1. Convert a temperature from degrees Celsius to degrees Fahrenheit. // 2. Convert a temperature from degrees Fahrenheit to degrees Celsius. // The program should then prompt for the temperature value to be entered and output the new value that results // from the conversion. To convert from Celsius to Fahrenheit you can multiply the value by 1.8 and then add 32. #include <stdio.h> #include <ctype.h> #define TOFARENHEIT(a) a*1.8+32.0 #define TOCELSIUS(a) (a-32.0)/1.8 int main(int argc, char* argv[]) { int choice = 0; // Stores user's menu choice. double temp = 0.0; // Stores user's temperature input. char repeat = 'Y'; while (toupper(repeat)=='Y') { choice = 0; while (choice == 0) { printf(...

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: Calculate Order Price Based On User's Input.

Image
 https://github.com/pereiradaniel/beginning_c/blob/master/ch2/exercises/2_3/exercise2_3.c // Exercise 2-3 // Daniele Grech Pereira 22 November 2022 // You’re selling a product that’s available in two versions: type 1 is a // standard version priced at $3.50, and type 2 is a deluxe version priced at $5.50. // Write a program using only what you’ve learned up to now that prompts for the user to // enter the product type and a quantity, and then calculates and outputs the price for // the quantity entered. #include <stdio.h> #include <ctype.h> // toupper #include <limits.h> // INT_MAX // FIXED PRODUCT PRICES: #define TYPE1 3.5f // Type1 price #define TYPE2 5.5f // Type2 price // A function that returns the calculated the price. double calculatePrice(int input, int quantity); // A function that validates a user's input. int checkInput(int input, int min, int max); // A function that returns the user's validated input. int getInput(char* msg, int min, int ma...

C64: Tonk in the Land of Buddybots (Longplay, Level 1).

Image
  Longplay of a very old 8 bit game by children's author Mercer Meyer. This is a fine example of the burgeoning software industry of the era, and the sort of creativity that people were finding from early computers. While I originally enjoyed this game on an ATARI 600XL, it is very similar to the Commodore 64 version.

C: Calculate Floor Area Based On User Input.

Image
 https://github.com/pereiradaniel/beginning_c/blob/master/ch2/exercises/2_2/exercise2_2.c // Exercise 2-2 // Write a program that prompts for input of the length and width of a room in // feet and inches, and then calculates and outputs the floor area in square yards with two // decimal places after the decimal point. #include <stdio.h> double getYards(int feet, int inches) { double result = (double)feet * 0.3333333 + (double)inches * 0.02777778; return result; } int main(int argc, char* argv[]) { int feet = 0, inches = 0; // vars for storing user input double length = 0, width = 0; // room dimensions in yards // Prompt user for length and width of a room in feet and inches. printf("ROOM LENGTH\n-----------\nEnter room length:\nFEET: "); scanf(" %d", &feet); printf("INCHES: "); scanf(" %d", &inches); length = getYards(feet, inches); printf("\nROOM WIDTH\n----------\nEnter room ...

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 /...

C: Tree Height Problem

Image
This example is taken from Chapter 2 of the book, "Beginning C", 5th edition . It can be found on page 77. I have made some of my own modifications to the example from the textbook, but it works in the same way. Tree Height Problem The Problem Your problem is to find out the height of a tree without using a very long ladder, which itself would introduce risk to life and limb. To find the height of a tree, you’re allowed the help of a friend—preferably a short friend unless you yourself are short, in which case you need a tall friend. You should assume that the tree you’re measuring is taller than both you and your friend. The Analysis Real-world problems are rarely expressed in terms that are directly suitable for programming. Before you consider writing a line of code, you need to be sure you have a complete understanding of the problem and how it’s going to be solved. Only then can you estimate how much time and effort will be involved in creating the solution. The analysis...