您好这是我的功能请帮我了解问题所在:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define LENGTH 10
void searchingFunc(int arr[],int number,int specialNumber);
void placesOfNum(int arr[],int number,int specialNumber);
int evenPlaces(int arr[],int number,int specialNumber);
int main()
{
int len =10,someNum =0,i=0,ans=0;
int someArr[LENGTH]={0};
for(i=0;i<len;i++)
{
printf("enter a number: \n");
scanf("%d",&someArr[i]);
}
printf("now, enter the other number\n");
scanf("%d",&someNum);
searchingFunc(someArr,len,someNum);
placesOfNum(someArr,len,someNum);
ans = evenPlaces(someArr,len,someNum);
printf("%d",ans);
return 0;
}
/**
this function
input:
output:
*/
void searchingFunc(int arr[],int number,int specialNumber)
{
int i = 0;
while ((i < number) && (specialNumber != arr[i]))
{
i++;
}
if (i < number)
{
printf("the number found\n");
}
else
{
printf("the Number not found\n");
}
}
/**
this function
input:
output:
*/
void placesOfNum(int arr[],int number,int specialNumber)
{
int i = 0;
for(i=0;i<number;i++)
{
if (specialNumber== arr[i])
{
printf("in the %d\n",i+1);
}
}
}
/**
this function
input:
output:
*/
int evenPlaces(int arr[],int number,int specialNumber)
{
int i=1,sum=0;
for(i=1;i<=number;i+2) //because the counting start from 0
{
sum = sum + arr[i];
}
return sum;
}
函数工作但最后,它停止工作,并且不打印最后一个数字 - 偶数的总和。请帮助我谢谢
答案 0 :(得分:0)
在evenPlaces
函数中,您未在i
循环中更新for
。您还必须将i<=number
更改为i < number
。
更改
for(i=1;i<=number;i+2)
到
for(i = 1; i < number; i = i+2) {}