0

C array, index

(Linear table) Try to write an algorithm to find the node with a value i in an 8-element sequence table. If found, the function returns the position of the node in the sequence table; if not found, it returns 0. example : input : 2 3 4 5 6 7 8 9 1 output: 0 input: 2 3 4 5 6 7 8 9 4 output: 3 Can any one help for this ? https://code.sololearn.com/cA1178A25A8a/#c

25th Mar 2021, 1:08 PM
NTADI MJ🇨🇬🌍🇨🇬
NTADI MJ🇨🇬🌍🇨🇬 - avatar
12 Antworten
+ 1
#include <stdio.h> int main() { int data[100]; int n,i; int elmnt; printf("enter the number of elements to be used : "); scanf("%d",&n); printf("enter the elements"); for(i = 0; i<n ;i++ ){ scanf("%d",&data[i]); } printf(" enter the value to get array index : "); scanf("%d",&elmnt ); for(i=0;i<n;i++){ if(elmnt == data[i]){ printf("%d",i); break; } else printf("0"); } return 0; } TRY THIS buddy!! It works
26th Mar 2021, 8:29 PM
kreddyt
kreddyt - avatar
+ 1
Hint : You can use for loop
25th Mar 2021, 1:47 PM
Med Amine Fh
Med Amine Fh - avatar
+ 1
Simple, but I am looking for it. I can take value but how to display the given element's index ?
25th Mar 2021, 1:59 PM
NTADI MJ🇨🇬🌍🇨🇬
NTADI MJ🇨🇬🌍🇨🇬 - avatar
+ 1
Thank you. So let me write and send it.
25th Mar 2021, 2:14 PM
NTADI MJ🇨🇬🌍🇨🇬
NTADI MJ🇨🇬🌍🇨🇬 - avatar
25th Mar 2021, 2:24 PM
NTADI MJ🇨🇬🌍🇨🇬
NTADI MJ🇨🇬🌍🇨🇬 - avatar
+ 1
Martin Thanks for answering. I declared the variable i because, when I don't do it CODE-BLOKS IDE don t recognize it. like for(int i = 0....)
25th Mar 2021, 2:48 PM
NTADI MJ🇨🇬🌍🇨🇬
NTADI MJ🇨🇬🌍🇨🇬 - avatar
+ 1
Thank you for your help, I really need someone like you with me. But the printf("%d",&index); //won't help me display 0 for the element which don't belong to the array;
25th Mar 2021, 3:42 PM
NTADI MJ🇨🇬🌍🇨🇬
NTADI MJ🇨🇬🌍🇨🇬 - avatar
+ 1
sorry for the placeholder on printf, it s mistake. nothing is displayed when the variable don't find the target. I want to diplay 0. Thank you for your help.
25th Mar 2021, 3:57 PM
NTADI MJ🇨🇬🌍🇨🇬
NTADI MJ🇨🇬🌍🇨🇬 - avatar
+ 1
Thank you, Enjoy day.
25th Mar 2021, 4:11 PM
NTADI MJ🇨🇬🌍🇨🇬
NTADI MJ🇨🇬🌍🇨🇬 - avatar
0
Maybe I have to download a new version.
25th Mar 2021, 3:08 PM
NTADI MJ🇨🇬🌍🇨🇬
NTADI MJ🇨🇬🌍🇨🇬 - avatar
0
I installed it when I began to learn C, So I don't know.
25th Mar 2021, 3:40 PM
NTADI MJ🇨🇬🌍🇨🇬
NTADI MJ🇨🇬🌍🇨🇬 - avatar
0
#include <stdio.h> int main() { char a[20]; fgets(a, 20, stdin); int b; scanf("%d", &b); for (int i=0;a[i];i++) { if (a[i] - 48 == b) { printf("%d", i / 2); break; } } return 0; } // Hope this helps
27th Mar 2021, 7:59 AM
Calvin Thomas
Calvin Thomas - avatar