please help fixing my bad logic issues.. im a noob...

// I am extremely bad at c++ and my class is giving me hell this semester and my instructor will not email me back to assist me with anything ive been doing poor on most of my recent assignments i started out good if some one could fix what i am doing wrong and perhaps write // comments explaining what i did wrong and what it should look like this would help me out alot this assignment hits alot of really confusing areas for me on the if while and for loops i will paste what i got followed by the algorithm i am supposed to be going by..

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````const int MAXADS = 100; int adcatcode[MAXADS]; int adwords[MAXADS]; int numads; int i; int j; int k; int subtotal; int temp; cout << " please enter the number of ads;" cin>> numads; if(numads > 0 + numads<=[MAXADS]) for(i=0; numads-1); i++) cout << "Please enter Advertisement Catagory code " << i + 1 << " Category Code (1-15):" << endl; cin >> adcatcode[i]; cout << "Please enter number of words for the advertisment:" cin>> adwords[i]; endl; for(i=0; numads-2);//really confused from this point on i think... for(j=0; numads-2);// for (temp = [j]); ([j] =[j+1]) ([j+1]; = temp) (temp = [j] = j+1 =temp); endl; cout << "total word counts sourted by category code" cin.. cout << "===============================" cin>> int k=0 while(k<=numads-1);(subtotal = 0 + curCode= adcatcode[k]) +(k<=numads-1);(subtotal=subtotal+adwords[k]); endl; else(cout"number adds requested less than 1 or is too large:ad limit is" cin>> MAXADS); system("PAUSE"); Return EXIT_SUCCESS;``````

// this is my algorithm i am to go by..
// 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

//any help with this would be appreciated its a long shot but if someone could spend the time to explain each part i did wrong in comment // or a method of choice to do so i would be grateful for any and all help thanks guys and gals

closed account (3CXz8vqX)
http://www.cplusplus.com/doc/tutorial/

Start from the top, work down. Most of what you need here can be learned in just a few hours. *says the guy who still has yet to master the concept of classes and pointers*.

Particularly take a look at for loops.

Edit. Starting from the top.

I don't know why your professor wants you to declare the variables at the top. It's actually really pointless and a legacy of Fortran I think. It's also not reader friendly. Define the variable where you need it not before.

Specifically when they revolve around loops such as int j.

You also need some braces eg. { }. These are key to containing blocks of code. For example.

 ``12345678`` ``````if( variable == condition ) { //Do lines of code here, make sure to indent for readability for( int loop_var = 0; loop_var < 10; loop_var = loop_var + 1 ) { //output! Should repeat 10 times. } }``````

Note how everything is contained within braces. You're missing them.

Also your first for loop pseudocode is going to lead you into errors.

Here's the code for the for loop.

`for( int i = 0; i < numads; i++ )`. This line cycles through all of the adverts and numads-1 means that you won't have the final advert. Oh yeah, typically we initialise variables on zero not one.

*complains that Professors are always trying to trip us up*

Edit.

Because of how your current line 15 is, you won't ever see the second cout statement. The lack of braces is the reason for this.
Last edited on
nice link man where on earth did you find something like this.... im going to have to read all this and throw out the text book sarcasm the book gives alot of bs this seems like a better apporach for me Thank you Ravenshade for linking me this bit of information now to burn some serious midnight oil and attempt to nip this thing in the bud. i fear i might be having to flunk this assignment tho do to sheer lack of time between work and school schedule i probably shouldn't have waited so long to seek help online i have to to keep repeating the mantra internet is my friend .. lol thanks again hopefully a light bulb will pop on and ill be saved.
or fail trying lol !
Topic archived. No new replies allowed.