T. Andrew Yang

Email: yang@uhcl.edu   Web page : http://sce.uhcl.edu/yang/   Tel.: (281) 283-3835

last updated

3/29/2019: corrected Lab 7

2/16/2019: Corrected Lab 3 (to hand-in)

1/21/2019: Labs one and two published

 

CSCI 1320 C Programming



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.

#include <stdio.h>  //comment 1:

 

int main(void) { //2.

   int number1; //3.

   int number2;

   int result;

   printf("Enter the first number: "); //4.

   scanf("%d", &number1); //5.

   printf("Enter the second number: ");

   scanf("%d", &number2);

   result = number1 * 2 - number2 / 3; //6.

   printf("The result of %d multiplied by 2 minus %d divided by 3 is %d.\n",

                 number1, number2, result); //7.

   return 0; //8.

} //main //9.

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.

Figure 1.2. Running the sample program

1.2.3.     Save the sample program in Figure 1 as a text file (with test.c as the file name). Note: Remember where in the file system you have saved it.

1.2.4.     Make the following revisions to the sample program. Be sure to save the revised program (as test2.c).

(a)    Print your own name as the first line of that program’s screen output.

(b)   Add appropriate comments to explain the purpose/functionality of each of the statements (comments 1 through 9).

(c)    Instead of getting two numbers from the user, the program will ask for three numbers from the user.

(d)   The result will be number1 + number2 * number3.

1.2.5.     On a computer with tcc or Microsoft Visual Studio installed, open a command prompt to run the revised program.

Note: You are strongly encouraged to install a C compiler on your own computer. TCC is an open-source software and free to download; visit http://download.savannah.gnu.org/releases/tinycc/ if you want to download tcc.

Note: Not being able to install a C compiler on your own computer should NOT be a reason to be late in submitting the lab. Computers in the PC labs have Visual Studio installed and you may use them to complete this portion of the lab.

1.2.6.     To hand in:

1.2.6.1.     (40 pts) The revised C source program per instructions above.

1.2.6.2.     (40 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.

 

Note: It is a student's responsibility to make sure the demo is given during the TA's office hours before the due date/time. The TA is NOT required to stay after his/her office hours in order to see your demo.

 

Go to the Index


2.      Lab 2

Total points= 100

2.1.   Binary and hexadecimal numbers

 

2.1.1.     (30 pts) Complete the following table by filling in missing values.

Decimal

Binary

Hexadecimal

32

 

 

33

 

 

34

 

 

35

 

 

36

 

 

37

 

 

38

 

 

39

 

 

40

 

 

41

 

 

42

 

 

43

 

 

44

 

 

45

 

 

46

 

 

47

 

 

48

 

 

49

 

 

50

 

 

51

 

 

52

 

 

2.1.2.     (10 pts) With 5 binary bits, how many different states or values can be represented?

 

Ans: ________________

Show all the different states that can be represented by those bits:

 

_________________________________________________________________________________

 

_________________________________________________________________________________

 

_________________________________________________________________________________

 

_________________________________________________________________________________

 

_________________________________________________________________________________

 

2.1.3.     (30 pts) Complete the following table by filling in missing values.

Decimal

Binary

Hexadecimal

 

100000

 

 

100001

 

 

100010

 

 

100011

 

 

101010

 

 

101011

 

 

101100

 

 

110101

 

 

110110

 

 

110111

 

 

111000

 

 

111010

 

 

111100

 

 

111110

 

 

111111

 

 

1111110

 

 

1111111

 

 

11111110

 

 

11111111

 

 

100000000

 

2.1.4.     (10 pts) Suppose a sensor device has 8 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.     (10 pts) Suppose a sensor device has 8 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.            101010b + 10100b = ____________

2.1.6.2.            010011 b + 10010b = ____________

2.1.6.3.            101001b + 10101b = ____________

2.1.6.4.            101001b + 10101b = ____________

2.1.6.5.            101010b + 10101b = ____________

 

2.1.7.  (5 pts) Explain what data overflow means. Use an example to explain what it is.

 

Go to the Index


3.      Lab 3

Total points= 100

3.1.   A C program is composed of functions, each of which has a clearly-defined functionality. A function may call other functions to perform specific tasks. The calling relationship can be illustrated using a calling graph.

3.1.1.    Study and run the following program to understand how it works and, in particular, how the various functions are related to each other. Draw a calling graph to clearly show the order of function calls and the returned value, if applicable.

#include <stdio.h>

 

int f1(int); //function declaration

void f2(int);

void f3(void);

char f4(int);

 

int main() {

            //printf("Hello. Welcome!\n My name is ...\n");

            printf("calling f1() ...\n");

            int f1Result = f1(23);

            printf("f1Result = %d\n", f1Result);

            f3();

} //main()

 

int f1(int data) {

            printf("in f1(): data = %d\n", data);

            data = data / 5;

            f2(data);

            return data;

}

 

void f2( int data) {

            printf("in f2(): data is %d\n", data);

            char f2Char = f4(55);

            printf("f2Char: %c\n", f2Char);

} //f2()

 

void f3() {

            printf("Bye.\n");

} //f3()

 

char f4(int x) {

            printf("in f4(): x is %d\n", x);

            return 'X';

} //f4()

3.1.2.    Revise the above program by replacing the commented-out line (//printf("Hello. Welcome!\n My name is ...\n"); ) with a function call greeting( ). That function will print a simple greeting message followed by introducing your own name. For example, when the greeting( ) function is called, it may display the following greeting message on the screen:

               Hello. Welcome!

               My name is John Doe. (Note: Use your real name.)

 

To hand in:

3.1.2.1.    (30 pts) The calling graph from 3.1.1 above.

3.1.2.2.    (40 pts) The revised C source program per instructions above.

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

 

Go to the Index


4.      Lab 4

Total points= 100

4.1.   A C program is composed of functions, each of which has a clearly-defined functionality. Write a function called numericToLetterGrade( ) 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, numericToLetterGrade(91) returns A, while numericToLetterGrade(77) returns B-, and so on.

Hint #1: Use nested if … else … to determine the letter grade.

Hint #2: The function should return a string. The following sample program demonstrate how to write a function that returns a string.

 

#include <stdio.h>

 

char * fReturnString();

int main() {

            char * aString = fReturnString();

            printf("value of aString: %s\n", aString);

}

char * fReturnString() {

            return "Have a good day!";

}

 

The main( ) function should first ask the user for a numeric value between 0 and 100, and then calls the numericToLetterGrade( ) function to get the letter grade. Once the letter grade is returned, it will be printed on the screen.

4.1.1.  (30 pts) Draw a flowchart to show the logic of the numericToLetterGrade(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


5.      Lab 5

Total points= 100

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

http://sceweb.uhcl.edu/yang/teaching/csci1320summer2018/Assignments_files/image004.jpg

 

5.1.1.    (30 pts) Draw a flowchart to show the logic of the calculateBMI( ) 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


6.      Lab 6

Total points= 100

6.1.   Write a program that asks the user to continue to enter two numbers (at a time). For each pair of numbers entered, the program calculates the product of those two numbers, and then accumulate that product. For each pair of numbers entered, the program also prints the entered numbers, their product, and the accumulated products. The process will continue until the user enters two zeros. See the following screen snapshot for a sample running of the completed program.

Hint: Use a while loop.

 

Requirements: Write your program such that it generates screen output as shown above.

6.1.1.    (30 pts) Write a pseudocode to show the logic of your program.

6.1.2.    (40 pts) The source program with proper comments.

6.1.3.    (30 pts) Before the due time, give the TA a demonstration of your running program.

Go to the Index


7.      Lab 7

Total points= 100

7.1.   The following is a partially completed program that continues to (a) first display a menu of commands, and then (b) asks the user to choose 1 to enter a grade or 0 to quit the input process. Your job is to complete the main( ) function by implementing a do … while loop.

 

#include <stdio.h>

 

void displayMenu( );

int getInput( );

 

void main() {

  

/* Build a loop that will continue to call the displayMenu() and

** the getInput() functions, until the user enters 0 to quit.

*/

 

 

   printf("\nBye.\n");

} //main

 

void displayMenu() {

   printf("\n");

   printf("Enter 1 to enter a grade.\n");

   printf("Enter 0 to quit the input process.\n");

   printf("Input? ");

}

 

int getInput() {

   int input;

   scanf("%d", &input);

   return input;

}

 

Requirements: Write your program such that it generates screen output as shown above.

7.1.1.     (30 pts) Write a pseudocode to show the logic of your main function.

7.1.2.     (40 pts) The source program with proper comments.

7.1.3.     (30 pts) Before the due time, give the TA a demonstration of your running program.

 

Go to the Index


8.      Lab 8

Total points= 100

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

8.2.      Design and implement 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.

 

Your revised program should generate screen output as shown in Figure 6.2.

 

Figure 6.2 Sample screen output of running the revised program

To hand in:

8.2.1.  (30 pts) Draw a flowchart to show your logic of the findMax( ) and the average( ) functions.

8.2.2.  (40 pts) The source program with proper comments.

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

Go to the Index