英文:
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 <stdio.h>
#include <stdlib.h>
int main(void) {
int size, i;
setbuf(stdout,NULL);
printf("Enter array limit: ");
scanf("%d",&size);
printf("Enter values: ");
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;
}
The result is:
36
3
36
1
答案1
得分: 0
你正在用美元符号的ASCII码替换数字。你可以选择以下两种方法之一:
-
保持数组不变,添加一个if语句,在打印时进行判断。如果是偶数,打印数字,否则打印美元符号($)。
-
创建一个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<size;i++){
printf("%d\n",arr[i]);
}
output elements storing the character constant `'$'` using the conversion specifier `%c`. For example
for ( i = 0; i < size; i++ ) {
printf( arr[i] == '$' ? "%c\n" : "%d\n", arr[i]);
}
Here is a demonstration program.
#include <stdio.h>
int main( void )
{
enum { N = 10 };
int arr[N] = { '$', 1, '$', 3, '$', 5, '$', 7, '$', 9 };
for (int i = 0; i < N; i++)
{
printf( arr[i] == '$' ? "%c\n" : "%d\n", 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 < N; i++)
{
printf( arr[i] == '$' ? "%c " : "%d ", arr[i] );
}
putchar( '\n' );
希望这对你有所帮助。
英文:
Just in this for loop
for(i=0;i<size;i++){
printf("%d\n",arr[i]);
}
output elements storing the character constant '$'
using the conversion specifier %c
. For example
for ( i = 0; i < size; i++ ) {
printf( arr[i] == '$' ? "%c\n", "%d\n", arr[i]);
}
Here is a demonstration program.
#include <stdio.h>
int main( void )
{
enum { N = 10 };
int arr[N] = { '$', 1, '$', 3, '$', 5, '$', 7, '$', 9 };
for (int i = 0; i < N; i++)
{
printf( arr[i] == '$' ? "%c\n" : "%d\n", 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 < N; i++)
{
printf( arr[i] == '$' ? "%c " : "%d ", arr[i] );
}
putchar( '\n' );
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论