lab3 code

This commit is contained in:
Haldrup-tech 2024-09-09 15:21:47 -04:00
parent e11f2c662e
commit 2f83861249
4 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,14 @@
1. Convert the following binary number to decimal. please show the conversion step.
For example: 0b1101 = 1 + 4 + 8 = 13
a. 0b1001
0b1001 = 8 + 1 = 9
b. 0b11000100
0b11000100 = 128 + 64 + 4 = 196
c. 0b100101
0b100101 = 32 + 4 + 1 = 37
d. 0b111010
0b111010 = 32 + 16 + 8 + 2 = 58
e. 0b1011
0b1011 = 8 + 2 + 1 = 11
2. How many possible integers can an unsigned 5 bits binary represent? What is the largest decimal number? How about 8 bits binary? n bits binary?
A 5 bit number can represent 32 unique numbers. The largest decimal number is 31. 8 bits can represent 256 unique numbers. n bits can represent 2^n unique numbers.

View File

@ -0,0 +1,13 @@
1. Convert the following decimal number to binary. please show the conversion step.
For example: 89 = 64 + 0 + 16 + 8 + 0 + 0 + 1 = 0b1011001
a. 32
32 = 32 + 0 + 0 + 0 + 0 + 0 = 0b100000
b. 56
56 = 32 + 16 + 8 + 0 + 0 + 0 = 0b111000
c. 207
207 = 128 + 64 + 0 + 0 + 8 + 1 + 1 + 1 = 0b11001111
d. 127
127 = 64 + 32 + 16 + 8 + 4 + 2 + 1 = 0b1111111
e. 93
93 = 64 + 0 + 16 + 8 + 4 + 0 + 1 = 0b1011101

BIN
csci218/Labs/Lab3/a.out Executable file

Binary file not shown.

80
csci218/Labs/Lab3/lab3.c Normal file
View File

@ -0,0 +1,80 @@
/* Name: Garrett Haldrup
<lab3.c>
Problem: Have two functions called in main
Function1(C2F): Convert Fahrenheit to Celcius
Function2(getShootingPercent): Calculate shooting percentage based on shots attempted and succeded.
Certification of Authenticity:
I certify that this assignment is entirely my own work.
*/
#include <stdio.h>
void greet();
void C2F();
void getShootingPercent();
int main() {
// call function greet()
greet();
/* Activity 1. write your code to call C2F() */
C2F();
/* Activity 2. write your code to call getShootingPercent() */
getShootingPercent();
return 0;
}
void greet(){
printf("Hello, world!\n");
}
void C2F() {
// State purpose of function
printf("This function will convert Celcius to Fahrenheit\n");
// Declare the Fahrenheit and Celcius float variables
float tempF, tempC;
// Scan in Fahrenheit value from user
printf("Please enter degrees Celcius(C°): ");
scanf("%f", &tempC);
// Convert Fahrenheit to Celcius using the equation C = 5.0/9.0(F - 32)
tempF = (tempC * (9.0 / 5.0)) + 32;
// Output calculated Celcius tempature
printf("The tempature %.1fC° converted to Fahrenheit(F°) is: %.1fF°\n", tempC, tempF);
}
void getShootingPercent() {
// State purpose of function
printf("This function will calculate your shooting percentage\n");
// Declare shots attempted and success as int and the shooting percentage as float
int shotsAttempt, shotsSuccess;
float shootPer;
// Scan in shots attempted and shots success from user
printf("Please enter number of shots attempted: ");
scanf("%d", &shotsAttempt);
printf("Please enter how many of the %d shots were succesfull: ", shotsAttempt);
scanf("%d", &shotsSuccess);
// Check to see if data entered is correct and calculate the percentage by (shotsSuccess/shotsAttempt) * 100
if (shotsSuccess > shotsAttempt) {
printf("Invalid Entry: More success than attempts\n");
} else {
shootPer = ((float)shotsSuccess / shotsAttempt) * 100;
// Output shot percentage to user
printf("Your shooting percentage is: %.1f%%\n", shootPer);
}
}