|
T. Andrew Yang Email:
yang@uhcl.edu Web page
: http://sce.uhcl.edu/yang/ Tel.: (281) 283-3835 |
last
updated 7/6/2018:
Lab #6 posted 6/11/2018:
Labs #2-#5 published 6/4/2018:
Lab one published |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Note: An important part of problem solving is correct
understanding of the given problem. -
Try to have a good grasp of the
problem before starting the process of finding the solution(s). Use any resources, including the instructor, the TA, your
classmates/friends, and online resources to ensure that you have correctly
understood the given problem. -
While trying to figure out
the solution(s), continue to verify your understanding of the problem. 1.
Lab 1 Total points= 100 1.1.
(20 pts)
Visit the
class discussion group (see the syllabus for the URL) and complete the
following tasks: 1.1.1.
Configure
your membership settings, so each
email sent to the group will be forwarded to your preferred email address
(that is, the email account that you regularly check each day). 1.1.2.
Post a message with your full name as the subject line. In your
post, briefly introduce yourself (including your full name) and one item you
most desire to learn in this class. 1.1.3.
Throughout
this class, you shall regularly visit the discussion group to find recent
announcements and reminders, and to participate at discussions. 1.2.
Developing C Programs 1.2.1.
Figure 1
shows a sample C program.
Figure 1.1. A sample C program
Note: The source program as shown in Figure 1.1 may
contain special characters when being saved into a text file. Fix the errors
by replacing/retyping those characters using a text editor. 1.2.2. Suppose that program is saved as a text file named test.c. Figure 1.2 is a screen
snapshot that shows commands used to compile and execute that program, using
the Tiny C Compiler (tcc), and the sample user input and the output produced
by that program. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Decimal |
Binary |
|
0 |
0 |
|
1 |
1 |
|
2 |
10 |
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
2.1.2. (5 pts) With 3 binary bits, how many different states or
values can be represented?
Ans:
________________
Show all
the different states that can be represented by 3 bits:
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
2.1.3. (15 pts) Complete the following table by filling in missing
values.
|
Decimal |
Binary |
|
|
100000 |
|
|
100001 |
|
|
100010 |
|
|
100011 |
|
|
101010 |
|
|
101011 |
|
|
101100 |
|
|
110101 |
|
|
110110 |
|
|
110111 |
|
|
111000 |
|
|
111010 |
|
|
111100 |
|
|
111110 |
|
|
111111 |
2.1.4. (5 pts) Suppose a sensor device has 4 bits to store data
values. With all the bits being used to represent zero and positive integer values,
what are the integers that can be represented by those bits? List them all.
Highlight the smallest and the largest values.
2.1.5. (5 pts) Suppose a sensor device has 4 bits to store data
values. With the leftmost bit being used to represent the sign, and the rest
of the bits being used to represent integer values, what are the integers
that can be represented by those bits? List them all. Highlight the smallest
and the largest values.
2.1.6. (5 pts) Show the sum of the following pairs of binary numbers.
2.1.6.1.
1010b
+ 1010b = ____________
2.1.6.2.
0101 b
+ 1010b = ____________
2.1.6.3.
1100b
+ 1101b = ____________
2.1.6.4.
1010b
+ 1011b = ____________
2.1.6.5.
1011b
+ 1011b = ____________
2.1.7. (5 pts) Explain what data overflow means. Use an example to
explain what it is.
2.2.
Write a
program that takes two numbers (say a and b) from the user, and then
display the following values.
(a)
The result of a div b. Note: div returns the quotient of an integer division.
(b)
The remainder
of a div b.
Example: Let a be 13 and b be 3. Then a div b is 4, and
the remainder of a div b is 1.
Hint: Use the function scanf( ) to get user input.
To hand in:
2.2.1. (20 pts) The source program with
proper comments and author information.
2.2.2. (20 pts) Before the lab is due, give
the TA a face-to-face demo during his/her office hours by showing how
you’d run the revised program using one of the computers in the lab.
Go to the Index
Total points= 100
3.1. A C program is composed of functions, each of which has a
clearly-defined functionality. Write a function called letterGrade( ) that
returns the letter grade of a given numeric value, according to the following
table.
|
Percentile |
Grade |
|
Percentile |
Grade |
|
90% or above |
A |
|
70% - 73% |
C |
|
87% - 89% |
A- |
|
67% - 69% |
C- |
|
84% - 86% |
B+ |
|
64% - 66% |
D+ |
|
80% - 83% |
B |
|
60% - 63% |
D |
|
77% - 79% |
B- |
|
57% - 59% |
D- |
|
74% - 76% |
C+ |
|
Less than 57% |
F |
For example, letterGrade(91)
returns A, while letterGrade(71) returns
C, and so on.
Hint: Use nested if … else … to determine the letter grade.
The main(
) function should first ask the user for a numeric value between 0 and
100, and then calls the letterGrade( ) function
to get the letter grade. Once the letter grade is returned, it will be
printed on the screen.
3.1.1. (30
pts) Draw a flowchart to show the logic of the letterGrade( ) function.
3.1.2. (40
pts) The source program with proper comments.
3.1.3. (30
pts) Before the lab is due, give the TA a face-to-face demo during
his/her office hours by showing how you’d run the revised program using
one of the computers in the lab.
Go
to the Index
Total points= 100
4.1. Write a program to get
a person’s height and weight first, and then print that person’s
BMI by using the formula as given in http://www.calculator.net/bmi-calculator.html.
Note: Your program should be constructed the
same way as shown below. That is, what you need to do is to complete all the
undefined functions (getHeight( ), getWeight( ), etc.) Do not change the
signatures of those functions. The printBMI( ) function will print the
calculated bmi, and also indicate the category of that person (underweight,
normal, or overweight).
#include <stdio.h>
int getHeight() {
} //getHeight()
double getWeight() {
}
float calculateBMI(int height, double
weight) {
}
void printBMI(float bmi) {
}
int main () {
int
height;
height
= getHeight();
double
weight;
weight
= getWeight();
float
bmi=0;
bmi
= calculateBMI (height, weight);
printBMI(bmi);
} //main
Below is a sample output of running this
program:

4.1.1. (30 pts) Draw a flowchart to
show the logic of the calculateBMI( ) function.
4.1.2. (40
pts) The source program with proper comments.
4.1.3. (30
pts) Before the lab is due, give the TA a face-to-face demo during
his/her office hours by showing how you’d run the revised program using
one of the computers in the lab.
Go
to the Index
Total points= 100
5.1. The Euclidian algorithm takes two
integers as the input, and returns their greatest
common divisor (gcd). Revise the main function of the sample program (as
shown in Figure 5.1) by adding a loop into the main function. Within the
loop, the program will prompt the user to enter two integers, num1 and num2, and then calls the greatestCommonDivisor( ) function to
calculate the gcd. Test your program by trying the following three pairs of
input numbers: (99, 66), (99, 0), (-5, 10). Note: Your program should print appropriate messages in response
to the user input. Figure 5.2 shows a sample running of the program.
|
//source: https://codereview.stackexchange.com/questions/37189/euclids-algorithm-greatest-common-divisor
#include <stdio.h> int greatestCommonDivisor(int
m, int n) //This function
implements the Euclidian Algorithm { int r; /* Check For Proper
Input */ if((m == 0) || (n == 0))
return 0; else if((m < 0) || (n
< 0))
return -1; do {
r = m % n; if(r == 0)
break;
m = n;
n = r; } while(true); return n; } int main(void) { int num1 = 600, num2 =
120; int gcd =
greatestCommonDivisor(num1, num2); printf("The GCD of
%d and %d is %d\n", num1, num2, gcd); getchar(); return 0; } |
Figure 5.1 Sample implementation of the Euclidian Algorithm

Figure 5.2 Sample console input and output
To hand in:
5.1.1. (30 pts) Write a pseudocode to
show your logic of the loop added to the main function.
5.1.2. (40
pts) The source program with proper comments.
5.1.3. (30
pts) Before the lab is due, give the TA a face-to-face demo during
his/her office hours by showing how you’d run the revised program using
one of the computers in the lab.
Go
to the Index
Total points= 100
6.1. The following sample
program gets data from the user, and then enters those data into an integer
array. The function getDataIntoArray(
) returns the number of items successfully entered into the array.
Sample screen of running the program is shown
in Figure 6.1.
|
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #define SIZE 100 void initializeArray(int[SIZE]); //initialize each element to be zero int getDataIntoArray(int[SIZE]); //This function takes as a parameter an array of
strings. void showDataFromArray(int[SIZE], int); //show all the elements in a given array void findMax(int[SIZE], int); //find the largest number in the given array double average(int[SIZE], int); //return the average of all numbers in the given
array void main() { int intArray[SIZE]; initializeArray(intArray); int number
= getDataIntoArray(intArray); printf("number: %d\n", number); /* Uncomment the following
statements when new functions have been coded. printf("\nCalling the
showDataFromArray() function ...\n"); showDataFromArray(intArray, number); //Display all the names currently in that array. printf("\nCalling the findMax()
function ...\n"); findMax(intArray, number); printf("\nCalling the average()
function ...\n"); printf("The average of all
entered numbers is %.2f.\n", average(intArray, number)); */ } void initializeArray(int arr[SIZE])
{ for (int i = 0; i < SIZE; i++) arr[i] = 0; } int getDataIntoArray(int arr[SIZE])
{ int num
= 0; int index
= 0; do { printf("Please enter a number
between 0 and 100 (or -1 to quit): "); scanf("%d", &num); printf("X: %d\n", num); if (num
!= -1) { printf("copying data into array
...\n"); arr[index] =
num; index++; } } while
(index < SIZE && num
!= -1); if (index
> 0) printf("\nYou have successfully
entered %d items.\n\n",
index); else printf("No item was
entered.\n"); return index; } |
|
Figure 6.1 Sample output of getting
user data into an array |
6.2. Design and implement
three functions that process the array intArray
created in the sample program, including showDataFromArray( ), findMax( ),
and average( ). Sample calls to those functions are shown as comments in the
sample program.
-
The function showDataFromArray(
) takes the intArray and the number of elements as parameters, and shows
each of the elements currently in the array (along with its respective index
value in that array).
-
The function findMax(
) searches through the intArray to find the largest number currently in
that array.
-
The function average(
) calculates and returns the average of all numbers currently in that
array.
Implement the showDataFromArray( ) and the
findMax( ) functions. When ready, uncomment the respective sample calls in
the main( ) function, and then run the revised program.
Note: The average( ) function is optional and,
when successfully implemented, a bonus project.
Your revised program should generate screen
output as shown in Figure 6.2 (except for the output produced by the average( ) function).
|
Figure 6.2 Sample screen output of
running the revised program |
To hand in:
6.2.1. (30 pts) Draw a flowchart to
show your logic of the findMax( ) function.
6.2.2. (40
pts) The source program with proper comments.
6.2.3. (30
pts) Before the lab is due, give the TA a face-to-face demo during
his/her office hours by showing how you’d run the revised program using
one of the computers in the lab.
6.3. (50 extra pts) Bonus project
Implement and test the average( ) function.
To hand in:
(a)
(15 pts) A flowchart showing
your design of the average( ) function.
(b)
(15 pts) The source program
(c)
(20 pts) Show the demo to the
TA.
Go
to the Index