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

i need problem 5 soluation: This is proplrm 4 with the arry this was my answer #

ID: 3855691 • Letter: I

Question

i need problem 5 soluation:

This is proplrm 4 with the arry

this was my answer

#import <Foundation/Foundation.h>
#include <stdio.h>
int main()
{
    /* initializing array */
    int mainArr[] = {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,-1};
    /* declaring and initializing variables */
    int i = 0, total = 0, min, max, min2, max2, sum = 0;
  
    /* assigning max and min variables with first array location */
    max = min = max2 = min2 = mainArr[0];
  
    printf(" Given Array is: ");
    for(i = 0; mainArr[i] != -1; i++){
        printf(" %d", mainArr[i]);
        total++;
        sum += mainArr[i];
      
        /* getting max from array */
        if(mainArr[i] > max){
            max2 = max;
            max = mainArr[i];
        }
        /* getting second max */
        else if(mainArr[i] > max2 && mainArr[i] != max){
            max2 = mainArr[i];
        }
      
        /* getting min from array */
        if(mainArr[i] < min){
            min2 = min;
            min = mainArr[i];
        }
        /* getting second min */
        else if(mainArr[i] < min2 && mainArr[i] != min){
            min2 = mainArr[i];
        }
    }
    printf(" Total number:   %20d", total);
    printf(" Average:    %20.1f", (float)(sum/total));
    printf(" Min element: %20d", min);
    printf(" Min2 element: %20d", min2);
    printf(" Max element: %20d", max);
    printf(" Max2 element: %20d", max2);
    printf(" ");
  
    return 0;
}

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:,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 (n 1) 7 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

#include <stdio.h>
void main()
{
/* initializing array */
int mainArr[] = {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,-1};
/* declaring and initializing variables */
int j,i = 0, total = 0, min, max, min2, max2, sum = 0;
int s=sizeof(mainArr);
int pos[s];
int dup[s];
int x=0,n=0;
int k,y,count=0;

/* assigning max and min variables with first array location */
max = min = max2 = min2 = mainArr[0];
  
printf(" Given Array is: ");
for(i = 0; mainArr[i] != -1; i++){
printf(" %d", mainArr[i]);
total++;
sum += mainArr[i];
  
/* getting max from array */
if(mainArr[i] > max){
max2 = max;
max = mainArr[i];
}
/* getting second max */
else if(mainArr[i] > max2 && mainArr[i] != max){
max2 = mainArr[i];
}
  
/* getting min from array */
if(mainArr[i] < min){
min2 = min;
min = mainArr[i];
}
/* getting second min */
else if(mainArr[i] < min2 && mainArr[i] != min){
min2 = mainArr[i];
}
}
printf(" Total number: %20d", total);
printf(" Average: %20.1f", (float)(sum/total));
printf(" Min element: %20d", min);
printf(" Min2 element: %20d", min2);
printf(" Max element: %20d", max);
printf(" Max2 element: %20d", max2);
printf(" ");

//Count number of duplicates
for(i = 0; mainArr[i] != -1; i++)
{
count=0;
x++;
for(k=0;k<s;k++)
{
if(dup[k]==mainArr[i]) // condition so that same number is not checked again
{
goto loop; //jump to label loop
}
}
pos[count]=i;
for(j=i+1;mainArr[j]!=-1;j++)
{


if(mainArr[i]==mainArr[j])
{
dup[x]=mainArr[i]; // when duplicate is found that number is stored in dup array
++count; // count is incremented by 1
pos[count]=j; // Index of that duplicate is stored in the array
}

}
y=count+1;
n+=(y*(y-1))/2; // calculate total number of duplicates

// To print the output
loop:
if(count>0)
{
  
printf("Value %d was found at indices:",mainArr[i]);
for(k=0;k<=count;k++)
{
if(k!=count)
{
printf("%d,",pos[k]);
}
else{
printf("%d. ",pos[k]);
}}
}

}
printf("Total duplicates found %d",n); // Count number of duplicates
}

Explanation of the program:

In the program I have just add extra code to program 4 code.

In this program I have used two loops of i and j to find duplicate values. If a duplicate number is found then I have stored its index in an array called pos. Each time a number duplicate is found I have increemented the counter. Also once the duplicate is found I have stored that value in dup array so that that value is not checked again.

To find Toatal number of duplicates I have used the formula (n*(n-1))/2 and print at the end of the program.