Write a program to replace all even numbers in an array with $ and print the array.

huangapple go评论56阅读模式
英文:

Write a program to replace all even numbers in an array with $ and print the array

问题

这是我的代码。但它不起作用。

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int size, i;

    setbuf(stdout, NULL);
    printf("输入数组大小:");
    scanf("%d", &size);
    printf("输入数值:");

    int arr[size];

    for (i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
        if (arr[i] % 2 == 0) {
            arr[i] = '$';
        }
    }

    for (i = 0; i < size; i++) {
        printf("%d\n", arr[i]);
    }
    return 0;
}

结果是:

36
3
36
1
英文:

This is my code. And it doesn't work.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

int main(void) {
	int size, i;

	setbuf(stdout,NULL);
	printf(&quot;Enter array limit: &quot;);
	scanf(&quot;%d&quot;,&amp;size);
	printf(&quot;Enter values: &quot;);

	int arr[size];

	for(i=0;i&lt;size;i++){
		scanf(&quot;%d&quot;,&amp;arr[i]);
	    if(arr[i]%2==0){
			arr[i]=&#39;$&#39;;
		}
	}

	for(i=0;i&lt;size;i++){
		printf(&quot;%d\n&quot;,arr[i]);
	}
	return 0;
}

The result is:

36
3
36
1

答案1

得分: 0

你正在用美元符号的ASCII码替换数字。你可以选择以下两种方法之一:

  1. 保持数组不变,添加一个if语句,在打印时进行判断。如果是偶数,打印数字,否则打印美元符号($)。

  2. 创建一个char*数组。为数组的每个元素分配20个字符的空间。使用sprintf在每个元素中打印数字或美元符号($),然后循环打印新数组的每个元素。

无论哪种方法,都不能将'$'存储在int数组中,然后确定它是一个字符。它只是一个数字。

英文:

You are replacing the number with the ascii code of the dollar character. You can do one of two things:

Leave the array alone and add an if statement where you print. If even, print the number, else print $

Create an array of char*. Allocate 20 chars for each element if the array. Use sprintf to print the number or $ in each element. Loop and print each element of the new array.

Anycase, you can't store '$' in the int array and then determine that It's a character. It is just a number.

答案2

得分: 0

仅翻译代码部分,如下所示:

Just in this for loop

    for(i=0;i&lt;size;i++){
        printf(&quot;%d\n&quot;,arr[i]);
    }

output elements storing the character constant `&#39;$&#39;` using the conversion specifier `%c`. For example

    for ( i = 0; i &lt; size; i++ ) {
        printf( arr[i] == &#39;$&#39; ? &quot;%c\n&quot; : &quot;%d\n&quot;, arr[i]);
    }

Here is a demonstration program.

    #include &lt;stdio.h&gt;

    int main( void )
	{
		enum { N = 10 };
		int arr[N] = { &#39;$&#39;, 1, &#39;$&#39;, 3, &#39;$&#39;, 5, &#39;$&#39;, 7, &#39;$&#39;, 9 };

		for (int i = 0; i &lt; N; i++)
		{
			printf( arr[i] == &#39;$&#39; ? &quot;%c\n&quot; : &quot;%d\n&quot;, arr[i] );
		}
	}

The program output is

    1
    $
    3
    $
    5
    $
    7
    $
    9

Or to output elements of the array in a line like

    $ 1 $ 3 $ 5 $ 7 $ 9

you can use the following for loop

    for (int i = 0; i &lt; N; i++)
    {
    	printf( arr[i] == &#39;$&#39; ? &quot;%c &quot; : &quot;%d &quot;, arr[i] );
    }
    putchar( &#39;\n&#39; );

希望这对你有所帮助。

英文:

Just in this for loop

for(i=0;i&lt;size;i++){
printf(&quot;%d\n&quot;,arr[i]);
}

output elements storing the character constant &#39;$&#39; using the conversion specifier %c. For example

for ( i = 0; i &lt; size; i++ ) {
printf( arr[i] == &#39;$&#39; ? &quot;%c\n&quot;, &quot;%d\n&quot;, arr[i]);
}

Here is a demonstration program.

#include &lt;stdio.h&gt;
int main( void )
{
enum { N = 10 };
int arr[N] = { &#39;$&#39;, 1, &#39;$&#39;, 3, &#39;$&#39;, 5, &#39;$&#39;, 7, &#39;$&#39;, 9 };
for (int i = 0; i &lt; N; i++)
{
printf( arr[i] == &#39;$&#39; ? &quot;%c\n&quot; : &quot;%d\n&quot;, arr[i] );
}
}

The program output is

1
$
3
$
5
$
7
$
9

Or to output elements of the array in a line like

$ 1 $ 3 $ 5 $ 7 $ 9

you can use the following for loop

for (int i = 0; i &lt; N; i++)
{
printf( arr[i] == &#39;$&#39; ? &quot;%c &quot; : &quot;%d &quot;, arr[i] );
}
putchar( &#39;\n&#39; );

huangapple
  • 本文由 发表于 2023年2月6日 14:50:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定