# three questions about hw assignment

• 09-09-2003
jlmac2001
I have to do three small programs that use loops. I know how to do the loops but am unsure about how to do the rest for exercises 1 and 2. I think that I can do exercise 3 fully on my own.

For the first part, I have to calculate the sum of the integer entered.

If the user typed in 698, how would I commute the sum? Would I declare sum= integer? (interger being the number entered).

Sample:
Please input an integer between 0 and 1000: 1001
* Number not in 0-1000 range. Please re-enter: -1
* Number not in 0-1000 range. Please re-enter: 456

Sum of the digits is 15

For the second one, the user has to enter 3 intergers and they have to be sorted from smallest to largest. I don't know how to get them sorted from smallest to largest.

Sample run:

Input integer 1 : 34
Input integer 2 : 200
Input integer 3 : -14

Sorted : -14 <= 34 <= 200
• 09-09-2003
axon
Try some code yourself and show us, and then we can help you even more. But you could start by reading the string of intergers into an array. Then you could use c-string operations to get your result for both parts...try it yourself!

axon
• 09-09-2003
Dante Shamest
Quote:

For the second one, the user has to enter 3 intergers and they have to be sorted from smallest to largest. I don't know how to get them sorted from smallest to largest.
1. Check two numbers first and obtain the smaller one. Let's call the result s.
2. Now compare s and the last remaining number to see which is smaller.
• 09-09-2003
joshdick
Re: three questions about hw assignment
Quote:

Originally posted by jlmac2001
For the first part, I have to calculate the sum of the integer entered.

If the user typed in 698, how would I commute the sum? Would I declare sum= integer? (interger being the number entered).

Sample:
Please input an integer between 0 and 1000: 1001
* Number not in 0-1000 range. Please re-enter: -1
* Number not in 0-1000 range. Please re-enter: 456

Sum of the digits is 15

For that one, suggest writing a function that will return a digit at a specified spot in an integer.
Code:

```int stripDigit(int num, int place); // if place == 1, return ones place digit // if place == 2, return tens place digit, etc. stripDigit(456, 2) == 5;```
That's how I'd do it, anyway.
• 09-09-2003
swoopy
For Exercise 1, here's one way:

456

Read this in as an int. Now you want to strip out each digit. If you divide by 10, and take the remainder, this will give you the first digit. Use the mod operator (%) to get the remainder:
456 % 10

Then divide by 10 to get 45, and repeat:
456 / 10

Each time you take the mod, add this to your sum (declared as an int).
• 09-09-2003
alphaoide
Read the input as string and read it character by character and take the sum at the same time

while COUNT < size of string
sum = sum + (int) string[COUNT]
COUNT++
• 09-10-2003
Hunter2
You could:
Code:

```int input; std::cin >> input; if(input > 1000 || input < 0)     doSomethingAboutIt(); char str[5]; //4 digits possible + 1 character for null 0 //break it into a string of characters representing the digits itoa(input, str, 10); //10 = the base that you're using int sum = 0; for(int i = 0; str[i] != '\0'; ++i) {     sum += (str[i] - '0'); // - '0' to get the actual numerical value } //now sum is the sum.```
I haven't tested it, but it just crossed my mind and I thought it might be an easy way to do it. ;)
• 09-11-2003
jlmac2001
need help
I started the two programs but am still unsure how to do the sum for the first one and how to sort the second one. I don't think we can use functions or strings because we haven't gotten that far in this class.

Here are the two programs that I wrote unfinished:

This one should read in a digit greater than0 but less than 1000. Then if the digit is valid, the sum of it should be calculated. I don't know how to do that part. Example: Enter a digit: 54
Sum is 9.

Code:

```#include <iostream> using std::cout; using std::cin; using std::endl; int main() { int integer; cout << "Please input an integer between 0 and 1000: "; cin >> integer; cout << " Number not in 0-1000 range. Please re-enter:  "; while( integer >= 0 || integer <= 1000) {  cout << "Please input an integer between 0 and 1000: ";  cin >> integer;    cout << " Number not in 0-1000 range. Please re-enter: <<  "; } return 0; }```

For this one, you have to put in three integers. Then, they have to be sorted from smallest to largest. example -9 < 98 <128. I don't know how to sort the numbers from largest to smallest. Can someone help me?
Code:

```#include <iostream> using std::cout; using std::cin; int main () { int num1, num2, num3; cout << "Enter integer 1 : "; cin >> num1; cout << "Enter integer 2 : "; cin >> num2; cout << "Enter integer 3 : " ; cin >> num3; return 0; }```
• 09-11-2003
Hunter2
Ok, some editing here:
Code:

```#include <iostream> using std::cout; using std::cin; using std::endl; int main() { int integer; cout << "Please input an integer between 0 and 1000: "; cin >> integer; //cout << " Number not in 0-1000 range. Please re-enter:  "; while( integer < 0 || integer > 1000) {  cout << " Number not in 0-1000 range. Please re-enter: <<  ";  cout << "Please input an integer between 0 and 1000: ";  cin >> integer; } return 0; }```
The changes I made are in red. This should get you to input a number in the range of 0 to 1000 inclusive. To get the sum, read the rest of the thread above.

Program 2:
If your class is at the stage that I think it is, then use a ..........load of if statements.