i need problem 5 soluation: This is proplrm 4 with the arry this was my answer #
ID: 3855690 • 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;
}
Explanation / Answer
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int a[50], i, j, n;
printf(" Enter size of an array: ");
scanf("%d",&n);
printf(" Enter %d elements in an array: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf(" Repeated Elements are");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
printf(" %d Found at the indices %d, %d", a[i], i, j);
}
}
}
return 0;
}
OUTPUT
Enter size of array:50
Enter 50 elements in an 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 -1
Duplicate elements are:
3000 Found at the indices 7, 9
2999 Found at the indices 8, 35
986 Found at the indices 12, 32
986 Found at the indices 12, 33
986 Found at the indices 32, 33
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.