Generating 4 columns of numbers - need help

Hi everyone,

I need to generate four columns of numbers which are
1) NUMBER: Odd numbers from 5 to 49 (inclusive)
2) SUMSQ: The sum of the squares of the numbers from 1 to NUMBER.
3) SUMCB: The sum of the cubes of the numbers from 1 to NUMBER.
4) PRIME: put a * in this column if NUMBER is prime.

I ahve the first column working, but having trouble with the second. Here is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cmath>

using namespace std;

int SumOfSquares (int numb){

int sum=0;

for(numb=5; numb<=49; numb+=2)
    for(int i=1; i<=numb; i++)
      sum += i*i;
      return sum;}

}

int main()
{
    int numb;

    cout << "NUMBER\t" << "SUMSQ\t" << "SUMCB\t" << "PRIME\t" << "\n\n";

    for(numb=5; numb<=49; numb+=2)

        cout << numb << endl;
        cout << "\t" << SumOfSquares(numb);
    return 0;
}



I should be getting a line of numbers in the second column that are the sum of squares (from 1 to that number) of each number in the first column.

Ex: The first number in the first column is 5. In the second column right next to it, the number 55 should appear.

Any help with this?
Your loop in sum of squares is wrong. You have a random bracket, so I can't tell you exactly what to fix, but the outer loop isn't doing you any good.

Get rid of endl on line 25. You won't see the numbers next to each other otherwise.
Get rid of endl on line 25. You won't see the numbers next to each other otherwise.


If I get rid of that endl, the column gets ruined. It should be vertical column. Getting rid of the endl lines up all the numbers horizontally...


As for the loop, should I get rid of it completely? Im lost at this point...
You cannot print one column at a time. You have to print one line at a time.


Let me rewrite your line 21 just give an idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// before
cout << "NUMBER\t" << "SUMSQ\t" << "SUMCB\t" << "PRIME\t" << "\n\n";

// after
#include <cstring>
#include <iostream>
#include <iomanip>

int main() {
  using std::strlen;
  using std::setw;
  using std::cout;
  constexpr auto tnum = "NUMBER";
  constexpr auto tssq = "SUMSQ";
  constexpr auto tqcb = "SUMCB";
  constexpr auto tpri = "PRIME";
  constexpr auto lnum = strlen(tnum);
  constexpr auto lssq = strlen(tssq);
  constexpr auto lscb = strlen(tscb);
  constexpr auto lpri = strlen(tpir);

  // print one line:
  cout << setw(lnum) << tnum << '\t';
  cout << setw(lssq) << tssq << '\t';
  cout << setw(lscb) << tscb << '\t';
  cout << setw(lpri) << tpri << '\n';

  cout << '\n';
  return 0;
}

The result is practically the same. (I did remove the trailing tabulator though.)
Lines 23-26 print one line. Look up what the std::setw does, although it does effectively nothing on this line.

You want to print one line for each odd number in [5,49]. Each line will print different values.
Last edited on
What I meant is that you get rid of the endl there and put it somewhere after all your outputs for n.

The loop. Again, you have a random bracket that can't be there, so I can't tell you what the problem is until you fix it.
What I meant is that you get rid of the endl there and put it somewhere after all your outputs for n.

The loop. Again, you have a random bracket that can't be there, so I can't tell you what the problem is until you fix it.


Ok, so I've made some progress. When I run the updated program below, it outputs the correct sum of squares, however, it does something weird with the first (NUMBER) column. I have no idea what that it. You'll have to run it in the c++ shell yourself and see. How do I fix that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cmath>

using namespace std;

int SumOfSquares (int numb){

int sum=0;

    for(int i=1; i<=numb; i++)
    sum += i*i;
    return sum;

}

int main()
{
    int numb;

    cout << "NUMBER\t" << "SUMSQ\t" << "SUMCB\t" << "PRIME\t" << "\n\n";

    for(numb=5; numb<=49; numb+=2)

        cout << numb <<
        cout << "\t" << SumOfSquares(numb) << endl;
    return 0;
}
It does nothing weird with the numb. The "understandable" part is after the numb.

You effectively do:
cout << cout;
The output looks like a hex number, like memory addresses usually are.


Do note that lines 24 and 25 are only one statement.

This is legal (but confusing):
1
2
3
4
cout
<<
numb
;



I do recommend using braces around the body of the loop statement, even though you have only one statement in the loop body.
Thank you all for your help. Im almost done, I have 3 out of the 4 rows complete.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <cmath>
#include <cstring>

using namespace std;

int SumOfSquares (int numb){

int sum=0;

    for(int i=1; i<=numb; i++)
    sum += i*i;
    return sum;

}
int SumOfCubes (int numb){

int sum=0;

    for(int i=1; i<=numb; i++)
    sum += i*i*i;
    return sum;

}

int numdiv (int numb){

    int cnt=0;

  for(int i=1; i<=numb; i++)
    if(numb%i==0) // checks for divisors
       cnt++; // counts number of divisors

       return cnt;

}


int Prime (int numb){

int divisors;

divisors=numdiv(numb); // calls function numdiv

        if (divisors==2) // a number is prime if it has only 2 divisors. 
            return "*"; // should post * in the 4 column if NUMBER is prime

}


int main()
{
    int numb;

    cout << "NUMBER\t" << "SUMSQ\t" << "SUMCB\t" << "PRIME\t" << "\n\n";

    for(numb=5; numb<=49; numb+=2){
        cout << numb;
        cout << "\t" << SumOfSquares(numb);
        cout << "\t" << SumOfCubes(numb);
        cout << "\t" << Prime(numb) << endl; }
    return 0;
}
}


For the 4th column, I called a function "Prime" which in turn calls a function "numdiv". However, im getting an error message. Apparently im not allowed to return a *.

How can I fix this? I thought including the string library would fix this and allow me to return an asterisk but that didn't work.

Any ideas?
Your function Prime has actually more than one issue.

It claims to return an int, but you apparently would be happy to get one character.
It claims to return a value, but what does it return if divisors isn't 2?
"*" is a C-string and its type is const char *. If you want to return single plain char, then you must write '*'. (Single vs double quotes.


Line 30: numb%1 and numb%numb are always 0. You could omit them from the loop range. Actually, the function could return a bool (true or false).
Ok, then. I have newly revised the code. See below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <cmath>


using namespace std;

int SumOfSquares (int numb){

int sum=0;

    for(int i=1; i<=numb; i++)
    sum += i*i;
    return sum;

}
int SumOfCubes (int numb){

int sum=0;

    for(int i=1; i<=numb; i++)
    sum += i*i*i;
    return sum;

}

int numdiv (int numb){

    int cnt=0;

  for(int i=1; i<=numb; i++)
    if(numb%i==0) // checks for divisors
       cnt++; // counts number of divisors

       return cnt;

}

int Prime (int numb){

int divisors;

divisors=numdiv(numb); // calls function numdiv

        if (divisors==2) // a number is prime if it has only 2 divisors.
            return 1;
            else return 0;


}

int primenumb (int numb){

int pr;
pr = Prime(numb);

if(pr==1)
    cout << " \t" << "*";


}

int main()
{
    int numb;

cout << "NUMBER\t" << "SUMSQ\t" << "SUMCB\t" << "PRIME\t" << "\n\n";

    for(numb=5; numb<=49; numb+=2){
        cout << numb;
        cout << "\t" << SumOfSquares(numb);
        cout << "\t" << SumOfCubes(numb);
        cout << "\t" << primenumb(numb) << endl;}

    return 0;
}


I dont understand why its giving me the output that its given me. Im supposed to have an asterisk in the fourth column for whenever NUMBER is prime. Instead its also giving me zero's where there arent asterisks as well as the number "4683872' to the right of the column.

So confused right now. Can someone break down the issue for me so I can fix it?
I'll first repeat the recommendation that the Prime should return a bool -- true or false.


Your primenumb promises to return a value, an int.
What does it return? Nothing.
What should it return? Definitely not an integer.
Your code has to become more consistent.


Have you heard about ternary operator?
Its syntax is: Cond ? A : B
It does return a value. It does return A, if Cond is true and B, if Cond is false.

Alternatively, you could have an if-clause in main() for printing the fourth column.


Gold star, if you notice how you can accumulate the sums in the main loop without repeating the sum from 1 to NUMBER for every NUMBER.
Alternatively, you could have an if-clause in main() for printing the fourth column.


I cant use bool or the ternary operator because I havent learned those yet. I only want to write this program using the tools I've already learned. Therefore I took your advice and tried an if clause. However, its still not coming out right. Try running it in the c++ shell, the columns are all screwed up.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <cmath>


using namespace std;

int SumOfSquares (int numb){

int sum=0;

    for(int i=1; i<=numb; i++)
    sum += i*i;
    return sum;

}
int SumOfCubes (int numb){

int sum=0;

    for(int i=1; i<=numb; i++)
    sum += i*i*i;
    return sum;

}

int numdiv (int numb){

int cnt=0;

  for(int i=1; i<=numb; i++)
    if(numb%i==0) // checks for divisors
       cnt++; // counts number of divisors

       return cnt;

}

int Prime (int numb){

int divisors;

divisors=numdiv(numb); // calls function numdiv

        if (divisors==2) // a number is prime if it has only 2 divisors.
            return 1;
            else return 0;

int main()
{
    int numb;

cout << "NUMBER\t" << "SUMSQ\t" << "SUMCB\t" << "PRIME\t" << "\n\n";

   

    for(numb=5; numb<=49; numb+=2){
        cout << numb;
        cout << "\t" << SumOfSquares(numb);
        cout << "\t" << SumOfCubes(numb);
            if(Prime(numb)==1)
        cout << "\t" << "*" << endl;}
    

    return 0;
}


Any idea what im doing wrong.
Last edited on
Line 61, put endl outside of the if block.

BTW, for keskiverto's gold star:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

using namespace std;

int main()
{
    int lastN = 0;
    int sumsq = 0;
    int sumcb = 0;
    int primes[50] = {0};
    int numPrimes = 2;
    bool isPrime;

    primes[0] = 2;
    primes[1] = 3;

    cout << "NUMBER\t\tSUMSQ\t\tSUMCB\t\tPRIME\n\n";

    for (int i = 5; i < 50; i += 2)
    {
        for (int j = lastN + 1; j <= i; ++j)
        {
            sumsq += j*j;
            sumcb += j*j*j;
        }
        lastN = i;
        cout << i << "\t\t" << sumsq << "\t\t" << sumcb;

        isPrime = true;

        for (int j = 0; j < numPrimes && isPrime; ++j)
            if (i % primes[j] == 0)
                isPrime = false;

        if (isPrime)
        {
            primes[numPrimes] = i;
            ++numPrimes;
            cout << "\t\t*";
        }
        cout << endl;
    }

    return 0;
}
Last edited on
Line 61, put endl outside of the if block.


Ah, I see the problem. I fixed it using an if-else construct. Thank you so much!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cmath>


using namespace std;

int SumOfSquares (int numb){

int sum=0;

    for(int i=1; i<=numb; i++)
    sum += i*i;
    return sum;

}
int SumOfCubes (int numb){

int sum=0;

    for(int i=1; i<=numb; i++)
    sum += i*i*i;
    return sum;

}

int numdiv (int numb){

int cnt=0;

  for(int i=1; i<=numb; i++)
    if(numb%i==0) // checks for divisors
       cnt++; // counts number of divisors

       return cnt;

}

int Prime (int numb){

int divisors;

divisors=numdiv(numb); // calls function numdiv

        if (divisors==2) // a number is prime if it has only 2 divisors.
            return 1;
            else return 0;

}

int main()
{
    int numb;


cout << "NUMBER\t" << "SUMSQ\t" << "SUMCB\t" << "PRIME\t" << "\n\n";



    for(numb=5; numb<=49; numb+=2){
        cout << numb;
        cout << "\t" << SumOfSquares(numb);
        cout << "\t" << SumOfCubes(numb);
            if (Prime(numb)==1)
        cout << "\t" << "  *" << endl;
        else cout << endl;}



    return 0;
}
Last edited on
Topic archived. No new replies allowed.