Posts

Showing posts with the label short programs

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: Looped program without using goto statement.

Image
Refactor of Exercise 3-4 without using the goto statement. Git repo: https://github.com/pereiradaniel/beginning_c/blob/master/ch3/exercises/3_4/refactor/refactor.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 formats date input by the user.

Image
Short program that prompts the user to enter the date as three integer values for the month, the day in the month, and the year, and outputs the date in the form "31st December 2003" when the user enters 12 31 2003, for example. Git repo: https://github.com/pereiradaniel/beginning_c/blob/master/ch3/exercises/3_2/exercise3_2.c Blog: https://pereiradpg.blogspot.com/2023/01/c-program-that-formats-date-input-by.html Book: http://www.mosaic-industries.com/embedded-systems/_media/c-ide-software-development/learning-c-programming-language/beginning-c-5th-edition-ivor-horton.pdf

C: Using scanf to assign more than one value.

Image
Short demonstration of how scanf can be used to accept input for more than one variable assignment. In this case, an integer and a double are assigned. Note what happens when a double is entered for an integer, and vice versa.

C: Nested Loops.

Image
https://github.com/pereiradaniel/c_programs/blob/master/nested/nested.c // Basic demonstration of nested for loops. #include <stdio.h> int main(int argc, char* argv[]) { // Outer loop will run until i equals 3 // (three times if starting at 0) for (int i=0; i<3; ++i) { printf("\ti = %d\n", i); // Nested loop will until j equals 6 // (three times if starting at 9) for (int j=9; j>6; --j) printf("\t\tj = %d\n", j); } return 0; } https://github.com/pereiradaniel/c_programs/blob/master/nested/nested.txt ==150== Memcheck, a memory error detector ==150== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==150== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==150== Command: ./nested ==150== i = 0 j = 9 j = 8 j = 7 i = 1 j = 9 j = 8 j =...

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

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: Using realloc() To Resize An Array Of Ints.

Image
 https://github.com/pereiradaniel/c_programs/blob/master/dyn_mem/realloc/realloc.c // Dynamic Memory Allocation - realloc // ----------------------------------- // This program accepts user input to create a dynamic array of ints. // calloc zeroes the space first! #include <stdio.h> #include <stdlib.h> // calloc! int main(int argc, char* argv[]) { // Prompt user for number of ints for dynamic memory allocation: int num_ints = 0; printf("This program will dynamically allocate memory for an array of ints.\nHow many ints would you like? "); scanf(" %d", &num_ints); // Create a dynamically allocated array of integers using user input: int *a = calloc(num_ints, sizeof(int)); // Displays zeroed data first: printf("Using calloc zeroes all the data first:\n"); for (int i=0; i<num_ints; ++i) printf("a[%d] = %d\n", i, a[i]); printf("\n"); // new line! // Use for loop to a...

C: Using calloc() For Dynamic Memory Allocation.

Image
Video demonstration:  https://youtu.be/xhnOzx4TzH4  https://github.com/pereiradaniel/c_programs/blob/master/dyn_mem/calloc/calloc.c // Dynamic Memory Allocation - calloc // ---------------------------------- // This program accepts user input to create a dynamic array of ints. // calloc zeroes the space first! #include <stdio.h> #include <stdlib.h> // calloc! int main(int argc, char* argv[]) { // Prompt user for number of ints for dynamic memory allocation: int num_ints = 0; printf("This program will dynamically allocate memory for an array of ints.\nHow many ints would you like? "); scanf(" %d", &num_ints); // Create a dynamically allocated array of integers using user input: int *a = calloc(num_ints, sizeof(int)); // Displays zeroed data first: printf("Using calloc zeroes all the data first:\n"); for (int i=0; i<num_ints; ++i) printf("a[%d] = %d\n", i, a[i]); printf("\n...

C: Using malloc() For Dynamic Memory Allocation.

Image
Video demonstration:  https://youtu.be/DUt2YFVxk5g https://github.com/pereiradaniel/c_programs/blob/master/dyn_mem/malloc/malloc.c // Dynamic Memory Allocation - malloc // ---------------------------------- // This program accepts user input to create a dynamic array of ints. #include <stdio.h> #include <stdlib.h> // malloc! int main(int argc, char* argv[]) { // Prompt user for number of ints for dynamic memory allocation: int num_ints = 0; printf("This program will dynamically allocate memory for an array of ints.\nHow many ints would you like? "); scanf(" %d", &num_ints); // Create a dynamically allocated array of integers using user input: int *a = malloc(sizeof(int) * num_ints); // Use for loop to assign values to the int array a. for (int i=0; i<num_ints; ++i) a[i] = i; // Use for loop to print contents of array a. for (int i=0; i<num_ints; ++i) printf("a[%d] = %d\n", i...

C: One rep max calculator.

Image
// Based on user input, calculate and output a scale of 50%-90% of 1RM #include <stdio.h> int main(int argc, int argv[]) { double onerm = 0; // stores user input for one rep max printf("Enter your one rep max: \n"); scanf("%lf", &onerm); for (int i=5; i <= 9; ++i) // Loop from 50% to 90% of one rep max printf("%.2f%% = %.2f lbs\n", i*0.1*100, i*0.1*onerm); return 0;  }