0% found this document useful (0 votes)
27 views3 pages

ARRAY

The document describes arrays used to store different data types: letter grades, sales commissions, daily temperatures, region populations, and student names. It then provides code snippets to: 1) Count the number of A grades in the letter grades array 2) Calculate the average commission in the commissions array 3) Find the highest and lowest temperatures in the temperatures array 4) Display regions with populations over 1 million 5) Sort and display student names alphabetically

Uploaded by

jerwinbolivar02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

ARRAY

The document describes arrays used to store different data types: letter grades, sales commissions, daily temperatures, region populations, and student names. It then provides code snippets to: 1) Count the number of A grades in the letter grades array 2) Calculate the average commission in the commissions array 3) Find the highest and lowest temperatures in the temperatures array 4) Display regions with populations over 1 million 5) Sort and display student names alphabetically

Uploaded by

jerwinbolivar02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

A.

Write an array declaration that will store the following data:


1. 50 letter grades:
char letterGrades[50];
2. Commissions of ten salesmen:
double commissions[10];
3. Daily temperature in Celsius for the month of August:
float dailyTemperaturesAugust[31]; // Assuming 31 days in August
4. Number of population of each region in the Philippines:
int populationByRegion[17]; // Assuming there are 17 regions in the Philippines
5. Names of 50 students:
std::string studentNames[50];

B. Write a program segment for the following:

1. Count how many A letter grades in array # A1:


int countA = 0;
for (int i = 0; i < 50; ++i) {
if (letterGrades[i] == 'A') {
countA++;
}
}
std::cout << "Number of A grades: " << countA << std::endl;
2. Get the average commission in array # A2:
double totalCommission = 0;
for (int i = 0; i < 10; ++i) {
totalCommission += commissions[i];
}
double averageCommission = totalCommission / 10;
std::cout << "Average commission: " << averageCommission << std::endl;
3. Display the day with the highest and lowest temperature in array # A3:
int maxTempDay = 0, minTempDay = 0;
for (int i = 1; i < 31; ++i) {
if (dailyTemperaturesAugust[i] > dailyTemperaturesAugust[maxTempDay]) {
maxTempDay = i;
}
if (dailyTemperaturesAugust[i] < dailyTemperaturesAugust[minTempDay]) {
minTempDay = i;
}
}
std::cout << "Day with highest temperature: " << maxTempDay + 1 << std::endl; // Adding 1
as days are usually counted from 1
std::cout << "Day with lowest temperature: " << minTempDay + 1 << std::endl;
4. Display the name of the region with a population more than 1 million:
for (int i = 0; i < 17; ++i) {
if (populationByRegion[i] > 1000000) {
std::cout << "Region with population more than 1 million: " << "Region " << i + 1 <<
std::endl; // Adding 1 as regions are usually counted from 1
}
}
5. Display the names of students in alphabetical order:
// Assuming you have the <algorithm> header included for std::sort
std::sort(std::begin(studentNames), std::end(studentNames));
// Display the sorted names
for (int i = 0; i < 50; ++i) {
std::cout << studentNames[i] << std::endl;
}

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);

// Convert the name to lowercase for case-insensitive comparison


for (char &c : name) {
c = std::tolower(c);
}

std::unordered_map<char, int> charCount;

// Count occurrences of each character


for (char c : name) {
if (std::isalpha(c)) { // Consider only alphabetic characters
charCount[c]++;
}
}

// Display characters that appeared more than once


std::cout << "Letters that appeared more than once and their occurrences:" << std::endl;
for (const auto &pair : charCount) {
if (pair.second > 1) {
std::cout << pair.first << " - " << pair.second << std::endl;
}
}

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;
};

bool compareCandidates(const Candidate& a, const Candidate& b) {


return a.votes > b.votes; // Compare candidates based on votes in descending order
}

int main() {
const int numCandidates = 15;
Candidate candidates[numCandidates];

// Input candidate names and votes


for (int i = 0; i < numCandidates; ++i) {
std::cout << "Enter name of candidate " << i + 1 << ": ";
std::getline(std::cin, candidates[i].name);

std::cout << "Enter total votes received by " << candidates[i].name << ": ";
std::cin >> candidates[i].votes;

// Consume the newline character left in the input buffer


std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

// Sort candidates in descending order based on votes


std::sort(candidates, candidates + numCandidates, compareCandidates);

// Display the data in descending order based on votes


std::cout << "\nCandidates in descending order based on votes:\n";
for (int i = 0; i < numCandidates; ++i) {
std::cout << "Name: " << candidates[i].name << ", Votes: " << candidates[i].votes <<
std::endl;
}

// 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;
}

You might also like