Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

c++ programs


ghost's Avatar
0 0

Hey guys. i was just browsing around looking for some good practice programs and came across a few. Some of the following are ones im not sure how to finish. Ideas hints and code appreciated.

Problem 1: Perfect Numbers A positive integer is said to be a perfect number if it is equal to the sum of its positive divisors less than itself. For example, 28 is perfect, because

28 = 1 + 2 + 4 + 7 + 14 On the other hand, 12 is not perfect, because

12 != 1 + 2 + 3 + 4 + 6 You are to write a program that prompts the user to enter a positive integer and responds by reporting whether or not the given number is perfect. The program should terminate when the user enters 0.

Sample Program Execution:

Enter a positive integer: 12 12 IS NOT perfect.

Enter a positive integer: 28 28 IS perfect.

Enter a positive integer: 0

Problem 3: English Mathematical Parser

Develop a program that reads an arithmetic expression written in English, such as

NINE PLUS SEVEN and that outputs the number that is the result of evaluating the expression.

More precisely, the input will be a one-digit number, written as a word in upper case letters, followed by a space, followed by one of the words "PLUS", "MINUS", "TIMES", or "DIVIDED-BY", followed by a space, followed by another one-digit number, written as a word in upper case letters.

Here, "DIVIDED-BY" means integer division. (Example: 8 divided by 3 is 2.) The spellings for one-digit numbers are as follows: "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", and "NINE". You may assume that no input expression will call for division by zero. Your program should repeat until the user enters an expression that evaluates to zero.

Sample Program Execution:

Input: NINE PLUS FOUR Result: 13

Input: FIVE MINUS SEVEN Result: -2

Input: EIGHT DIVIDED-BY THREE Result: 2

Input: ZERO TIMES SIX Result: 0

Problem 6: Square Roots Develop a program that takes as input a positive integer and outputs an approximation to its square root. Your program may not include a call to any built-in root-finding subprogram that may be available in your programming language environment.

The value computed by your program is considered to be correct if it satisfies at least one of the following conditions:

It differs from the actual square root of the input by no more than 0.001. Its square differs from the input by no more than 0.001. The program should terminate when the user enters zero as input.

Sample Program Execution:

Enter an integer: 9 Square root is 3.0008

Enter an integer: 27 Square root is 5.196

Enter an integer: 0

**^ without including any math files?^

and lastly

**Problem 4: **Conjugates Two strings X and Y are said to be conjugates if, by shifting the symbols of Y in a cyclic fashion zero or more times, you can arrive at X. For example, the strings ababba and abbaab are conjugates because by shifting the symbols of the latter two positions to the right (or, equivalently, four positions to the left), you arrive at the former. On the other hand, ababba and abaabb are not conjugates, as you can easily verify. It is clear from the definition that two strings cannot be conjugates unless they have the same length.

You are to develop a program that prompts the user to enter two strings (the maximum length for a string is 20 characters) and that responds by reporting whether the two are conjugates. The program terminates when the user enters the empty string.

Sample Program Execution:

Enter 1st string: ababba Enter 2nd string: abbaab The two strings are conjugates.

Enter 1st string: ababba Enter 2nd string: abaabb The two strings are NOT conjugates.

Enter 1st string:

thanks again, -Tonz


ghost's Avatar
0 0

i dont know c++, but i do know Java and fron what i hear they are similer… if u want me to post a java version i will code one for u


ghost's Avatar
0 0

I've just done problem one but I'm not sure if you want the full code or not so I won't post it… if you do let me know… now to start the others :P


ghost's Avatar
0 0

For the first one…

  1. Find all the factors
  2. Push them in to an array
  3. Add up all the array's contents
  4. If the sum of the array's contents equals you number, it is perfect

Quick, badly indented code:


#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

bool IsPerfect(int number)
{
	int n = number - 1;
	int totalOfFactors = 0;

	std::vector<int> factors;

	while (n > 0)
	{
		if (number % n == 0)
		{
			factors.push_back(n);
		}

		--n;
	}

	for (std::vector<int>::iterator iter = factors.begin(); 
       iter != factors.end(); iter++)
	{
		totalOfFactors += *iter;
	}

	if (totalOfFactors == number) 
		return true;

	return false;
}

int GetNumber()
{
	int enteredNumber;

	cout << "Enter a positive integer: ";
	cin >> enteredNumber;

	if (enteredNumber > 0)
		return enteredNumber;

	else
		return GetNumber();
}

int main()
{
	int theNumber;

	while (1)
	{
		theNumber = GetNumber();

		if (theNumber == 0)
		{
			return 0;
		}
		else
		{
			ostringstream resultString;

			resultString << theNumber << " is " << 
(IsPerfect(theNumber) ? "IS " : "IS NOT ") << "perfect.";

			cout << resultString.str() << endl << endl;
		}
	}

	return 0;
}


ghost's Avatar
0 0

I did it with a simple:

	{
	    if ( num % i == 0 )
	    {
	        x += i;
	    }
	}```

Where num is the number you are testing and x is a random integer, originally 0... that way if x == num at the end of that loop the number is perfect

Edit: Also where did you find these? Seems like a good way to waste some time and I have 2 weeks off school in a few days

ghost's Avatar
0 0

thanks guys. im actually printing all this out. Xmas celery again i gotta say… i love ur name.

zeke thanks for the link bookmarked it.


ghost's Avatar
0 0

ChristmasCelery: Use of a vector there isn't necessary, it's more efficient to just make a temporary variable and increment it accordingly.

All those programs are basic and can be completed by reading a single useful book on C++.

~T


ghost's Avatar
0 0

@T-Metal: I know :/ That was me coding too quickly without thinking.

@OP: Thanks ;)