## garc2541

I'm trying to figure out the avg of the sum of squares which I calculated by making a function in my program. The only problem is I dont how to count it. I think I can use the sq variable and divide it by the count, but I dont know how to count something that is calculated in a function. How would I do this? I also need to do this for the sum of cubes function. Any help is greatly appreciated thank you in advance. (function has comments)

``````#include <iostream>
using namespace std;

int sumsq(int); // sum of squares prototype
int sumcb(int);
int prime(int);

int main()
{
cout<<" Number    SumSq      SumCb        PRIME "<<endl;
cout<<endl;

int numb;
int sum;
int sq;
int scb;
int count = 0;
int count1 = 0;
int count2 = 0;

for(numb=5;numb<=49;numb+=2){
count++;
sum = sum + numb;

if(prime(numb)==1){
count1++;
cout<<"                                     "<<"*"<<endl;
}
else
cout<<endl;

cout<<"  "<<numb<<"           "<<sumsq(numb)<<"         "<<sumcb(numb)<<endl;

sq = sumsq(numb); // sum of squares

scb = sumcb(numb);

}

cout<<endl;
cout<<"The Average of the Numbers is: "<<sum/count<<endl;
cout<<"The Average of the sum of squares is: "<<endl;
cout<<"The Average of the sum of cubes is: "<<endl;
cout<<"The Number of the prime number is: "<<count1<<endl;

system ("pause");
return 0;
}

int sumsq(int numb){  // sum of squares function

int yoyo;
int sum = 0;

for(yoyo=1;yoyo<=numb;yoyo++)

sum+= yoyo*yoyo;
return sum;

}
int sumcb(int numb){

int val;
int sum1 = 0;

for(val=1;val<=numb;val++)

sum1+= val*val*val;
return sum1;
}

int prime(int numb) {

int p;

for (p=2; p<=numb/2; p++) {
if (numb%p==0)  {
return 0;
}
}
return 1;
}``````

## WaltP 2,905

Not a clue what you are asking, and your code is too hard to read. Try formatting the code using this info so we can follow what you're trying to do.

## vmanes 1,165

Besides making the code more readable, I think you need to clarify what you mean (or what the assignment means) by the "average of the sum of the squares/cubes".

What your code appears to be doing, is for each number 5 through 49, you are adding the squares of each number from 1 up to the current number. Each call to the sumsq( ) or sumcb() functions does the same work as the last time, adding one more iteration. When your main loop does its final iteration, your sq and scb variables will in fact hold the sum of the squares/cubes of all values 1 to 49. Given that, the functions need not be called inside the loop, but be called afterwards with the terminal counter from the loop (this saves lots of wasted computations, fwiw). You know the count of numbers up to that point, so divide to find your averages.

Val

## garc2541

I really appreciate the responses and I'm trying to learn how to format my code as I know it's not proper. The question im asking is how do i calculate the avg of the sum of squares. I know you use sum/count but how do I count if and sum if the sum of squares calculation is in a function???

## vmanes 1,165

Time to learn how to walk through your code.

You have the statement:

``sq = sumsq(numb); // sum of squares``

which gives the sum of the squares of the numbers 1 through numb. So what do you get when you divide sq by numb?

The code you have seems to very nearly do what I think it is you want it to. Look at the results you get - what's wrong with the average? Did your compiler give you any hint as to how to fix this?

You print a mostly blank line with an asterisk at the end for any primes found, then print the line with the number. Reading the output, it's not clear which lines are being marked as prime. What if you first output the number and its square/cube, the did the prime test and display * as needed?

Val