Posts

Showing posts from November, 2022

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(&q

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

C: Display A Char's Numerical Value.

Image
The char type in C is an integer which gets decoded as a character. You can print the char's numerical type. https://github.com/pereiradaniel/beginning_c/blob/master/ch2/char.c // Chars // Daniel Pereira, 16 November 2022. // Display a char and it's numerical value. #include <stdio.h> int main(int argv, char* argc[]) { char c = 0; printf("Enter a character: \n"); scanf(" %c", &c); printf("The character: %c\nIt's numerical value: %d\n", c, c); return 0; } https://github.com/pereiradaniel/beginning_c/blob/master/ch2/char.txt ==87== Memcheck, a memory error detector ==87== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==87== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==87== Command: ./char ==87== Enter a character: X The character: X It's numerical value: 88 ==87== ==87== HEAP SUMMARY: ==87== in use at exit: 0 bytes in 0 blocks ==87== total heap usage: 2 al

C: Usage Of limits.h And float.h To Access Min And Max Constants.

Image
https://github.com/pereiradaniel/beginning_c/blob/master/ch2/limits.c // Limits // Daniel Pereira, 04 November 2022. // Use limits.h and float.h to access MIN and MIX variables. #include <stdio.h> #include <limits.h> // limits on integer types #include <float.h> // limits on floating point types int main(int argc, char* argv[]) { printf("INTEGER TYPE, MIN, MAX.\n"); printf("-----------------------\n"); printf("char, %d, %d\n", CHAR_MIN, CHAR_MAX); printf("unsigned char, NA, %u\n", UCHAR_MAX); printf("short, %d, %d\n", SHRT_MIN, SHRT_MAX); printf("unsigned short,NA,%u\n", USHRT_MAX); printf("int, %d, %d\n", INT_MIN, INT_MAX); printf("unsigned int, NA, %u\n", UINT_MAX); printf("long, %ld, %ld\n", LONG_MIN, LONG_MAX); printf("unsigned long, NA, %lu\n", ULONG_MAX); printf("long long, %lld, %lld\n", LLONG_MIN, LLONG_

C: Basic Arithmetic Operators.

Image
 https://github.com/pereiradaniel/beginning_c/blob/master/ch2/arithm.c // arithm.c // Daniele Grech Pereira, 02 November 2022 // Use basic arithmetic operators. #include <stdio.h> int main (int argc, char* argv[]) { // Declare and initialize vars: int add[2] = {0}, mult[2] = {0}, div[2] = {0}; double div2[2] = {0}; printf("All vars have been declared and initialized:\n"); printf("add1: %d, add2: %d, mult1: %d, mult2: %d, div1: %d, div2: %d, div3: %.2lf, div4: %.2lf.\n", add[0], add[1], mult[0], mult[1], div[0], div[1], div2[0], div2[1]); printf("Enter first whole number to add: \n"); scanf(" %d", &add[0]); printf("Enter second whole number to add: \n"); scanf(" %d", &add[1]); printf("add1 + add2 = ?\n%d + %d = %d\n", add[0], add[1], add[0]+add[1]); printf("Enter first whole number to multiply: \n"); scanf(" %d", &mult[0]);

C: Effects Of Accessing Uninitialized Variables Using printf()

Image
 https://github.com/pereiradaniel/beginning_c/blob/master/ch2/init.c // init.c // Daniele Grech Pereira, 02 November 2022 // Demonstration of variables before and after initialization. #include <stdio.h> #define LENGTH(a) sizeof(a) / sizeof(a[0]) void display(char* string, int array[], int size); int main(int argc, char* argv[]) { int num[10] = {1,2,3,4,5,6,7,8,9,10}; // Simultaneously declare and initialize a variable. int num2[10]= {1,2,3}; // First three are specified, the rest set to 0. int num3[10]= {0}; // Easy way to initialize all array values with 0. int arr[10]; // Declare an array of ints without initializing. // Printing the uninitialized array int arr[] will display garbage and generate warnings // from valgrind. display("Printing int num[] which has been initialized with data:", num, LENGTH(num)); display("Printing int num2[] which has been initialized with

C: Simple Usage Of Variables.

Image
 https://github.com/pereiradaniel/beginning_c/blob/master/ch2/vars.c // vars.c // Daniele Grech Pereira, 02 November 2022 // Demonstrate and explain briefly the simple usage of variables. #include <stdio.h> int main(int argc, char* argv[]) { int num, // // Variable declaration, also a definition*. num2; // Declare a second int variable for user input. num = 777; // Arithmetic assignment statement**. // FOR DEMONSTRATION ONLY!! // Uncomment these two lines to see what happens when you try to print a var that has not been initialized. printf("If you try to print num2 before it has a value assigned, you will retrieve the last data that was stored at that memory address!\n"); printf("num: %d, num2: %d\n", num, num2); printf("Enter a number to assign to num2: "); scanf(" %d", &num2); printf("Accessing int variable num: %d\n", num); // *** printf("Accessi