Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write code in C. #include <stdio.h> Thanks in advance! Problem 1 (30 poin

ID: 3591637 • Letter: P

Question

Please write code in C. #include <stdio.h>

Thanks in advance!

Problem 1 (30 points): IMEI (International Mobile Equipment Identifier) IMEI is a unique number of 15 digits given to every mobile phone resource: https://en.wikipedia.org/wiki/International_Mobile_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 b) the double of the digits in the even positions. 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 l+6 = 7 (and 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 34 (2x5) +7 + (2x6) + 8 + (2x3) + 0 + (2x3) + 6 + (2x2) + 5+ (2x7) + 3t(2x7) 3+(10) +7(12) +8+(6) +0+(6) +6+(4) +5+ (14) +3+ (14) = 62 Then the check digit = 10-2-8. Since the check digit (-8) is equal to the Luhn digit (-8), this IMEI is valid.

Explanation / Answer

#include<stdio.h>

void print_IMEI(int IMEI[]) {
   int i;
   printf("IMEI:");
   for (i = 0; i < 15; i++)
       printf("%d", IMEI[i]);
   printf(" ");
}


void calc_sum(int IMEI[], int *sum_14digits) {
   int tmpSum = 0, i;
   *sum_14digits = 0;
   for (i = 0; i < 15; i++)
       (*sum_14digits) += IMEI[i];  
}

int main() {
   FILE *fp = fopen("list_IMEI.txt", "r");
   char line[20];
   int IMEI[15];
   int i, sum_14digits, sum;

   while (fgets(line, 20, fp)) {
       for (i = 0; i < 15; i++)
           IMEI[i] = line[i] - '0';
      
       calc_sum(IMEI, &sum_14digits);
       sum = 0;
       while (sum_14digits > 0) {
           sum += sum_14digits % 10;
       }
       print_IMEI(IMEI);
       printf("Total sum of 14 digits = %d ", sum_14digits);
       printf("Lugn digit = %d ", sum);
       printf("Check digit = %d ", IMEI[14]);
       if (sum == IMEI[14])
           printf("IMEI is valid ");
       else
           printf("IMEI is invalid ");
   }
   fclose(fp);
}