13,901,343 members
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.

What I have tried:

we tried by writing the pseudo code
Posted
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?" ?

## 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!

## 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.

Top Experts
Last 24hrsThis month
 OriginalGriff 220 Maciej Los 185 phil.o 105 KarstenK 75 Patrice T 75
 OriginalGriff 4,218 Richard MacCutchan 1,904 Maciej Los 1,515 Gerry Schmitz 1,469 CPallini 1,173