Function for multiple repetitions

Rena

Member
Joined
Apr 29, 2017
Messages
8
Programming Experience
1-3
Please help me write this function
A 123 array is an array that contains multiple repetitions of the values 1, 2, 3 in that order. For example {1, 2, 3, 1, 2, 3, 1, 2, 3} is a 123 array but {1, 2, 3, 1, 2} is not. Write a function named is123Array which returns 1 if its array argument is a 123 array, otherwise it returns 0.

If you are programming in Java or C#, the function prototype is int is123Array(int[ ] a)

More examples:
if a is return reason
{1, 2, 3, 3, 2, 1} 0 elements a[3], a[4], a[5] are not 1, 2, 3
{0, 1, 2, 3, 1, 2, 3} 0 elements a[0], a[1], a[2] are not 1, 2, 3
{1, 2, 3, 3, 2, 1, 1, 2, 3} 0 elements a[3], a[4], a[5] are not 1, 2, 3
{1, 2, 3} 1
{} 0 must contain at least one 1, 2, 3 triad
 
We're not here to do your homework for you. If you are having a specific issue with something you've done then we can help with that but you would need to have done something for that to be the case. Think the problem through, do what you think is required and then, if it doesn't work, post what you've done here and tell us exactly how it doesn't meet your expectations. That said, to actually consider writing code, you'd need to have a pretty good idea of what that code is supposed to do. That means that you need to basically solve the problem BEFORE you write the code. If you couldn't explain to someone how to perform the task manually, i.e. with pen and paper, then how could you possibly write code to do it? If you could provide that explanation then, at the very least, you can provide that here to show that you've made some effort on your own behalf.
 
Sorry,jmcilhinney


I didn't mean at all but I did write the function and I need to what is the problem as it doesn't display the output correctly.your feedback will be highly appreciated.
int is123Array(int[ ] a)
{
int ret = 0;
int count = 0;
int invalidvlue = 0;
for (int i = 0,j=i+1; i < a.Length && j<a.Length;i++,j++)
{
// for (int j = i; j >= i;j++ )
{
if (a == 1 && a[j] == 2 && a[j + 1] == 3)
{
count++;
}
else
invalidvlue++;
// i+=2;
}


}
if (count>0 && invalidvlue==0)
ret = 1;
return ret;
​}

jmcilhinney


your quick help will be highly appreciated.
 
When you post code snippets, please post it as plain text within the appropriate formatting tags, i.e.

[xcode=c#]your code here[/xcode]

As for your code, if it's not doing what you expect then the you need to debug it. Place a breakpoint at the top of the code and then, when execution breaks, you can step through the code line by line. Before a step, examine all your variables and other relevant expressions to see what they evaluate too and determine EXACTLY what you expect to happen, i.e. what line will it step to and what variables will change and what to. You then step and check whether everything you expected to happen actually did happen. If it did then you perform the process again for the next step. If it didn't then you have found an issue and you can analyse why. If you can't work out the problem then at least you can tell us ALL the relevant information. If you get through the whole debugging session without expectation differing from reality but still get the wrong result then your code must correctly implement your algorithm but your algorithm must be wrong, so you need to rethink that.
 
I did but the problem is that I didn't reach the accurate output. would you please help me solve this function?

the function should return 1 if the arguments values begins with {1,2,3} but if the array is {1,2,3,1,2} ,it should return 0 and if the array is {1,2,3,1} ,it should return 0 .if the array is {1,2,3,1,2,3},it should return 1. hope you get my point.

your help would be highly appreciated.
 
I already got your point. You don't have to explain it again. It seems that you didn't get my point. If you have debugged then one of two things must have happened. Either you found place in the code that reality differed from expectation or you didn't. If you did then explain EXACTLY where and how. If you didn't then, as I said, there must be an issue with your algorithm so you need to go back and review that. You haven't told us what your algorithm is so there's no way that we can tell you what's wrong with it.
 
Alright. I got your point. I need to know your feedback in the following code below:
static int is123Array(int[ ] a)
{
int ret = 0;
int count = 0;
int invalidvlue = 0;
for (int i = 0,j=i+1; i < a.Length && j<a.Length;i++,j++)
{
// for (int j = i; j >= i;j++ )
{
if (a == 1 && a[j] == 2 && a[j + 1] == 3)
{
count+=3;
}
else
invalidvlue++;
i+=2;
j += 2;
}


}
if (count==a.Length)
ret = 1;
return ret;
}
your feedback really matters me.
 
Alright. I got your point.

Clearly you didn't get my point because you have ignored everything I said, including how to post your code properly. I will not be contributing further to this thread.
 
Clearly you didn't get my point because you have ignored everything I said, including how to post your code properly. I will not be contributing further to this thread.
am really sorry.Here's the code below:

[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] is123Array([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][ ] a)[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]       {
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ret = 0;[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] count = 0;[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] invalidvlue = 0;[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] i = 0,j=i+1; i < a.Length && j<a.Length;i++,j++)[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]           {
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]// for (int j = i; j >= i;j++ )[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]               {
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] (a[i] == 1 && a[j] == 2 && a[j + 1] == 3)[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]                   {
                       count++;
                   }
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]                       invalidvlue++;
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]//  i+=2;[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]               }


           }
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] (count>0 && invalidvlue==0)[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]               ret = 1;
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ret;[/SIZE][/FONT][/SIZE][/FONT]
 ​}

 
Read what I posted. Do what I said.
post [code] as plain text within the appropriate formatting tags
Does that look like plain text?
If you have debugged then one of two things must have happened. Either you found place in the code that reality differed from expectation or you didn't. If you did then explain EXACTLY where and how. If you didn't then, as I said, there must be an issue with your algorithm so you need to go back and review that.
Even if you do post your code properly, I'm not going to look at it until you've done that.
 
staticint is123Array(int[ ] a)
{
int ret = 0;
int count = 0;
int invalidvlue = 0;
for (int i = 0,j=i+1; i < a.Length && j<a.Length;i++,j++)
{
// for (int j = i; j >= i;j++ )
{
if (a == 1 && a[j] == 2 && a[j + 1] == 3)
{
count+=3;
}
else
invalidvlue++;
i+=2;
j += 2;
}


}
if (count==a.Length)
ret = 1;
return ret;
}
 
Back
Top Bottom