Respuesta :
Answer:
#include<iostream>
using namespace std;
#define MA 100
int main()
{
int temp;
int i,j,k;
int advertisementCategoryCode[MA];
int subtotal;
int adwords[MA];
int curCode;
int totalAds;
cout<<"Please enter the total ads: ";
cin>>totalAds;
if((totalAds > 0) and (totalAds <= MA))
{
for (i = 0;i<totalAds;i++)
{
cout<<"Please enter Advertisement Category Code (1 - 15): ";
cin>>advertisementCategoryCode[i];
cout<<"Please enter number of words for the advertisement: ";
cin>>adwords[i];
}
for (i=0;i<totalAds-1;i++)
{
for (j = 0;j<totalAds-1;j++)
{
if (advertisementCategoryCode[j] > advertisementCategoryCode[j+1])
{
temp = advertisementCategoryCode[j];
advertisementCategoryCode[j] = advertisementCategoryCode[j+1];
advertisementCategoryCode[j+1] = temp;
temp = adwords[j];
adwords[j] = adwords[j+1];
adwords[j+1] = temp;
}
}
}
cout<<"\n\n Total Word Counts Sorted By Category Code"<<endl;
k = 0;
while(k<=totalAds-1)
{
subtotal = 0;
curCode = advertisementCategoryCode[k];
while ( (curCode == advertisementCategoryCode[k])&& (k <= totalAds - 1) )
{
subtotal = subtotal + adwords[k];
k = k + 1;
}
cout<<"Category: "<<advertisementCategoryCode[k - 1]<<" "<<"Word Count: "<<subtotal<<endl;
}
}
else
{
cout<<"Number adds requested less than 1 or is too large; ad limit is :"<<MA;
}
return 0;
}
Explanation:
- Use a while loop that runs until the variable k <= (totalAds-1) .
- Assign the current code to the curCode variable.
- Add the words for advertisement and assign it to the subTotal variable.