The daily trumpet newspaper accepts classified advertissement in 15 categories s
ID: 3764193 • Letter: T
Question
The daily trumpet newspaper accepts classified advertissement in 15 categories such as Apartments for rent and pets for sale. Develop the logic for a program that accepts classified advertissement data including a category code( aan integer 1 through 15) and the number of words in the advertissement. Store these values in parallel arrays , then sort the array such that records are sorted in ascending order by category.The output lists each category number, the number of advertissement in the category, and the total number of words in the advertissement in the category .
b) Modify the newspaper advertising program in the the question above to display a descriptive string with each category , for example, category 1 might be Apartments for rent.
Explanation / Answer
a) Code in CPP
#include<iostream>
#include<string>
using namespace std;
int main(){
string category[15];
int categoryNo[15];
int words[15];
int noOfAd[15];
//take inputs from user
for(int i=0;i<15;i++){
cout<<"Category "<<i+1<<" ";
cin>>category[i];
cout<<"Category number: ";
cin>>categoryNo[i];
cout<<"No of words: ";
cin>>words[i];
cout<<"No of Ad ";
cin>>noOfAd[i];
}
//sort ascending order of category
for(int i=0;i<15;i++){
for(int j=0;j<15;j++){
if(category[i].compare(category[j])<0)
{ //swap category
string temp=category[i];
category[i]=category[j];
category[j]=temp;
//swap category no
int temp1=categoryNo[i];
categoryNo[i]=categoryNo[j];
categoryNo[j]=temp1;
//swap no of words
int temp2=words[i];
words[i]=words[j];
words[j]=temp2;
//swap no of ad
int temp3=noOfAd[i];
noOfAd[i]=noOfAd[j];
noOfAd[j]=temp3;
}
}
}
//display sorted details
cout<<"Details sorted according to category are: ";
for(int i=0;i<15;i++){
cout<<categoryNo[i]<<" "<<category[i]<<" "<<noOfAd[i]<<" "<<words[i]<<endl;
}
}
b) Modified code a)
#include<iostream>
#include<string>
using namespace std;
int main(){
string category[15];
int categoryNo[15];
int words[15];
int noOfAd[15];
string description[15];
//take inputs from user
for(int i=0;i<15;i++){
cout<<"Category "<<i+1<<" ";
cin>>category[i];
cout<<"Category number: ";
cin>>categoryNo[i];
cout<<"Description: ";
cin>>description[i];
cout<<"No of words: ";
cin>>words[i];
cout<<"No of Ad ";
cin>>noOfAd[i];
}
//sort ascending order of category
for(int i=0;i<15;i++){
for(int j=0;j<15;j++){
if(category[i].compare(category[j])<0)
{ //swap category
string temp=category[i];
category[i]=category[j];
category[j]=temp;
//swap category no
int temp1=categoryNo[i];
categoryNo[i]=categoryNo[j];
categoryNo[j]=temp1;
//swap description
string temp4=description[i];
description[i]=description[j];
description[j]=temp4;
//swap no of words
int temp2=words[i];
words[i]=words[j];
words[j]=temp2;
//swap no of ad
int temp3=noOfAd[i];
noOfAd[i]=noOfAd[j];
noOfAd[j]=temp3;
}
}
}
//display sorted details
cout<<"Details sorted according to category are: ";
for(int i=0;i<15;i++){
cout<<categoryNo[i]<<" "<<category[i]<<" "<<description[i]<<" "<<noOfAd[i]<<" "<<words[i]<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.