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;
}
Related
C++ code where a user enters 2 integers, then the program outputs how many numbers were multiples of 3 between those integers, including both numbers, and how many numbers were divisible by 5.
Here is my code. I think I am not calling the if statements correctly. Maybe I need a switch?
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int numb1, numb2;
int sentinel;
int counter = 0;
int mult3 = 0;
int mult5 = 0;
cout << "Enter an integer:";
cin >> numb1;
cout << "Enter another integer:";
cin >> numb2;
cout << endl;
sentinel = (abs(numb2-numb1)+1);
if(numb1 % 3 == 0 && counter <= sentinel) {
mult3++;
numb1++;
counter++;
}
else {
numb1++;
counter++;
}
cout << endl;
counter = 0;
if(numb1 % 5 == 0 && counter <= sentinel) {
mult5++;
numb1++;
}
else {
numb1++;
counter++;
}
cout << endl;
cout << mult3 << " " << "numbers are divisible by 3 in between your entered integers." << endl;
cout << mult5 << " " << "numbers are divisible by 5 in between your entered integers.";
cout << endl;
return 0;
}
A short version for your code is given below. Checkout the while loop(not ran, just from top of my head).
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int numb1, numb2;
int sentinel;
int counter = 0;
int mult3 = 0;
int mult5 = 0;
cout << "Enter an integer:";
cin >> numb1;
cout << "Enter another integer:";
cin >> numb2;
cout << endl;
while(numb1 <= numb2){
mult3 += (numb1%3)==0?1:0; // (numb1%3)?0:1
mult5 += (numb1%5)==0?1:0;
++counter;
++numb1;
}
cout << endl;
cout << mult3 << " " << "numbers are divisible by 3 in between your entered integers." << endl;
cout << mult5 << " " << "numbers are divisible by 5 in between your entered integers.";
cout << endl;
return 0;
}
You're not looping it at all, so this will only execute once.
while(counter < sentinel)
{
//run your tests and increment the appropriate variables if the numbers are divisible by 3 or 5
counter++;
}
start by evaluating the lowest of the 2 entered numbers + counter % 3 == 0
I got it. I had to use 3 separate while loops. One for <, one for >, and one for =. When I just had a <= and >=, the first loop would feed into the second. I took out the sentinel and the counter thanks to Avezan's help. Thanks everyone.
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;
}
I am creating a program to make a frequency table range from <=0 to >=10 of integer numbers from a text file. However when I run the program, if it contains number that <= 0 or >= 10 all other numbers are added up but the negative numbers is not counted. I think my problem lies in my If-statement but I dont know how to correct it. This is my code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream abc("beef.txt");
int num;
int tem;
int N;
int noNum = 0;
cout << "Class" << " | " << "Frequency" << endl;
cout << "_________|___________" << endl;
while (abc >> tem) {
noNum++;
}
for (num = 0; num < 11; num++) {
N = 0;
while (abc >> tem) {
if (num == tem) {
N = N + 1;
}
if (tem < 0) {
N = N + 1;
}
if (tem > 10) {
N = N + 1;
}
}
abc.clear(); //clear the buffer
abc.seekg(0, ios::beg); //reset the reading position to beginning
if (num == 0) {
cout << "<=0" << " | " << N << endl;
}
else if (num == 10) {
cout << ">=10" << " | " << N << endl;
}
else {
cout << " " << num << " | " << N << endl;
}
}
cout << "The number of number is: " << noNum << endl;
}
For example if there is -5 in the text file the program would run like this
The problem is twofold. First, you forgot two clear and reset the buffer after counting the number of elements. Second, you always count the numbers lower than zero and greater than 10. You should only do this when num is 0 or 10 respectively.
The correct code should look like this:
ifstream abc("beef.txt");
int num;
int tem;
int N;
int noNum = 0;
cout << "Class" << " | " << "Frequency" << endl;
cout << "_________|___________" << endl;
while (abc >> tem) {
noNum++;
}
for (num = 0; num < 11; num++) {
abc.clear(); //clear the buffer
abc.seekg(0, ios::beg); //reset the reading position to beginning
N = 0;
while (abc >> tem) {
if (num == tem) {
N = N + 1;
}
if (tem < 0 && num == 0) {
N = N + 1;
}
if (tem > 10 && num == 10) {
N = N + 1;
}
}
if (num == 0) {
cout << "<=0" << " | " << N << endl;
}
else if (num == 10) {
cout << ">=10" << " | " << N << endl;
}
else {
cout << " " << num << " | " << N << endl;
}
}
cout << "The number of number is: " << noNum << endl;
Hi here is my question.
Write a program that uses a do-while statement. It reads in an integer
n from the keyboard. If n is not in the range 1 to 10 it makes an audible
“beep” and asks for another integer. If n is in the correct range, it writes
out “You have input n” and then exits.
Here is my answer.
#include <iostream>
#include <Windows.h>
using namespace std;
void main()
{
int number = 0;
do
{
cout << "Enter an integer." << endl;
cin >> number;
if (!(number >= 1 && number <= 10))
{
Beep(400, 400);
}
}
while (!(number >= 1 && number <= 10));
cout << "You have input " << number << endl;
system("PAUSE");
}
You can see the line
(!(number >= 1 && number <= 10))
is repeated. Is there any workaround for this?
int GetNumber()
{
int number;
cout << "Enter an integer." << endl;
cin >> number;
return number;
}
void main()
{
int n = GetNumber();
while(n < 1 || n > 10)
{
Beep(400, 400);
n = GetNumber();
}
cout << "You have input " << n << endl;
system("PAUSE");
}
#include <iostream>
#include <Windows.h>
using namespace std;
void main()
{
int number = 0;
bool invalid_input = true;
do
{
cout << "Enter an integer." << endl;
cin >> number;
invalid_input = !(number >= 1 && number <= 10);
if (invalid_input)
{
Beep(400, 400);
}
}
while (invalid_input);
cout << "You have input " << number << endl;
system("PAUSE");
}
Perhaps...
int number = 0;
while (true)
{
cout << "Enter an integer." << endl;
if (cin >> number && number >= 1 && number <= 10)
break;
Beep(400, 400);
}
cout << "You have input " << number << endl;
system("PAUSE");
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;
}