length array in formation

Write a function that accepts an array of integers and returns its length several different organs (apart)


if i wirth formation 4; 1,2,1,2 its return 1 why is that???
tnx for help!



#include <iostream>
#include <math.h>
using namespace std;


int setSize(int a[],int size)
{
int count=1;
for(int i=0;i<size-1;i=i+1)
{

for(int j=i; j<size-1 ; j++)
{
if(a[i]!=a[j+1])
count++;
else
count=0;
}
return count;
}


system ("pause");
}


void main()
{
int a[100];int size;
cout<<"Enter size of rray:"<<endl;
cin>>size;
cout<<"Enter numbers into array:"<<endl;
for(int i=0; i<size; i=i+1)
{
cin>>a[i];
}
int ans;
ans= setSize(a,size);
cout<<"ans; "<<ans<<endl;
system("pause");

}
1
2
3
4
if(a[i]!=a[j+1])
  count++;
else
  count=0; // reset the count to 0 way too often 
but where?
Your input is essentially {a, b, c, d}.
Your nested loop generates pairs: {ab, ac, ad, bc, bd, cd}

On one hand you do try to count the number of pairs that are not equal, but in the other time you reset the count back to 0 if a pair is equal.

b=2 == d=2, so second to last iteration sets count to 0.
c=1 != d=2, so last iteration increases the count.

Therefore you do get that 1 as result.


The question is not "where?" but "What your code should do?"
Topic archived. No new replies allowed.