Write a function remove_digits that receives two arrays of type int. The first array contains a number of integers, and the second array is an array of digits. It is necessary to remove all digits from second array which are present in first array.
The function returns 1 if the ejection was successful.The function returns 1 if the eject was successful, and 0 if the array of digits is incorrect for some reason, if the array contains a value less than 0 or greater than 9, or if one of the members is repeated.
EXAMPLE 1:
int first[2]={12345, -12345},second[2]={3,5};
OUTPUT: 124 -124
EXAMPLE 2:
int first[5]={25, 235, 1235, 252, 22552255},second[3]={2,3,5};
OUTPUT: 0 0 1 0 0
My algorithm:
- check if digit in second array is less than 0 or grater than 9 or digit is repeated, and in that case return 0 (finish program)
- for negative numbers make them positive and in the end of first (for) loop make them negative
- in the second (while) loop break number into digits, and for every number check if it's present in second array
- if it is present, remove last digit
- continue to the rest of elements
Code:
#include <stdio.h>
#include <stdlib.h>
int sum_of_digits(int n) {
int i, sum = 0;
while (n > 0) {
sum ;
n /= 10;
}
return sum;
}
int divide(int n) {
int num_of_digits = sum_of_digits(n);
switch (num_of_digits) {
case 1:
break;
case 2:
break;
case 3:
n /= 10;
break;
case 4:
n /= 100;
break;
case 5:
n /= 1000;
break;
case 6:
n /= 1000;
break;
case 7:
n /= 10000;
break;
case 8:
n /= 100000;
break;
case 9:
n /= 1000000;
default:
break;
}
return n;
}
int remove_digits(int *first, int n, int *second, int vel) {
// first - removing digits from second
// second - searching for digits
int i, j, num, digit, neg = 0;
for (i = 0; i < vel; i ) {
// invalid digit
if (second[i] < 0 || second[i] > 9)
return 0;
for (j = i 1; j < vel; j )
// repeated digit
if (second[j] == second[i])
return 0;
}
for (i = 0; i < n; i ) {
// negative case
if (first[i] < 0) {
first[i] = abs(first[i]);
neg = 1;
}
num = first[i];
while (num > 0) {
digit = num % 10;
for (j = 0; j < vel; j )
if (second[j] == digit)
// remove last digit
first[i] = divide(first[i]) - digit;
num /= 10;
}
if (first[i] <= 0)
first[i] = 0;
if (neg == 1)
first[i] *= -1;
}
return 1;
}
int main() {
int first[2] = {12345, 12345}, second[2] = {3, 5}, i;
remove_digits(first, 2, second, 2);
for (i = 0; i < 2; i )
printf("%d ", first[i]);
return 0;
}
MY OUTPUT: 4 4
Could you help me to modify my algorithm to work correctly?
CodePudding user response:
simplified approach for your problem would be as follows,
#include <stdio.h>
#include <stdlib.h>
int removeDigit(int src, int digit){
int neg = (src < 0)?-1:1;
int num = abs(src);
src = 0;
//remove digit
while(num){
int num_digit = num;
if(num_digit != digit){
src = src * 10 num_digit;
}
num /= 10;
}
//reverse number
while(src){
num = num * 10 src;
src /=10;
}
return num*neg;
}
int remove_digits(int *first, int n, int *second, int m) {
// first - removing digits from second
// second - searching for digits
int i, j;
for (i = 0; i < m; i ) {
// invalid digit
if (second[i] < 0 || second[i] > 9)
return 0;
for (j = i 1; j < m; j )
// repeated digit
if (second[j] == second[i])
return 0;
}
for (i = 0; i < n; i) {
for(j =0; j<m; j){
first[i]= removeDigit(first[i],second[j]);
}
}
return 1;
}
int main() {
{
printf("Test 1\n");
int first[] = {12345, 12345}, second[] = {3, 5}, i;
remove_digits(first, sizeof(first)/sizeof(first[0]), second, sizeof(second)/sizeof(second[0]));
for (i = 0; i < sizeof(first)/sizeof(first[0]); i )
printf("%d ", first[i]);
}
{
printf("\n\nTest 2\n");
int first[] = {25, 235, 1235, 252, 22552255}, second[] = {2,3,5}, i;
remove_digits(first, sizeof(first)/sizeof(first[0]), second, sizeof(second)/sizeof(second[0]));
for (i = 0; i < sizeof(first)/sizeof(first[0]); i )
printf("%d ", first[i]);
}
return 0;
}