YourNameAssign05.cpp This program will do the same than Assignment 4 did, except
ID: 3712943 • Letter: Y
Question
YourNameAssign05.cpp
This program will do the same than Assignment 4 did, except this time the number of stores will be a number n from 1 to 10 (inclusively). The main method should obtain this number n from the user. The program will not proceed until a proper number n is given. Once the number n is obtained, the main method will create a dynamic array of n structures. The structures will be defined in the program to contain 5 numeric attributes for each store. These will be integer and double number. We will not use string since they may complicate your programs.
After declaration, creation of initialization of this dynamic array, the main method will call 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 dynamic array 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 (n rows) and every column will contain the information of the same attribute for all stores (5 columns with number attributes). Print appropriate headers for the table and each column. Print an index number (from 1 to n+1) in front of each row. All columns should be aligned.
sortData function
This function will receive the dynamic array defined in main and sort it 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 this time you are sorting only sorting one array, but is an array of structures with multiple data per store..
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
#include <iostream> // used for cout, cin
#include <iomanip>>
using namespace std;
//Defination of structure contianing five attribues of store
struct store
{
int attrib1, attrib2,attrib3;
double attrib4,attrib5;
};
// This function will take dynamic array as parameter & print the store details on screen
// Each row will contain the information for the same store and
// every columns will contain the information of the same attribute for all stores
printfTable(store *array,int n)
{ int i;
cout<<endl<<endl<<setw(50)<<"Information about Different Store"<<endl; //Printing Heading
//printing column heading
cout<<"Index No."<<setw(13)<<right<<"Attribute1"<<setw(13)<<right<<"Attribute2"<<setw(13)<<right<<"Attribute3"<<setw(13)<<right<<"Attribute4"<<setw(13)<<right<<"Attribute5"<<endl;
for(i=0;i<n;i++)
{
//Printing the content of array
cout<<i+1<<setw(15)<<right<<array[i].attrib1<<setw(15)<<right<<array[i].attrib2<<setw(15)<<right<<array[i].attrib3<<setw(15)<<right<<array[i].attrib4<<setw(15)<<right<<array[i].attrib5<<endl;
}
cout<<endl<<endl;
}
//This function will sort the table on first integer attribute
sortData(store *array,int n)
{
store temp;
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(array[j].attrib1>array[j+1].attrib1)
{//swap the elements of arrays
temp=array[j]; array[j]=array[j+1]; array[j+1]=temp;
}
}
}
}
//main() function will dynamically allocate array for n elements
//It will store the elements in dynamic array by taking input from the user
// Then this function will call other function for printing table & sorting the table
int main()
{
int n,i;
store *array;
//Taking input the value of n between 1 to 10
do
{
cout<<"Enter the number of store:";
cin>>n;
if(n<1 || n>10)
{ cout<<"Re enter Input: Number should be between 1 to 10"<<endl;
continue;
}
}while(n<1 || n>10);
array= new store[n]; // Dynamically allocate the memory for array of n elements
//this code will store the elements in array by taking input from the user
for(i=0;i<n;i++)
{
cout<<"Input data for store "<<i+1<<endl;
cout<<"Enter integer value for attribute1:";
cin>>array[i].attrib1;
cout<<"Enter integer value for attribute2:";
cin>>array[i].attrib2;
cout<<"Enter integer value for attribute3:";
cin>>array[i].attrib3;
cout<<"Enter double value for attribute4:";
cin>>array[i].attrib4;
cout<<"Enter integer value for attribute5:";
cin>>array[i].attrib5;
}
printfTable(array,n); // calling function for printing the of orginal array by passing array and n as a parameter
sortData(array,n); //calling function to sort the array in ascending order
cout<<endl<<"Printing sorted List"<<endl<<endl;
printfTable(array,n); // calling function for printing the of sorted array by passing array and n as a parameter
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.