how do you exclude the SENTINEL value from the output in c++?
For a school project.
The SENTINEL value is skewing the output and adding it to the calculations. My professor was vague on how to exclude the SENTINEL from my calculations, but I am still confused as to how to exclude it.
My output for the largest value keeps equaling 99999 which is the SENTINEL value.
It is also adding the SENTINEL value to all the calculations.
#include "stdafx.h"
#include <iostream>
#include <climits>
using namespace std;
int SENTINEL = 99999;
int main()
{
int min = INT_MAX, max = INT_MIN, num = 0, counter = 0, pos = 0, neg = 0, sum = 0, num2 = 0, tot = 0;
int even = 0;
int odd = 0;
double total = 0;
while (num != SENTINEL)
{
cout << "Enter in a number" << endl;
cout << "Enter 99999 to exit" << endl;
cin >> num;
if (num > max)
max = num;
if (num < min)
min = num;
if ((num % 2) != 0)
{
odd++;
}
else if (num % 2 == 0)
{
even++;
}
if (num < 99999) {
neg++;
}
if (num < 99) {
num2++;
}
total += num;
tot += num;
counter++;
}
total = total / counter;
cout << "The smallest Number of the list is: " << min << endl;
cout << "The largest Number of the list is: " << max << endl;
cout << "The sum of all the numbers is: " << tot << endl;
cout << "The average of all numbers is: " << total << endl;
cout << "There are " << neg << " numbers" << endl;
cout << "There are " << even << " even numbers" << endl;
cout << "There are " << odd << " odd numbers" << endl;
cout << "There are " << num2 << " numbers smaller than 99" << endl;
return 0;
}
There are some changes you will have to make:
Read the number at least once before the while loop to ensure that the while
is not executed if number is 99999.
Move the reading of the number to the end of the while loop so that it can be checked right at the beginning before any further
processing.
Have a check to see if any numbers have to been entered to avoid dividing by zero in the statement total = total / counter;
The various statements must be output using cout only if there are any numbers in the list. Otherwise, it does not make much sense.
After the above changes, the code will look like this:
#include <iostream>
#include <climits>
using namespace std;
int SENTINEL = 99999;
int main()
{
int min = INT_MAX, max = INT_MIN, num = 0, counter = 0, pos = 0, neg = 0, sum = 0, num2 = 0, tot = 0;
int even = 0;
int odd = 0;
double total = 0;
cout << "Enter in a number" << endl;
cout << "Enter 99999 to exit" << endl;
cin >> num;
while (num != SENTINEL)
{
if (num > max)
max = num;
if (num < min)
min = num;
if ((num % 2) != 0)
{
odd++;
}
else if (num % 2 == 0)
{
even++;
}
if (num < 99999) {
neg++;
}
if (num < 99) {
num2++;
}
total += num;
tot += num;
counter++;
cout << "Enter in a number" << endl;
cout << "Enter 99999 to exit" << endl;
cin >> num;
}
if(counter != 0)
{
total = total / counter;
cout << "The smallest Number of the list is: " << min << endl;
cout << "The largest Number of the list is: " << max << endl;
cout << "The sum of all the numbers is: " << tot << endl;
cout << "The average of all numbers is: " << total << endl;
cout << "There are " << neg << " numbers" << endl;
cout << "There are " << even << " even numbers" << endl;
cout << "There are " << odd << " odd numbers" << endl;
cout << "There are " << num2 << " numbers smaller than 99" << endl;
}
else
{
cout << "No numbers were entered" << endl;
}
return 0;
}
Sample Output:
./a.exe
Enter in a number
Enter 99999 to exit
99999
No numbers were entered
./a.exe
Enter in a number
Enter 99999 to exit
34
Enter in a number
Enter 99999 to exit
23
Enter in a number
Enter 99999 to exit
12
Enter in a number
Enter 99999 to exit
99999
The smallest Number of the list is: 12
The largest Number of the list is: 34
The sum of all the numbers is: 69
The average of all numbers is: 23
There are 3 numbers
There are 2 even numbers
There are 1 odd numbers
There are 3 numbers smaller than 99
Related
Good afternoon,I was asked to make a function called void printStatistics(int n) which should print the average, sum, min and max number of n numbers passed in to it. for example if I did printStatistics(5) then I should ask the user to enter a number 5 times then print the stats.
All I managed to do was create a while loop that asked the user for a number until a negative number was entered, but I seem to have some trouble turning it into a function that asks the user to enter a number n times and then printing out the sum, average, min and max.This is what I have.
int main()
{
int value, sum;
int average, min, max;
int count;
sum = 0;
count = 0;
cout << "Enter a number: " << endl;
cin >> value;
min = value;
max = value;
while(value >= 0)
{
sum += value;
count++;
if(value > max)
max = value;
else if(value < min)
min = value;
cout << "Enter a numnber: " << endl;
cin >> value;
}
if(count == 0)
cout << "Nothing entered." << endl;
else
{
average = sum / count;
cout << "Average is " << average << endl;
cout << "Minimum is " << min << endl;
cout << "Maximum is " << max << endl;
cout << "Sum is " << sum << endl;
}
}
From what I understood from your question, you wish to have a function (printStatistics) that takes the parameter of 'int n' with 'n' corresponding to the amount of numbers the user wishes to enter. Then you ask for those specific numbers, then find the MIN, MAX, SUM, and AVERAGE of that set. If I understood this correctly, then this should do the trick:
#include <iostream>
void printStatistics(int n);
void printStatistics(int n)
{
int response;
int sum = 0;
int max;
int min;
for (int i = 1; i <= n; ++i)
{
std::cout << "Please enter the number in the " << i << " position: ";
std::cin >> response;
if (i == 1)
{
min = response;
max = response;
}
(min > response) ? min = response : min = min;
(max < response) ? max = response : max = max;
sum += response;
}
std::cout << "Sum: " << sum << std::endl;
std::cout << "Average: " << (float)sum / n << std::endl;
std::cout << "Min: " << min << std::endl;
std::cout << "Max: " << max << std::endl;
}
int main()
{
int numberCount;
do
{
std::cout << "How many numbers would you like to print? ";
std::cin >> numberCount;
}while(numberCount <= 0);
printStatistics(numberCount);
}
I am trying to make a program that reads five integers and performs maximum, minimum, average, sum of negative integers, and sum of positive integers. The current issue is that the average turns out the same as the sum of the positive integers, of which the sum is lower than what it should be. Example: I inserted 5, 5, 5, 5, 1 integers. 16 comes out as the average, and 16 comes as the sum of all the positive integers.
int main()
{
int min = 0, max = 0, num = 0, counter = 1, pos = 0, neg = 0;
double total = 0;
do {
cout << "Enter in a number: ";
cin >> num;
if (num > max)
max = num;
if (num < min)
min = num;
if (num < 0) {
neg += num;
}
if (num > 0) {
pos += num;
}
total += num;
counter++;
} while (counter <= 5);
total /= counter;
cout << "Smallest Number of the list is: " << min << endl;
cout << "Largest Number of the list is: " << max << endl;
cout << "The sum of negative numbers is: " << neg << endl;
cout << "The sum of positive numbers is: " << pos << endl;
cout << "The average of all numbers is: " << total << endl;
return 0;
}
I've updated the code for you. Learning to use a debugger is a worthwhile exercise, you can step through the code line by line to find the issue. It is a great way to learn the language and save yourself hours of misery.
#include <iostream>
#include <climits> // INT_MAX, INT_MIN
using namespace std;
int main()
{
//make min and max the opposite so the first number entered with replace both
int min = INT_MAX, max = INT_MIN, num = 0, counter = 0, pos = 0, neg = 0;
double total = 0;
do {
cout << "Enter in a number: ";
cin >> num;
if (num > max)
max = num;
if (num < min)
min = num;
if (num < 0) {
neg += num;
}
if (num > 0) {
pos += num;
}
total += num;
counter++;
} while (counter < 5); // go from 0 to 4, ending with counter == 5
total /= counter;
cout << "Smallest Number of the list is: " << min << endl;
cout << "Largest Number of the list is: " << max << endl;
cout << "The sum of negative numbers is: " << neg << endl;
cout << "The sum of positive numbers is: " << pos << endl;
cout << "The average of all numbers is: " << total << endl;
return 0;
}
The value of the variable counter equals 6 after the while loop, therefore the average is not correct. Either replace it with 5 or counter-1.
total /= 5;
Or,
total /= (counter-1);
You are using total as your average value:
Here you are summing up all your numbers: total += num;
Here you are printing the sum instead of an average:
cout << "The average of all numbers is: " << total << endl;
Instead, your print line should be:
cout << "The average of all numbers is: " << total /= counter << endl;
To elaborate, total /= counter will divide total by counter and set the new value to total.
I am a beginner in c++. I wrote a program to separate the digits in an integer entered and display them and their sum. However, when the loop repeats, the program hangs even though it compiled perfectly. I tried a '... while' loop and a while loop but the problem persists. What should I do to have it repeat (ask user for next integer, calculate and display the results) without problems? Any help will be appreciated.
//Preprocessor directives
#include <iostream>
#include <cmath>
//Standard library
using namespace std;
//enter function main
int main()
{
int num;
int digit;
int sum = 0;
int pwr = 0;
cout << "Enter an integer: " << endl;
cin >> num;
cout << endl;
while (num != 0 )
{
//while loop to ask user to enter another number
cout << "The integer you entered is: " << num << endl;
cout << "The digits of " << num << " are: " << endl;
if (num < 0)
num = -num;
//find the highest number of 10 that divides the number
while (num / static_cast<int>(pow(10.0, pwr)) >= 10)
pwr++;
while (num > 0)
{
digit = num / static_cast<int>(pow(10.0, pwr));
cout << digit << " ";
sum = sum + digit;
num = num % static_cast<int>(pow(10.0, pwr));
pwr--;
}
if (pwr != -1) //Either num is 0 or there are trailing zeros in num
while (pwr != -1)
{
cout << 0 << " ";
pwr--;
}
cout << endl;
cout << "The sum of the digits = " << sum << endl;
while (num != 0);
cout << "Enter another integer: " << endl;
cin >> num;
cout << endl;
}
return 0;
}
//Preprocessor directives
#include <iostream>
#include <cmath>
//Standard library
using namespace std;
//enter function main
int main()
{
while (true )
{
//reset initial values every loop
int num;
int digit;
int sum = 0;
int pwr = 0;
cout << "Enter an integer: " << endl;
cin >> num;
cout << endl;
//same exit condition
if (num == 0)
break;
//while loop to ask user to enter another number
cout << "The integer you entered is: " << num << endl;
cout << "The digits of " << num << " are: " << endl;
if (num < 0)
num = -num;
//find the highest number of 10 that divides the number
while (num / static_cast<int>(pow(10.0, pwr)) >= 10)
pwr++;
while (num > 0)
{
digit = num / static_cast<int>(pow(10.0, pwr));
cout << digit << " ";
sum = sum + digit;
num = num % static_cast<int>(pow(10.0, pwr));
pwr--;
}
if (pwr != -1) //Either num is 0 or there are trailing zeros in num
while (pwr != -1)
{
cout << 0 << " ";
pwr--;
}
cout << endl;
cout << "The sum of the digits = " << sum << endl;
//extraneous
/*while (num != 0);
cout << "Enter another integer: " << endl;
cin >> num;
cout << endl;*/
}
return 0;
}
I think you should use vector to store the digits... also the logic inside should be a bit smaller (see note 1), (note that I didn't test for negatives, you may use abs from cmath)
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
//Standard library
using namespace std;
//enter function main
int main()
{
int num;
std::vector<int> digits;
int sum = 0;
int pwr = 0;
cout << "Enter an integer: " << endl;
cin >> num;
cout << endl;
while (num) {
while (num) {
int temp = num % 10;
digits.push_back(temp);
sum += temp;
num /= 10;
}
std::reverse(digits.begin(), digits.end());
cout << sum << endl;
for(auto & a : digits)
{
cout << a << " ";
}
cout << endl;
cout << "Enter another integer: " << endl;
cin >> num;
cout << endl;
}
return 0;
}
note 1:
while (num) {
int temp = num % 10;
sum += temp;
num /= 10;
}
The program asks the users for numbers until the total of the numbers is greater than 30. Also, the user has to count how many numbers are even and how many are odd.
I can get the first part of the problem but i am having trouble with the counting part.
i.e.
Total is 0
Please enter an integer: 20
Total is 20
You had 1 even numbers and 0 odd numbers.
#include <iostream>
#include <string>
using namespace std;
int main (){
int integer;
int total = 20;
int even_count = 0;
int odd_count = 0;
cout << "Total is 0" << endl;
cout << "Please enter an integer: ";
cin >> integer;
cout << integer << endl;
while ( total <= 30){
cout << "Total is " << total << endl;
cout << "Please enter an integer: ";
cin >> integer;
cout << integer << endl;
total = integer + total;
}
if (integer % 2 == 0) {
even_count = even_count + 1;
}
if (integer % 2 != 0){
odd_count = odd_count + 1;
}
cout << "You had " << even_count << " even numbers and ";
cout << odd_count << " odd numbers.";
cout << endl;
return 0;
}
This code should work:
#include <iostream>
#include <string>
using namespace std;
int main (){
int integer=0;
int total = 0;
int even_count = 0;
int odd_count = 0;
cout << "Total is " << total << endl;
while ( total <=30 && integer <=30){
cout << "Please enter an integer: ";
cin >> integer;
total = integer + total;
if(total <=30){
cout << "Total is " << total << endl;
}
if (integer % 2 == 0) {
even_count = even_count + 1;
}
if (integer % 2 == 1){
odd_count = odd_count + 1;
}
}
cout << "You had " << even_count << " even numbers and " << odd_count <<
" odd numbers." << endl;
}
Here is a sample output:
Total is 0
Please enter an integer: 25
Total is 25
Please enter an integer: 5
Total is 30
Please enter an integer: 1
You had 0 even numbers and 3 odd numbers.
The program output should look like this:
Enter an even number: 23
The number is not a positive even number.
Enter an even number: -6
The number is not a positive even number.
Enter an even number: 4
20 20.25 20.50 20.75 21
The sum is 102.5
program doesn't run properly. the odd/ even numbers are identified, but the loop to increment the variables (20 + 1 / (even number entered)) does not work right.
#include <iostream>
int main(int argc, char *argv[])
{
float val, sum, incr;
int num;
cout << "Enter an even number: ";
cin >> num;
if (num % 2 != 0)
cout << "The number " << num << " is not a positive even number." << endl;
else
cout << num << " is even!" << endl << endl;
incr = 1 / num;
for (val = 20.0F; val <= 21.0; val += incr)
{
cout << val;
sum += val;
}
cout << "The sum is " << sum << endl;
return 0;
}
If you divide one integer 1 between another num the result is an integer that as chris said is 0.
You should do:
incr = 1.0F / (float)num;
And for exiting for wrong introduced values you should return from main
#include <iostream>
int main() {
float val, sum, incr;
int num;
cout << "Enter an even number: ";
cin >> num;
if (num < 0 || num % 2 != 0){
cout << "The number " << num << " is not a positive even number." << endl;
return -1;
}
else {
cout << num << " is even!" << endl << endl;
}
incr = 1.0 / num;
for (val = 20.0F; val <= 21.0; val += incr) {
cout << val << " ";
sum += val;
}
cout << "The sum is " << sum << endl;
return 0;
}