我需要编写一个减去数字的函数。 如果用户输入2345,则输出应为111(5-4、4-3、3-2);另一个示例是683,其中输出应为25(3-8(采用绝对值),8-6)。
我编写了以下代码,仅在声明数组的大小时有效。
int subtraction(int arr[], int size) {
int sub = 0;
for (int i = 0; i < size-1; i++) {
sub = sub * 10 + abs(arr[i] - arr[i+1]);
}
return sub;
}
但是,用户输入的数字是随机的,可以有不同的数字,所以我不知道在for循环中应设置什么限制。
例如:
int arr[] = {1, 2, 55, 56, 65, 135}, i;
subtraction(arr, 6);
for (i=0; i<6; i++)
printf("%d ", arr[i]);
预期输出:0 0 0 1 1 22
该功能应该从最后一位中减去倒数第二位,顺便说一句,/从右至左/从用户输入的随机数中减去;例如,如果输入为5789,则假定输出为211(9-8、8-7、7-5);如果用户输入一个负数,程序应将其取为绝对值,然后进行减法。如果用户输入的是一位数字,则结果应为0。
我编写的函数仅在声明数组大小时才有效。我不知道如何在未声明大小的情况下使它工作(我相信指针和malloc是必需的,因为这是我通过谷歌搜索了很长时间才发现的,但是不幸的是,我不知道该怎么做)
请帮助?
答案 0 :(得分:0)
您实际上并没有更改任何值,这是您需要查看的行。
sub = sub * 10 + abs(arr[i] - arr[i+1]);
在打印数组时,实际上需要将计算出的值再次存储在数组中。
答案 1 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
int subtract(int n)
{
int factor = 1;
int total = 0;
int lastPlace = n%10;
n /= 10;
while (n>0)
{
total += (factor * abs((n%10) - lastPlace));
factor *= 10;
lastPlace = n%10;
n /= 10;
}
return total;
}
void subtractArray(int* arr, unsigned int size)
{
for (int i=0; i<size; ++i)
{
if (arr[i] < 0)
arr[i] = abs(arr[i]);
arr[i] = subtract(arr[i]);
}
}
int main()
{
int arr[] = {1, 2, 55, 56, 65, 135};
int size = sizeof(arr)/ sizeof(arr[0]);
subtractArray(arr, size);
for (int i=0; i<size; ++i)
{
printf("%d ", arr[i]);
}
return 0;
}
答案 2 :(得分:0)
这是解决您问题的简单代码:)
"noImplicitAny": "false"
{1、2、55、56、65、135}的输出:
#include <stdio.h>
#include <stdlib.h>
int *subtraction(int arr[], int size)
{
int *sub = calloc(sizeof(int*) , size), i = 0, rev; //allocating memory
for (i = 0; i < size; i++)
{
rev = 0;
arr[i] = abs(arr[i]);
for (int a = 0; arr[i] != 0; arr[i] /= 10)
rev = (rev * 10) + (arr[i] % 10);
for (i; (rev / 10) != 0; rev /= 10) //the loop ends when rev = 0
sub[i] = ((sub[i] * 10) + abs( (rev % 10) - ((rev / 10) % 10) )); //easy math => for example rev = 21 > sub[i] = (0 * 10) + ( (21 % 10) - ((21 / 10) %10)) = abs(1 - 2) = 1;
}
return sub;
}
int main()
{
int arr[] = {-9533, 7, -19173}, i;
int len = sizeof(arr)/sizeof(arr[0]); //size of arr
int *sub = subtraction(arr, len);
for(int i = 0; i < len; i++) //for test
printf("%d ", sub[i]);
return 0;
}
{987654321、123456789、111111111}的输出:
0 0 0 1 1 22
{38279}的输出:
11111111 11111111 0
{-9533,7,-19173}的输出:
5652
答案 3 :(得分:0)
关于大小不确定的数组。您可能想要的是一个动态分配的数组。
在这里,我们当然可以根据用户输入获得数组元素的数量。
首先,我们将使用fgets()
从用户那里获得数字,这将给我们一个字符串,然后我们将使用strtol()
将数字部分转换为标量(int)。您可以根据需要使用scanf("%d", &n)
。
然后我们可以从该数字开始计算数字,该值将成为数组元素的数量。
#include <stdio.h>
#include <stdlib.h> //for strtol(), malloc() and NULL guaranteed
//you may also want to add
#include <limits.h>
#include <errno.h>
#define MAX_STRLEN 12 // can hold all digits of INT_MAX plus '\0' and a posible, AND very likely, '\n'
#define DEC 10 // for strtol base argument
/*
* I'm lending you my old get_n_dits() function that I used to count decimal digits.
*/
int get_n_dits(int dnum) {
unsigned char stop_flag = 0; //we'll use to signal we're done with the loop.
int num_dits = 1, dpos_mult = 1; //num_dits start initialized as 1, cause we're pretty sure that we're getting a number with at least one digit
//dpos_mult stands for digital position multiplier.
int check_zresult; //we'll check if integer division yields zero.
/**
* Here we'll iterate everytime (dnum / dpost_mult) results in a non-zero value, we don't care for the remainder though, at least for this use.
* every iteration elevates dpost_mult to the next power of ten and every iteration yielding a non-zero result increments n_dits, once we get
* the zero result, we increment stop_flag, thus the loop condition is no longer true and we break from the loop.
*/
while(!stop_flag) {
dpos_mult *= 10;
check_zresult = dnum / dpos_mult;
(check_zresult) ? num_dits++ : stop_flag++;
}
return num_dits;
}
int main(void) {
int num, ndits; //we'll still using int as per your code. you can check against INT_MAX if you want (defined in limits.h)
int *num_array = NULL; //let's not unintentionally play with an unitialized pointer.
char *num_str = malloc(MAX_STRLEN); //or malloc(sizeof(char) * MAX_STRLEN); if there's any indication that (sizeof(char) != 1)
printf("please enter a number... please be reasonable... or ELSE!\n");
printf(">>> ");
if(!fgets(num_str, MAX_STRLEN, stdin)) {
fprintf(stderr, "Error while reading from STDIN stream.\n");
return -1;
}
num = (int)strtol(num_str, NULL, DEC); //convert the string from user input to scalar.
if(!num) {
fprintf(stderr, "Error: no number found on input.\n");
return -1;
}
ndits = get_n_dits(num);
if(ndits <= 0) {
fprintf(stderr, "Aw, crap!\n");
return -1;
}
printf("number of digits: %d\n", ndits);
num_array = malloc(sizeof(int) * ndits); //now we have our dynamically allocated array.
return 0;
}