181k views
5 votes
The Daily Trumpet newspaper accepts classified advertisements in 15 categories such as Apartments for Rent and Pets for Sale. Develop the logic for a program that accepts classified advertising data, including a category code (an integer 1 through 15) and the number of words in an ad. Store these values in parallel arrays. Then sort the arrays so that the records are sorted in ascending order by category. The output lists each category number, the number of ads in the category, and the total number of words in the ads in the category. Using the following pseudocode. Thank you.

// Pseudocode PLD Chapter 8 #7a pg. 366

// Start

// Declarations

// num MAXADS = 100

// num adcatcode[MAXADS]

// num adwords[MAXADS]

// num curCode

// num numads

// num i

// num j

// num k

// num subtotal

// num temp

// output "Please enter the number of ads: "

// input numads

// if ((numads > 0) and (numads <= MAXADS))

// for i = 0 to numads - 1

// output "Please enter Advertisement Category Code (1 - 15): "

// input adcatcode[i]

// output "Please enter number of words for the advertisement: "

// input adwords[i]

// endfor

// for i = 0 to numads - 2

// for j = 0 to numads - 2

// if (adcatcode[j] > adcatcode[j+1])

// temp = adcatcode[j]

// adcatcode[j] = adcatcode[j+1]

// adcatcode[j+1] = temp

// temp = adwords[j]

// adwords[j] = adwords[j+1]

// adwords[j+1] = temp

// endif

// endfor

// endfor

// output "Total Word Counts Sorted By Category Code"

// output "========================================="

// k = 0

// while k <= numads - 1

// subtotal = 0

// curCode = adcatcode[k]

// while ( (curCode == adcatcode[k]) and (k <= numads - 1) )

// subtotal = subtotal + adwords[k]

// k = k + 1

// endwhile

// output "Category: ",adcatcode[k - 1], " ","Word Count: ", subtotal

// endwhile

// else

// output "Number adds requested less than 1 or is too large; ad limit is ", MAXADS

// endif

// Stop

User Sean Hill
by
4.8k points

1 Answer

4 votes

Answer:

see explaination

Step-by-step explanation:

#include<iostream>

using namespace std;

#define MAXDAS 100

int main()

{

//int MAXADS = 100;

int adcatcode[MAXDAS];

int adwords[MAXDAS];

int curCode;

int numads;

int i,j,k;

int subtotal;

int temp;

cout<<"Please enter the number of ads: ";

cin>>numads;

if((numads > 0) and (numads <= MAXDAS))

{

for (i = 0;i<numads;i++)

{

cout<<"Please enter Advertisement Category Code (1 - 15): ";

cin>>adcatcode[i];

cout<<"Please enter number of words for the advertisement: ";

cin>>adwords[i];

}

for (i=0;i<numads-1;i++)

{

for (j = 0;j<numads-1;j++)

{

if (adcatcode[j] > adcatcode[j+1])

{

temp = adcatcode[j];

adcatcode[j] = adcatcode[j+1];

adcatcode[j+1] = temp;

temp = adwords[j];

adwords[j] = adwords[j+1];

adwords[j+1] = temp;

}

}

}

cout<<"Total Word Counts Sorted By Category Code"<<endl;

cout<<"========================================="<<endl;

k = 0;

while(k<=numads-1)

{

subtotal = 0;

curCode = adcatcode[k];

while ( (curCode == adcatcode[k])&& (k <= numads - 1) )

{

subtotal = subtotal + adwords[k];

k = k + 1;

}

cout<<"Category: "<<adcatcode[k - 1]<<" "<<"Word Count: "<<subtotal<<endl;

}

}

else

{

cout<<"Number adds requested less than 1 or is too large; ad limit is :"<<MAXDAS;

}

return 0;

}

See attachment for output

The Daily Trumpet newspaper accepts classified advertisements in 15 categories such-example-1
User Daniel Heinrich
by
5.4k points