C- Function that compares two four digit number arrays - c

I'm trying to create a function that compares two four digit numbers and
returns the number of similar digits between the two. For example, with a generated number of 4311 and the user entered 1488,
the score should return 2 (4 and 1).
If it was 4311 and the other is 1147,
the score should return three (1, 1 and 4). I don't know why it isn't giving me the right outputs, hope you can help.
int getSameDigitScore(int playerGuess, int generatedNum) {
int score = 0;
int i;
int j;
int k;
int generatedNumArray[4];
int playerGuessArray[4];
// turns playerGuess into an array
while (playerGuess > 0 ) {
i = 0;
playerGuessArray[i] = playerGuess % 10;
i++;
playerGuess /= 10;
}
// turns generatedNum into an array
while (generatedNum > 0) {
i = 0;
generatedNumArray[i] = generatedNum % 10;
i++;
generatedNum /= 10;
}
// compares the two arrays
for (k = 3; k >= 0; k--) {
for (j = 3; j >= 0; j--) {
if (generatedNumArray[k] == playerGuessArray[j]) {
score++;
playerGuessArray[j] = 0;
j = -5;
}
}
}
return score;
}

You are assigning i = 0 inside the while loop while generating the playerGuessArray and generatedNumArray. Due to which the playerGuess and generatedNumArray array will have elements as first digit of your number 0 0 0 .
Move the initialization out of the loop.

int getSameDigitScore(int playerGuess, int generatedNum) {
int score = 0;
int i, j, k, n;
int generatedNumArray[4];
int playerGuessArray[4];
// turns playerGuess into an array
i = 0; // This has been out of while loop
while (playerGuess > 0 ) {
playerGuessArray[i] = playerGuess % 10;
i++;
playerGuess /= 10;
}
// turns generatedNum into an array
int n = 0; // This has been out of the while loop
while (generatedNum > 0) {
generatedNumArray[n] = generatedNum % 10;
n++;
generatedNum /= 10;
}
// compares the two arrays
for (k = 3; k >= 0; k--) {
for (j = 3; j >= 0; j--) {
if (generatedNumArray[k] == playerGuessArray[j]) {
score++;
playerGuessArray[j] = 0;
j = -5;
}
}
}
return score;
}
int main() {
int m;
n = getSameDigitScore(1231, 2342);
printf("Score is: %d\n", m);
}
You're re-initializing increment variable i on every iteration which should be moved out of the while loop. With that moved out the above code works fine.

There are the following issues with the code.
You are initializing the integer i inside the while loop. This needs to be done before the loop for each loop.
You need a separate array to get the output of equal digits. See AnswerArray in code below. Also it is a good design practice to pass this array to the function and clear this array inside the function.
In the last for loop, you should break from the inner loop after getting a match. This is to take care of cases where playerGuess == 1222 and generatedNum = 1111 In the code shown this will result in a score of 1.
See the final code below with some test cases.
int getSameDigitScore(int playerGuess, int generatedNum, int *AnswerArray) {
int score = 0;
int i;
int j;
int k;
int generatedNumArray[4] = {0};
int playerGuessArray[4] = {0};
memset(AnswerArray,0,4*sizeof(int));
// turns playerGuess into an array
i = 0;
while (playerGuess > 0 ) {
playerGuessArray[i] = playerGuess % 10;
i++;
playerGuess /= 10;
}
// turns generatedNum into an array
i = 0;
while (generatedNum > 0) {
generatedNumArray[i] = generatedNum % 10;
i++;
generatedNum /= 10;
}
// compares the two arrays
score=0;
for (k = 3; k >= 0; k--) {
for (j = 3; j >= 0; j--) {
if (generatedNumArray[k] == playerGuessArray[j]) {
AnswerArray[score++] = generatedNumArray[k];
playerGuessArray[j] = -1;
break;
}
}
}
return score;
}
int main(void)
{
int AnswerArray[4],score;
score = getSameDigitScore(4311,1488,AnswerArray);
printf ("\nScore = %d \n Answer Array = ",score);
for (int i=0; i<score; i++)
{
printf ("%d ",AnswerArray[i]);
}
score = getSameDigitScore(4311,1147,AnswerArray);
printf ("\nScore = %d \n Answer Array = ",score);
for (int i=0; i<score; i++)
{
printf ("%d ",AnswerArray[i]);
}
score = getSameDigitScore(1222,1111,AnswerArray);
printf ("\nScore = %d \n Answer Array = ",score);
for (int i=0; i<score; i++)
{
printf ("%d ",AnswerArray[i]);
}
score = getSameDigitScore(1111,1222,AnswerArray);
printf ("\nScore = %d \n Answer Array = ",score);
for (int i=0; i<score; i++)
{
printf ("%d ",AnswerArray[i]);
}
}

The initializing i=0 which you made inside the loop should be outside the loop.
while (playerGuess > 0 ) {
i = 0;
playerGuessArray[i] = playerGuess % 10;
i++;
playerGuess /= 10;
}
If the initialization is inside the looop then,
Everytime playerGuessArray[0] value will be updated.
FYI:
If playerGuess can contain 0 aat the begin of four digit like 0123
For example, playerGuessValue is 0123, Then by using
while (playerGuess > 0 ) {
i = 0;
playerGuessArray[i] = playerGuess % 10;
i++;
playerGuess /= 10;
}
playerGuessArray will contain only [1,2,3] instead of [0,1,2,3].
So, the better solution would be taking two temporary variables and checking last digit one by one.
Like this:
int temp1=playerGuess, temp2=GeneratedNum;
int i=0;
bool flag = true;
while(flag && i < 4){
if(temp1%10 != temp2%10){
flag = false;
}
temp1 /= 10;
temp2 /= 10;
i++;
}
if(flag){
score++;
}
FYI:
Debugging will help you in finding out these little mistakes.So, try to debug your code with multiple inputs and verify your answer.
Here are few reference on how to debug:
https://blog.hartleybrody.com/debugging-code-beginner/
https://www.codementor.io/mattgoldspink/how-to-debug-code-efficiently-and-effectively-du107u9jh%60
Thanks.

Related

ReArrange a number with ascending digits

I have to solve a problem where one of the important tasks is to reorder the digits of the input in ascending order and we are not allowed to use arrays and lists. I have no problem with that and my code works, but only if we do not consider leading 0, which we should in this problem. The only way I see how to do is to check digit by digit and then add then ordered by multiplying the number by 10 and adding the next digit. (1*10 = 10, 10+3= 13, we got 1 and 3 ordered) However, if we have a 0 in our number this method will not work because if I want to make 0123 with the * 10 method, I won't be able to have the 0 as the first digit never. Does anyone know how to solve this? My code is below:
int ascendingNumbers (int n) { //This function sorts the number on an ascending order
int number = n;
int sortedN = 0;
for (int i = 0; i <= 9; i++) {
int toSortNumber = number;
for (int x = 0; x <= 4; x++) {
int digit = toSortNumber % 10;
if (digit == i) {
if (digit == 0) {
sortedN==10;
}
sortedN *= 10;
sortedN += digit;
}
toSortNumber /= 10;
}
}
return sortedN;
}
Normally I don't do homework problems, but for especially awful ones I'll make an exception.
(Also I'm making an exception to my general rule not to have anything to do with these absurd "desert island" constraints, where you're stranded after a shipwreck and your C compiler's array functionality got damaged in the storm, or something.)
I assume you're allowed to call functions. In that case:
#include <stdio.h>
/* count the number of digits 'd' in 'n'. */
int countdigits(int n, int d)
{
int ret = 0;
/* do/while so consider "0" as "0", not nothing */
do {
if(n % 10 == d) ret++;
n /= 10;
} while(n > 0);
return ret;
}
int main()
{
int i, n;
printf("enter your number:\n");
scanf("%d", &n);
printf("digits: ");
for(i = 0; i < 10; i++) {
int n2 = countdigits(n, i);
int j;
for(j = 0; j < n2; j++) putchar('0' + i);
}
printf("\n");
}
This solution does not involve a function int ascendingNumbers() as you asked about. If you want to handle leading zeroes, as explained in the comments, you can't do it with a function that returns an int.
Your zero problem is solved, check it...
class Main {
public static void main(String[] args) {
int number = 24035217;
int n = number, count = 0;
int sortedN = 0;
while (n != 0) {
n = n / 10;
++count;
}
for (int i = 9; i >= 0; i--) {
int toSortNumber = number;
for (int x = 1; x <= count; x++) {
int digit = toSortNumber % 10;
// printf("\nBefore i = %d, x = %d, toSortNumber = %d, sortedN = %d, digit = %d",i,x,toSortNumber,sortedN,digit);
if (digit == i) {
sortedN *= 10;
sortedN += digit;
}
// printf("\nAfter i = %d, x = %d, toSortNumber = %d, sortedN = %d, digit = %d",i,x,toSortNumber,sortedN,digit);
toSortNumber /= 10;
}
}
System.out.print(sortedN);
}
}

Problems with printing output C

So I have to write a code for school. I did, but my outputs are not the way they asked for. This code gives me prime number between 2 different numbers. So i have to print those numbers in rows. But yeah there are getting zeros between the answers below you can see what I mean. How can I fix this?
#include <stdio.h>
int is_prime (int number)
{
int is_prime= 1, i;
if (number < 2)
{
is_prime = 0;
}
else
{
for(i = 2; (i * i) <= number; i++)
{
if ((number % i) == 0)
{
is_prime = 0;
break;
}
else
{
is_prime = 1;
}
}
}
return is_prime;
}
int main (void)
{
int lower_limit, upper_limit, i;
scanf("%d\n%d", &lower_limit, &upper_limit);
for(i = lower_limit; i <= upper_limit; i++)
{
if (is_prime (i))
{
printf("\n%d", i);
}
else
{
printf("\n%d", is_prime(i));
}
}
return 0;
}
Output
0
11
0
13
0
0
0
17
0
19
0
Reference
11
13
17
19
It's in this if block:
if (is_prime (i))
{
printf("\n%d", i);
}
else
{
printf("\n%d", is_prime(i));
}
What this says is "if the number is prime print it, otherwise print whether it is prime (which at this point you've established it's not)".
Just get rid of the else block.
If the number is prime number just print it. No else needed - even worse it is incorrect.
You can simplyfy the the is_prime function
int is_prime (int number)
{
int is_prime = number > 1, i;
for(i = 2; (i * i) <= number; i++)
{
if ((number % i) == 0)
{
is_prime = 0;
break;
}
}
return is_prime;
}
int main (void)
{
int lower_limit, upper_limit, i;
scanf("%d\n%d", &lower_limit, &upper_limit);
for(i = lower_limit; i <= upper_limit; i++)
{
if (is_prime (i))
{
printf("\n%d", i);
}
}
return 0;
}
https://godbolt.org/z/4d8qhx
Another problem: overflow.
Avoid int overflow in i*i, which is undeifned behavior (UB).
This can happen when number is a prime near INT_MAX.
// for(i = 2; (i * i) <= number; i++)
for(i = 2; i <= number/i; i++)
A good compiler will see the nearby number%i and number/i and emit efficient code for the two of them, thus not incurring an expensive 2nd operation.
The below also overflows when upper_limit == INT_MAX
for(i = lower_limit; i <= upper_limit; i++)
Perhaps
for(i = lower_limit; i - 1 < upper_limit; i++)
OK as long as lower_limit > INT_MIN.

Deleting an array by the number in it, C

int main() //8th task
{
int longNum, shortNum, tempNum[5], i;
printf("Please enter 2 numbers (5 digits and 1 digit, ex: 12345 and 5)\n");
scanf("%d%d", &longNum, &shortNum);
for (i = 4; i >= 0; i--)
{
if (longNum % 10 != shortNum)
{
tempNum[i] = longNum % 10;
longNum /= 10;
}
else tempNum[i] = ; // Delete the digit that == shortNum.
}
for (i = 0; i < 5; i++)
{
printf("%d", tempNum[i]);
}
printf("\n");
return 0;
}
This program check if longNum has shortNum in it and suppose to remove the number (and his array slot) from longNum.
I've tried couple of things to make it work with no success.
I'd like to know what is the best way to do it (im not sure what the 'else' should be).
It is possible to skip all shortNum digits in the parsing loop. One more variable is needed to track number of deleted digits:
int n = 5;
for (i = 4; i >= 0; i--)
{
int tmp = longNum % 10;
longNum /= 10;
if (tmp != shortNum)
tempNum[--n] = tmp;
}
// here n is number of deleted digits
for (i = n; i < 5; i++)
{
printf("%d", tempNum[i]);
}
So, actually elements are not deleted from array. They are not written to that array. It is also possible to reverse elements order, so the first array element will be meaningful. Now if some element is skipped the first element of tempNum contains junk.
you need to skip the value that you don't want, and not insert it at all to the array.
int len = 0;
for (i = 4; i >= 0; i--)
{
if (longNum % 10 != shortNum)
{
tempNum[len] = longNum % 10;
len++;
}
longNum /= 10;
}
for (i = 0; i < len; i++)
{
printf("%d", tempNum[i]);
}
else tempNum[i] = ; this part is very wrong. You have to assing something like else tempNum[i] = 0;. And you can't actually delete anything from these arrays - they are not dynamic. I suggest you read up on dynamic arrays.

Largest palindrome made from the product of two 3-digit numbers with C

The code is trying to find the largest palindrome made from the product of two 2-digit numbers. The answer is 91*99 = 9009 but I keep getting 990, which is not even a palindrome. I really appreciate the help!
#include <stdio.h>
int main()
{
int i = 10;
int j = 10;
int a = 0;
int b = 0;
int array[100] = {0};
int divider = 10;
int num;
int great;
int product;
int n;
int flag;
/*Loop through first 2 digit number and second 2 digit number*/
while (i<100)
{
while (j < 100)
{
product = i*j;
array [a] = product % 10;
n = product / divider;
while (n != 0)
{
a++;
num = n%10;
divider *=10;
array[a]=num;
n = product/divider;
}
flag = 0;
while (b<a)
{
if (array[b] != array[a])
{
flag = 1;
}
b++;
a--;
}
if (flag == 0)
{
great = product;
}
j++;
a = 0;
b = 0;
}
i++;
}
printf("The largest palindrome is %d \n", great);
return 0;
}
Here is a code snippet you can try.
#include <stdio.h>
void main()
{
int a = 1; // first integer
int b = 1; // second integer
int currentNumber;
int currentPalin; if a palindrome is found, its stored here
while (a<100){ //loop through the first number
while (b<100){ // loop through the second number
currentNumber = a*b;
if (currentNumber == reverse(currentNumber) ){ //check for palindrome
currentPalin = currentNumber;
}
b = b+1; //increment the second number
}
b = a; // you could have set b=1 but it would not be an efficient algorithm because
//some of the multiplication would occur twice. eg- (54*60) and (60*54)
a = a +1; //increment the first number
}
printf ("Largest palindrom is %d \n", currentPalin);
getchar();
}
// method for finding out reverse
int reverse(int n){
int reverse = 0;
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
// when you divide a number by 10, the
//remainder gives you the last digit. so you are reconstructing the
//digit from the last
n = n/10;
}
return reverse;
}
Update:- As suggested by M Oehm, I have modified the code to make it more general.
#include <stdio.h>
void main()
{
int a = 1;
int b = 1;
int currentNumber;
int currentPalin=0;
while (a<100){
while (b<100){
currentNumber = a*b;
if (currentNumber == reverse(currentNumber) ){
if (currentNumber>currentPalin){
currentPalin = currentNumber;
}
}
b = b+1;
}
b = 1;
a = a +1;
}
if (currentPalin==0){
printf("No Palindrome exits in this range");
}
else {
printf ("Largest palindrome is %d \n", currentPalin);
}
getchar();
}
int reverse(int n){
int reverse = 0;
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
return reverse;
}
An alternative approach to solve the problem.
#include<stdio.h>
int reverse(int num)
{
int result = 0;
while( num > 0)
{
result = result * 10 + (num%10);
num/=10;
}
return result;
}
int main()
{
int last_best = 1;
int best_i=1;
int best_j = 1;
const int max_value = 99;
for( int i = max_value ; i > 0 ; --i)
{
for(int j = i ; j > 0 ; --j){
int a = i * j;
if( last_best > a )
break;
else if ( a == reverse(a) )
{
last_best = a;
best_i = i;
best_j = j;
}
}
}
printf("%d and %d = %d\n", best_i,best_j,last_best);
}
And it is quite simple to follow.
It seems that you do not reinitialize variables at the beginning of loop. They keeps values from previous iterations. For example, j and divider. Put
j = 10;
before starting "j" loop, i.e.:
j = 10;
while (j < 100) ...
The same for divider:
...
j = 10;
while (j < 100) {
divider = 10;
...
If you were using for loops you would avoid this problem naturally:
for(i=10; i<100; i++) {
for(j=10; j<100; j++) {
...
}
}

adding leading zeros to decimal to binary array C

I am converting decimal to binary and I need my output to be 32 (bits?) long. This works as I intend, but I am not getting the leading zeros, for instance, the input "3" gives me "11", instead of "00000000000000000000000000000011"
int i = 0;
int bi[31];
while(num > 0){
if(num % 2 == 0)
bi[i] = 0;
else
bi[i] = 1;
i++;
num = num / 2;
}
for(int j = i - 1; j >= 0; j--){
printf("%d", bi[j]);
}
I originally thought this would be as simple as changing my printout to just loop down from 31 to 0 and print out all the contents of the array, assuming zeros would be in everything not in my bi[] array. But that does not work :)
Thanks
for(int j = 0; j < 32; j++) // this for loop is initializing all the places with zeroes
{
b[j] = 0;
}
i = 31 // starting from the leftmost place of the array
while(num > 0) //as the values in the array gets updated the remaining place is left with trailing
{
if(num % 2 == 0)
bi[i] = 0;
else
bi[i] = 1;
i--;
num = num / 2;
} zeroes
for(int j = 0; j < 32; j++){
printf("%d", bi[j]);
}
Due to the LIFO nature of this problem, consider a recursive (stack-based) solution:
#include <stdio.h>
#include <limits.h>
void recFoo(int num,int index)
{
if (index > 0)
recFoo(num/2, index-1);
printf("%d", num%2);
}
void foo(int num)
{
recFoo(num, sizeof(num)*CHAR_BIT);
}
The reson is because the first loop only loops twice, which is the highest set bit in the input. The first loop needs to loop unconditionally the number of bits you want, and so should the printing loop.
First of all you need a 32 long array. Try this one..... Initializing your array
void foo (int num) {
int i = 31;
int bi[32] = {0};
while(num > 0){
if(num % 2 == 0)
bi[i] = 0;
else
bi[i] = 1;
i--;
num = num / 2;
}
for(int j = i - 1; j >= 0; j--){
printf("%d", bi[j]);
}

Resources