Ejemplo n.º 1
0
int main()
{
    int arr[100];
    int n,i;
    printf("Enter Size:");
    scanf("%d",&n);
    printf("Enter the elements:");
    for(i=0;i<n;i++)
	scanf("%d",&arr[i]);
    quickSort(arr, 0, n-1);
    printf("Sorted array: \n");
    printArray(arr, n);
    return 0;
}
Ejemplo n.º 2
0
int main(void)
{
	int Input [] = {3,0,1};
	int Input_size = sizeof(Input)/sizeof(int);

	printf("Input array:\n");
	printArray(Input, Input_size);

	int missedNum = missingNumber(Input, Input_size);
	
	printf("The missed number is: %d\n", missedNum);

	return 0;
}
Ejemplo n.º 3
0
int main(void)
{
	int array [NUM][NUM];
	int i,j;
	for(i=0; i<NUM; i++)
	{
		for(j=0; j<NUM; j++)
		{
			array[i][j] = i*NUM + j;
		}
	}

	printArray(array);
}
Ejemplo n.º 4
0
int main() {
Vector dynamArray; //initialize the Dynamic array
vector_init(&dynamArray);

//user can make following input:
// - add to array eg. "1" then request for data to add
// - list all elements eg. "2"
// - check capacity eg. "3" 
// - check second highest element ex. "4"
// - exit the program with exit key ex. "5"
int input;
int numToAppend = 0; //number to append to the array
int loop = 0; //loop continues while loop is 0

while(loop == 0){
	
	printf("What would you like to do?\n");
	printf("Press:\n '1' to add to the array\n '2' to list all elements,\n '3' to check array capacity,\n '4' to find the second highest element,\n '5' to exit the program\n");
	scanf("%d", &input);
	switch (input) {
		case 1:
			printf("What would you like to add to array?\n");
			scanf("%d", &numToAppend);
			vector_append(&dynamArray, numToAppend);
			break;
		case 2:
			printArray(&dynamArray);
			break;
		case 3:
			printf("Current capacity of the array is ");
			printCapacity(&dynamArray);
			break;
		case 4:
			printf("The second highest element in the array is %d\n", findSecondHighest(&dynamArray));
			break;
		case 5:
			printf("Quitting program now.\n");
			loop = 1;
			break;
		default:
			printf("Incorrect input, try again\n");
			break;
		}
			
}


vector_free(&dynamArray); //free at the end of the program
return 0;
}
Ejemplo n.º 5
0
//测试用例
void main()
{
 int list[20];           //存储输入的数据
 int index;              //保存输入的数据的长度
 int node;               //临时存储输入的数据
 printf("\n please input the values you want to sort[exit for 0]\n");
 index = 0;
 scanf("%d",&node);      //给node赋值

 while (node != 0)
 {
   list[index] = node;    //通过node给数据赋值
   index++;
   scanf("%d",&node);
 }
 printf("排序前:\n");

 printArray(list,index);  //打印排序前的状态
 shellSort(list,index);  //进行排序
 printf("\n排序后:\n");  
 printArray(list,index);   //打印排序后的状态
 system("pause");
}
Ejemplo n.º 6
0
int main()
{
    int *array = randArray(SORTCOUNT, 1000);
    printArray(array, SORTCOUNT, 0);
/*
    bubbleSort(array, SORTCOUNT);
    printArray(array, SORTCOUNT, 1);
    insertSort(array, SORTCOUNT);
    printArray(array, SORTCOUNT, 1);
    shellSort(array, SORTCOUNT);
    printArray(array, SORTCOUNT, 1);
    quickSort(array, SORTCOUNT);
    printArray(array, SORTCOUNT, 1);
    selectionSort(array, SORTCOUNT);
    printArray(array, SORTCOUNT, 1);
*/
    mergeSort(array, SORTCOUNT);
    printArray(array, SORTCOUNT, 1);


    free(array);
    return 0;
}
Ejemplo n.º 7
0
int main()
{
  int array[MAX];
  int max;

  getNums(array);
  max = computeMax(array);

  printArray(array);

  printf("Max is %d\n", max);

  return 0;
}
Ejemplo n.º 8
0
void PrintAllPathExists(tree_node *root, int *paths, int level, int sum){
	if(root == NULL) return;
	paths[level] = root->data;
	int i = level, tmp_sum = 0;
	for(; i>= 0; i--){
		tmp_sum += paths[i];
		if(tmp_sum == sum){
			printArray(paths, sum, i, level);
		}
	}
	level++;
	PrintAllPathExists(root->left, paths, level, sum);
	PrintAllPathExists(root->right, paths, level, sum);
}
Ejemplo n.º 9
0
int main()
{
    int arr[] = {1, 2, 3, 4, 5, 6};
    int n = sizeof(arr)/sizeof(arr[0]);

    int d = 2;
    //scanf("%d", &d);

    //rotateLeft(arr, n, d);
    rotateRight(arr, n, d);

    printArray(arr, n);
    return 0;
}
Ejemplo n.º 10
0
int main(void)
{
	int Input[] = {7,1,5,3,6,4};
	//int Input[] = {1,7,1};
	int Input_size = sizeof(Input)/sizeof(int);

	printf("The input is:\n");
	printArray(Input, Input_size);

	int max_profit = maxProfit(Input, Input_size);
	printf("The max profit is:%d\n", max_profit);
	
	return 0;
}
Ejemplo n.º 11
0
void printAll(struct tree *root,int path[],int pathLen)
{
	if(root==NULL) return;
	path[pathLen]=root->data;
	pathLen++;
	if(root->left == NULL && root->right==NULL)
		printArray(path,pathLen);
	else
	{
		printAll(root->left,path,pathLen);
		printAll(root->right,path,pathLen);
	}

}
Ejemplo n.º 12
0
void AdjacencyMatrix(int a[][100], int n){
    int i,j;
    for(i = 0;i < n; i++)
    {
        for(j = 0;j < i; j++)
        {
            a[i][j] = a[j][i]= rand()%50;
            if( a[i][j]>40)a[i][j]=a[j][i]=999;
             
        }
    a[i][i] = 999;
    }
    printArray(a,n);
}
Ejemplo n.º 13
0
// function main
int main()
{
	int array[5] = {1, 2, 3, 4 ,5};
	int i = 0; 
	int len = 5;
	// print the orignal array
	for (i = 0; i < len; i++) {
		printArray(array, len);

	    // mapped by twice
		map(array, len, twice);
	}
	return 0;
}
main()
{
    int N = 8, maxSum = 0;    
    char p[256];
    char *str = p;
    ElementType A[] = {4, -3, 5, -2, -1, 2, 6, -2};

    printf("\t ========== [third method-devide and conquer] test for maximum sum of subsequence ==========\n\n");
    str = "initial array A: ";
    printf("%50s",str);
    printArray(A, N);    

    str = "after computing maximum sum of subsequence: ";
    printf("%50s", str);
    maxSum = maxSubsequenceSum(A, 0, N-1);
    printArray(A, N);

    str = "maximum sum of array subsequence is: ";
    printf("%50s", str);
    printf("%4d\n\n", maxSum);

    return 0;
}
Ejemplo n.º 15
0
int main(int argc, const char * argv[]) {
    int size, k, val, valAfterSorting;
    int * arr;
    
    for(size = 1; size < 11; size++) {
        arr = createRandomArray(size);
        printArray(arr, size);
        
        k = randMinMax(0, size-1);
        val = selectKthOrder(arr, 0, size-1, k);
        printArray(arr, size);
        printf("k: %d, val: %d\n", k, val);
        
        quickSort(arr,0, size-1);
        valAfterSorting = arr[k];
        printArray(arr, size);
        printf("arr[k]: %d\n", valAfterSorting);
        printf("isSame: %d\n", isSame(val, valAfterSorting));
        free(arr);
        printf("-------------------------------------\n");
    }
    return 0;
}
Ejemplo n.º 16
0
void printArray(std::ostream& o, cpptoml::array& arr) {
    o << "{\"type\":\"array\",\"value\":[";
    auto it = arr.get().begin();
    while (it != arr.get().end()) {
        if ((*it)->is_array())
            printArray(o, *(*it)->as_array());
        else
            printValue(o, *it);
        
        if (++it != arr.get().end())
            o << ", ";
    }
    o << "]}";
}
Ejemplo n.º 17
0
int main ()
{
	time_t current;
	time( & current );
	srand( ( unsigned int ) current );

	const int SIZE = 10;
	int data[ SIZE ];
	for ( int i = 0; i < SIZE; i++ )
		data[ i ] = i + 1;

	printArray( data, SIZE );

	randomShuffle( data, SIZE, 1 );

	printArray( data, SIZE );

	randomShuffle( data, SIZE );

	printArray( data, SIZE );

	return 0;
}
Ejemplo n.º 18
0
int main(){
    const int arrSize = 10;
    int Arr1[arrSize] = { 1, 27, 89, 12, 78, 19, 0, 1, 12, 3};
    int Arr2[arrSize] = { 4, 13, 90, 90, 15, 13, 4, 5, 6, 12};
    //time_t start = time(NULL);
    std::thread thread1(bubbleSort, Arr1, arrSize);
    //thread1.join(); // sequential
    std::thread thread2(insertSort, Arr2, arrSize);
    std::thread threadP1(printArray, Arr1, arrSize);
    std::thread threadP2(printArray, Arr2, arrSize);
    std::cout << "ARRAYS READY\n";
    thread1.join();
    thread2.join();
    //time_t stop = time(NULL);
    threadP1.join();
    threadP2.join();
    //cout << "ELAPSED " << (stop - start) << "\n"
    std::cout << "Printing out the array without using threads "<< std::endl;
    printArray(Arr1, arrSize);
    std::cout << std::endl;
    printArray(Arr2, arrSize);
    return 0;
}
Ejemplo n.º 19
0
void tdesTest()
{
  byte out[8];
  byte in[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  byte key[] = { 
                  0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key A
                  0x92, 0x2f, 0xb5, 0x10, 0xc7, 0x1f, 0x43, 0x6e, // key B
                  0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key C (in this case A)
                };
  
  Serial.println();
  Serial.println("====== Triple-DES test ======");
  
  //encrypt
  Serial.print("Encrypt...");
  unsigned long time = micros();
  des.tripleEncrypt(out, in, key);
  time = micros() - time;
  Serial.print("done. (");
  Serial.print(time);
  Serial.println(" micros)");
  printArray(out);
  
  //decrypt
  for (int i = 0; i < 8; i++)
  {
    in[i] = out[i];
  }
  Serial.print("Decrypt...");
  time = micros();
  des.tripleDecrypt(out, in, key);
  time = micros() - time;
  Serial.print("done. (");
  Serial.print(time);
  Serial.println(" micros)");
  printArray(out);
}
Ejemplo n.º 20
0
int main(int argc, char **argv) {

	int n;
	int a[N];
	int i, j;

	printf("Nhap vao so phan tu: ");
	scanf("%d", &n);

	printf("Nhap cac phan tu cua mang\n");
	for (i = 0; i < n; ++i) {

		printf("a[%d] = ", (i + 1));
		scanf("%d", &a[i]);
	}

	printf("Mang vua nhap la: ");
	printArray(a, n);

	for (j = 0; j < n; j++) {

		for (i = n - 1; i > j; i--) {

			if (a[i] < a[i - 1]) {

				int t = a[i];
				a[i] = a[i - 1];
				a[i - 1] = t;
			}
		}
	}

	printf("\nMang sau khi thuc hien thuat toan: ");
	printArray(a, n);

	return 0;
}
Ejemplo n.º 21
0
void threeBuckets(int *array, int length, int low, int high) {
	int i = 0;
	int lowIndex = 0;			// initial low pivot index
	int highIndex = length-1;	// initial high pivot index
	int temp = 0;

	for(i = 0; i < length; i++) {
		// when two pivot indices met
		if (lowIndex == highIndex) {
			break;
		}

		if (low < array[i]) {
			// swap(array, lowIndex+1, i);
			printf("low swap: ");
			printArray(array, length);
			temp = array[lowIndex+1];
			array[lowIndex+1] = array[i];
			array[i] = temp;
			printf("low swap: ");
			printArray(array, length);
			lowIndex++;
		}

		if (array[i] < high) {
			// swap(array, highIndex-1, i);
			printf("high swap: ");
			printArray(array, length);
			temp = array[highIndex-1];
			array[highIndex-1] = array[i];
			array[i] = temp;
			printf("high swap: ");
			printArray(array, length);
			highIndex--;
		}
	}
}
Ejemplo n.º 22
0
/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* findDuplicates(int* nums, int numsSize, int* returnSize) {
	int i, j;
	int *result = (int *) malloc(sizeof(int) * numsSize);
	*returnSize = 0;
	
	memset(result, 0, sizeof(int) * numsSize);
#ifdef DEBUG
	printArray(result, numsSize);
#endif

	for (i=0;i<numsSize;i++) {
		result[nums[i]-1]++;
		if (result[nums[i]-1] > 1)
			(*returnSize)++;
	}

#ifdef DEBUG
	printArray(result, numsSize);
#endif

	int *final_list = (int *) malloc(sizeof(int) * (*returnSize));
	for (i=0, j=0;i<numsSize;i++) {
		if (result[i] > 1) {
			final_list[j] = i+1;
			j++;
		}
	}

#ifdef DEBUG
	printArray(final_list, *returnSize);
#endif

	free(result);
	

    return final_list;
}
Ejemplo n.º 23
0
int main()
{
  float **data = nullptr;
  int i, j, N = 1000, M = 2;
  FILE *fin;

//  fin = fopen( "C:\\Users\\Lazar\\Desktop\\flame-clustering-master\\matrix.txt", "r" );
//  fscanf( fin, "%i %i\n", & N, & M );

  //  printf( "Reading dataset with %i rows and %i columns\n", N, M );

  printf( "Creating dataset with %i rows and %i columns\n", N, M );

  data = new float*[ N ];
  for( i=0; i<N; i++ ){
    data[i] = new float[ M ];
    for( j=0; j<M; j++ )
        data[i][j] = (rand() % 100000) / 100.0;
//        fscanf( fin, "%f ", & data[i][j] );
//    printf("data: %f, %f \n", data[i][0], data[i][1] );
  }

  printf( "Putting data into Flame class.\n" );
  Flame flame = Flame( data, N, M );

  printf( "Detecting Cluster Supporting Objects ..." );
  fflush(stdout);
  flame.defineSupports( 10, .5 );
  printf( "done, found %i\n", flame.getCsoCount() );

  printf( "Propagating fuzzy memberships ... " );
  fflush( stdout );
  flame.localApproximation( 500, 1e-6 );
  printf( "done\n" );


  printf( "Defining clusters from fuzzy memberships ... " );
  fflush( stdout );
  flame.makeClusters( -1.0 );
  printf( "done\n" );

  printArray( flame.getClusters(), flame.getN() );



  printf( "All done.\n" );

  return 0;
}
Ejemplo n.º 24
0
int main(int argc, char* argv[])
{
    char **wordArray;  /* declare the dynamic array of strings */
                       /* the array is NOT allocated yet */
    int capacity = INITIAL_ARRAY_MAX;
    int wordCount = 0;

    if (argc != 3)
    {
        fprintf(stderr,"Usage: %s inputfile outputfile\n", argv[0]);
        return 1;
    }

    if (loadArray(argv[1], &wordArray, &wordCount, &capacity) != 0)
    {
        fprintf(stderr,"    loadArray failed! Program terminating...\n");
        return 1;  /* don't need to call exit - we're in main! */
    }

    printf("\n  Finished loading %d words.\n", wordCount);

/* HERE IS WHAT OUR ARRAY LOOKS LIKE NOW
    wordArray is pointing to a 50-element block of memory

    wordArray[0] is a char * -> "horse\0"
             [1] is a char * -> "cat\0"
             [2] is a char * -> "rat\0"
             [3] is a char * -> "oryx\0"
             ...      char * -> "bat\0"
             ...      char * -> "alligator\0"
             ...      char * -> "crocodile\0"
             ...      char * -> "pig\0"
             ...      char * -> "monkey\0"
             [9] is a char * -> "aardvark\0"
            [10] is a char * -> ????
             ...
             ...
             ...
            [49] is a char * -> ????

   REMEMBER the 50 char * pointers are stored consecutively in 1 block
   of memory (which is why wordArray[5], for example, is well-defined,
   but the strings are distributed "randomly" throughout the heap.
*/

    printArray(wordArray, wordCount);/* comment this out if using a big file!*/

    return 0;
}
Ejemplo n.º 25
0
int main( int argc, char* argv[] )
{
  
  int results_data[DATA_SIZE];

  // Output the input array

#if HOST_DEBUG
  printArray( "input1", DATA_SIZE, input1_data );
  printArray( "input2", DATA_SIZE, input2_data );
  printArray( "verify", DATA_SIZE, verify_data );
#endif

  // If needed we preallocate everything in the caches

#if PREALLOCATE
  vvadd( DATA_SIZE, input1_data, input2_data, results_data );
#endif

  // Do the vvadd

  setStats(1);
  vvadd( DATA_SIZE, input1_data, input2_data, results_data );
  setStats(0);

  // Print out the results

#if HOST_DEBUG
  printArray( "results", DATA_SIZE, results_data );
#endif

  // Check the results
 
  finishTest(verify( DATA_SIZE, results_data, verify_data ));

}
Ejemplo n.º 26
0
void median( unsigned int answer[], int size_of_array )
{
	int median;
	if (size_of_array % 2 == 0)
	{
		median = (answer[size_of_array/2] + answer[(size_of_array/2)-1]) / 2;
	} else {
		median = answer[size_of_array / 2];
	}
	printf("\n%s\n%s\n%s\n%s",
			"********", "Median", "********",
			"The unsorted array of responses is");

	printArray( answer );
	bubbleSort( answer );

	printf( "%s", "\n\nThe sorted array is");
	printArray( answer );

	printf("\n\nFor this run the median is %u\n\n",
		   median);

	// if length of answer %2 == 0 : then middle value + middle value -1 / 2 = median
}
Ejemplo n.º 27
0
/* 
 Recursive helper function -- given a node, and an array containing 
 the path from the root node up to but not including this node, 
 print out all the root-leaf paths. 
*/ 
void printPathsRecur(struct node* node, int path[], int pathLen)
{
   if(node ==NULL  )
   {    return ;   }
	
   path[pathLen]=node->data;
	pathLen++;
   if (node ->left==NULL && node ->right==NULL) 
   {    printArray( path ,pathLen);   }
   else 
   {
   printPathsRecur (node ->left,path,pathLen);
   printPathsRecur(node->right,path,pathLen);
   }
}
Ejemplo n.º 28
0
//to generate all the permutation of the keys
void permute(int *a, int i, int n) 
{
	int j;
	if(i==n)
		printArray(a,n);
	else 
	{
	        for (j = i; j <= n; j++) 	
		{
	          swap((a+i), (a+j));
	          permute(a, i+1, n);
	          swap((a+i), (a+j)); 
	       }
	}
}
Ejemplo n.º 29
0
void benchmark(int threaded){
    int i, n;
    int *A;
    clock_t start, end;
    double elapsed_time;

    A = (int *) malloc(sizeof(int) * N);
    if (A == NULL) {
        printf("Fail to malloc\n");
        exit(0);
    }
    for (i = N - 1; i >= 0; i--) {
        A[N - 1 - i] = i;
    }

    // print the last ten elements
    printArray(&A[N - 10], 10);
    start = clock();
    if(threaded){
        parallelBubbleSort(A, N);
    }else{
        bubbleSort(A, N);
    }
    end = clock();

    // print the last ten elements
    printArray(&A[N - 10], 10);
//    printArray(A, N);
    elapsed_time = (end - start) / (double) CLOCKS_PER_SEC;
    for(int i = 0; i < N; i++){
        if(A[i] != i){
            printf("Assertion fail at %d != %d\n", i, A[i]);
        }
    }
    printf("elapsed time = %lf\n", elapsed_time);
}
Ejemplo n.º 30
0
int main (int argc, char *argv[]){

	int proc_id,procs_num;
	int iloop;
	int A[totalSize];
	int a[chunkSize];

	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &proc_id);
	MPI_Comm_size(MPI_COMM_WORLD, &procs_num);

	if (proc_id == 0) {
		generateArray("A",A,totalSize);
		printArray("A",A,totalSize,0);
	}

	MPI_Scatter(A, chunkSize, MPI_INT, 
				a, chunkSize, MPI_INT, 
				0, MPI_COMM_WORLD);

	printArray("a",a,chunkSize,proc_id);

	MPI_Finalize();
}