I can't figure out this assignment.

Hello, I'm trying to write a C++ program to read integers until 0 is entered using sentinel. I want the before 0 integers to be read but the 0 not to be and the program to display the results before ending the program. I've been trying to do this for 3 days with no luck besides getting someone to help with the sentinel.

(1) Display the sum of the two-digit numbers (both positive and negative)
(2) Display the largest of the positive integers
(3) Display the smallest of the negative integers
(4) Display how many numbers are divisible by 7

I have to submit this by 11:59 pm CST tonight and can't fucking figure it out so I was hoping one of y'all could save the day. All I have is the sentinel and then a simple smallest, largest, and sum. However, I need to change those to fit the assignment requirements. Thanks, guys.

Code:

#include <iostream>
#include <limits>
using namespace std;
const int MinInt = std::numeric_limits<int>::min();
const int MaxInt = std::numeric_limits<int>::max();
int main()
{
int number, sum = 0, smallest = MaxInt, largest = MinInt;
while (cin >> number)
{
if(number == 0)
break;
sum += number;
if (number > largest)
largest = number;
if (number < smallest)
smallest = number;
}
cout << "Smallest: " << smallest << "\n";
cout << "Largest: " << largest << "\n";
cout << "Sum: " << sum << "\n";
}
Last edited on
First, posting code in code tags makes it more readable. See https://www.cplusplus.com/articles/jEywvCM9/

How does the thing that you have now differ from what is required? Looks quite close to 1,2,3.

Do you know when a number is divisible by 7?
You're actually very close to having this done. You just need to add some conditions and add #4.

Currently, you always add to the sum. You're only supposed to do that if it's a 2-digit number. A number has 2 digits if it's greater than 9 and less than 100, or it's greater than -100 and less than -9.

For 2 and 3, you're supposed to display the largest positive number and the smallest negative number. Right now you're displaying the largest and smallest of all the numbers.

First, recognize that the "smallest number" will be the same as "the smallest negative number" if there are any negative numbers. The same is true for the largest number.

But what should you display if there are no negative numbers? Re-read the assignment to see if it addresses this condition.

I'd get the first 3 things working first and then take care of #4.
Here's some test input and the result I get:
Input:
1
3
7
10
14
99
100
-101
-100
-99
-10
-9
-8
0

Output:
Sum of 2-digit numbers: 14
Largest positive: 100
Smallest negative: -101
Number divisible by 7: 2

How would I go about making a different message appear when no positive integers or negative integers are entered, instead of it showing a really large or small number? This is what I've got 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
#include <iostream>
#include <limits>
using namespace std;
const int MinInt = numeric_limits<int>::min();
const int MaxInt = numeric_limits<int>::max();
int main()
{
	int number, sum = 0, smallest = MaxInt, largest = MinInt;
	cout << "Enter integers and when done enter 0: ";
	while (cin >> number)
	{
		if (number == 0)
			break;
		if (number > 9 && number < 100 or number > -100 and number < -9)
			sum += number;
		if (number > largest and number > -1)
			largest = number;
		if (number < smallest and number < 1)
			smallest = number;
	}
	cout << "Smallest of the negative integers entered: " << smallest << "\n";
	cout << "Largest of the positive integers entered: " << largest << "\n";
	cout << "Sum of all double digit integers: " << sum << "\n";
} 


Input:
1
2
3
4
5
1
2
3
4
0


Output:
1
2
3
Smallest of the negative integers entered: 2147483647
Largest of the positive integers entered: 4
Sum of all double digit integers: 0
Last edited on
Topic archived. No new replies allowed.