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

This must be done in C. Functions cannot be used. The hard part for me is when t

ID: 3854763 • Letter: T

Question

This must be done in C. Functions cannot be used. The hard part for me is when there are 3 elements with the same value. I can't get it to look like what is asked. Example: 986 appears 3 times in the array and it needs to look like "986 was found at indices: 12, 32, 33." There must be a period at the end of the last indices. The program must also be able to work for more than 3 indices and always have commas separating with a period at the end

Here are the numbers given for the array to test:

Introduction to Programming CS1325 Assignment #5 – Arrays and Loops Program #5: Finding Duplicates Please take the array for Program #4 and determine how many duplicate values there are in the array and what their index positions are. The output should look like:

Value 1899 was found at indices: 1, 9.

Value 1059 was found at indices: 20, 224, 475.

Value 1607 was found at indices: 25, 120.

.......

Value 2535 was found at indices: 415, 488.

Total Duplicates found: XX

All of the indices for a given value are listed on one line. Your program should not have more than one entry for the same number. Only numbers that have at least one duplicate should be reported. Note that the meaning of a duplicate is “two numbers at different index positions that have the same value.” This means that if a number appears n times in the array, there are a total of (n choose 2) = (n (n 1))/ 2 duplicates for that number. Thus, if a number appears twice in the array, that contributes 1 duplicate to the total duplicate count. If a number appears 3 times, that contributes 3 duplicates. And if a number appears 4 times, that contributes (4*3)/2 = 6 duplicates to the total duplicate count.

Explanation / Answer

NOTE: Please check and let me know if you face any issues, i will revert back within 24 hours.

Code:

#include <stdio.h>

int main()
{
   /* initializing array and variables */
   int array[] = {120,80,72,143,123,124,125,3000,2999,3000,82,876,986,345,1990,2367,98,2,444,993,635,283,544,923,18,543,777,234,549,864,39,97,986,986,1,2999,473,776,9,23,397,15,822,1927,1438,1937,1956,7,29,123, 123, 123, 123, 123, -1};
   int i, j, k, l=0;
   int flag;
   int dup[100];
   int checked[50];
   int num_dup = 0;

   /* iterating through array elements */
   for(i = 0; array[i] != -1; i++){

       /* checking if the array element is already seen */
       for(flag = 0, j = 0; j < l; j++)
           if(array[i] == checked[j]) flag = 1;
       if(flag) continue;

       /* counting number of duplicates if element occurs more than once in the array */
       dup[0] = i;
       for(k = 1, j = 0; array[j] != -1; j++)
           if(array[i] == array[j] && i != j)
               dup[k++] = j;
       /* if the value of k is >= 2 that means element has repeated atleast twice */
       if(k >= 2){
           /* storing the array element duplicated in another array to check if the element is already seen */
           checked[l++] = array[i];
           printf(" Value %d was found at indices: ", array[i]);
           for(j = 0; j < k; j++)
               printf("%d, ", dup[j]);
           printf(".");
           num_dup += k - 1;
       }
   }
   printf(" Total Duplicates found: %d ", num_dup);

   return 0;
}

Execution and output:

Program output can also be found at https://pasteboard.co/GzRo3rV.png

Unix Terminal> ./a.out

Value 123 was found at indices: 4, 49, 50, 51, 52, 53.
Value 3000 was found at indices: 7, 9.
Value 2999 was found at indices: 8, 35.
Value 986 was found at indices: 12, 32, 33.
Total Duplicates found: 9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote