int nth_element_of_merged_table(int x, int y, int n){
	int *table_x = NULL;
	int *table_y = NULL;
	int result;
	
	if(n == 0){
		return 0;
	}
	
	if(x == y){
		return n * x;
	}
	
	table_x		 = (int*) calloc(n, sizeof(int));
	table_y = (int*) calloc(n, sizeof(int));
	
	construct_table(table_x, x, n);
	construct_table(table_y, y, n);
	
	return nth_number_of_merge_table(table_x, table_y, n);
}
Exemplo n.º 2
0
int main(int argc, char const *argv[])
{
	int n;
	printf("Enter size\n");
	scanf("%d",&n);
	int i;
	int a[n+1];
	printf("Enter values\n");
	for ( i = 1; i <= n; ++i)
	{
		scanf("%d",&a[i]);
	}
	int sum ;
	printf("Enter sum\n");
	scanf("%d",&sum);
	construct_table(sum, a, n);
	return 0;
}
Exemplo n.º 3
0
Arquivo: hash.c Projeto: pchapin/spica
int main(void)
{
  hash_table table;
  char *strings[] = {
    "The first key string",
    "The second key string",
    "The third key string",
    "The fourth key string",
    "The fifth key string",
    "The sixth key string",
    NULL
  };

  char *junk[] = {
    "The first data",
    "The second data",
    "The third data",
    "The fourth data",
    "The fifth data",
    "The sixth data"
  };

  int i;
  void *j;

  construct_table(&table, 200);
  for (i = 0; NULL != strings[i]; i++) insert(strings[i], junk[i], &table);
  for (i = 0; NULL != strings[i]; i++)
    {
      printf("\n");
      enumerate(&table, printer);
      del(strings[i], &table);
    }
  for (i = 0; NULL != strings[i]; i++)
    {
      j = lookup(strings[i], &table);
      if (NULL == j)
        printf("\n'%s' is not in table", strings[i]);
      else
        printf("\nERROR: %s was deleted but is still in table.", strings[i]);
    }
  free_table(&table, NULL);
  return 0;
}
Exemplo n.º 4
0
int main(int argc, char const *argv[])
{
	int n;
	printf("Enter number of items\n");
	scanf("%d",&n);
	int i;
	int weight[n+1], value[n+1];
	weight[0] = value[0]= 0;
	printf("Enter weight and vale of each item\n");
	for ( i = 1; i <= n; ++i)
	{
		scanf("%d %d",&weight[i],&value[i]);

	}
	int knapsack;
	printf("Enter knapsack size\n");
	scanf("%d",&knapsack);
	construct_table(knapsack, weight, value, n);
	
	return 0;
}
Exemplo n.º 5
0
/*
 * Function to find the multiplicative inverse of a given poly
 * @param: a valid poly
 * @output: multiplicative inverse of poly if it has one and print out the processes to stdout
 */
void inverse(uint8_t inv_poly[])
{
	//Construct lookup_table to find multiplicative inverse of a byte
	struct lookup_table mytable = construct_table();

	uint8_t temp[5]; //initialize temp to be x^4 + 1 or {01}{00}{00}{00}{01}, but ignore the coefficient of x^4
	uint8_t quo[4];
	uint8_t aux[4];
	uint8_t temp_aux[4];
	uint8_t rem[4];
	//initialize reminder, quotient and aux
	for(int k=0; k < 4; k++)
	{
		quo[k] = 0x00;
		aux[k] = 0x00;
		rem[k] = 0x00;
		temp[k] = 0x00;
		temp_aux[k] = 0x00;
	}
	//set the last byte of temp to be {01} since we have x^4 + 1
	temp[3] = 0x01;
	//print first round
	printOutput_inverse(1, temp, quo, temp_aux);

	//Set rem to be poly
	memcpy(rem, inv_poly, 4);
	//set last byte of aux to be {01} since now x=1 for second round
	aux[3] = 0x01;
	//print second round
	printOutput_inverse(2, rem, quo, aux);

	/*
	 * Now go through the last 4 rounds to complete our multiplicative inverse process.
	 * We could end sooner if the poly has no inverse (rem = 00) or we arrive at rem = const sooner than 4 rounds
	 */
	int isInverse = 1;
	for(int k = 3; k < 7; k++)
	{
		struct long_division result;
		if(k == 3) //take special care for first division since the dividend has 4th degree (x^4 + 1)
		{
			result = divide(temp, rem, mytable, 1);
		}
		else
		{
			result = divide(temp, rem, mytable, 0);
		}
		/*
		 * Compute aux
		 */
		struct modprod_result quo_aux = modprod(result.quo, aux, 0);
		//aux holder to later update the previous aux
		uint8_t aux_holder[4];
		memcpy(aux_holder, aux, 4);
		//Perform XOR with previous aux
		for(int i = 0; i < 4; i++)
		{
			aux[i] = quo_aux.modprod_poly[i] ^ temp_aux[i];
		}
		//Update previous aux
		memcpy(temp_aux, aux_holder, 4);

		//print output
		printOutput_inverse(k, result.rem, result.quo, aux);

		/*
		 * Perform check on rem to see if we can stop early for the following cases:
		 * Case 1: rem = {00}{00}{00}{00} - stop the loop and report that the input poly does not have multiplicative inverse
		 * Case 2: rem = {00}{00}{00}{01} - rem is a constant with reminder = 01 signaling that we are done
		 */
		if(result.rem[0] == 0x00 && result.rem[1] == 0x00 && result.rem[2] == 0x00)
		{
			if(result.rem[3] == 0x00)
			{
				isInverse = 0;
				break;
			}
			else if(result.rem[3] == 0x01)
			{
				break;
			}
		}

		/*
		 * Update temp and rem for next round
		 * now temp = rem and rem = result.rem
		 */
		memcpy(temp, rem, 4);
		memcpy(rem, result.rem, 4);
	}

	//print last statement for inverse process
	if(!isInverse)
	{
		fprintf(stdout, "{%02x}{%02x}{%02x}{%02x} does not have a multiplicative inverse\n", inv_poly[0], inv_poly[1], inv_poly[2], inv_poly[3]);
	}
	else
	{
		fprintf(stdout, "Multiplicative inverse of {%02x}{%02x}{%02x}{%02x} is {%02x}{%02x}{%02x}{%02x}\n", inv_poly[0], inv_poly[1], inv_poly[2], inv_poly[3], aux[0], aux[1], aux[2], aux[3]);
	}

}