Problem overview
Many brands now sell the same product. They live in two parallel vectors: brands holds the names and ratings holds the ratings, so ratings[i] belongs to brands[i]. The program finds the highest-rated brand with a loop and prints it as 1. Brand (rating). If several brands share the top rating, the one listed first wins.
The starter data has ratings 4, 5, 3, 5 and 4. Two brands, MegaCorp and NeoByte, both have 5, and MegaCorp comes first, so the output is:
1. MegaCorp (5)
The previous version compared exactly two brands with if/else. A vector can hold any number of brands, so the comparison moves into a loop.
Approach: keep the position of the best so far
Start by assuming the brand at position 0 is the best. Then look at the remaining positions one by one, and whenever a brand's rating beats the best so far, make that brand the new best.
The variable to keep is the position, bestIndex, not the rating. A position gives you both the name and the rating, because both vectors use the same index. If you stored only the rating, you could print the number but not whose it is.
The comparison is strictly greater. When a later brand ties with the current best, the current best stays, which is the tie rule from the statement: MegaCorp at position 1 is not replaced by NeoByte at position 3, which has the same rating.
Step by step
- Create
bestIndexat 0. For now the first brand is the best. - Write the loop. The
intistarts at 1 and the loop runs whilei < (int)ratings.size(). It starts at 1 because position 0 is already the best so far. The(int)cast makes the size a signed number so that both sides of the comparison areint. - Compare inside the loop. An
ifchecks whetherratings[i]is strictly greater thanratings[bestIndex]. - Update the position. Inside the
if, setbestIndextoi. - Print the winner after the loop, using
brands[bestIndex]andratings[bestIndex]in the1. Brand (rating)format.
The solution in C++
vector<string> brands = {"TechPro", "MegaCorp", "Quantum", "NeoByte", "Zenith"};
vector<int> ratings = {4, 5, 3, 5, 4};
int bestIndex = 0;
for (int i = 1; i < (int)ratings.size(); i++) {
if (ratings[i] > ratings[bestIndex]) {
bestIndex = i;
}
}
cout << "1. " << brands[bestIndex] << " (" << ratings[bestIndex] << ")" << endl;
Details worth a closer look:
- The cast.
ratings.size()is unsigned. Comparing it with the signediproduces a warning, and the cast avoids it. - Tracing the starter data.
bestIndexstarts at 0 with rating 4. Ati = 1, the rating 5 beats 4, sobestIndexbecomes 1. Ati = 2, 3 does not beat 5. Ati = 3, 5 does not beat 5, sobestIndexstays 1. Ati = 4, 4 does not beat 5. The scan ends withbestIndexat 1, which is MegaCorp. - Printing numbers. The
coutchain printsratings[bestIndex]directly, so noto_stringis needed in the final print.
Complexity
The loop looks at each brand once, so the time is O(n) for n brands. Only bestIndex and i are stored besides the input, so the extra space is O(1).
Edge cases and common mistakes
- Ties. With brands A, B, C and ratings 2, 2, 2, nothing is strictly greater, so
bestIndexstays 0 and the output is1. A (2). Using>=would end at the last brand instead. - The best brand last. With ratings 1, 2, 3, the loop updates twice and the output is
1. C (3). - One brand. The loop starts at 1 with a size of 1, so it never runs and the only brand is printed:
1. Solo (3). - An empty vector.
brands[0]would be out of range, which is undefined behaviour. The lesson's data is never empty, but a general version should check first. - Storing the best rating only. The brand's name would be lost.
- Starting the loop at 0. It works, because comparing position 0 with itself is never strictly greater, but it does one pointless comparison.
- Mismatched vectors. The scan uses
ratings.size()as the bound, so ifbrandswere shorter,brands[bestIndex]could read out of range. The program relies on both vectors having the same length.
Testing
| Input | Output |
|---|---|
| the starter data | 1. MegaCorp (5) |
| A, B, C with ratings 2, 2, 2 | 1. A (2) |
| A, B, C with ratings 1, 2, 3 | 1. C (3) |
| Solo with rating 3 | 1. Solo (3) |
