I am trying to insert an integer if array position is equal to 3
but unfortunately when I am printing my array a
I am getting absured results like this:
我试图插入一个整数,如果数组位置等于3但不幸的是,当我打印我的数组时,我得到了这样的结果:
0
0
2
whereas, the result expected is:
而预期的结果是:
0
0
128
0
0
My code is:
我的代码是:
#include<stdio.h>
int substitute_and_sum(int *a, int len)
{
int sum = 0, i;
for (i = 0; i < 5; i++)
{
if (i == 3)
{
a[i] = 128;
}
}
printf("%d\n", *a);
return 0;
}
int main()
{
int arr[] = { 0, 0, 2, 0, 0 };
int j = 5, i;
for (i = 0; i < 5; i++)
{
substitute_and_sum(&arr[i], j);
}
}
5 个解决方案
#1
1
one for loop is enough
一个for循环就足够了
#include<stdio.h>
int substitute_and_sum(int* a, int len) {
int sum =0, i;
for(i=0;i< len;i++)
{
if(i==3)
{
a[i] = 128;
}
printf("%d\n" , a[i]);
}
return 0;
}
int main(){
int arr[]={0,0,2,0,0};
int j=5,i;
substitute_and_sum(&arr[i],j);
}
replace 5
with len
otherwise what is the point of the argument and the last printf()
is pointless because it only prints the value of the first element;
用len替换5,否则参数的重点是什么,最后一个printf()是没有意义的,因为它只打印第一个元素的值;
#2
3
You have two for
loops which don't play well together. Remove one or the other. For example:
你有两个for循环,它们不能很好地结合在一起。删除其中一个。例如:
int main()
{
int arr[] = { 0, 0, 2, 0, 0 };
int j = 5;
substitute_and_sum(arr, j);
for (int i = 0; i < 5; i++)
{
printf("%d\n", a[i]);
}
}
Note that I moved the printf
into main
. Your existing program is pretty weird.
请注意,我将printf移动到main。你现有的程序很奇怪。
#3
3
You just need to call like following
你只需要像下面这样打电话
int main(){
int arr[]={0,0,2,0,0};
int j=5,i;
//for(i=0;i<5;i++)
//{
substitute_and_sum(arr,j);
//}
}
Or use :-
或使用: -
void substitute_and_sum(int *a)
{
*a = 128;
}
And in main
:
在主要:
for (i = 0; i < 5; i++)
{
if (i == 2) //Note its index 2, not 3
substitute_and_sum(&arr[i]);
}
#4
1
You loop over your array in both functions, just do it in one of them.
您可以在两个函数中遍历数组,只需在其中一个函数中执行。
#5
1
this works, whenever you send individual value of the array make it an habit of sending its index, if required for example in case like yours.
这是有效的,每当你发送数组的单个值时,就养成发送索引的习惯,如果需要,例如像你的那样。
#include<stdio.h>
int substitute_and_sum(int *a, int i)
{
if (i == 3)
{
*a = 128;
}
printf("%d\n", *a);
return 0;
}
int main()
{
int arr[] = { 0, 0, 2, 0, 0 };
int i;
for (i = 0; i < 5; i++)
{
substitute_and_sum(&arr[i], i);
}
}
#1
1
one for loop is enough
一个for循环就足够了
#include<stdio.h>
int substitute_and_sum(int* a, int len) {
int sum =0, i;
for(i=0;i< len;i++)
{
if(i==3)
{
a[i] = 128;
}
printf("%d\n" , a[i]);
}
return 0;
}
int main(){
int arr[]={0,0,2,0,0};
int j=5,i;
substitute_and_sum(&arr[i],j);
}
replace 5
with len
otherwise what is the point of the argument and the last printf()
is pointless because it only prints the value of the first element;
用len替换5,否则参数的重点是什么,最后一个printf()是没有意义的,因为它只打印第一个元素的值;
#2
3
You have two for
loops which don't play well together. Remove one or the other. For example:
你有两个for循环,它们不能很好地结合在一起。删除其中一个。例如:
int main()
{
int arr[] = { 0, 0, 2, 0, 0 };
int j = 5;
substitute_and_sum(arr, j);
for (int i = 0; i < 5; i++)
{
printf("%d\n", a[i]);
}
}
Note that I moved the printf
into main
. Your existing program is pretty weird.
请注意,我将printf移动到main。你现有的程序很奇怪。
#3
3
You just need to call like following
你只需要像下面这样打电话
int main(){
int arr[]={0,0,2,0,0};
int j=5,i;
//for(i=0;i<5;i++)
//{
substitute_and_sum(arr,j);
//}
}
Or use :-
或使用: -
void substitute_and_sum(int *a)
{
*a = 128;
}
And in main
:
在主要:
for (i = 0; i < 5; i++)
{
if (i == 2) //Note its index 2, not 3
substitute_and_sum(&arr[i]);
}
#4
1
You loop over your array in both functions, just do it in one of them.
您可以在两个函数中遍历数组,只需在其中一个函数中执行。
#5
1
this works, whenever you send individual value of the array make it an habit of sending its index, if required for example in case like yours.
这是有效的,每当你发送数组的单个值时,就养成发送索引的习惯,如果需要,例如像你的那样。
#include<stdio.h>
int substitute_and_sum(int *a, int i)
{
if (i == 3)
{
*a = 128;
}
printf("%d\n", *a);
return 0;
}
int main()
{
int arr[] = { 0, 0, 2, 0, 0 };
int i;
for (i = 0; i < 5; i++)
{
substitute_and_sum(&arr[i], i);
}
}