Kindly pardon my english.
I am taking an introductory course to C and I have some problems with the logic of my program. It will only sometimes produce the required output.
The task is to write a function that takes the elements of the array, return the largest even digit in the element.
int ary1[] = {123, 456, -7890, 12};
Would return -7890
as the largest value with 8
as the largest even digit and its occurrences.int ary2[5] = {-123, 654, 78, 15, 189};
Would return 189
as the largest value with 8
as the largest even digit and its occurrences.int ary3[2] = {9, 9};
Would not return anything.int ary4[] = {123, 123, 0, 12};
Would return 123
as the largest value with 2
as the largest even digit and its occurrences.int ary5[] = {24, 45, -789, 24, 1};
Would return -789
as the largest value with 8
as the largest even digit and its occurrences.int ary6[] = {-749, -241, 1, 45};
Would return 45
as the largest value with 4
as the largest even digit and its occurrences.
Here is my code:
#include <stdio.h>
void digitExtract(int[], int);
int main() {
int ary1[] = { 123, 456, -7890, 12 };
int ary2[5] = { -123, 654, 78, 15, 189 };
int ary3[2] = { 9, 9 };
int ary4[] = { 123, 123, 0, 12 };
int ary5[] = { 24, 45, -789, 24, 1 };
int ary6[] = { -749, -241, 1, 45 };
int ary7[] = { 1, 3, 5 };
printf("\nCalling function in ary1[]:\n");
digitExtract(ary1, 4);
printf("\nCalling function in ary2[]:\n");
digitExtract(ary2, 5);
printf("\nCalling function in ary3[]:\n");
digitExtract(ary3, 2);
printf("\nCalling function in ary4[]:\n");
digitExtract(ary4, 4);
printf("\nCalling function in ary5[]:\n");
digitExtract(ary5, 5);
printf("\nCalling function in ary6[]:\n");
digitExtract(ary6, 4);
printf("\nCalling function in ary7[]:\n");
digitExtract(ary7, 3);
}
void digitExtract(int Array[], int array_size) {
int tempValue;
int x;
int myArr[10] = { 0 };
int evenCount = 0;
int max = Array[0];
for (int i = 1; i < array_size; i++)
{
if (Array[i] < 0) {
Array[i] = -Array[i];
if (Array[i] > max) {
max = Array[i];
}
}
}
tempValue = (max < 0) ? -max : max;
do {
myArr[tempValue % 10]++;
tempValue /= 10;
} while (tempValue != 0);
for (x = 8; x > 0; x -= 2) {
if (myArr[x]>0) {
printf("Displaying from inside of function():\n");
printf("\nThe largest even digit: %d\n", x );
printf("\nThe digit %d occurs %d times.\n", x, myArr[x]);
evenCount++;
break;
}
} if (evenCount == 0)
printf("\nNo even digits found!\n\n");
}
I know there is an error in my logic as for ary2[]
it will produce the even digit of 6
and it's occurrences when it should be 8
, but I don't know where.
The function will work for an array that has odd values as it's element.
Where or what I am doing wrong?
Thank you.
To find logical errors, read through your program line by line and try to relate what each line is doing to the behavior you're seeing in your project. The LilyPad does exactly what your program tells it to do. Your job is to find the mismatch between what you want it to do and what the program is telling it to do.
Logic errors occur when there is a fault in the logic or structure of the problem. Logic errors do not usually cause a program to crash. However, logic errors can cause a program to produce unexpected results.
Logic errors A logic error is an error in the way a program works. The program can run but does not do what it is expected to do. Logic errors can be caused by the programmer: incorrectly using logical operators, eg expecting a program to stop when the value of a variable reaches 5, but using <5 instead of <=5.
Your task is to find the largest even digit, then find the largest value that has that digit... not find the largest value, then find the largest even digit within it.
I would start by writing a function named max_even_digit
to operate on a single int
, then verify that and work from there.
int max_even_digit(int x) {
int max = 0;
while (x) {
int digit = x % 10;
digit = digit < 0 ? -digit : digit;
if (x % 2 == 0 && digit > max) {
max = digit;
}
x /= 10;
}
return max;
}
Once you've done this, loop over your array as though you'd be finding the maximum value, but let the return value of max_even_digit
take precedence over the actual value.
int max_even_digit_value(int *array, size_t size) {
if (size == 0) {
return 0;
}
int max_value = array[0],
max_digit = max_even_digit(max_value);
while (--size) {
int value = array[size],
digit = max_even_digit(value);
if (digit > max_digit || (digit == max_digit && value > max_value)) {
max_value = value;
max_digit = digit;
}
}
return max_value;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With