Program debugging - c++

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;
}

Related

how to exclude the SENTINEL value from the output in c++?

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

Program hangs and does not loop

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;
}

Count how many even or odd numbers there in the program in C++

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.

Separating integer into digits, printing in reverse, and printing the sum of the digits C++

My problem is that I'm having to take a 5-digit integer input given by the user, and separate the integer into its individual digits. I then need to print those digits in reverse order and also print out the sum of those digits. Here is what I have coded so far to separate the 5-digit integer into its individual digits. Please note that I am limited to using integer division and modulo operators for separating the integer into its digits.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a five-digit number: ";
cin >> number;
cout << number / 10000 << " ";
number = number % 10000;
cout << number / 1000 << " ";
number = number % 1000;
cout << number / 100 << " ";
number = number % 100;
cout << number / 10 << " ";
number = number % 10;
cout << number << endl;
return 0;
}
For example, when the user inputs a 5-digit number like 77602, the program should output it as
7 7 6 0 2 and the sum of the digits is 22.
How do I go about printing this in reverse order as well as the sum of the individual digits?
Edit: Few spelling and grammatical errors.
It's a lot easier to reverse a string than an integer. It's also a lot easier to accumulate individual digits of a string containing a number than an integer. Try this:
#include <iostream>
#include <string>
#include <algorithm>
int main(int argc, char *argv[])
{
std::string number;
int accum = 0;
std::cout << "Enter a five digit number: ";
std::cin >> number;
if (number.length() != 5)
{
std::cout << std::endl << "I asked for five digits!" << std::endl;
return 0;
}
for (int i = 0; i < 5; i++)
{
if (number.at(i) < '0' || number.at(i) > '9')
{
std::cout << std::endl << "Non-integer string entered" << std::endl;
return 0;
}
accum += (number.at(i) - '0');
}
std::reverse(number.begin(), number.end());
std::cout << "Reversed: " << number << std::endl << "Sum of digits: " << accum << std::endl;
return 0;
}
This is probably easiest solution for you to understand:
#include <iostream>
using namespace std;
int main()
{
int number;
int number2;
int numberReverse;
int sum = 0;
cout << "Enter a five-digit number: ";
cin >> number;
numberReverse = number;
cout << number / 10000 << " ";
sum = sum + number/10000;
number = number % 10000;
cout << number / 1000 << " ";
sum = sum + number/1000;
number = number % 1000;
cout << number / 100 << " ";
sum = sum + number/100;
number = number % 100;
cout << number / 10 << " ";
sum = sum+number/10;
number = number % 10;
cout << number << endl;
sum = sum+number;
cout << "Reverse: " << endl;
number2 = numberReverse%10;
cout << number2 << " ";
number2 = (numberReverse/10)%10;
cout << number2 << " ";
number2 = (numberReverse/100)%10;
cout << number2 << " ";
number2 = (numberReverse/1000)%10;
cout << number2 << " ";
number2 = (numberReverse/10000)%10;
cout << number2 << endl;
cout << "Sum is " << sum;
return 0;
}
A simple solution using the same logic and tools of your code..
int sum = 0;
for(int i = 0; i < 5; ++i)
{
int digit = number % 10;
sum += digit;
cout << digit << " ";
number /= 10;
}
cout << "\nSum of digits: " << sum << "\n";
I think, you need to do something like this:
int number;
... some IO operations ...
std::vector<int> digits;
do {
digits.push_back(number % 10);
number /= 10;
} while (number);
After this all you need to do is 2-3 loops through vector digits
for (std::size_t k = digits.size() - 1; k >= 0; --k)
std::cout << digits[k] << " ";
std::cout << std::endl;
for (std::size_t k = 0; k < digits.size(); ++k)
std::cout << digits[k] << " ";
std::cout << std::endl;
int sum = 0;
for (std::size_t k = 0; k < digits.size(); ++k)
sum += digits[k];
std::cout << sum;

find digits occurence in an integer

I guys I have a problem to create a function to count the occurrence of a number in my integers.
I create 2 int, int n which contains value from 0 to 9. Where the int value is a number that can be 1 digit up to 9 digit.
I have to create a function countOccurence to count how many times each digit occurs in the value that I put in. For example, if I type "12345", then 1 2 3 4 5 occurs once, while 6 7 8 9 0 occurs zero times. I try it but I just got stuck.
Here is what I come up so far, I just cant figure it out.
#include <iostream>
using namespace std;
int main()
{
int countOccurance(int, int);
int findDig(int);
int value;
int n = 0;
cout << "Please enter a positive number: " << endl;
cin >> value;
cout << "The value is " << value << endl;
while ((value < 0) || (value > 999999999))
{
cout << "Invalid value. Please try again!" << endl;
cout << "Please enter a positive number: " << endl;
}
//process the value
}
int countOccurance(int findDig, int value)
{
}
thank you for your help, I really appreciate it
Something along these lines should give you what you're looking for.
int findDig(int n)
{
if (n < 10)
return 1;
else
return 1 + findDig(n / 10);
}
int countOccurance(int value)
{
for(int i = 0 ; i < 10 ; i++)
{
int val = value;
int count = 0;
while(val > 0)
{
if(val % 10 == i)
count ++;
val /= 10;
}
cout << count << " occourences of " << i << endl;
}
}
int main()
{
int value;
int n = 0;
cout << "Please enter a positive number: " << endl;
cin >> value;
cout << "The value is " << value << endl;
while ((value < 0) || (value > 999999999))
{
cout << "Invalid value. Please try again!" << endl;
cout << "Please enter a positive number: " << endl;
cin >> value //you need this here, otherwise you're going to be stuck in an infinite loop after the first invalid entry
}
//process the value
cout << endl;
cout << "This " << value << " number is a " << findDig(value) << " digit integer number." << endl;
cout << "Digit counts" << endl;
countOccourance(value);
}
edit
If you need countOccourence to return a value, this should do it.
int countOccourence(int dig, int val)
{
if(n > 0)
{
return countOccourence(dig, val / 10) + (val % 10 == dig);
}
else
return 0;
}