•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,970 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,773 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1041 | Replies: 11
![]() |
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
The purpose of this assignment is to give practice programming in writing functions, developing algorithms and using
loop structures.
Positive integers can have the following personalities: prime or composite, happy or unhappy, square or not
square, smug or not smug, and honest or dishonest.
You will write a C++ program which will contain the following functions : isPrime, isHappy, isSquare, isSmug,
isHonest. All of the functions return boolean (i.e. true or false).
Note that, in numberPersonalities.cpp, all of methods currently return false. This done so that your program
can compile while you work on one method at time.
Personality Description
1. Prime and composite numbers
A positive number is prime if its only positive divisors are itself and one. For example, 7 is prime because it is
evenly divisible by 1 and 7, but not by any number in between (in particular, it is not evenly divisible by 2, 3, 4, 5,
or 6).
A positive number is composite if it is not prime. For example, 10 is composite because it is evenly divisible by 2
and 5; 12 is composite because it is evenly divisible by 2, 3, 4, and 6. As a special case, 1 is considered to be
composite.
2. Happy and unhappy numbers
Repeatedly apply the following procedure to a number:
1. Square each of the digits of the number.
2. Add the squares together.
If by doing this you eventually get to 1, then the number is happy.
For example, if you start with 13:
• 1*1 + 3*3 = 1 + 9 = 10
• 1*1 + 0*0 = 1 and the number is happy.
If instead you get into an infinite loop, the number is unhappy. So if your program runs forever, the number is
unhappy. This isn't a very useful test, however. Fortunately, every unhappy number will eventually get into this
cycle:
4, 16, 37, 58, 89, 145, 42, 20, 4, ...
so if you get any one of these numbers, then you can stop and conclude that the number is unhappy. Doing
this in your method is fairly simple because all of the above numbers in the cycle will give you 4. For this
reason, to check if a number is unhappy, just compare the sums of squares of its digits with 4. If you get 4,
then number is unhappy.
Programming note : You can get the last digit of a number n with the expression (n % 10). You can subsequently
discard this digit by doing an "integer division" by 10; when you get a zero, there are no more digits.
3. Square numbers
A square number is a number of the form 1 + 3 + 5 + 7 + ... + n, for some odd positive n. (The
figure should help you understand why this definition is the same as the usual definition of square
numbers.)
4. Smug numbers
A number is smug if it is the sum of two square numbers. The first few smug numbers are 2(1+1), 5 (1+4), 8
(4+4), 10 (1+9), 13 (4+9), etc.
5. Honest and dishonest numbers
A number is dishonest if it "pretends" to be a square number, but isn't one. Specifically, n is dishonest if there
is a number k such that n/k = k (using integer division), but k*k is not n. A number is honest if it is not dishonest.
Final Output
For each of the numbers 1 through 100, you will test the methods (functions) you developed and according to
the method return values, print out the numbers, one per line, along with a list of its characteristics (on the
same line).
Your output should look approximately like this:
1 composite, happy, square, not smug, honest
2 prime, unhappy, not square, smug, honest
3 prime, unhappy, not square, not smug, honest
4 composite, unhappy, square, not smug, honest
5 prime, unhappy, not square, smug, dishonest
...
Hence you need to also write a main method that will declare a variable as int limit = 100; and test the numbers 1 through
limit, inclusive. This makes it easy to change the limit later. Also test your program with two or three large numbers (say,
five or six digits), to make sure that your program can handle them.
Program skeleton
Your program must follow the structure given in the NumberPersonalities.cpp (that is, you need to fill in the blanks
ONLY ..
loop structures.
Positive integers can have the following personalities: prime or composite, happy or unhappy, square or not
square, smug or not smug, and honest or dishonest.
You will write a C++ program which will contain the following functions : isPrime, isHappy, isSquare, isSmug,
isHonest. All of the functions return boolean (i.e. true or false).
Note that, in numberPersonalities.cpp, all of methods currently return false. This done so that your program
can compile while you work on one method at time.
Personality Description
1. Prime and composite numbers
A positive number is prime if its only positive divisors are itself and one. For example, 7 is prime because it is
evenly divisible by 1 and 7, but not by any number in between (in particular, it is not evenly divisible by 2, 3, 4, 5,
or 6).
A positive number is composite if it is not prime. For example, 10 is composite because it is evenly divisible by 2
and 5; 12 is composite because it is evenly divisible by 2, 3, 4, and 6. As a special case, 1 is considered to be
composite.
2. Happy and unhappy numbers
Repeatedly apply the following procedure to a number:
1. Square each of the digits of the number.
2. Add the squares together.
If by doing this you eventually get to 1, then the number is happy.
For example, if you start with 13:
• 1*1 + 3*3 = 1 + 9 = 10
• 1*1 + 0*0 = 1 and the number is happy.
If instead you get into an infinite loop, the number is unhappy. So if your program runs forever, the number is
unhappy. This isn't a very useful test, however. Fortunately, every unhappy number will eventually get into this
cycle:
4, 16, 37, 58, 89, 145, 42, 20, 4, ...
so if you get any one of these numbers, then you can stop and conclude that the number is unhappy. Doing
this in your method is fairly simple because all of the above numbers in the cycle will give you 4. For this
reason, to check if a number is unhappy, just compare the sums of squares of its digits with 4. If you get 4,
then number is unhappy.
Programming note : You can get the last digit of a number n with the expression (n % 10). You can subsequently
discard this digit by doing an "integer division" by 10; when you get a zero, there are no more digits.
3. Square numbers
A square number is a number of the form 1 + 3 + 5 + 7 + ... + n, for some odd positive n. (The
figure should help you understand why this definition is the same as the usual definition of square
numbers.)
4. Smug numbers
A number is smug if it is the sum of two square numbers. The first few smug numbers are 2(1+1), 5 (1+4), 8
(4+4), 10 (1+9), 13 (4+9), etc.
5. Honest and dishonest numbers
A number is dishonest if it "pretends" to be a square number, but isn't one. Specifically, n is dishonest if there
is a number k such that n/k = k (using integer division), but k*k is not n. A number is honest if it is not dishonest.
Final Output
For each of the numbers 1 through 100, you will test the methods (functions) you developed and according to
the method return values, print out the numbers, one per line, along with a list of its characteristics (on the
same line).
Your output should look approximately like this:
1 composite, happy, square, not smug, honest
2 prime, unhappy, not square, smug, honest
3 prime, unhappy, not square, not smug, honest
4 composite, unhappy, square, not smug, honest
5 prime, unhappy, not square, smug, dishonest
...
Hence you need to also write a main method that will declare a variable as int limit = 100; and test the numbers 1 through
limit, inclusive. This makes it easy to change the limit later. Also test your program with two or three large numbers (say,
five or six digits), to make sure that your program can handle them.
Program skeleton
Your program must follow the structure given in the NumberPersonalities.cpp (that is, you need to fill in the blanks
ONLY ..
/*-------------------------------------------------
* CSE1101 Introduction to Programming Homework 2
* Author:
* Date:
*
*--------------------------------------------------*/
#include <iostream>
using namespace std;
//This method checks if the number (var) is a prime or composite.
//If number is prime then it returns true (i.e.prime), else false (i.e. composite)
bool isPrime(int var) {
//Fill in the code
return false;
}//end of isPrime
//This method checks if the number (var) is a happy.
//If number is happy then it returns true, else false
bool isHappy(int var) {
//Fill in the code
return false;
}//end of isHappy
//This method checks if the number (var) is square.
//If number is square then it returns true, else false
bool isSquare(int var) {
//Fill in the code
return false;
}//end of isSquare
//This method checks if the number (var) is smug.
//If number is smug then it returns true, else false.
bool isSmug(int var) {
//Fill in the code
return false;
}//end of isSmug
//This method checks if the number (var) is honest.
//If number is honest then it returns true, else false.
bool isHonest(int var) {
//Fill in the code
return false;
}//end of isHonest
int main() {
/*
* Final Output - Sample
*
1 composite, happy, square, not smug, honest
2 prime, unhappy, not square, smug, honest
3 prime, unhappy, not square, not smug, honest
4 composite, unhappy, square, not smug, honest
5 prime, unhappy, not square, smug, dishonest
...
*
*/
//Fill in code
} Last edited by Ancient Dragon : Nov 24th, 2007 at 5:20 pm. Reason: add code tags
So you have demonstrated you can copy and paste your assignments. Is there any other hidden talents you have?
... the hat of 'is this a cat in a hat?'
Whether or not he can help you doesn't matter. The fact is, he probably could help you.
What is also a fact is that you've shown nothing which makes you deserving of help.
A much better post would be "Here is my prime number code but it keeps showing 9 up as prime, and obviously it isn't". You've shown effort, and asked a specific question. This would get our attention.
For example, you've made no attempt at all to fill in any of the "Fill in code" sections of your assignment. In other words, this is just the boiler-plate code your tutor has handed out to everyone, which you're supposed to complete.
http://www.daniweb.com/forums/announcement8-2.html
What code there is is an unformatted mess.
http://www.daniweb.com/forums/announcement8-3.html
There are many places where you're reminded to use code tags, and you've decided to ignore all of them. What does that say about you and how much regard for the forum you might have? Are we just a bunch of geeks to be used and discarded once you've got your free ticket and you can get back to your oh-so-important partying and social life?
Oh, and putting a date on the topic is no different to saying "URGENT".
http://www.catb.org/~esr/faqs/smart-...ns.html#urgent
What is also a fact is that you've shown nothing which makes you deserving of help.
A much better post would be "Here is my prime number code but it keeps showing 9 up as prime, and obviously it isn't". You've shown effort, and asked a specific question. This would get our attention.
For example, you've made no attempt at all to fill in any of the "Fill in code" sections of your assignment. In other words, this is just the boiler-plate code your tutor has handed out to everyone, which you're supposed to complete.
http://www.daniweb.com/forums/announcement8-2.html
What code there is is an unformatted mess.
http://www.daniweb.com/forums/announcement8-3.html
There are many places where you're reminded to use code tags, and you've decided to ignore all of them. What does that say about you and how much regard for the forum you might have? Are we just a bunch of geeks to be used and discarded once you've got your free ticket and you can get back to your oh-so-important partying and social life?
Oh, and putting a date on the topic is no different to saying "URGENT".
http://www.catb.org/~esr/faqs/smart-...ns.html#urgent
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
Whether or not he can help you doesn't matter. The fact is, he probably could help you.
What is also a fact is that you've shown nothing which makes you deserving of help.
A much better post would be "Here is my prime number code but it keeps showing 9 up as prime, and obviously it isn't". You've shown effort, and asked a specific question. This would get our attention.
For example, you've made no attempt at all to fill in any of the "Fill in code" sections of your assignment. In other words, this is just the boiler-plate code your tutor has handed out to everyone, which you're supposed to complete.
http://www.daniweb.com/forums/announcement8-2.html
What code there is is an unformatted mess.
http://www.daniweb.com/forums/announcement8-3.html
There are many places where you're reminded to use code tags, and you've decided to ignore all of them. What does that say about you and how much regard for the forum you might have? Are we just a bunch of geeks to be used and discarded once you've got your free ticket and you can get back to your oh-so-important partying and social life?
Oh, and putting a date on the topic is no different to saying "URGENT".
http://www.catb.org/~esr/faqs/smart-...ns.html#urgent
i did only part of prime number but i haven't got any idea about the other parts you can say only how can i do other parts.. or you can't say.. it's your choice
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation:
Rep Power: 40
Solved Threads: 972
•
•
•
•
i did only part of prime number but i haven't got any idea about the other parts you can say only how can i do other parts.. or you can't say.. it's your choice
You're missing the point -- we are not mind readers so we have no idea what you want. Posting your assignment is not asking for help. To get the help you want you have to ask specific questions. My guess is that your are getting overwhelmed with such a large assignment. Take it only a little bit at a time and soon you will have it done yourself.
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
i think if someone make a part of this i can make other parts but anyone don't say anything about how can i do... maybe someone make happy number or anything else i can do others..therefore please help me to make any part..
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation:
Rep Power: 40
Solved Threads: 972
What have YOU done to write any of the functions? For example, isPrime? There are hundreds of programs posted here that show you how to do it so there is no point is us repeating ourselves for you. All you have to do is enter prime in the search engine and you will get lots of hits.
Last edited by Ancient Dragon : Nov 25th, 2007 at 8:44 am.
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
i know... i did it thanks to you...but if you say something about the how can i do others maybe i can do
> maybe someone make happy number or anything else i can do others
So if I pick say isSmug(), then you're saying you can already do that right?
Tell me, how did you get this far into your course? This can't be the first homework assignment of the class right? Look back at how you solved(*) your previous homework assignments and draw on those experiences to at least try to solve one of the questions in front of you now.
> 2. Happy and unhappy numbers
> Repeatedly apply the following procedure to a number:
> 1. Square each of the digits of the number.
> 2. Add the squares together.
OK, how about beginning with a short bit of code which extracts each digit from a number?
Do you think you can apply /10 and %10 in a loop to do that?
(*) that is assuming you did your own homework up to now. If you've been wandering from board to board for free homework, it's now painfully obvious that you don't have a clue and deserve to fail spectacularly.
So if I pick say isSmug(), then you're saying you can already do that right?
Tell me, how did you get this far into your course? This can't be the first homework assignment of the class right? Look back at how you solved(*) your previous homework assignments and draw on those experiences to at least try to solve one of the questions in front of you now.
> 2. Happy and unhappy numbers
> Repeatedly apply the following procedure to a number:
> 1. Square each of the digits of the number.
> 2. Add the squares together.
OK, how about beginning with a short bit of code which extracts each digit from a number?
Do you think you can apply /10 and %10 in a loop to do that?
(*) that is assuming you did your own homework up to now. If you've been wandering from board to board for free homework, it's now painfully obvious that you don't have a clue and deserve to fail spectacularly.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Personal tuition wanted VB & SQL Server (Software Development Job Offers)
- Error when tring to run program PLEasE Help (C++)
- i need to add function for add, subtract, multiply and division. (C++)
- urgent..i need help.. (C++)
- have a try if you are interested in... (C++)
Other Threads in the C++ Forum
- Previous Thread: c++ handle help?
- Next Thread: need help writing function


Linear Mode