0% found this document useful (0 votes)
72 views5 pages

C++ Programming Challenges Solutions

The document describes 5 programming challenges involving the use of arrays: 1. A program that accepts test scores, stores them in an array, and reports the number of perfect scores. 2. A program that converts decimal numbers 1-20 to Roman numerals by using an array of string values. 3. A program that tracks salsa sales over 5 types using parallel arrays of strings for names and integers for amounts sold. 4. A program that stores the amount of food eaten each day by 3 monkeys in a 2D array and reports feeding statistics. 5. A program that stores weather conditions over a 3 month period in a 3D array and reports monthly and total rainfall statistics.

Uploaded by

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

C++ Programming Challenges Solutions

The document describes 5 programming challenges involving the use of arrays: 1. A program that accepts test scores, stores them in an array, and reports the number of perfect scores. 2. A program that converts decimal numbers 1-20 to Roman numerals by using an array of string values. 3. A program that tracks salsa sales over 5 types using parallel arrays of strings for names and integers for amounts sold. 4. A program that stores the amount of food eaten each day by 3 monkeys in a 2D array and reports feeding statistics. 5. A program that stores weather conditions over a 3 month period in a 3D array and reports monthly and total rainfall statistics.

Uploaded by

Ammar Ahmed Khan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Chapter 8 Programming Challenges

1. Perfect Scores

Write a modular program that accepts up to 5 integer test scores in the range of 0 to

100 from the user and stores them in an array. Then main should report how many perfect scores were
entered (i.e., scores of 100), using a value-returning countPerfect function to help it.
#include <iostream>
using namespace std;
void getScore(int t[]);
void displayArray(int t[]);
int countPerfect(int t[]);
int main()
{
int test[5];
getScore(test);
displayArray(test);
cout << "There are " << countPerfect(test) << " perfect scores.\n";

system("pause");
return 0;
}
void getScore(int t[])
{
int testScore;
int counter = 0;
do
{
cout << "Enter a score between 0 to 100.\n";
cin >> testScore;
if (testScore >= 0 && testScore <= 100)
{
t[counter] = testScore;
counter++;
}
else
cout << "Invalid test score entered.\n";
} while (counter < 5);

}
void displayArray(int t[])
{
for (int i = 0; i < 5; i++)
{
cout << "test[" << i << "] = " << t[i] << endl;
}
}
int countPerfect(int t[])
{
int perfectScore = 0;
for (int i = 0; i < 5; i++)
{
if (t[i] == 100)
perfectScore++;
}
return perfectScore;
}

2. Roman Numeral Converter

Write a program that displays the roman numeral equivalent of any decimal number between 1 and 20
that the user enters. The roman numerals should be stored in an array of strings and the decimal
number that the user enters should be used to locate the array element holding the roman numeral
equivalent. The program should have a loop that allows the user to continue entering numbers until an
end sentinel of 0 is entered.

Pre- test while loop – to allow user to enter numbers until user enters a 0 to stop the program.

Pre- test while loop – to control user to enter a number from 1 to 20.
#include <iostream>
#include <string>
using namespace std;
int getInput();
void display(int dN);
const string ROMAN_NUM[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX",
"X"};
int main()
{
int decNum;
cout << "This program converts decimal numbers to a roman numeral.\n";
decNum = getInput();//First read.
display(decNum);
system("pause");
return 0;
}
int getInput()
{
int num;
cout << "Enter a number between 1 to 10.\n";
cin >> num;
while (num < 0 || num > 10)//Out of range.
{
cout << "Invalid number. Enter a number between 1 to 10.\n";
cin >> num;
}
return num;
}
void display(int dN)
{
while (dN != 0)
{
cout << "The roman numeral of " << dN << " is " << ROMAN_NUM[dN] << "." <<
endl;
dN = getInput();//First Read.
}
}

3. Chips and Salsa


Write a program that lets a maker of chips and salsa keep track of their sales for five different types of
salsa they produce: mild, medium, sweet, hot, and zesty. It should use two parallel five-element arrays:
an array of strings that holds the five salsa names and an array of integers that holds the number of jars
sold during the past month for each salsa type. The salsa names should be stored using an initialization
list at the time the name array is created. The program should prompt the user to enter the number of
jars sold for each type. Once this sales data has been entered, the program should produce a report that
displays sales for each salsa type, total sales, and the names of the highest selling and lowest selling
products.

String name[5] = {“mild”, “medium”, “sweet”, “hot”, zesty”};

For loop to ask user to enter how any jars sold for each type of salsa.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string name[5] = {"mild", "medium", "sweet", "hot", "zesty"};
int sales[5];
for (int i = 0; i < 5; i++)
{
cout << "Jars sold for " << name[i] << "." << endl;
cin >> sales[i];
while (sales[i] < 0)
{
cout << "Invalid entry, number of jars sold must be greater than
0.\n";
cin >> sales[i];
}
}
cout << "\n\n The Sales Report\n\n";
cout << " Name Jars Sold\n";
cout << "__________________________";
for (int i = 0; i < 5; i++)
{
cout << endl << " " << left << setw(14) << name[i] << sales[i] << endl;
}
system("pause");
return 0;
}

4. Monkey Business

A local zoo wants to keep track of how many pounds of food each of its three monkeys eats each day
during a typical week. Write a program that stores this information in a two-dimensional 3 × 7 array,
where each row represents a different monkey and each column represents a different day of the week.
The program should first have the user input the data for each monkey. Then it should create a report
that includes the following information:

• Average amount of food eaten per day by the whole family of monkeys.

• The least amount of food eaten during the week by any one monkey.
• The greatest amount of food eaten during the week by any one monkey.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string monkeyFood[3][3] = {{ "Banana", "Honeydew", "Lychee"},
{"Grapes", "Orange", "Pear"},
{"Mango", "Apple", "Pineapple"}};
for (int monkey = 0; monkey < 3; monkey++)
{
for (int day = 0; day < 3; day++)
{
cout << "Monkey " << (monkey + 1) << " , Day " << (day + 1) <<
": Food is " << monkeyFood[monkey][day] << endl;

}
}
system("Pause");
return 0;
}

5. Rain or Shine

An amateur meteorologist wants to keep track of weather conditions during the past year’s three month
summer season and has designated each day as either rainy (‘R’), cloudy (‘C’), or sunny (‘S’). Write a
program that stores this information in a 3 × 30 array of characters, where the row indicates the month
(0 = June, 1 = July, 2 = August) and the column indicates the day of the month. Note that data is not
being collected for the 31st of any month. The program should begin by reading the weather data in
from a file. Then it should create a report that displays for each month and for the whole three-month
period, how many days were rainy, how many were cloudy, and how many were sunny. It should also
report which of the three months had the largest number of rainy days. Data for the program can be
found in the [Link] file.

New program: Develop a C++ program that allow the user to enter a month and then displays how many
days are in that month. It does this by “looking up” information stored in arrays.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name[12] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October",
"November", "December"};
int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int number;
//Pre-test while loop to get input until user enters a sentinel of 0.
cout << "Enter a number between 1 to 12.\n";
cin >> number;
while (number != 0)
{
if (number < 1 || number > 12)
cout << "Invalid month number. Enter a number between 1 to 12.\n";
else
cout << "There are " << days[number - 1] << " days in " <<
name[number - 1] << endl;
cout << "Enter a number between 1 to 12.\n";
cin >> number;
}
system("pause");
return 0;
}

You might also like