C code programming (NOT C++)

profilesnowflakes997
Homework6.pdf

1

ECE 175: Computer Programming for Engineering Applications Homework Assignment 6

Due: Tuesday March 12, 2019 by 11.59 pm Conventions: Name your C programs as hwxpy.c where x corresponds to the homework number and y corresponds to the problem number. For example, the C program for homework 6, problem 1 should be named as hw6p1.c. Write comments to your programs. Programs with no comments will receive PARTIAL credit. For each program that you turn in, at least the following information should be included at the top of the C file:

- Author and Date created - Brief description of the program: - input(s) and output(s) - brief description or relationship between inputs and outputs

Submission Instructions: Use the designated Dropbox on D2L to submit your homework. Submit only the .c files.

Problem 1 (15 points) Write a program that returns the minimum value and its location, max value and its location and average value of an array of integers. Your program should call a single function that returns that min and its location, max and its location and mean value of the array. Print the results in the main function (not within the array_func function). See sample code execution below. The declaration of this function is given below:

void array_func (int *x, int size, int *min_p, int *minloc_p, int *max_p, int *maxloc_p, double *mean_p)

/* x is a pointer to the first array element size is the array size min_p is a pointer to a variable min in the main function that holds the minimum minloc_p is a pointer to a variable minloc in the main function that holds the location where the minimum is. max_p is a pointer to a variable max in the main function that holds the maximum maxloc_p is a pointer to a variable maxloc in the main function that holds the location where the maximum is. mean_p is a pointer to a variable mean in the main function that holds the mean */

Declare the following array of integers within the main function: Sample code execution:

int data_ar[] = { -3, 5, 6, 7, 12, 3, 4, 6, 19, 23, 100, 3, 4, -2, 9, 43, 32, 45, 32, 2, 3, 2, -1, 8 }; int data_ar2[] = { -679,-758,-744,-393,-656,-172,-707,-32,-277,-47,-98,-824,-695, -318,-951,-35,-439,-382,-766,-796,-187,-490,-446,-647}; int data_ar3[] = {-142, -2, -56, -60, 114, -249, 45, -139, -25, 17, 75, -27, 158, -48, 33, 67, 9, 89, 33, -78, -180, 186, 218, -274};

2

Problem 2 (20 points): A barcode scanner verifies the 12-digit code scanned by comparing the code’s last digit to its own computation of the check digit calculated from the first 11 digits as follows:

1. Calculate the sum of the digits in the odd-numbered indices (the first, third, …, ninth digits) and multiply this sum by 3.

2. Calculate the sum of the digits in the even-numbered indices (the 0th, second, … tenth digits).

3. Add the results from step 1 and 2. If the last digit of the addition result is 0, then 0 is the check digit. Otherwise, subtract the last digit of the result from 10 to calculate the check digit.

4. If the check digit matches the last digit of the barcode, the barcode is valid.

Write an interactive C program that prompts the user to enter the 12 digits of a barcode separated by spaces. The program should store the digits in an integer array (-10 points if the array is NOT used in your program), calculate the check digit, and compare it to the last digit of the barcode. If the digits match, output with the message “barcode is validated.” If not, output with the message “error in barcode.” See sample code execution below: Note: loop structure should be used to access the array in step 1 and 2 above. Sample code execution (Red entered by a user)

Enter 12-digit barcode (separate each digit by space) 0 7 9 4 0 0 8 0 4 5 0 1 sum of digits in the odd-numbered indices = 16 sum of digits in the even-numbered indices = 21 sum from the first 11 digits = 69 barcode is valid Continue (q/Q to quit): y Enter 12-digit barcode (separate each digit by space) 0 1 1 1 1 0 8 5 6 8 0 7 sum of digits in the odd-numbered indices = 15 sum of digits in the even-numbered indices = 16 sum from the first 11 digits = 61 error in barcode Continue (q/Q to quit): y Enter 12-digit barcode (separate each digit by space) 0 1 1 1 1 0 8 5 6 8 2 7 sum of digits in the odd-numbered indices = 15 sum of digits in the even-numbered indices = 18 sum from the first 11 digits = 63 barcode is valid Continue (q/Q to quit): y Enter 12-digit barcode (separate each digit by space) 0 2 4 0 0 0 1 6 2 8 6 9 sum of digits in the odd-numbered indices = 16 sum of digits in the even-numbered indices = 13 sum from the first 11 digits = 61 barcode is valid Continue (q/Q to quit): q

For the barcode 0 7 9 4 0 0 8 0 4 5 0 1 => sum of odd = 7 + 4 + 0 + 0 + 5 = 16 => sum of even = 0 + 9 + 0 + 8 + 4 + 0 = 21 => sum from first 11 digits = 16*3 + 21 = 69 Since the last digit of 69 is 9 (not 0), check digit = 10 – 9 = 1 Since 1 (from the line above) = the last digit of the barcode => valid barcode

3

Problem 3 (35 points): Write an interactive C program that lets a user play a game of Hangman 1) Your program gets the secret word (7 letters in each word) by calling the given function:

void get_word(char word[]) { char WORD[][8] = { "program", "puzzles", "squeeze", "circuit", "devoted", "journey", "version", "totally", "respect" }; int i, num; num = (rand() % 9); for (i = 0; i < 7; i++) //exclude NULL word[i] = WORD[num][i]; }

Suggestion: During the time that you implement your code, pick one word to test your code and hard-code it in your main program, i.e. char word[7] = "squeeze"; After your code works, call the get_word function to randomly pick a secret word. 2) In your main program, include the followings (so that the rand() function in get_word works correctly)

#include <stdlib.h> // enable use of rand() #include <time.h> // enable use of time()

and srand((int)time(0));

3) Initially the program prints on the screen the number of letters of the word to be guessed. This is in the form of successive stars (see sample code execution on the next page).

The player makes a guess on the letters belonging to the secret word one by one. At each step, the program prints on the screen the letters that have been guessed, and the number of wrong guesses. The program terminates when either

a) all letters have been guessed correctly (the player wins) or b) 7 guesses have been made (the player loses).

Your program must be modular. Create at least TWO meaningful functions that abstract details such as printing the word after a letter guess is attempted or searching for a letter within a word.

Suggestion: Use another array, guessed, to keep track of the solution so far. Initialize all elements of guessed to the '*' symbol. Each time a letter in word is guessed correctly, replace the corresponding '*' in guessed with that letter.

4

Sample code execution for word squeeze Red entered by a user Lab 6 Assignment (30 points) – you will complete this during the week of March 11-15, 2019

Let's play Hangman. The secret word is: ******* Guess a letter: e e was found 3 times in the secret word ***ee*e Guess a letter: t t is not in the secret word, You have 6 tries left. ***ee*e Guess a letter: z z was found 1 times in the secret word ***eeze Guess a letter: a a is not in the secret word, You have 5 tries left. ***eeze Guess a letter: q q was found 1 times in the secret word *q*eeze Guess a letter: s s was found 1 times in the secret word sq*eeze Guess a letter: y y is not in the secret word, You have 4 tries left. sq*eeze Guess a letter: u u was found 1 times in the secret word squeeze Congratulations! You found the secret word: squeeze

Let's play Hangman. The secret word is: ******* Guess a letter: a a is not in the secret word, You have 6 tries left. ******* Guess a letter: t t is not in the secret word, You have 5 tries left. ******* Guess a letter: s s was found 1 times in the secret word s****** Guess a letter: u u was found 1 times in the secret word s*u**** Guess a letter: x x is not in the secret word, You have 4 tries left. s*u**** Guess a letter: o o is not in the secret word, You have 3 tries left. s*u**** Guess a letter: m m is not in the secret word, You have 2 tries left. s*u**** Guess a letter: w w is not in the secret word, You have 1 tries left. s*u**** Guess a letter: k k is not in the secret word, You have 0 tries left. s*u**** Game over! The secret word was: squeeze