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
21 Respostas
+ 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
+ 1
Hint : You can use for loop
+ 1
Simple, but I am looking for it.
I can take value but how to display the given element's index ?
+ 1
Thank you. So let me write and send it.
+ 1
https://code.sololearn.com/cA1178A25A8a/#c
Here is the code
+ 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....)
+ 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;
+ 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.
+ 1
Thank you, Enjoy day.
0
Maybe I have to download a new version.
0
I installed it when I began to learn C, So I don't know.
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