Please write this in C and add comments so I can follow alon

Please write this in C, and add comments so I can follow along!

The text file looks like this

Program #2 Finding Duplicates For your second program, please take the above array and determine how many duplicate values there are in the array and what their index positions are. You get full credit if your output looks like this: Duplicate of 1899 found at index 1, was also found at index 9 Duplicate of 1059, found at index 20, was also found at index 224 Duplicate of 1059 found at index 20, was also found at index 475 Duplicate of 2535 index 415, was found at index 488 Total Duplicates found: XX This report indicates that the value 1899, which was found at index 1, was also found at index 9 The value 1059, which was found at index 20, was also found at indexes 224 and 475. (Ifyou look at the array values, you will see that these results are true.) Bonus You can get an extra 5 bonus points on this problem if your output looks like: value 1899 was found at indices Value 1059 was found at indices 20, 224, 475. Value 2535 was found at indices 415 488 Total Duplicates found XX page: 3 CS1325-Introduction to Programming The difference is that all of the duplicates for a given value are listed on one line, instead of listing them on separate lines. If you use this format, your program should not have more than one entry for the same number 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 my n K (n 1) duplicates for that number. Thus, if a number appears twice in the array, that counts as l duplicate. If a number appears 3 times, that counts as 3 duplicates. And ifa number appears 4 times, that counts as (4 3V2 36 duplicates

Solution

#include <stdio.h>
#include <stdlib.h>

int main()
{

    FILE *myFile; //declare a pointer of type file
    int arr[1000];//since dont know the exact number of integers in the file i am assuming it to be less than 1000
    int i,j,k,key,count,sum=0,m;
    myFile = fopen(\"numbers.txt\", \"r\");//myFile points to numbers.txt which is the input text file and it is opened in read mode

    if (myFile == NULL)//check whether the file is empty
    {
        printf(\"File is Empty\ \");
        exit (0);
    }
    for (i = 0; arr[i]!=-1; i++)   //read the elements of file into array arr.
    {
        fscanf(myFile, \"%d, \", &arr[i] );//here \"%d, \" is used as the input format is \"number, number\"

    }
    

    fclose(myFile);//close the file

       for(i=0,j=0;arr[i]!=-1;i++)
       {
          

       count=1;
       System.out.print(arr[i]+\"found at index : \");
       for(key=0;arr[key]!=-1;key++)//loop to search whether the particular number is present in array
       {
       if(arr[key]==arr[i])//if the number is found
       {
           if(key>i)//this eliminates the counting a same element twice
       count++;//increment count of that number
       System.out.print(key+\",\");
       System.out.print(\" \");
       }
       }
       if(count>1)//to count duplicates
       {
       sum=sum+(count*(count-1))/2;//sum indicates total duplicates in the array
       }System.out.println();
       }
       System.out.print(\"total number of duplicates are: \"+sum);
  

    return 0;
}

Please write this in C, and add comments so I can follow along! The text file looks like this Program #2 Finding Duplicates For your second program, please take
Please write this in C, and add comments so I can follow along! The text file looks like this Program #2 Finding Duplicates For your second program, please take

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site