+ 1

I want to find a count of odd numbers and even number in a 20 element array using function and this is my attempt

#include<stdio.h> int oddeven(int i); int main(){ printf("please enter 20 numbers (without decimal)\n"); int x[20]; int i,search; for (i = 0; i < 20; i++) { printf("Enter the number %d \n",i); scanf("%d", &x[i]); } printf("Enter a number to search\n"); scanf("%d", &search); for(i=0;i<20;i++){ if(x[i]==search) printf("%d location %d\n",search,i+1); } int k=printf("%d",x[i]); } int oddeven(int i) { int size,odd_count,even_count,x[i]; for(i = 0; i < size; i++) if(x[i] % 2 == 1) odd_count++; else even_count++; }

1st Aug 2020, 10:58 AM
narthana wickramasinghe
narthana wickramasinghe - avatar
13 ответов
+ 1
Oh, now I get it. You can add a flag variable. Think of it like a boolean that is 0 if the search is unsuccessful and 1 if it succeeded. So, after the loop you check this variable. For example: int found = 0; ... for(i=0;i<20;i++){ if(x[i] == search) { printf("found it") ; found = 1; } if(found==0) printf("Sorry") ;
3rd Aug 2020, 10:40 AM
Johnny Kaz
Johnny Kaz - avatar
+ 1
There are a few issues with the function. Firstly, the array x is local (in the main function), so the function oddeven won't be able to see it (out of scope). To be able to return both the odd count and the even count, you need to have as input some pointers to integers and change their value in the function. An implementation would look something like this: void oddeven(int* array, int size, int* odd_count, int* even_count) { int i; for(i=0; i<size; i++) { if(array[i] % 2 == 1) *odd_count++; else *even_count++; } } To call this function, you would write (in main) : int odd, even; oddeven(x, 20, &odd, &even); The results would be stored in variables odd and even
1st Aug 2020, 1:03 PM
Johnny Kaz
Johnny Kaz - avatar
+ 1
Thank you and I recode and show you
1st Aug 2020, 1:29 PM
narthana wickramasinghe
narthana wickramasinghe - avatar
+ 1
The problem is the if-else statement. If you have a positive odd number, the odd counter won't be updated, because the c1 counter will be increased and the other cases (else) won't be tested. You can change it like this: if(x[i] >0) c1++; else if(x[i] <0) c2++; else c3++; if(x[i] % 2 == 0) c4++; else c5++;
3rd Aug 2020, 8:06 AM
Johnny Kaz
Johnny Kaz - avatar
+ 1
Thank you very much it works properly now
3rd Aug 2020, 9:49 AM
narthana wickramasinghe
narthana wickramasinghe - avatar
+ 1
You are welcome. Good luck on your coding journey
3rd Aug 2020, 10:08 AM
Johnny Kaz
Johnny Kaz - avatar
+ 1
You can add something like: printf("Sorry\n") ; outside of the loop. What do you mean by count searching result?
3rd Aug 2020, 10:22 AM
Johnny Kaz
Johnny Kaz - avatar
+ 1
I have a search unit in my code it shows the location of the particular no if i want to count the locations of particular no what i van do ?
3rd Aug 2020, 10:28 AM
narthana wickramasinghe
narthana wickramasinghe - avatar
0
I recoded it and but i unable to print that oddvount amd even count if i want to print the count shoud i use printf to &odd or normal odd?
2nd Aug 2020, 11:33 AM
narthana wickramasinghe
narthana wickramasinghe - avatar
0
#include<stdio.h> void count(int x[]); int main(){ printf("please enter 20 numbers (without decimal)\n"); int x[20]; int i,search; for (i = 0; i < 20; i++) { printf("Enter the number %d \n",i); scanf("%d", &x[i]); } printf("Enter a number to search\n"); scanf("%d", &search); for(i=0;i<20;i++){ if(x[i]==search) printf("%d location %d\n",search,i+1); if(x[i]!=search) printf("sorry\n"); } count(x); } void count(int x[]) { int i,c1=0,c2=0,c3=0,c4=0,c5=0; for(i=0;i<20;i++) { if (x[i]>0) c1++; else if (x[i]%2==0) c4++; else if(x[i]%2==1) c5++; else if(x[i]<0) c2++; else c3++; }
3rd Aug 2020, 4:07 AM
narthana wickramasinghe
narthana wickramasinghe - avatar
0
In above code i want to print sorry one time and it not works in odd numbers
3rd Aug 2020, 4:07 AM
narthana wickramasinghe
narthana wickramasinghe - avatar
0
And one last thing cannt i count searching result or print sorry once ?
3rd Aug 2020, 10:12 AM
narthana wickramasinghe
narthana wickramasinghe - avatar
0
Okkkkkkk thank you 😍😍😍😍😍
3rd Aug 2020, 10:42 AM
narthana wickramasinghe
narthana wickramasinghe - avatar