Directions The purpose of this assignment is to practice arrays, and functions w
ID: 3707261 • Letter: D
Question
Directions
The purpose of this assignment is to practice arrays, and functions with arrays, in C++.
Create a C++ program and name it using the following name format: YourNameAssign04.cpp. Example: If your name is Smith John (last name first), then your C++program should be: SmithJohnAssign04.cpp.
Students will be assigned the same business stores that were assigned in Assignment 01 (see table below).
YourNameAssign04.cpp
Create a C++ program that will create and initialize 6 arrays of attributes for 10 stores in main. Each array will contain the values for one distinct attribute from the assigned stores. Each array will have the type that best accommodate the kind of attribute being stored. Like assignment 1, at least one attribute will be a string, one an integer, and one a floating point. The types of the other three attributes are at the discretion of the student. After declaration and initialization, all that the program will do is to call, from main, the following functions:
1. printable to print all data in the arrays properly labeled and aligned.
2. sortData to sort all 6 arrays in parallel
3. printable again to print all sorted data.
These functions above are described as follows:
printTable function
This function will receive the arrays that were defined in main and print a table containing the information contained within the array. Every row in the table will contain the information for the same store (10 rows) and every column will contain the information of the same attribute for all stores (6 columns with attributes). Print appropriate headers for the table and each column. Print an index number (from 1 to 10) in front of each row. All columns should be aligned.
sortData function
This function will receive the arrays that were defined in main and sort them based on one of your integer attributes from lower to higher value. You may review the insert code described in Figure 6.16 in the textbook and modify it to create your own function. Remember that you are sorting 6 arrays based on one of them, so you need to keep track and move the data inside all arrays in parallel to produce a correct sort.
Do not forget to add comments to the code. The .cpp file must have a head of comments indicating the name of the file, the name of the author, and a brief description of the program. Each function must have also have a head of comments indicating the name of the function and a brief description of what it does. All variable names must also be documented.
Directions
The purpose of this assignment is to practice arrays, and functions with arrays, in C++.
Create a C++ program and name it using the following name format: YourNameAssign04.cpp. Example: If your name is Smith John (last name first), then your C++program should be: SmithJohnAssign04.cpp.
Students will be assigned the same business stores that were assigned in Assignment 01 (see table below).
YourNameAssign04.cpp
Create a C++ program that will create and initialize 6 arrays of attributes for 10 stores in main. Each array will contain the values for one distinct attribute from the assigned stores. Each array will have the type that best accommodate the kind of attribute being stored. Like assignment 1, at least one attribute will be a string, one an integer, and one a floating point. The types of the other three attributes are at the discretion of the student. After declaration and initialization, all that the program will do is to call, from main, the following functions:
1. printable to print all data in the arrays properly labeled and aligned.
2. sortData to sort all 6 arrays in parallel
3. printable again to print all sorted data.
These functions above are described as follows:
printTable function
This function will receive the arrays that were defined in main and print a table containing the information contained within the array. Every row in the table will contain the information for the same store (10 rows) and every column will contain the information of the same attribute for all stores (6 columns with attributes). Print appropriate headers for the table and each column. Print an index number (from 1 to 10) in front of each row. All columns should be aligned.
sortData function
This function will receive the arrays that were defined in main and sort them based on one of your integer attributes from lower to higher value. You may review the insert code described in Figure 6.16 in the textbook and modify it to create your own function. Remember that you are sorting 6 arrays based on one of them, so you need to keep track and move the data inside all arrays in parallel to produce a correct sort.
Do not forget to add comments to the code. The .cpp file must have a head of comments indicating the name of the file, the name of the author, and a brief description of the program. Each function must have also have a head of comments indicating the name of the function and a brief description of what it does. All variable names must also be documented.
Explanation / Answer
// C++ program to take input of 10 stores and sort them based on their ID
#include <iostream>
#include<limits>
#include <string>
#include <iomanip>
using namespace std;
// function declaration
void printTable(int id[], string name[],string state[], string city[], string zipcode[],float revenue[],int size);
void sortData(int id[], string name[],string state[], string city[], string zipcode[],float revenue[],int size);
int main() {
const int size =10;
int id[size];
string name[size],state[size],city[size],zipcode[size];
float revenue[size];
// inputs
cout<<" Enter the details of "<<size<<" stores:"<<endl;
for(int i=0;i<size;i++)
{
cout<<" Store "<<(i+1)<<endl;
cout<<" ID : ";
cin>>id[i];
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),' '); //cin leaves in the input so remove it
cout<<" Name : ";
getline(cin,name[i]);
cout<<" City : ";
getline(cin,city[i]);
cout<<" State : ";
getline(cin,state[i]);
cout<<" Zipcode : ";
getline(cin,zipcode[i]);
cout<<" Revenue : $";
cin>>revenue[i];
cout<<endl;
}
// output the initial data
cout<<" Initial data : "<<endl;
printTable(id,name,state,city,zipcode,revenue,size);
// sort the data based on ID
sortData(id,name,state,city,zipcode,revenue,size);
cout<<" Sorted data : "<<endl;
// output the sorted data
printTable(id,name,state,city,zipcode,revenue,size);
return 0;
}
// function to print the details of the store
void printTable(int id[], string name[],string state[], string city[], string zipcode[],float revenue[],int size)
{
cout<<setw(10)<<" ID "<<setw(20)<<" Name "<<setw(20)<<" City "<<setw(15)<<" State "<<setw(15)<<" ZipCode "<<setw(15)<< " Revenue($) "<<endl;
for(int i=0;i<size;i++)
cout<<(i+1)<<setw(10)<<id[i]<<setw(20)<<name[i].substr(0,name[i].length()-1)<<setw(20)<<city[i].substr(0,city[i].length()-1)
<<setw(15)<<state[i].substr(0,state[i].length()-1)<<setw(15)<<zipcode[i].substr(0,zipcode[i].length()-1)<<setw(15)<<revenue[i]<<endl;
}
// function to sort the stores based on their id in ascending order
void sortData(int id[], string name[],string state[], string city[], string zipcode[],float revenue[],int size)
{
int min;
for(int i=0;i<size-1;i++)
{
min = i;
for(int j=i+1;j<size;j++)
{
if(id[j] < id[min])
min = j;
}
if(min != i)
{
int tempId = id[i];
id[i] = id[min];
id[min] = tempId;
string tempName = name[i];
name[i] = name[min];
name[min] = tempName;
string tempCity = city[i];
city[i] =city[min];
city[min] = tempCity;
string tempState = state[i] ;
state[i] = state[min];
state[min] = tempState;
string tempZip = zipcode[i] ;
zipcode[i] = zipcode[min];
zipcode[min] = tempZip;
float tempRevenue = revenue[i];
revenue[i] = revenue[min];
revenue[min] = tempRevenue;
}
}
}
//end of program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.