Overview
This section addresses fundamental array manipulation problems, covering tasks such as compuitng score statistics, identifying peak elements, filtering genetic sequences, determining family members exceeding average height, and analyzing exam score distributions.
Problem 1: Basic Score Statistics
Description: After an examination, a teacher wants to detemrine how many students scored at least 90 points. Write a program to calculate the number of high-performing students.
Input Format:
- First line: integer
nrepresenting the number of students (n ≤ 100) - Second line:
nspace-separated integers representing scores
Output Format:
- Single integer indicating the count of students with scores ≥ 90
Example Input:
5
98 88 85 99 90
Example Output:
3
Implementation:
#include <iostream>
using namespace std;
int main() {
int count = 0;
int studentCount;
cin >> studentCount;
for (int i = 0; i < studentCount; ++i) {
int score;
cin >> score;
if (score >= 90) {
count++;
}
}
cout << count << endl;
return 0;
}
Problem 2: Finding Peak Elements
Description: Identify numbers in a sequence that are greater than both their immediate neighbors. These elements are called "peaks".
Input Format:
- First line: integer
mrepresenting the number of elements (3 ≤ m ≤ 100) - Second line:
mspace-separated integers
Output Format:
- Each peak element on a separate line
Example Input:
14
1 3 2 4 1 5 3 9 7 10 8 23 85 43
Example Output:
3
4
5
9
10
85
Implementation:
#include <iostream>
using namespace std;
int main() {
int size;
cin >> size;
int numbers[100];
for (int i = 0; i < size; ++i) {
cin >> numbers[i];
}
for (int i = 1; i < size - 1; ++i) {
if (numbers[i] > numbers[i - 1] && numbers[i] > numbers[i + 1]) {
cout << numbers[i] << endl;
}
}
return 0;
}
Problem 3: Filtering Alien Genes
Description: Remove alien genes from a DNA sequence where the square of each gene index modulo 7 equals 1.
Input Format:
- First line: integer
n(3 ≤ n ≤ 200) representing the number of genes - Second line:
nspace-separated integers representing gene values
Output Format:
- Space-separated integers representing non-alien genes
Example Input:
4
6 2 8 12
Example Output:
2 12
Implementation:
#include <iostream>
using namespace std;
int main() {
int geneCount;
cin >> geneCount;
for (int i = 0; i < geneCount; ++i) {
int geneValue;
cin >> geneValue;
if ((i * i) % 7 != 1) {
cout << geneValue << " ";
}
}
return 0;
}
Problem 4: Family Height Comparison
Description: Determine which family members have heights above the average family height.
Input Format:
- First line: integer
n(1 < n < 11) representing family members - Second line:
nspace-separated integers representing heights
Output Format:
- First line: average height formatted to one decimal place
- Second line: member indices and heights of those above average
Example Input:
7
175 160 172 158 178 162 142
Example Output:
AVE=163.9
1:175 3:172 5:178
Implemantation:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int familySize;
cin >> familySize;
int heights[100];
double totalHeight = 0;
for (int i = 0; i < familySize; ++i) {
cin >> heights[i];
totalHeight += heights[i];
}
double average = totalHeight / familySize;
cout << fixed << setprecision(1) << "AVE=" << average << endl;
for (int i = 0; i < familySize; ++i) {
if (heights[i] > average) {
cout << (i + 1) << ":" << heights[i] << " ";
}
}
return 0;
}
Problem 5: Exam Score Distribution
Description: Calculate the average score and count students who scored above and below the average.
Input Format:
- First line: integer
n(n ≤ 100) representing the number of students - Second line:
nspace-separated integers representing scores
Output Format:
- Average score (one decimal), count of students scoring ≥ average, count of students scoring < average
Example Input:
5
100 98 97 99 90
Example Output:
96.8 4 1
Implementation:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int studentCount;
cin >> studentCount;
int scores[1000];
double sum = 0;
int aboveAverage = 0;
for (int i = 0; i < studentCount; ++i) {
cin >> scores[i];
sum += scores[i];
}
double average = sum / studentCount;
for (int i = 0; i < studentCount; ++i) {
if (scores[i] > average) {
aboveAverage++;
}
}
cout << fixed << setprecision(1) << average << " " << aboveAverage << " " << (studentCount - aboveAverage);
return 0;
}