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:
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 linefor each odd number in [5,49]. Each line will print different values.
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?
#include <iostream>
#include <cmath>
#include <cstring>
usingnamespace 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.
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 constchar *. 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).
#include <iostream>
#include <cmath>
usingnamespace 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;
elsereturn 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.