Reputation: 61
I'm don't know how to find duplicates in an array. After finding the duplicates,
e.g. input: 3 5 7 7 7 7 7 12 12 12 18 20
/* program calculating n * n * n * y = score
7 appears 5 times = 5*5*5*7 = 875
12 appears 3 times = 3*3*3*12 = 324 */
output: 7 is the highest scoring duplicate at 875.
I am restricted to using only arrays, if/else, printf/scanf, loops..
My code so far (only works for some inputs):
#include <stdio.h>
Upvotes: 0
Views: 116
Reputation: 41
//Provisional value int hi_score = INT_MIN;//#include
int selectIndex = -1;
for(i = 0; i < nNumbers; i++) {
int dupCount = 0;//
if(selectIndex != -1 && array[selectIndex] == array[i])
continue;//skip because It has already been calculated
for(j = 0; j < nNumbers; j++){//Examine all because unsorted
if((array[i] == array[j]))
++dupCount;
}
if(dupCount > 1) {
// need ?
int score = dupCount * dupCount * dupCount * array[i];
if(hi_score <= score)
{
hi_score = score;
selectIndex = i;
}
}
} if(selectIndex != -1)
printf("%d\n", array[selectIndex]);
Upvotes: 0
Reputation: 40145
like this:
//Provisional value
int hi_score = INT_MIN;//#include <limits.h>
int selectIndex = -1;
for(i = 0; i < nNumbers; i++){
int dupCount = 0;
if(selectIndex != -1 && array[selectIndex] == array[i])
continue;//skip because It has already been calculated
for(j = 0; j < nNumbers; j++){//Examine all because unsorted
if((array[i] == array[j]))
++dupCount;
}
if(dupCount > 1){// need ?
int score = dupCount * dupCount * dupCount * array[i];
if(hi_score <= score){
hi_score = score;
selectIndex = i;
}
}
}
if(selectIndex != -1)
printf("%d\n", array[selectIndex]);
Upvotes: 2
Reputation:
One way is a brute force algorithm:
#include <stdio.h>
#include <limits.h>
#define MAX_NUMBERS 15
int calc_score(int number, int times)
{
return times*times*times*number;
}
int main(void) {
int a[MAX_NUMBERS] = { 3, 5, 7, 7, 7, 7, 7, 12, 12, 12, 18, 20,13,13,14 };
int n;
int score;
int scoreMax = INT_MIN;
for (int i = 0; i < MAX_NUMBERS; i++)
{
int times = 0;
for (int j = 0; j < MAX_NUMBERS; j++) { if (a[i] == a[j])times++; }
score = calc_score(a[i], times);
if (score > scoreMax) {
scoreMax = score;
n = a[i];
}
}
printf("%d is the highest scoring duplicate at %d\n", n, scoreMax);
//7 is the highest scoring duplicate at 875
}
Upvotes: 0