The task is to write a function which takes a number and finds the digit that is repeated most times in that number. It should print the found digit and the times it is repeated.
I had a problem with the case when two digits were repeating a same number of times.
For example with given number 788995 it should return 8 -> 2 \\ 9 -> 2
How can I print that?
Here is the function:
void maxDigitInNumber (long long n)
{
if (n < MIN || n > MAX)
{
cout << -1;
return;
}
n = abs(n);
int numOfDigits = (int)log10(n)+1;
int digits[100];
int helper[100] = {0};
int counter = 0;
int maxSize = 0;
int number = 0;
for (int i = 0; i <= numOfDigits; i++)
{
digits[i] = n%10;
n /= 10;
}
for(int i = 0; i < numOfDigits; i++)
{
if(helper[i] == 0)
{
counter = 0;
for(int j = i; j < numOfDigits; j++)
{
if(digits[j] == digits[i])
{
counter++;
helper[j] = 1;
}
if(counter > maxSize)
{
maxSize = counter;
number = digits[i];
}
}
}
}
if (number == 0)
{
for (int i = 0; i < numOfDigits; i++)
{
cout << digits[i] << "->" << maxSize << endl;
}
}
else
{
cout << number << "->" << maxSize << endl;
}
}
You should store the count for each digit before picking the max. After that you can pick the max value among all counts, and print all entries matching that max:
int count[10] = {0};
do {
count[n%10]++;
n /= 10;
} while (n != 0);
int maxCount = 0;
for (int i = 0 ; i != 10 ; i++) {
maxCount = max(maxCount, count[i]);
}
bool first = true;
for (int i = 0 ; i != 10 ; i++) {
if (count[i] == maxCount) {
if (!first) {
cout << " \\\\ ";
} else {
first = false;
}
cout << i << "->" << maxCount;
}
}
There are only 10 digits, so an histogram of the digits in the number takes up only 10 words.
// ....
int hist[10] = {}; // Full tally available for further analysis
int max_count = 0; // result.
int max_digit = -1;
for (int i = 0; i <= numOfDigits; i++)
{
int digit = n % 10;
if (++hist[digit] > max_count)
{
max_count = hist[digit]; // could also be ++max_count ;)
max_digit = digit;
}
n /= 10;
}
Here are some algorithms you can use:
// prints digits with a certain score:
void print_if_score_is(const int hist[10], int score)
{
for (int i = 0; i < 10; ++i)
if (hist[i] == score)
std::cout << " digit: " << i << ", score: " << score << "\n";
}
int get_next_best_score(const int hist[10], int score)
{
int new_max = -1;
for (int i = 0; i < 10; ++i)
if (hist[i] > new_max && hist[i] < score)
new_max = i;
return new_max;
}
Usage:
// ....
std::cout << "Digit most frequently found: \n";
print_if_score_is(hist, max_count);
std:: cout << "next in list: \n";
int next_best = get_next_best_score(hist, max_count);
print_if_score_is(hist, next_best);
//...
Structure your program like this:
One function accepts the number to be analyzed and returns a std::multiset. multiset allows multiple entries for the same key. So for the number 788995 you would end up with a multiset { 1: [5, 7], 2: [8, 9] }
Another function analyzes the multiset and returns the numbers for the highest-ranking key in the set.
Related
I've come across this problem where I have to transform a number from the decimal system to the binary and compare if the numbers are equal
Example:
7 is becoming 111
The output is false
and where 5 is 101
The output is true
I've figured out how to transform the numbers with an array
But have no idea how to compare them
#include<iostream>
using namespace std;
int main()
{
int num;
cin >> num;
int arr[8] = {};
if ((num >= 0) && (num <= 255))
{
while (num != 0)
{
for (int i = 0; i < 8; i++)
{
if (num % 2 == 0)
arr[i] = 0;
else
{
arr[i] = 1;
}
num = num / 2;
}
}
for (int i = 7; i >= 0; i--)
cout << arr[i];
cout << endl;
}
else
{
cout << "error" << endl;
return 1;
}
system("pause");
return 0;
}
I want to convert a number into binary and store those binary value:
Example:
11 is 1011.. so I want to store those 2^3, 2^1, 2^1 numbers, so I can use those number. but my code doesn't store them right correctly. I got 8 4 2 for that instead of 8 2 1
Here is my code:
using namespace std;
int main() {
int num, arr[64];
int binary[10000];
cin >> num;
int expmax;
expmax = log2(num);
cout << expmax << endl;
int b = expmax;
int i = 0, r;
while (num != 0)
{
r = num % 2;
arr[i++] = r;
num /= 2;
}
for (int m = 0; m <= expmax ; m++)
{
cout << "array " << arr[m] << endl;
}
cout << endl;
int n = 0;
for (n++; n <= expmax; expmax--)
{
if (arr[n] = 1)
{
binary[n] = pow(2, expmax);
cout << binary[n] << endl;
}
else
{
binary[n] = pow(2, expmax)*0;
cout << binary[n] << endl;
}
}
cout << endl;
for (int j = i - 1; j >= 0; j--)
{
cout << arr[j];
}
cout << endl;
}
Your statement:
if (arr[n] = 1)
sets arr[n] to 1 and is always true (it evaluates to 1).
You want:
if (arr[n] == 1)
which is only true if arr[n] equals 1.
You are also bumping n to 1 at the start of the loop with:
int n = 0;
for (n++; n <= expmax; expmax--)
^^
Use this instead:
for (int n = 0; n <= expmax; ++n)
I have an assignment for school where I need to create a lottery program. It is supposed to allow the user to input six numbers and then generate six random numbers for comparison. I got the inputs working, but I have encountered a problem where the random number generator (located in the while loop) is stuck in an infinite loop, and I have absolutely no idea what is causing it since I have never had an infinite loop in any previous programs. If someone could please look through the code and possibly establish what is wrong, I would greatly appreciate it.
#include<iostream>
#include<time.h>
using namespace std;
void randomizeSeed();
int randomRange(int min, int max);
int getInteger();
int main()
{
randomizeSeed();
const int minNumber = 1;
const int maxNumber = 49;
const int Size = 6;
int luckyNumbers[6] = {};
int randomNumber = randomRange(minNumber, maxNumber);
int winningNumbers[6] = {};
cout << "Enter six numbers between 1 and 49...\n";
{
for (int i = 0; i < Size; i++)
{
luckyNumbers[i] = getInteger();
}
for (int i = 0; i < Size; i++)
{
for (int i = 0; i < Size - 1; i++)
{
if (luckyNumbers[i] > luckyNumbers[i + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[i + 1];
luckyNumbers[i + 1] = temp;
}
}
}
cout << "Lucky Numbers: ";
for (int i = 0; i < Size; i++)
{
cout << luckyNumbers[i] << " ";
}
cout << "\n";
cout << "Press any button to see the Winning Numbers.\n";
system("pause");
bool exist = true;
while (exist == true)
{
int count = 0;
cout << "Winning Numbers: ";
for (int j = 0; j < 6; j++)
{
winningNumbers[j] = randomRange(1, 49);
cout << winningNumbers[j] << " ";
system("pause");
}
}
}
}
void randomizeSeed()
{
srand(time(NULL));
}
int randomRange(int min, int max)
{
int randomValue = rand() % (max + 1 - min) + min;
return randomValue;
}
int getInteger()
{
int value = 0;
while (!(cin >> value) || (value >= 50) || (value <= 0))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return value;
}
for (int i = 0; i < Size; i++)
for (int i = 0; i < Size - 1; i++)
if (luckyNumbers[i] > luckyNumbers[i + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[i + 1];
luckyNumbers[i + 1] = temp;
}
You have two loops and they both use i. You probably mean to use the second loop with another variable name, for example:
for (int i = 0; i < Size; i++)
{
for (int k = 0; k < Size - 1; k++)
{
if (luckyNumbers[i] > luckyNumbers[k + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[k + 1];
luckyNumbers[k + 1] = temp;
}
}
}
If you set your compiler warning level to 4 then compiler should warn you about these errors. Try to resolve all compiler warnings.
I am supposed to compare two consecutive integers, i and j, that are given from a list of integers separated by whitespace which end with a 0 and, if i is less than j, I compare j to max and i to min. If the opposite, I compare j to min and i to max. The output is supposed to be each comparison I do with max, min, i, and j. Additionally, the list must be greater than 2 integers. If it is less then I am supposed to output 0. However the program does not seem to execute the if statements correctly.
int i = 1;
int j;
int max = 0;
int min = 0;
int counter = 0;
while (i != 0) {
cin >> i;
if (counter == 0) {
cout << 0 << endl;
i = min;
j = max;
} else if (counter == 1) {
cout << 0 << endl;
i = min;
j = max;
} else {
if (i < j) {
if (j > max) {
cout << j << " " << max << endl;
max = j;
}
if (i < min) {
cout << i << " " << min << endl;
min = i;
}
}
else {
if (j < min) {
cout << j << " " << min << endl;
min = j;
}
if (i > max) {
cout << i << " " << max << endl;
max = i;
}
}
}
j = i;
counter += 1;
}
}
You are overwriting i (and j) in the first iteration of the while loop (counter == 0) with min which is 0. As your while says while (i != 0) you immediately exit, after one while iteration. The ifs should be fine.
Input and counter logic can be improved by:
#include <iostream>
using namespace std;
int main(){
int i;
int j;
int max = 0;
int min = 0;
int counter = 0;
bool flag = true; // fix the input logic
while (flag){
cin >>i;
cin>>j;
if(i==0 || j==0){
flag = false;
break;
}else{
counter ++;
if(counter < 2 ){
//your code
}
else if(i<j){
cout<<"i: "<<i<<" j: "<<j;
j = max;
i = min;
// your code
}
else if(i>j){
//your code
}
}
}
return 0;
}
This is my first question so don't be mad at me, if I did something wrong.
I have to make a C++ program which returns an element from a selected row, for example:
Triangle 4 0 1 2 3
should return elements: 0, 1, 2 and 3 from row number 4, but it returns strange things, like:
Element 0: 1
Element 1: 10179988
Element 2: 50792126
Element 3: 91425820
I have no idea why
Here's my code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Pascal {
private:
int *tab;
public:
Pascal(int n) throw(string) {
if (n < 0)
throw (string)"";
tab = new int[n+1];
for(int i = 1; i <= n; i++) {
for(int k = i; k >=0; k--) {
if (k - 1 >= 0)
tab[k] += tab[k-1];
else
tab[k] = 1;
}
}
};
int element(int m) {
return tab[m];
}
};
int main(int argc, char* argv[]) {
int n = 0, m = 0, elem = 0;
try {
n = strtol(argv[1], NULL, 0);
Pascal *row;
for(int i = 2; i < argc; i++) {
try {
m = strtol(argv[i], NULL, 0);
row = new Pascal(n+1);
if (m <= n && m >= 0) {
elem = row->element(m);
cout << "Element " << m << ": "<< elem << endl;
} else
cout << m << " - bad element index" << endl;
} catch (string ex) {
cout << argv[i] << " - bad element index!" << endl;
continue;
}
delete[] row;
}
} catch (string e) {
cout << argv[1] << " - bad row index!" << endl;
return 0;
}
}
I'll be grateful for any answer
tab = new int[n+1];
for(int i = 1; i <= n; i++) {
for(int k = i; k >=0; k--) {
if (k - 1 >= 0)
tab[k] += tab[k-1];
else
tab[k] = 1;
}
}
first iteration: i=1, k=1, tab[1]+=tab[0];
second iteration: i=1, k=2, tab[2]+=tab[1];
So you are not properly initializing your array, you are simply adding whatever values are in memory...
I think replacing if (k - 1 >= 0) with if (k - 1 > 0) should solve your problem
Try
tab = new int[n+1];
for(int i = 0; i <= n; i++) {
tab[i] = 1;
for(int k = i; --k > 0; )
tab[k] += tab[k-1];
}