Click here to Skip to main content
13,901,343 members
Rate this:
Please Sign up or sign in to vote.
See more:
algorithm to find an array of characters for a character (user-).

a pseudocode and a code in C implementing algorithm. The pseudocode should be included in the C program as a multiline comment.

The output should be an array of elements. For instance, if the character array is:

{'a', 'b', 'c', 'b', 'z', 'b'}

and we search for 'b', the output should be an integer array containing {1, 3}.

If we search for 'c', however, the output should be {2, -1} (-1 because there is only one 'c' in the array). If a character does not appear in your array, the output should be {-1, -1}.

The output should be displayed on the screen. Please do not forget to inform the user.

Please compute the number of operations in your code.

What I have tried:

we tried by writing the pseudo code
Updated 6-Dec-18 4:48am
CPallini 6-Dec-18 9:22am
Are you sure those are the exact requirements?
It is strange you have to return {2,-1} in second case ({2} would be better in my opinion).
It is also strange {-1,-1} in third case ({-1} would be better, again in my opinion).
Rick York 6-Dec-18 10:45am
Do you have a question, other than "will you do my homework for me?" ?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think! Start by thinking about what you have to return: and array which contains two numbers. When does it return what values? How would it know when to return -1 as a value?
Hint: I'd start by prefilling the "return array" with -1 in all elements. Then if you don;t find any, it's ready. Think about why you would fill each of the elements after that.

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The -1 is for not found, because it is an invalid index in array. So lookup the array and fill the position into the answer array. At best you initialize all elements the answer array with -1.

What if the char is more than 2x in the array?

Use some C++ tutorial to learn the language like accessing an array and screen output like Learn C++. Install Visual Studio.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190306.1 | Last Updated 6 Dec 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100