ECE175 Computer Programming for Engineering Applications

Homework Assignment 6

Due Date: Tuesday March 8, 2016 11:59 PM, via D2L

Conventions: Name your C programs as hwxpy.c

where x corresponds to the homework number and y corresponds to the problem number.

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

– Author:

– Date created:

– Brief (two lines) description of the program:

Submission Instructions: Submit your .c files via D2L Dropbox.

Problem 1 (35 pts): IMEI (International Mobile Equipment Identifier)

Every mobile phone operating in wireless networks is characterized by a unique

identifier of 15 digits, called IMEI

(https://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity)

A method to check if the device is really made by the official manufacturer is to compare

the IMEI’s last digit, called Luhn digit, with a check digit. If the Luhn digit is equal to the

check digit, the device is most probably authentic. Otherwise, it is not authentic for sure.

The check digit is calculated as follows:

1) First, we calculate the SUM of the first IMEI’s 14 digits by adding

a) the digits in the odd positions (meaning even index for the array in C)

b) the double of the digits in the even positions. But if the double of the digit is a

two-digit number, we add each digit separately. For example, suppose that the value

of the digit is 8, its double is 16. We therefore add to the SUM the result of 1+6 = 7

(not 16).

2) If the last digit of the calculated SUM is 0, that is the check digit. If not, we subtract the

last digit of the calculated SUM from 10 and that is the check digit.

For example: let’s check the IMEI 357683036257378.

Note: the last digit of the given IMEI above is 8 which is the Luhn digit.

Apply the above algorithm (by hand) to the first 14 digits, we get

3 + (2×5) +7 + (2×6) + 8 + (2×3) + 0 + (2×3) + 6 + (2×2) + 5+ (2×7) + 3+ (2×7)

3 + (10) +7 + (12) + 8 + (6) + 0 + (6) + 6 + (4) + 5+ (14) + 3+ (14)

3 + (1+0) +7 + (1+2) + 8 + (6) + 0 + (6) + 6 + (4) + 5+ (1+4) + 3+ (1+4)

3 + (1) +7 + (3) + 8 + (6) + 0 + (6) + 6 + (4) + 5+ (5) + 3+ (5)

= 62

Then the check digit = 10 – 2 = 8.

Since the check digit (=8) is equal to the Luhn digit (=8), this IMEI is valid.

1

Write a C program that

– Prompt the user to enter the 15 digits of IMEI separated by spaces. The program

should store the digits in an integer array.

– Display the sum of the first 14 digits using the algorithm on the previous page

– Calculate and display the check digit, and

– Compare it to the Luhn digit. If they match, output with the message “IMEI is valid.”

If not, output with the message “IMEI is invalid”.

Sample Code Execution: Digits are entered by a user

Enter 15-digit barcode (separate each digit by space)

357683036257378

total sum of 14 digits = 62

check digit = 8

IMEI is valid

Enter 15-digit barcode (separate each digit by space)

357613004448485

total sum of 14 digits = 55

check digit = 5

IMEI is valid

Enter 15-digit barcode (separate each digit by space)

490154203237519

total sum of 14 digits = 52

check digit = 8

IMEI is invalid

Enter 15-digit barcode (separate each digit by space)

012646000827762

total sum of 14 digits = 38

check digit = 2

IMEI is valid

Enter 15-digit barcode (separate each digit by space)

352255010132837

total sum of 14 digits = 41

check digit = 9

IMEI is invalid

2

Problem 2 (35 pts): A two-dimensional array of size mn, X[m][n], can be used to represent

a terrain of land where each element is elevation information (i.e. height in feet). For this

problem, we want to determine number and location of peaks in the terrain, where a peak is

a point that has lower elevations all around. We also want to determine the highest peak and

its location.

How to decide whether a location (i, j) is a peak:

For 2-D array below, each element, except those on the boundaries (edges), has

exactly 8 neighboring cells (e.g. Above, Below, Left, Right, and four Diagonals).

Diagonal

#1 11al1)

Left

Diagonal

#3

Above

(i, j)

Below

Diagonal

#2 Right

Diagonal

#4

1) We decide that the value at (i, j) is a peak if the value at (i, j) is higher than the

values at the “Above”, “Below”, “Left” and “Right” locations of (i, j). In other

words, if the values of all 4 neighboring points are less than the value at (i,j),

then location (i,j) is considered a peak.

2) The points on the edge cannot be a peak since we do not have elevation

information on all 4 sides. In other words, you can ignore the elements at the

boundary (edge) of the array. That is, start your element procession from row 1:

elements (1,1) up to (1, n-2), continue until row m-2: elements (m-2, 1) to cell (m2, n-2).

Hint: in your program, “Above”, “Below”, “Left” and “Right” locations of (i, j) can be

written in terms of i and j. For example, “Above” location of (i, j) is location (i-1, j)

Write a C program that

a) Read the terrain data from Prob2.txt (you will create this text file).

See sample code executions on the next page.

b) Determine and print all the peak values and the location of those peaks. Your program

should examine every location (i, j) starting from i=1 and j=1 and decide whether that

location is a peak using the criteria above.

c) Determine and print the highest peak and its location.

Note: (nested) loops MUST be used to read from the text file in order to create 2-D array

used in your program and the loops should be used to access elements of 2-D array for

data processing. Otherwise, -20 pts.

3

Sample code execution #1:

If the terrain data in Prob2.txt (again, you are to create this text file for your program) is

5039 5127 5238 5259 5248 5310 5299

5150 5392 5410 5501 5320 5820 5321

5290 5560 5490 5421 5530 5831 5210

5110 5429 5430 5411 5459 5630 5319

4920 5129 4921 5821 4722 4921 5129

5023 5129 4822 4872 4794 4862 4245

Display the following on the output screen:

Terrain data

5039 5127 5238 5259 5248 5310 5299

5150 5392 5410 5501 5320 5820 5321

5290 5560 5490 5421 5530 5831 5210

5110 5429 5430 5411 5459 5630 5319

4920 5129 4921 5821 4722 4921 5129

5023 5129 4822 4872 4794 4862 4245

Explanation,

1) Consider element[1][1] (=5392). Its 4

neighbors are 5127, 5150, 5560, 5410.

Since 5392 is not the maximum (among

the 4 neighbors), this point is not a peak.

Peak 5501 at row 1 col 3

Peak 5560 at row 2 col 1

Peak 5831 at row 2 col 5

Peak 5821 at row 4 col 3

The maximum peak is 5831 at row 2 col 5

2) Consider element[1][3] (=5501). Its 4

neighbors are 5259, 5410, 5421, 5320.

Since 5501 is the maximum (among the 4

neighbors), this point is a peak.

Sample code execution #2:

If the terrain data in Prob2.txt (you are to create this text file for your program) is

5039 5127 5238 5259 5248 5310 5299

5150 5392 5410 5401 5320 5750 5321

5290 5560 5990 5421 5530 5731 5210

5110 5429 5430 5411 5459 5830 5319

4920 5829 4921 5321 4722 5921 5129

5023 5129 4822 4872 4794 4862 4245

Display the following on the output screen:

Terrain data

5039 5127 5238 5259 5248 5310 5299

5150 5392 5410 5401 5320 5750 5321

5290 5560 5990 5421 5530 5731 5210

5110 5429 5430 5611 5459 5830 5319

4920 5829 4921 5321 4722 5921 5129

5023 5129 4822 4872 4794 4862 4245

Peak 5750 at row 1 col 5

Peak 5990 at row 2 col 2

Peak 5611 at row 3 col 3

Peak 5829 at row 4 col 1

Peak 5921 at row 4 col 5

The maximum peak is 5990 at row 2 col 2

4