Question
Lab 5: Arrays and Strings Objectives In this lab assignment, students will learn: How to declare one-dimensional and two-dimensional Arrays How to save data in
Lab 5: Arrays and Strings
Objectives
In this lab assignment, students will learn:
How to declare one-dimensional and two-dimensional Arrays
How to save data in one-dimensional and two-dimensional Arrays
How to use a for loop to iterate one-dimensional and two-dimensional Arrays
How to access and retrieve data from one-dimensional and two-dimensional Arrays
How to pass one-dimensional and two-dimensional Arrays to a function
How to use parallel arrays
How to declare and use the c-string
COURSE PREPARATION
Read the following documents:
Create Project Programs: In Course Resources
CSC134-Coding Guidelines: In Course Resources
Textbook:
Read Chapters 8 in Maliks C++ Programming 8th edition textbook.
Chapter 8 Exercise: (100 pts)
Introduction to Arrays
Goals:
In this lab assignment, students will demonstrate the ability to:
Learn how to create an array and use c-string.
-Declare one-dimensional and two-dimensional Arrays
-Save data in one-dimensional and two-dimensional Arrays
-Use a for loop to iterate one-dimensional and two-dimensional Arrays
-Access and retrieve data from one-dimensional or two-dimensional Arrays
-Pass one-dimensional and two-dimensional Arrays to a function
-Use parallel arrays
-Declare and use the c-string
Grading:
Be sure to follow the Coding Standard Guidelines. You must properly indent and comment your code. This program is worth 100 points.
Grading rubric:
Indent code and insert comments to document your program. [10 pts]
Program must be implemented and run as instructed. [80 pts]
The folder containing the program project should be zipped up into a file called (Lab5_yourName.zip or Lab5_yourName.zipx or Lab5_yourName.rar). The zipped file is submitted to Blackboard. Please use these file names exactly. [10 pts]
Instructions:
Please read this lab exercise thoroughly, before attempting to code it. It would be a good idea to use pencil and paper to organize your thoughts on how best to solve this exercise.
Program Description:
The United Nations provided 2009 Marital Status of Men and Women in the US (http://data.un.org/DocumentData.aspx?id=322).
Write a program to analyze the marital status of the combined total of men and women in the US. The categories being analyzed is as follows:
Single Men
Married Men
Widowed Men
Divorced Men
Separated Men
Single Women
Married Women
Widowed Women
Divorced Women
Separated Women
The program first reads headings, categories and the statistics from the file and saves them into three arrays:
Create a one-dimensional string array of the tableHeadings.
Create a one-dimensional string array Categories to store the different categories.
Create a (parallel) two-dimensional double array CatPerc to store percentage in each category. This array has 11 columns of the percentages in each age group.
The program then retrieves the percentages from the array CalcPerc, calculates average percentage of the combined men and women in each category. (Ignore age range 60-64 in calculation)
i.e. (Single Men Average + Single Women Average)/2.0
where Single Men Average = (Sum of Single Men Percentages) / 10.0 and Single Women Average = (Sum of Single Women Average)/10.0 .
Use the table below to assign the representation code to each combined men and women average in each combined category.
Average | Representation Code |
>= 40 | H - High |
>= 25 | N - Normal |
< 25 | L -Low |
The program must contain at least the following functions:
(1) A function read_data to read and store data into tableHeadings, categories and calcPerc arrays.
(2) A function analyze_stats to calculate the combined men and women averages, determine the Representation Code, get the number of High, Normal and Low Representation Codes, find the highest and lowest average of all combined categories.
(3) A function display_results to display the results.
Sample Output:
Use the following driver to test your functions:
#include
#include
#include
#include
using namespace std;
const int NUM_HEADINGS = 13;
const int NUM_CATEGORIES = 10;
const int NUM_COMBINED_PERC = 5;
const int NUM_PERCENTAGE = 11;
void read_data(ifstream &input, string tableHeadings[], string categories[],
double catPerc[][NUM_PERCENTAGE]);
void analyze_stats(double catPerc[][NUM_PERCENTAGE], double combinedPerc[],
int &high_cnt, int &norm_cnt, int &low_cnt,
double &highest,double &lowest);
void display_results(string categories[],double combinedPerc[], int high_cnt, int norm_cnt,
int low_cnt, double highest, double lowest);
int main()
{
string tableHeadings[NUM_HEADINGS];
string categories[NUM_CATEGORIES];
double catPerc[NUM_CATEGORIES][NUM_PERCENTAGE];
double combinedPerc[NUM_COMBINED_PERC];
double highest = 0.0, lowest = 0.0;
char representativeCode[NUM_COMBINED_PERC];
int high_cnt = 0, norm_cnt = 0, low_cnt = 0;
ifstream inFile;
inFile.open("lab5-MaritalStatus2009Survey.txt", ios::in);
if (!inFile.is_open())
{
cout << "Error: Cannot open file...Exiting." << endl;
system("pause");
exit(0);
}
//Read the input file and populate the arrays.
read_data(inFile, tableHeadings, categories,catPerc);
//Use the populated category percentage array to derive the combined percentage,
//count of high, norm and low representation codes.
analyze_stats(catPerc,combinedPerc,high_cnt,norm_cnt,low_cnt,highest,lowest);
//Display the results
display_results(categories,combinedPerc, high_cnt,norm_cnt,low_cnt,highest,lowest);
inFile.close();
system("pause");
return 0;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started