// The main function that calulates distances of shortest paths from src to all
// vertices. It is a O(ELogV) function
void dijkstra(struct Graph* graph, int src)
{
    int V = graph->V;// Get the number of vertices in graph
    int dist[V];      // dist values used to pick minimum weight edge in cut

    // minHeap represents set E
    struct MinHeap* minHeap = createMinHeap(V);

    // Initialize min heap with all vertices. dist value of all vertices
    for (int v = 0; v < V; ++v)
    {
        dist[v] = INT_MAX;
        minHeap->array[v] = newMinHeapNode(v, dist[v]);
        minHeap->pos[v] = v;
    }

    // Make dist value of src vertex as 0 so that it is extracted first
    minHeap->array[src] = newMinHeapNode(src, dist[src]);
    minHeap->pos[src]   = src;
    dist[src] = 0;
    decreaseKey(minHeap, src, dist[src]);

    // Initially size of min heap is equal to V
    minHeap->size = V;

    // In the followin loop, min heap contains all nodes
    // whose shortest distance is not yet finalized.
    while (!isEmpty(minHeap))
    {
        // Extract the vertex with minimum distance value
        struct MinHeapNode* minHeapNode = extractMin(minHeap);
        int u = minHeapNode->v; // Store the extracted vertex number

        // Traverse through all adjacent vertices of u (the extracted
        // vertex) and update their distance values
        struct AdjListNode* pCrawl = graph->array[u].head;
        while (pCrawl != NULL)
        {
            int v = pCrawl->dest;

            // If shortest distance to v is not finalized yet, and distance to v
            // through u is less than its previously calculated distance
            if (isInMinHeap(minHeap, v) && dist[u] != INT_MAX &&
                                          pCrawl->weight + dist[u] < dist[v])
            {
                dist[v] = dist[u] + pCrawl->weight;

                // update distance value in min heap also
                decreaseKey(minHeap, v, dist[v]);
            }
            pCrawl = pCrawl->next;
        }
    }

    // print the calculated shortest distances
    printArr(dist, V);
}
Exemple #2
0
int main()
{
   int A[200];
   int B[200];
   int C[200];
   srand(time(NULL));
   randomArr(A,B,C);
   printArr(A,B,C);
}
int main(){
	int a[]={1,34,3,98,9,76,45,4};
	int n=sizeof(a)/sizeof(a[0]);
	findMax(a,n);
	printf("\nMaximum number is:");
	printArr(a,n);
	printf("\n");
	return 0;
}
Exemple #4
0
void main(){
	int x, arr[MAX_ARR];	
	do{
		printf("x=");
		scanf("%d", &x);
	}while( x < 0 || x >= 10 );
	fillArray(arr, MAX_ARR, x);
	printArr( arr, MAX_ARR );
}
int main() {
	ARRAYLIST myArrayList = arraylist_init();
	myArrayList->Array = malloc(10*sizeof(int));

    int j,k,l,m,n;
    j = 1;
    k = 2;
    l = 5;
    n = 3;
	m = 4;

    arraylist_prepend(myArrayList, &j);
	printArr(myArrayList);

    arraylist_append(myArrayList, &k);
    arraylist_append(myArrayList, &l);
    arraylist_append(myArrayList, &j);
    arraylist_append(myArrayList, &n);
	printArr(myArrayList);

	arraylist_insert(myArrayList, &m, 1);//insert 4 to index 1
	printArr(myArrayList);

    int u = 8;
	arraylist_set(myArrayList, &u, 3);// set index 3 as 8
	printArr(myArrayList);

    int *a = arraylist_get(myArrayList, 3);//get address of index 3
	printf("get %d\n", *a);

    arraylist_insert(myArrayList, &m, 10);//insert 4 to index of 10 which does not exist
	printArr(myArrayList);

    arraylist_insert(myArrayList, &k, 15);//insert 2 to index of 15 which does not exist
    printArr(myArrayList);

    arraylist_set(myArrayList, &k, 7);//set index 7 as 2
	printArr(myArrayList);

    arraylist_remove(myArrayList, 1);//remove index of 9
	printArr(myArrayList);

    arraylist_removeall(myArrayList);//remove everything
    printArr(myArrayList);

    arraylist_free(myArrayList);
    return 1;
}
Exemple #6
0
int main(void)
{
	int arr[] = {34, 55, 3, 554, 345};
	int len = sizeof(arr) / sizeof(int);

	printArr(arr, len);
	printf("----------after---\n");

	return 0;
}
Exemple #7
0
void main()
{
//int arr[] = {-2,1,-3,4,-1,2,1,-5,4};
int arr[] = {-1,0 };
int start, end, maxsum;
int n1 = sizeof(arr)/sizeof(arr[0]);
maxsum = maxSubArray(arr,n1);
printArr(arr, n1);
printf("Max Sum is %d\n", maxsum);
}
Exemple #8
0
int main(void) {
	int n , m;
	printf ("vvedite razmer matritsu:\n");
	fflush(stdin);
	scanf (" %i %i" , &n, &m);
	int arr[n][m];
	fillRand(n , m , arr);
	printArr( n , m , arr);
	return 0;
}
Exemple #9
0
int dijkstra(PWDG graph, int src, int dst) {

  int V = graph->count;
  int dist[V];

  // Clone node:
  if (src == dst) {
    wdg_clone(graph, src);
    dst = V; V++;
  }

  pMinHeap minHeap = createMinHeap(V);

  for (int v=0; v<V; ++v) {
    dist[v] = INT_MAX;
    minHeap->array[v] = newMinHeapNode(v, dist[v]);
    minHeap->pos[v] = v;
  }

  dist[src] = 0;
  decreaseKey(minHeap, src, dist[src]);
  minHeap->size = V;

  while (!isEmpty(minHeap)) {

    pMinHeapNode minHeapNode = extractMin(minHeap);
    int u = minHeapNode->v;

    if (u == dst) {
      wdg_unclone(graph);
      return dist[u];
    }

    PNODE pCrawl = graph->list[u].head;

    while (pCrawl != NULL) {

      int v = pCrawl->dst;

      if (isInMinHeap(minHeap, v) && dist[u] != INT_MAX && pCrawl->wgt + dist[u] < dist[v]) {
        dist[v] = dist[u] + pCrawl->wgt;
        decreaseKey(minHeap, v, dist[v]);
      }

      pCrawl = pCrawl->next;
    }
  }

  #ifdef DEBUG
  printArr(dist, V, src);
  #endif

  wdg_unclone(graph);
  return -1;
}
Exemple #10
0
int main()
{
  int totalNodesInTree , inFixArr[20] , preFixArr[20] ;
  tree *root = NULL;
  tree* pred = NULL;
  int sum;
  
  printf("\nEnter length of arr : ");
  scanf("%d",&totalNodesInTree);


  printf("\nEnter values of nodes in preorder\n");
  scanArr(preFixArr,totalNodesInTree);
  printArr(preFixArr,totalNodesInTree);


  printf("\nEnter values of nodes in inorder\n");
  scanArr(inFixArr,totalNodesInTree);
  printArr(inFixArr,totalNodesInTree);


  printf("\n\nPost Fix Order as shown :\n");
  root = toPostFix(inFixArr,preFixArr,0,totalNodesInTree-1,0);
  printf("\n");
  printf("after creating tree :- \n");
  printf("\nPrefix Notation\n");
  prefix(root);
  printf("\nInfix Notation\n");
  infix(root);
  printf("\nPostfix Notation\n");
  postfix(root);
  printf("\n");

  if(isBst(root, &pred)){
    printf("Its BST \n");
  }
  else{
    printf("Its not BST\n");
  }
  printf("Final root pred => %d\n",pred -> data);
  return 0;
}
Exemple #11
0
int main()
{
    int totalNodesInTree, inFixArr[15],preFixArr[15],nodeToBeDeleted;
  treeNode *root = NULL;
  
  printf("\nEnter length of arr : ");
  scanf("%d",&totalNodesInTree);


  printf("\nEnter values of nodes in preorder\n");
  scanArr(preFixArr,totalNodesInTree);
  printArr(preFixArr,totalNodesInTree);


  printf("\nEnter values of nodes in inorder\n");
  scanArr(inFixArr,totalNodesInTree);
  printArr(inFixArr,totalNodesInTree);


  printf("\n\nPost Fix Order as shown :\n");
  root = toPostFix(inFixArr,preFixArr,0,totalNodesInTree-1,0);
  printf("\n");
  printf("after creating tree :- \n");
  printf("\nPrefix Notation\n");
  prefix(root);
  printf("\nInfix Notation\n");
  infix(root);
  printf("\nPostfix Notation\n");
  postfix(root);
  printf("\n");

/*  printf("Enter node to be deleted :- ");
  scanf("%d",&nodeToBeDeleted);
  root = deleteFromBst(root,nodeToBeDeleted);
*/
  ClearTree(&root);
  printf("After dispose :%d\n",root->data);
  postfix(root);
  printf("\n");

  return 0;
}
int main (void){
    
    
    //경우1 테스트
    clock_t start, end;
    double duration1=0;
    int list1[] ={15, 11, 9, 8, 7, 5, 3, 2};
    int size = ((int)sizeof(list1)/sizeof(int));
    
    printArr(list1, size);
    start= clock();
    selectionSort(list1, size);
    end= clock();
    printArr(list1, size);
    
    duration1 = ((double)end-start)/CLOCKS_PER_SEC;
    
    printf("time : %f \n\n", duration1);
    
    FILE *time = fopen("time", "wb");
    char temp[100];
    
    int i, n, step = 100;
    int a[MAX_SIZE];
    double duration;
    
    for(n=0; n<=MAX_SIZE; n+=step){
        
        for(i=0;i<n;i++) a[i] = n - i;
        start = clock();
        selectionSort(a, n);
        end = clock();
        
        duration = ((double) (end - start))/CLOCKS_PER_SEC;
        
        sprintf(temp, "time %d : %f\n", n, duration);
        fputs(temp, time);
    }
    
    fclose(time);
    return 0;
}
Exemple #13
0
int main() {
    unsigned size ;
    printf("\n enter size of array:");
    scanf("%u" , &size);
    int arr[size];
    fillArray(size , arr);
    sumArray(size ,arr);
    printArr(size ,arr);
    getchar();
    return 0;
}
Exemple #14
0
void main(){
	int arr[MAX_ARR], x;
	do{
		printf("x=");
		scanf("%d", &x);
	}while( x < 0 || x > 10 );
	
	if(fillArray(arr, MAX_ARR, x)){
		printArr(arr, MAX_ARR);
	}
}
Exemple #15
0
// Driver program to test above functions
int main()
{
	int arr[] = { 3,0};
	// int arr[] = {4, 3, 5, 2, 1, 3, 2, 3};
	int n = sizeof( arr ) / sizeof( *arr );
	printf("n: %d\n", n);
	quickSortIterative( arr, 0, n - 1 );
	printArr( arr, n );
	printf("\n");
	return 0;
}
Exemple #16
0
int main2()
{
	size = 10;
	int i;
	int a[size];
	generateSortedArr(a,size);
	printArr(a,size);
	scanf("%d",&i);
	printf("Index is = %d.\n",value(a,size,i));

	return 0;
}
Exemple #17
0
void main03()
{
	Teacher Array[3];
	int i=0;
	int num=3;
	for(i=0;i<num;i++){
		printf("\n enter your age:");
		scanf("%d",&(Array[i].age));
	}
	for(i=0;i<num;i++){
		printf("age:%d\n",Array[i].age);
	}
	printf("print===============\n");
	printArr(Array,num);//数组首元素代表地址
	sortArr(&Array,num);
	printf("print===============\n");
	printArr(Array,num);//数组首元素代表地址
	printf("%s\n","hello,world!");
	system("pause");

}
Exemple #18
0
void main(){
	int x, arr[MAX_ARR];
	
	do{
		printf("x=");
		scanf("%d", &x);
		printf("\n");
	}while( x <= 0 || x >= 10 );
	fillArray(arr, MAX_ARR);
	bubbleSort(arr, endsWith, MAX_ARR, x);
	printArr(arr, MAX_ARR);
}
Exemple #19
0
int main(void) {
    int i;
    int pos=3;
    
    STU stuArr[stuNum] = {
        {13010470, "박수빈", 2, 100, 100, 100},
        {10182929, "남궁성", 1, 40, 20, 60},
        {16273271, "이재승", 3, 80, 100, 20}
    };
    
    STU* pArr[stuNum];
    for(i=0;i<pos;i++)
        pArr[i] = &stuArr[i];
    
    printArr(pArr, &pos);
    puts("");
    addStudent(pArr, &pos);
    printArr(pArr, &pos);
    
    
}
Exemple #20
0
int main(){

	int x, arr[SIZE];
	
	x = enterX();

	findFib(x, arr);

	printArr(arr);

	return 0;
}
Exemple #21
0
int main()
{
  int hight,totalNodesInTree, inFixArr[15],preFixArr[15],nodeToBeDeleted;
  treeNode *root = NULL;
  
  printf("\nEnter length of arr : ");
  scanf("%d",&totalNodesInTree);


  printf("\nEnter values of nodes in preorder\n");
  scanArr(preFixArr,totalNodesInTree);
  printArr(preFixArr,totalNodesInTree);


  printf("\nEnter values of nodes in inorder\n");
  scanArr(inFixArr,totalNodesInTree);
  printArr(inFixArr,totalNodesInTree);


  printf("\n\nPost Fix Order as shown :\n");
  root = toPostFix(inFixArr,preFixArr,0,totalNodesInTree-1,0);
  printf("\n");
  printf("after creating tree :- \n");
  printf("\nPrefix Notation\n");
  prefix(root);
  printf("\nInfix Notation\n");
  infix(root);
  printf("\nPostfix Notation\n");
  postfix(root);
  printf("\n");

//  printf("Enter node to be deleted :- ");
  //scanf("%d",&nodeToBeDeleted);
  //root = deleteFromBst(root,nodeToBeDeleted);

  hight = hightOfBtree(root);
  printf("Hight of Tree is :- %d\n",hight);

  return 0;
}
Exemple #22
0
void automataTests()
{
    int moves[5] = {18, 18, 208, 5, 208};
    int checkRes[20] = {55, 5, 2};
    int movesLen = sizeof(moves)/sizeof(moves[0]);
    int res[20];
    int resLen = sizeof(res)/sizeof(res[0]);
    int output = run(moves, movesLen, res, resLen);
    puts("These are moves:");
    printArr(moves, movesLen);
    puts("These are results from index 0 to return value:");
    printArr(res, output);
    puts("These are expected results with zeros:");
    printArr(checkRes, resLen);
    printf("Automata is working %scorrectly \n", arrIsEqual(res, checkRes, resLen) ? "" : "in");
    puts("");
    int moves1[5] = {18, 10, 18, 208, 18};
    movesLen = sizeof(moves1)/sizeof(moves1[0]);
    int res1[2] = {0};
    resLen = sizeof(res1)/sizeof(res1[0]);
    int checkRes1[2] = {55, 11};
    output = run(moves1, movesLen, res1, resLen);
    puts("These are moves:");
    printArr(moves1, movesLen);
    puts("These are results from index 0 to return value:");
    printArr(res1, output);
    puts("These are expected results with zeros:");
    printArr(checkRes1, resLen);
    printf("Automata is working %scorrectly\n", arrIsEqual(checkRes1, res1, resLen) ? "" : "in");
}
Exemple #23
0
int main()
{
  int arr[25],i,value;

  printf("\nEnter length of array : ");
  scanf("%d",&length);

  printf("\nEntre elemnets :- \n");

  for(i=0;i<length;i++)
    {
      scanf("%d",&arr[i]);
    }

  printArr(arr);
  swap(arr,2,5);
  //  value = partition(arr,0,length-1);
  quickSort(arr,0,length-1);
  printArr(arr);

  return 0;
}
Exemple #24
0
void bubble_sort ( int arr[], int size ) {
    for (int i = 0; i <= size - 2; i++) {
        for (int j = size - 1; j >= i + 1; j--) {
            if (arr[j] < arr[j - 1]) {
                arr[j] ^= arr[j - 1];
                arr[j - 1] ^= arr[j];
                arr[j] ^= arr[j - 1];
                printf("Intermediary array: ");
                printArr (arr, size);
            }
        }
    }
}
Exemple #25
0
int main(){
	int x, arr[SIZE], i;
	
	x = enterX();

	for(i=1; i<=SIZE; i++){
		arr[i-1] = sumN(x*i);
	}

	printArr(arr);

	return 0;
}
Exemple #26
0
int main() {

    int arr[] = {1,2,3,4,5,6,7,8,9,10};
    int arr2[2][2];
    int arr3[2][2];
    int *p = arr+1;
    int i, j;

    printf("%d %d %d \n", arr[0], arr[-5], arr[-21]);
    printf("%d %d %d \n", p[0], p[-1], p[-2]);
    printf("avg: %f\n\n", avg(arr, 10));

    set_arr1(arr2, 2);
    printArr(arr2, 2);

    set_arr2(arr3, 2);
    printArr(arr3, 2);

    test_pointer_arr();
    mul_matrix(arr2, arr3);
    return 0;
}
Exemple #27
0
// Basic test. Makes @size chunks of 64 elements and writes in them random values, reverse them and checks if they are ok. 
// IF @flagForPrintValues is set, prints the valus of the arrays(and the result for every chunk).
void testValueValidation(void(*pFunc)(char*, int), int size, char flagForPrintValues)
{
	// Allocate memory for the control values(@arr) and the same one in @arrReversed which will be reversed
	int sizeMul64 = size << 6;
	char * arr = new char[sizeMul64];
	char * arrReversed = new char[sizeMul64];

	printf("Starting value validation tests with %d chunks (%d elements)!\n", size, sizeMul64);

	// Fill with some random values.
	srand(0);
	for (size_t i = 0; i < sizeMul64; ++i)
		arr[i] = arrReversed[i] = rand() % 256 - 128; // -128 to 127

	// Prints the origin values.
	if (flagForPrintValues)
		printArr(arr, sizeMul64);

	// Reverse it in every chunk
	pFunc(arrReversed, size);

	if (flagForPrintValues)
	{
		printf("\n[REVERSED]\n\n");

		// Prints the reversed values values.
		printArr(arrReversed, sizeMul64);
		printf("\nLet us check the values:\n");
	}

	// Check if it`s reversed correctly.
	if (compareArrayWithReversedOneByChunks(arr, arrReversed, size, flagForPrintValues))
		printf(" ---> [Passed!]\n");
	else
		printf(" ---> [Failed!]\n");
	// Delete memory
	delete[] arr;
	delete[] arrReversed;
}
Exemple #28
0
int main(void) {

	sf::RenderWindow window(sf::VideoMode(1920,1080), "TEST");
	int word[8] = {7,3,6,2,6,9,1,6};
	printArr(word, 8);
	SORT(word, word + 8);
	printArr(word, 8);

	char numbers[6] = "54321";
	SORT(numbers, numbers + 5);
	printf("%s\n", numbers);

	sf::Sprite sprite;
	sf::Texture texture;
	texture.loadFromFile("../../bin/tileSheets/walkSequence.png");
	//texture.loadFromFile("../../bin/planet.png");

	sprite.setTexture(texture);
	sprite.setTextureRect(sf::IntRect(6144-512,512,512,512));
	HitBoxBase<sf::FloatRect> hbox = *(HitBoxBase<sf::FloatRect>*)GenerateHitBox(sprite);
	printv(hbox._hbox.top);	
	printv(hbox._hbox.left);	
	printv(hbox._hbox.width);	
	printv(hbox._hbox.height);	
	while (window.isOpen()) {
		sf::Event event;
		while (window.pollEvent(event)) {
			if (event.type == sf::Event::Closed)
				window.close();
		}
		window.clear();
		window.draw(sprite);
		window.display();
	}
	

	return 0;
}
int main()
{
	int StockPrices[] =
		{100, 113, 110, 85, 105, 102, 86, 63, 81, 101, 94, 106, 101, 79, 94, 90, 97};
	int n = sizeof(StockPrices)/sizeof(int);
	printArr(StockPrices, n);
	// find change in price from the previous day
	int prices[n-1];
	for(int i = 1; i <n; i++) {
		prices[i-1] = StockPrices[i] - StockPrices[i-1];
	}
	printArr(prices, n-1);
	// Should be {13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7};
	// the objective now is to maximize the sum of the consecutive
	// elements of the subarray
	// because that would mean the difference between the first ans last elements
	// of the subarray is maximum => max profit 
	// use modified version of Kadane's algorithm
	std::pair<int, int> result = Kadane(prices, 16);
	printResults(StockPrices, prices, result);

	return 0;
}
Exemple #30
0
int main(){
	int n;
	scanf("%d",&n);
	int arr[n][n];
	
	int magicNumber = magicSquare(n);
	printf("Magic Number is %d\n",magicNumber);
	
	initialize(n,arr);
	
	printArr(n, arr);
	
	return 0;
}