C array, index | Sololearn: Learn to code for FREE!

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

3/25/2021 1:08:05 PM

NTADI M.J

21 Answers

New Answer

+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

+2

That's simple, let's see your code.

+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

Next we read the target number to search for. This needs to be done outside of the loop otherwise we will need to enter it 8 times. scanf("%d", &elmnt); Again good to see that you remembered the & symbol.

+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

Finally, now that we have all of the required data we can loop through the array again to find the target value. To do this we need an index value that we can use outside of out search loop. int index = 0; We initialise it to 0 to indicate that we have not found the target. Next we go over the loop, as you did earlier, searching for the target. for(int i=0; i<8; i++) { if(elmnt == array[i]) { index = i+1; break; // Break out of the loop if we have found the target } } Now show the result of the search loop printf("%d\n", index); Notice that if the target is found we break out of the loop because there is no need to keep searching. Notice also that we add 1 to the array index because arrays in C start at 0 but you want a 1 based index. If the target is not found the loop ends when it reaches the end of the array and the variable index remains at 0 indicating that the number was not found. See if you can put it all together in the code playground now.

+1

declaring the data types in the loop is a standard C technique. I doubt Code::Blocks (CB) is complaining about it because CB is an Integrated Development Environment (IDE) not a compiler. If your compiler is rejecting such code it must be a very old compiler (pre C99 standard). Which compiler are you using with Code::Blocks? Normally it is bundled with a relatively modern copy of the GNU compiler.

+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

Type your code into the code playground and provide a link here. We will look at your code and let you know what needs to be done. We will not write the code for you. You say you can take the value, that's good, show us what you have attempted and we will guide you. If we spoon feed you answers you will not learn anything. "Tell me and I forget, teach me and I may remember, involve me and I learn." - Benjamin Franklin

0

OK Joseph. There are a few issues with your code but you are on the right track. You have declared the input array as 9 when only 8 are required. Arrays in C are 0 based. So for 8 integers the indexes are 0 to 7 inclusive. int data[8]; Your for loop is attempting to read more values than are entered. It is also trying to process the data as it is entered, which is not possible. You need to break the process down into distinct stages. To read the 8 entries into the array we simply loop around 8 times to get the values. for(int i=0; i<8; i++) { scanf("%d, &data[i]); } That's all you need to do to read the data. Good work on remembering to use the & before the array variable. A lot of people forget that one. Notice that the variable in the for loop does not need to be declared in advance. (I will break this reply down into several posts so you can follow along)

0

Maybe I have to download a new version.

0

Do you know which compiler you are using? Do you know which version number of Code::Blocks you are using?

0

I installed it when I began to learn C, So I don't know.

0

it's just printf("%d", index); without the & this time. The variable index is an integer variable that is used to store the index number if we find the target. It will either have the index where the target is found or 0 if it is not found. Remember to declare and initialise the variable "index" before the search loop, or at the start of the program if you prefer.

0

It is important that you know which compiler you have installed and it's version number. You should also know where it is installed. I am unfamiliar with Code::Blocks but most IDEs have a window which displays messages from the compiler while code is building. If you look at this window it should provide the name, and possibly the version, of the compiler used. Anyway, here is my solution to the problem. It gives a more verbose output than what is required and prompts for inputs. It is heavily commented and works on the code playground (gcc compiler) and on my system (msvc compiler) https://code.sololearn.com/cA13A7a1a8a1/#c By the way I seem to have problems viewing threads with more than 20 replies in them when using a browser. I'm trying to get some data together on it for a bug report to SoloLearn. So if this thread grows beyond 20 posts I won't be able to see the additional posts. This means that I won't be able to reply to things I can't see. It does not mean that I'm being rude and ignoring you.