Skip to content

Instantly share code, notes, and snippets.

@slok
Created April 18, 2011 21:00
Show Gist options
  • Save slok/926165 to your computer and use it in GitHub Desktop.
Save slok/926165 to your computer and use it in GitHub Desktop.
This program returns the position of an array that after this position all the numbers are repeated in the numbers before the returned array positio
/*
Copyright (C) 2011 Xabier Larrakoetxea (slok) <slok69@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#define TAM 10
int isInAuxArray(int number, int auxArray[TAM][2])
{
int result = 0;
int i;
for(i=0 ; i < TAM; i++)
{
if(number == auxArray[i][0])
{
result=1;
break;
}
}
return result;
}
int searchForNumber(int number, int auxArray[TAM][2])
{
int result = 0;
int i;
for(i=0 ; i < TAM; i++)
{
if(number == auxArray[i][0])
{
result = i;
break;
}
}
return result;
}
void printArray2D(int auxArray[TAM][2])
{
int i;
for(i=0 ; i < TAM; i++)
{
printf ("%d, ", auxArray[i][0]);
}
}
void printArray(int auxArray[TAM])
{
int i;
for(i=0 ; i < TAM; i++)
{
printf ("%d, ", auxArray[i]);
}
}
int main(int argc, char **argv)
{
int cont=0, contAux=1, cont2=0;
int randomNumbers[TAM] = {0, 4, 5, 2, 0, 4, 6, 6, 6, 4};
int aux[TAM][2], i;
aux[0][0]=randomNumbers[0];
//get all the nonrepeated numbers
for(i=0; i < TAM; i++)
{
if(isInAuxArray(randomNumbers[i], aux) == 0)
{
aux[contAux][0] = randomNumbers[i];
aux[contAux][1] = 0;
contAux++;
}
}
//second pass
for(i=0; i < TAM; i++)
{
//printf("*: %d , %d", isInAuxArray2D(randomNumbers[i], aux), aux[searchForNumber(randomNumbers[i], aux)][1]);
if(aux[searchForNumber(randomNumbers[i], aux)][1] == 0)
{
//add the repeated before this non repeated
cont = cont + cont2;
//restart the repeated number counter
cont2=0;
cont++;
//check as saw this number
aux[searchForNumber(randomNumbers[i], aux)][1]++;
}
else
//we need the extra passes, maybe there is a non repeated in the future and we need to add
cont2++;
}
printArray(randomNumbers);
printf ("\nResult in array(starts in zero): %d \n", (cont-1) );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment