ARRAY
ARRAY
1. Write a program that will ask the user to enter a name and then display the letters that
appeared more than once and the number of their occurrence.
Enter a name: Elyse Raina Villanueva
a - 4,e - 3,i - 2,l - 3,n - 2,v - 2
#include <iostream>
#include <unordered_map>
#include <cctype> // For std::tolower
int main() {
std::cout << "Enter a name: ";
std::string name;
std::getline(std::cin, name);
return 0;
}
2. There are 15 candidates for the position of Barangay Captain. Using an array, write a
program that will ask the user to enter the names of the candidate and the total votes he/she
received. After the data entry, display the data in descending order based on the votes of
the candidates. Compute and display also the total votes cast for this election.
#include <iostream>
#include <algorithm> // For std::sort
struct Candidate {
std::string name;
int votes;
};
int main() {
const int numCandidates = 15;
Candidate candidates[numCandidates];
std::cout << "Enter total votes received by " << candidates[i].name << ": ";
std::cin >> candidates[i].votes;
// Compute and display the total votes cast for this election
int totalVotes = 0;
for (int i = 0; i < numCandidates; ++i) {
totalVotes += candidates[i].votes;
}
std::cout << "\nTotal votes cast for this election: " << totalVotes << std::endl;
return 0;
}