This is a very straightforward implementation and fairly easy to read. Nice work! Here are some ways it could be improved:
Since you asked about performance, let's start there. Looking at your inner loop, it looks like you are going through the entire array for every element of the array. (Actually, I think there's a mistake here. It looks like you intended to write
for (j = i + 1... on the inner loop, but instead wrote
for j = 1 + 1....)
Rather than doing it that way, what if we had the numbers already in order? Then it would be really easy to do, wouldn't it? You could just make 1 pass through the array, counting up the number of occurrences of each number as you go. Once you find the predominant one, you can skip the rest.
Furthermore, if at any point you have fewer than 1/2 + 1 numbers left and you switch to a new number, you automatically know that there is no predominant number. So you never have to traverse the entire array, unless the entire second half of the array contains the same number. So in the vast majority of cases you won't traverse the entire array.
So I would add a call to
qsort() to sort the numbers after reading in the array. Then your remaining code would be significantly simplified, and would probably be significantly faster.
Your variable names could use some help. You're on the right track, but I would go further. Instead of
number which is quite generic (everything's a number to the computer!), why not
j are fine for loop indexes.
Ap stand for? I see
maxAp, etc. But I don't understand what it means. As such, I'd expand it to whatever it stands for.
I would break the program into separate functions with good names that describe what it does. For example, you could have a
readEntries() function that reads in the array, a
findPredominantNumber() function that finds the numbers you're looking for, and a
displayResults() function that prints everything out. It would make the code more readable and easier to understand at a glance.
I would suggest a few stylistic improvements. In general it's best to put one variable declaration per line. It's easier to read and can avoid some common errors, like thinking that you're declaring a pointer when you're not.
Use more whitespace! I see only a single blank line in your function. It would be nice to see some more between the last
for and the final
I would also recommend using brackets around single-line statements in an
for body because this can also reduce the possibility of errors. This one is especially important on code that will be maintained over years.