my heads gonna explode i need to convert this i got like 120 errors can someone do this correctly so i can fix my mistakes..

// Pseudocode PLD Chapter 8 #7 pg. 350
// 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
this is supposed to be converted in c++ language im using bloodshed as a compiler when i do this algorithm all i get is like 120 errors perhaps im completely off on how i am supposed to convert this.
Let's see what you've done so far(remember to include the code tags).
[#include <cstdlib> // please dont tease first c++ class and im bombing it so far due to
not fully understanding what it is i am doing exactly i know this is insanely
wrong i just dont understand the correct logic
#include <iostream>

using namespace std;
int main (intargc, char* argv[])
int MAXADS = 100;  // not sure if i did this right to begn is this some kind of constand int?
int adcatcode[100];
int adwords;
int curCode;
[int numads;
[int i; 
[int j;
[int k;
[int subtotal;
[int temp;]

cout <<"please enter the number of ads:" cin>> endl // i know this part is quite messed up 
i am about terrible at the cout clauses

[for (i >0; numads-1)]
//for i = 0 and numads-1  // not sure if this is remotly what i should be doing here..

[for (i=0 numads-2)]
[for (j=0 numads-2)// not sure about these 2 for s]

[if  (j>[j+1] )]
[temp = [j] =[j+1]]

//and thats about as far as i got the following beyond i know my while // if i could just get it broken down to where i can make sence of it i systematically took each section of the
algorithm and tryed to make some code out of it but its terrible it would be humiliating to post the rest as you can see i am not good with this problem im trying to solve
its mainly due to not taking this inclass and having any type of tutor or help with these things the book shows me some of this but not nearly enough to give good concrete example
of the excercise im trying to accomplish. i am to embarassed to post the rest of this hog wash ... ;(

not sure what you mean by code tags but im assuming the //?
Last edited on
closed account (3CXz8vqX)
int adcatcode[MAXADS]; is what it should be but you did the first declaration correctly. I'd define it as const int though if you don't expect it to change.

Remember to wrap the code in [ code] tags when you post here...


const int MAXADS = 100;
int adcatcode[MAXADS]; idea why you're including cstdlib...might as well get rid of it.

Edit, okay lemme explain line 6?

int MAXADS = 100; /* Here you are defining a variable as an integer, those are the 
 first two words. Then you are 'assigning' ( = ) the variable the value of 100. 
 So this is perfectly valid*/
int adcatcode[100]; /* While technically correct this isn't what your pseudocode wants you
 to do. Your adcatcode is an 'array' of integers, but you don't want to define it as 100, you
 want to define it as whatever the value is contained in MAXADS (ergo my previous example)*/ 

This is the whole point about 'variables' think Algebra. They are there as placeholders that you can change later. (You'll go meta on this later with pointers).
Last edited on
so to wrap code in[] the whole thing or each line?
closed account (3CXz8vqX)
Whole thing. Highlight all your code, then press the <> icon on the right of the text entry box.
oh ok sorry ty.. never used a forum before
ok i see what you did there with ur /* examples raven that helps alot on the declaration of int MAXADS and adcatcode in this algorithm
closed account (3CXz8vqX)
Line 14....

cout << variable/text
cin >> variable

You're problem though is your semicolons and you're not allowing the user to input data that you can use either. You're just ending the line and I'm not even sure if that's valid....

cout << "text" << endl;
cin >> variable;

Here's an example of a for loop.
for( int count = 0; count < 10; count = count + 1 )
    cout << count << endl;

So in text version

"We have zero counters, print how many counters we have then increase counters by 1 until we have 10 counters. " Is pretty much what we're thinking here.


I found issues with the Pseudocode. Your first for loop (as far as I can tell) shouldn't be 'numads - 1' it should just be 'numads' otherwise you'll never assign all of your adverts.

Tip for readability (screw the lecturer)

            cout << "Please enter Advert number " << i + 1 << " Category Code (1-15)" << endl;
            cin >> adcatcode[i];

Your first cout should be as follows. The reason for this is because otherwise you don't know which advert you're inputting. The i+1 is specifically for readability only, we don't normally count from 0 we usually count from 1. So your first advert would be 1. But in your code, thanks to arrays it would be assigned to 0 instead. (That was a fun exercise, thanks!)
Last edited on
Topic archived. No new replies allowed.