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

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

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

问题

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

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(void) {
  4. int size, i;
  5. setbuf(stdout, NULL);
  6. printf("输入数组大小:");
  7. scanf("%d", &size);
  8. printf("输入数值:");
  9. int arr[size];
  10. for (i = 0; i < size; i++) {
  11. scanf("%d", &arr[i]);
  12. if (arr[i] % 2 == 0) {
  13. arr[i] = '$';
  14. }
  15. }
  16. for (i = 0; i < size; i++) {
  17. printf("%d\n", arr[i]);
  18. }
  19. return 0;
  20. }

结果是:

  1. 36
  2. 3
  3. 36
  4. 1
英文:

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

  1. #include &lt;stdio.h&gt;
  2. #include &lt;stdlib.h&gt;
  3. int main(void) {
  4. int size, i;
  5. setbuf(stdout,NULL);
  6. printf(&quot;Enter array limit: &quot;);
  7. scanf(&quot;%d&quot;,&amp;size);
  8. printf(&quot;Enter values: &quot;);
  9. int arr[size];
  10. for(i=0;i&lt;size;i++){
  11. scanf(&quot;%d&quot;,&amp;arr[i]);
  12. if(arr[i]%2==0){
  13. arr[i]=&#39;$&#39;;
  14. }
  15. }
  16. for(i=0;i&lt;size;i++){
  17. printf(&quot;%d\n&quot;,arr[i]);
  18. }
  19. return 0;
  20. }

The result is:

  1. 36
  2. 3
  3. 36
  4. 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

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

  1. Just in this for loop
  2. for(i=0;i&lt;size;i++){
  3. printf(&quot;%d\n&quot;,arr[i]);
  4. }
  5. output elements storing the character constant `&#39;$&#39;` using the conversion specifier `%c`. For example
  6. for ( i = 0; i &lt; size; i++ ) {
  7. printf( arr[i] == &#39;$&#39; ? &quot;%c\n&quot; : &quot;%d\n&quot;, arr[i]);
  8. }
  9. Here is a demonstration program.
  10. #include &lt;stdio.h&gt;
  11. int main( void )
  12. {
  13. enum { N = 10 };
  14. int arr[N] = { &#39;$&#39;, 1, &#39;$&#39;, 3, &#39;$&#39;, 5, &#39;$&#39;, 7, &#39;$&#39;, 9 };
  15. for (int i = 0; i &lt; N; i++)
  16. {
  17. printf( arr[i] == &#39;$&#39; ? &quot;%c\n&quot; : &quot;%d\n&quot;, arr[i] );
  18. }
  19. }
  20. The program output is
  21. 1
  22. $
  23. 3
  24. $
  25. 5
  26. $
  27. 7
  28. $
  29. 9
  30. Or to output elements of the array in a line like
  31. $ 1 $ 3 $ 5 $ 7 $ 9
  32. you can use the following for loop
  33. for (int i = 0; i &lt; N; i++)
  34. {
  35. printf( arr[i] == &#39;$&#39; ? &quot;%c &quot; : &quot;%d &quot;, arr[i] );
  36. }
  37. putchar( &#39;\n&#39; );

希望这对你有所帮助。

英文:

Just in this for loop

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

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

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

Here is a demonstration program.

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

The program output is

  1. 1
  2. $
  3. 3
  4. $
  5. 5
  6. $
  7. 7
  8. $
  9. 9

Or to output elements of the array in a line like

  1. $ 1 $ 3 $ 5 $ 7 $ 9

you can use the following for loop

  1. for (int i = 0; i &lt; N; i++)
  2. {
  3. printf( arr[i] == &#39;$&#39; ? &quot;%c &quot; : &quot;%d &quot;, arr[i] );
  4. }
  5. 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:

确定