SlideShare a Scribd company logo
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

More Related Content

PDF
17 Jo P May 08
PPTX
CPP Homework Help
PPT
python fundamental for beginner course .ppt
DOCX
CS150 Assignment 7 Cryptography Date assigned Monday.docx
PDF
A01
PDF
R-Language-Lab-Manual-lab-1.pdf
PDF
R-Language-Lab-Manual-lab-1.pdf
PDF
R-Language-Lab-Manual-lab-1.pdf
17 Jo P May 08
CPP Homework Help
python fundamental for beginner course .ppt
CS150 Assignment 7 Cryptography Date assigned Monday.docx
A01
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf

Similar to 1 ECE 175 Computer Programming for Engineering Applica.docx (20)

DOCX
Gsp 215 Effective Communication / snaptutorial.com
PDF
Sample Program file class 11.pdf
DOCX
GSP 215 RANK Education Your Life--gsp215rank.com
DOCX
GSP 215 RANK Lessons in Excellence-- gsp215rank.com
DOCX
GSP 215 RANK Inspiring Innovation--gsp215rank.com
PPTX
C++ lecture 01
DOCX
Important C program of Balagurusamy Book
DOCX
Oop lab assignment 01
PPTX
Programming Fundamentals in Python - Sequence Structure
DOCX
Ecs 10 programming assignment 4 loopapalooza
DOCX
GSP 215 RANK Education Counseling -- gsp215rank.com
PDF
C++11 and 64-bit Issues
DOCX
703497334-ICSE-Class-9-Computer-Applications-Sample-Question-Papers.docx
DOCX
Ece175 computer programming for engineering applications homework assignment ...
PDF
Functionssssssssssssssssssssssssssss.pdf
PPTX
Programming python quick intro for schools
PDF
C Programming Interview Questions
PPTX
lecture 2.pptx
PDF
GSP 215 RANK Education Counseling--gsp215rank.com
Gsp 215 Effective Communication / snaptutorial.com
Sample Program file class 11.pdf
GSP 215 RANK Education Your Life--gsp215rank.com
GSP 215 RANK Lessons in Excellence-- gsp215rank.com
GSP 215 RANK Inspiring Innovation--gsp215rank.com
C++ lecture 01
Important C program of Balagurusamy Book
Oop lab assignment 01
Programming Fundamentals in Python - Sequence Structure
Ecs 10 programming assignment 4 loopapalooza
GSP 215 RANK Education Counseling -- gsp215rank.com
C++11 and 64-bit Issues
703497334-ICSE-Class-9-Computer-Applications-Sample-Question-Papers.docx
Ece175 computer programming for engineering applications homework assignment ...
Functionssssssssssssssssssssssssssss.pdf
Programming python quick intro for schools
C Programming Interview Questions
lecture 2.pptx
GSP 215 RANK Education Counseling--gsp215rank.com
Ad

More from oswald1horne84988 (20)

DOCX
1 Network Analysis and Design This assignment is.docx
DOCX
1 Name _____________________________ MTH129 Fall .docx
DOCX
1 Lab 8 -Ballistic Pendulum Since you will be desig.docx
DOCX
1 I Samuel 8-10 Israel Asks for a King 8 When S.docx
DOCX
1 Journal Entry #9 What principle did you select .docx
DOCX
1 HCA 448 Case 2 for 10042018 Recently, a pat.docx
DOCX
1 Chapter 2 Understanding Rhetoric Goal To re.docx
DOCX
1 HC2091 Finance for Business Trimester 2 2.docx
DOCX
1 Cinemark Holdings Inc. Simulated ERM Program .docx
DOCX
1 Figure 1 Picture of Richard Selzer Richard Selz.docx
DOCX
1 Films on Africa 1. A star () next to a film i.docx
DOCX
1 Contemporary Approaches in Management of Risk in .docx
DOCX
1 Assignment front Sheet Qualification Unit n.docx
DOCX
1 BBS300 Empirical Research Methods for Business .docx
DOCX
1 ASSIGNMENT 7 C – MERGING DATA FILES IN STATA Do.docx
DOCX
1 Assessment details for ALL students Assessment item.docx
DOCX
1 CDU APA 6th Referencing Style Guide (Febru.docx
DOCX
1 BIOL 102 Lab 9 Simulated ABO and Rh Blood Typing.docx
DOCX
1 Business Intelligence Case Project Backgro.docx
DOCX
1 Essay #2 Multimodal Analysis In this 4-5 page p.docx
1 Network Analysis and Design This assignment is.docx
1 Name _____________________________ MTH129 Fall .docx
1 Lab 8 -Ballistic Pendulum Since you will be desig.docx
1 I Samuel 8-10 Israel Asks for a King 8 When S.docx
1 Journal Entry #9 What principle did you select .docx
1 HCA 448 Case 2 for 10042018 Recently, a pat.docx
1 Chapter 2 Understanding Rhetoric Goal To re.docx
1 HC2091 Finance for Business Trimester 2 2.docx
1 Cinemark Holdings Inc. Simulated ERM Program .docx
1 Figure 1 Picture of Richard Selzer Richard Selz.docx
1 Films on Africa 1. A star () next to a film i.docx
1 Contemporary Approaches in Management of Risk in .docx
1 Assignment front Sheet Qualification Unit n.docx
1 BBS300 Empirical Research Methods for Business .docx
1 ASSIGNMENT 7 C – MERGING DATA FILES IN STATA Do.docx
1 Assessment details for ALL students Assessment item.docx
1 CDU APA 6th Referencing Style Guide (Febru.docx
1 BIOL 102 Lab 9 Simulated ABO and Rh Blood Typing.docx
1 Business Intelligence Case Project Backgro.docx
1 Essay #2 Multimodal Analysis In this 4-5 page p.docx
Ad

Recently uploaded (20)

PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
RMMM.pdf make it easy to upload and study
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
master seminar digital applications in india
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Insiders guide to clinical Medicine.pdf
PPH.pptx obstetrics and gynecology in nursing
RMMM.pdf make it easy to upload and study
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Renaissance Architecture: A Journey from Faith to Humanism
Module 4: Burden of Disease Tutorial Slides S2 2025
master seminar digital applications in india
Microbial disease of the cardiovascular and lymphatic systems
Supply Chain Operations Speaking Notes -ICLT Program
VCE English Exam - Section C Student Revision Booklet
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Basic Mud Logging Guide for educational purpose
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Abdominal Access Techniques with Prof. Dr. R K Mishra
Insiders guide to clinical Medicine.pdf

1 ECE 175 Computer Programming for Engineering Applica.docx

  • 1. 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
  • 2. 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:
  • 3. 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. 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
  • 5. 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",
  • 6. "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
  • 7. 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.
  • 8. 4 Sample code execution for word squeeze Red entered by a user
  • 9. 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
  • 10. 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.
  • 11. 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