#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main ()
{
	int numb, sum, divis3, divis5;
	numb = 20;
	sum = 0;
	double value = sqrt(numb);
	
	cout.setf(ios::fixed,ios::floatfield);
	cout << setw(8) << left << "NUMBER" 
		 << setw(8) << left << "DIVIS3"
		 << setw(8) << left << "DIVIS5"
		 << setw(8) << left << "SQROOT"
		 << setw(8) << left << "FRTHROOT\n" << endl;
	
	while (numb <=60) {
		
		numb = numb +2;
		value = sqrt(numb);
		divis3 = divis3;
		divis5 = divis5;
		
		cout.setf(ios::fixed,ios::floatfield);
		
		cout << setw(8) << numb ;
		
		if (numb%3==0) {
			cout << setw(8) << "*";
			divis3 = divis3 +1;
		}
		else
			cout << setw(8) << " " ;
		
		if (numb%5==0) {
			cout << setw(8) << "*" ;
			divis5 = divis5 +1;
		}
		else
			cout << setw(8) << " ";
		
		cout << setw(8) << setprecision(3) << value ;
		
		cout << setw(8) << setprecision(3) << sqrt(value) << endl;
	}
	
	cout << endl << "The number of numbers divisible by 3 is " << divis3 << "." << endl;
	cout << "The number of numbers divisible by 5 is " << divis5 << "." << endl;
	cout << "The average of the numbers divisible by both 3 and 5 is " << "." << endl;
	cout << "The average of the square roots is " << "." << endl;
	cout << "The sum of the numbers is " << "." << endl;
	cout << "The sum of the square roots is " << "." << endl;
	
    return 0;
}

It was working, until I was messing around trying to get the average of the numbers divisible by both 3 and 5 (next on the list) -- needless to say, that didn't work (or I would have kept it in), and not only that -- but now my count goes to <=62. I can't find what the f is wrong. It explicitly states, <=60. What's wrong?

Besides/after that, how do I work out the rest of that list? (How do I find the average of the numbers divisible by both 3 and 5, the average of the sqrts, the sum of the numbers, and the sum of the sqrts?)

Thanks so much.

while(numb<=60)//means run this code while numb is in the range numb -> 60 inclusive

Your increment numb by 2 everytime the loop runs. Therefore, once it reaches 60, and it will because you included it in your range, it will run again and therefore numb will be 62.

What do you mean how do you find the average of the numbers divisible by 3 or 5? Do you want to add all the numbers that are divisible by 3 and get the average of them, and do the same for numbers divisible by 5?

Because that reads to me as, how do I find the average of a number

It was working, until I was messing around trying to get the average of the numbers divisible by both 3 and 5 (next on the list)

You need to check if something is divisible by 3 and 5 before you check division by 3 or 5 individually, with something like if((numb%3 == 0) && (numb%5 == 0)) . If you think about it, if you have the 3 & 5 check after the 3 check and the 5 check, then all the numbers that would go into the 3 & 5 check are already checked.

now my count goes to <=62. I can't find what the f is wrong. It explicitly states, <=60. What's wrong?

Look at what you loop does:

  • numb gets to 60, it passes the numb <= 60 test
  • the while loop executes again and numb is immediately incremented to 62

If you only want it to get to 60, you should use while(numb < 60)

how do I work out the rest of that list? (How do I find the average of the numbers divisible by both 3 and 5, the average of the sqrts, the sum of the numbers, and the sum of the sqrts?)

You can either make new variables that get updated with these values every time the loop repeats, or you can store everything in an array and do it at the end.

Move numb = numb + 2 to the very end of your loop. I assume you want it to go from 20 to 60?

Move numb = numb + 2 to the very end of your loop. I assume you want it to go from 20 to 60?

And that will help how?

Edit: please don't say "Because it wont print numb!"

And that will help how?

Well... then numb will go from 20 to 60. When it becomes 62, the loop will end.

Well... then numb will go from 20 to 60. When it becomes 62, the loop will end.

Wow. What happens if the loop is only meant to calculate something until numb == 60? Just calculate it 1 more time for fun?

No. I did that. It just fucked more up. It did 22 to 60. It was working fine before the way I had it. Now I have it back right, 20 - 60. I need something to do WITH that line -- I have no idea what to do.... Teacher wrote "avg" on the board like he wrote "sum" and shit that we have to include in the code. How do I declare avg. How do I make avg. What do I do with

if((numb%3==0) && (numb%5==0))

? Where do I put it, with what, to include the AVERAGE OF THE NUMBERS DIVISIBLE BY 3 AND 5 at the end of the SENTENCE towards the end, where it SAYS so and there's a fucking BLANK SPACE where the ANSWER is supposed to be. How do I do that. It's pretty straightforward. You see how I got the ANSWERS for the FIRST TWO SENTENCES? That's what I need for the last few. Please help, for real. Thanks. I'm sorry but I am angry and frustrated; bear with me and understand that I have no clue what what you're saying means; you'll need to be a little more thorough in explaining to me.

To find the average of the numbers divisible by three and the numbers divisible by five, I was thinking maybe I'd have to create a count within the if statement for the divis3 & divis5? and then somehow add the #'s divisible by three and the ones divisible by 5, divided by their collective counts (both added together). How the hell do I go about executing this. I can't seem to organize that....

Oh I forgot it's not the average of all the numbers divisible by three and the numbers divisible by five, it's the average of the numbers divisible by BOTH 3 & 5, so take that into account, oops, forgot... just double checked... still confused as to how to do that though.

Got the 3 & 5 one on to the next.

Never mind I got how many there are I keep getting confused. There are two. But I need the average of those two. Fuck.

It went from 22 - 60 because you have it initialised as 20. When it enters the loop, it is incremented by 2 straight away = 22.

Ok, first of all, you have not initialised your divis3 and divis5 variables. Therefore, how can you increment by 1 each time a divisor is found? The answer: You cannot add a number to nothing. Initialise those variables at the top of your code to 0.

There is also no need to set divis3 = divis3. It knows what it is, you don't have to tell it.

>>? Where do I put it, with what, to include the AVERAGE OF THE NUMBERS DIVISIBLE BY 3 AND 5 at the end of the SENTENCE towards the end, where it SAYS so and there's a ****ing BLANK SPACE where the ANSWER is supposed to be.

Well, you will need to store each of the numbers which are divisible, and then add them, count them and find the average. You will need 2 arrays, One for 5's and one for 3's. When a number is divisible, you add that number to the array.

Then, You will simply use a loop to add each element in the array to the next one, and then calculate your average.

Wow. What happens if the loop is only meant to calculate something until numb == 60? Just calculate it 1 more time for fun?

I do not understand what you mean. If you put numb = numb + 2 at the end of the loop it WILL iterate trough 20 to 60. numb will start at 20 and end at 60.. or am I missing something?

When you first enter the loop numb will be 20.. numb will be increased a couple of times.. when numb is 60 the loop will run again and at the very end it will be 62.. this will stop the loop!!

I do not understand what you mean. If you put numb = numb + 2 at the end of the loop it WILL iterate trough 20 to 60. numb will start at 20 and end at 60.. or am I missing something?

When you first enter the loop numb will be 20.. numb will be increased a couple of times.. when numb is 60 the loop will run again and at the very end it will be 62.. this will stop the loop!!

I honestly cannot understand how you can argue this. You just re-iterated my point. It will run 1 more time than was intended. All putting the increment at the bottom will do is stop the number being printed. It is still running 1 extra than he intended.

No. I did that. It just fucked more up. It did 22 to 60. It was working fine before the way I had it. Now I have it back right, 20 - 60. I need something to do WITH that line -- I have no idea what to do.... Teacher wrote "avg" on the board like he wrote "sum" and shit that we have to include in the code. How do I declare avg. How do I make avg. What do I do with

if((numb%3==0) && (numb%5==0))

? Where do I put it, with what, to include the AVERAGE OF THE NUMBERS DIVISIBLE BY 3 AND 5 at the end of the SENTENCE towards the end, where it SAYS so and there's a fucking BLANK SPACE where the ANSWER is supposed to be. How do I do that. It's pretty straightforward. You see how I got the ANSWERS for the FIRST TWO SENTENCES? That's what I need for the last few. Please help, for real. Thanks. I'm sorry but I am angry and frustrated; bear with me and understand that I have no clue what what you're saying means; you'll need to be a little more thorough in explaining to me.

Please try and remain calm. Programming requires patience and a lot of sitting still and thinking things through. Ultimately, the computer is pretty stupid, and you are most likely pretty smart, so you can win this kind of fight!

OK, first off, an apology: I misread your original code. The thing I said before wasn't wrong, but it was a little pointlessly restrictive. Alright, lets go over what you have and what you want. I have modified a section of your code below.

/* Declare these variables, and initialise them to zero */
/* We can use avg to store the average values           */
int divis3 = 0
int avg3 = 0;    /* This value will be used to calculate the average of the numbers    */
                 /* divisible by 3.  Actually, it will calculate the sum of these      */
                 /* numbers, but since you already have the counts, then we can easily */
                 /* calculate the average at the end by doing avg3 /= divis3           */
int divis5 = 0;
int divis3_5 = 0, avg3_5 = 0;

/* Now do the loop */
while(numb < 60) {
    numb += 2;    /* this is a kind of shorthand for numb = numb + 2; */
    value = sqrt(numb);
//    divis3 = divis3;   /* Removed these, since they don't do anything */
//    divis5 = divis5;

    if (numb%3 == 0){
        cout << setw(8) << "*";
        divis3++;     /* this is a kind of short hand for divis3 += 1 */
        avg3 += numb; /* Update the average value */
    }
    else
        cout << setw(8) << " " ;
 
    if (numb%5 == 0){
        cout << setw(8) << "*" ;
        divis5++;
    }
    else
        cout << setw(8) << " ";
/* Now catch the things divisible by 3 & 5 */
    if ((numb%5 == 0) && (numb%3 == 0)){
        cout << setw(8) << "*" ;
        divis5_3++;    /* New variable that counts things divisible by 3 & 5 */
    }
    else
        cout << setw(8) << " ";

    cout << setw(8) << setprecision(3) << value ;
    cout << setw(8) << setprecision(3) << sqrt(value) << endl;
}

Using this kind of template, I'm sure you can see how to add averages for the other numbers and find the sum of all the numbers and all those things.

Have a play with this, and see how you get on. Remember: remain calm. If you need to, print out your code and go through it with a pencil and calculate by hand what it should be doing. You will then be able to understand what you're asking the computer to do.

I honestly cannot understand how you can argue this. You just re-iterated my point. It will run 1 more time than was intended. All putting the increment at the bottom will do is stop the number being printed. It is still running 1 extra than he intended.

Ok, then I'm really sorry. I thought he wanted to loop through 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, .... 54, 56, 58, 60.

Ok, then I'm really sorry. I thought he wanted to loop through 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, .... 54, 56, 58, 60.

You are not getting this are you? That is what he wanted to do. But your solution would loop until 62. Even though it would only print up to 60. Do you understand? Plus, sorry to OP for slight offtopic.

Also, at ravenous, You basically just wrote code for what I explained, except for the array part which I suggested(your approach is alot better). Not really any point in repeating stuff ;)

Also, at ravenous, You basically just wrote code for what I explained, except for the array part which I suggested(your approach is alot better). Not really any point in repeating stuff ;)

Yeah, I try not to just write code in response to questions, but this thread was getting quite confused, with lots of confusing "put this here" and "don't do that there" (and more cursing that I'm used to on here!). I thought a clean(ish) slate and an example demonstration would do the OP good. It's not a complete solution though, there are still some gaps that need to be filled in :)

You are not getting this are you? That is what he wanted to do. But your solution would loop until 62. Even though it would only print up to 60. Do you understand? Plus, sorry to OP for slight offtopic.

Also, at ravenous, You basically just wrote code for what I explained, except for the array part which I suggested(your approach is alot better). Not really any point in repeating stuff ;)

Obviously you do not understand what I am saying.

int numb = 20;
while (numb <= 60) {
  // calculate
  numb += 2;
}

// print stuff
// exit program

Yes, numb will be 62 __right before__ you exit the loop. I did not see any harm in numb being 62 when it is never used with that value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.