Ir ao conteúdo
  • Cadastre-se

Natal junio

Membro Júnior
  • Posts

    8
  • Cadastrado em

  • Última visita

posts postados por Natal junio

  1. Então preciso construir um programa em C, que:

    –O usuário escolhe o tamanhos do vetor ((a)5!, (b)10!, (c)15!, (d) 20! ou (e) 25!).

    –Após a escolha do tamanho, o vetor é preenchido utilizando função Randon.

    –O programa deve:

    •Ordenar uma cópia deste vetor original para cada método de ordenação.

    •Contar quantas comparações e trocas são feitas, e o tempo em ms gasto por cada método de ordenação.

     

     

    queria saber se tem como usar o seguinte codigo que fiz, caso alguem consiga também poderia me ajudar.?
     

    #include <stdlib.h>
    #include <stdio.h>
    #include <locale.h>
    #include <time.h>
    #include <windows.h>
    
    #define m 1000
    #define k 10000
    #define o 100000
    #define p 400000
    #define true 1
    
    void insertionSort(int vet[ ],int n)
    {
    	int i,j,aux;
    	for (i=1; i<n; i++)
    	{
    		aux = vet[i];
    		
    		for(j=i-1; j>=0 && vet[j] > aux; j--)
    		{
    			vet[j+1]=vet[j];
    		}
    		vet[j+1]=aux;
    	}
    }
    
    void mergeSort(int vetor[], int tamanho){
    	
      int meio = tamanho / 2;
      int i = 0;
      int j = meio;
      int t = 0;
      int aux[tamanho];
      
    	while( i < meio && j < tamanho ){
    	    if( vetor[i] <= vetor[j] )
    	    	aux[t] = vetor[i++];
    	    else
    	    	aux[t] = vetor[j++];
    	    t++;
    	}
     
    	if( i == meio ){
        	while( j < tamanho ){
                aux[t] = vetor[j++];
                t++;
          }
    	}else {
        	while( i < meio ){
                aux[t] = vetor[i++];
                t++;
        	}
    	}
    	
    	for( i = 0; i < tamanho; i++ ){
        	vetor[i] = aux[i];
    	}
    }
    
    int mergeSortRec(int vetor[], int tamanho)
    {
    	int meio = tamanho / 2;
    
    	if( tamanho > 1 ){
    		mergeSortRec(vetor, meio);
    		mergeSortRec(vetor + meio, tamanho - meio);
    		mergeSort(vetor, tamanho);
    	}
    }
    
    void selectionSort(int vet[], int tam){
    	
    	int i, j, eleito, menor, pos;
    	
    	for(i=0; i<=tam; i++){
    		eleito = vet[i];
    		menor = vet[i+1];
    		pos = i+1;
    		
    		for(j=i+1; j<=tam; j++){
    			if(vet[j] < menor){
    				menor = vet[j];
    				pos = j;
    			}
    		}
    		
    		if(menor < eleito){
    			vet[i] = vet[pos];
    			vet[pos] = eleito;
    		}
    	}
    }
    
    void bubbleSort(int vet[], int tam){
    	
    	int i, n, aux;
    	int troca;
    	
    	troca = 1;
    	
    	while (n < tam && troca == 1){
    		troca = 0;
    		
    		for(i=0; i<tam; i++){
    			if(vet[i] > vet[i+1]){
    				troca = 1;
    				aux = vet[i];
    				vet[i] = vet[i+1];
    				vet[i+1] = aux;
    			}	
    		}
    		
    		n = n+1;
    	}
    }
    
    void quickSort(int vet[], int esq, int dir) {
        
    	int i, j, pivo, aux;
    	int qtd;
         
        i = esq;
        j = dir;
        
        pivo = vet[(esq + dir) / 2];
        
        while(i <= j) {
        	
            while(vet[i] < pivo && i < dir) {
                i++;
            }
            
            while(vet[j] > pivo && j > esq) {
                j--;
            }
            
            if(i <= j) {
                aux = vet[i];
                vet[i] = vet[j];
                vet[j] = aux;
                i++;
                j--;
            }
        }
        
        if(j > esq) {
            quickSort(vet, esq, j);
        }
        if(i < dir) {
            quickSort(vet, i, dir);
        }
    }
    
    void heapSort(int a[], int n) {
    	
    	int i = n / 2, pai, filho, t;
       
    	while(true) {
       	
    		if (i > 0) {
    			i--;
            	t = a[i];
        	} else {
    			n--;
              	if (n <= 0) return;
              	
              	t = a[n];
              	a[n] = a[0];
          	}
          	
    		pai = i;
    		
          	filho = i * 2 + 1;
          	
    		while (filho < n) {
            	if ((filho + 1 < n)  &&  (a[filho + 1] > a[filho]))
                  	filho++;
    
              	if (a[filho] > t) {
                 	a[pai] = a[filho];
                 	pai = filho;
                 	filho = pai * 2 + 1;
              	} else {
                	break;
            	}
          }
          
    	  a[pai] = t;
       }
    }
    
    zerarVetor(int vet[], int vetCopy[], int tam){
    	int i;
    	for(i = 0; i < tam; i++){
    		vetCopy[i] = vet[i];
    	}
    }
    
    imprimiVetor(int vet[], int tam){
    	int i;
    	printf("Vetor: ");
    	for(i = 0; i < tam; i++){
    		printf("[%d] ",vet[i] );
    	}
    }
    
    int existe(int valores[], int tam, int valor){
    	int i;
    	
    	for(i=0; i<tam; i++){
    		if(valores[i]==valor){
    			return 1;
    		}
    	}
    	return 0;
    }
    
    void geraAleatorio(int num[], int quant, int limite){
    	
    	int i, val;
    	
    	for(i=0; i<quant; i++){
    		
    		val = rand() % limite;
    		
    		while(existe(num, i, val)){ 
    			val = rand() % limite;
    		}
    		num[i] = val;
    	}
    }
    
    calcula(int tam){	
    	int i;
    	//vetores originais
    	int *vet = (int *) malloc(tam * sizeof(int));
    	
    	//vetores copidas, para que assim sempre seja ordenado os memos números
    	int *vetCopy = (int *) malloc(tam * sizeof(int));
    	
    	//variaveis para calcular o tempo
     	LARGE_INTEGER tempoInicial, tempoFinal, freq;
     	float tempoTotal;	
    	
    	//preencer vetores de 1000
    	geraAleatorio(vet,tam,(tam+1));
    		
    	zerarVetor(vet, vetCopy, tam);
    		
    	//calcular tempo de ordenação para vetores tamanho m, k, o ou p.
    	//m = 1000   |    k = 10000   |     o = 100000    |    p = 400000
    	
    	srand(time(NULL));
    	QueryPerformanceCounter(&tempoInicial);
    	insertionSort(vetCopy,tam);
    	QueryPerformanceCounter(&tempoFinal);
    	QueryPerformanceFrequency(&freq);
    	tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 			
    	printf("Insertion Sort: %f ms", tempoTotal);
    	printf("\n");
    	zerarVetor(vet, vetCopy, tam);
    				    			
    	srand(time(NULL));
    	QueryPerformanceCounter(&tempoInicial);
    	selectionSort(vetCopy,tam);
    	QueryPerformanceCounter(&tempoFinal);
    	QueryPerformanceFrequency(&freq);
    	tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 			
    	printf("Selection Sort: %f ms", tempoTotal);
    	printf("\n");
    	zerarVetor(vet, vetCopy, tam);
    				    			
    	srand(time(NULL));
    	QueryPerformanceCounter(&tempoInicial);
    	mergeSortRec(vetCopy,tam);
    	QueryPerformanceCounter(&tempoFinal);
    	QueryPerformanceFrequency(&freq);
    	tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 			
    	printf("Merge Sort: %f ms", tempoTotal);
    	printf("\n");
    	zerarVetor(vet, vetCopy, tam);
    				    			
    	srand(time(NULL));
    	QueryPerformanceCounter(&tempoInicial);
    	heapSort(vetCopy,tam);
    	QueryPerformanceCounter(&tempoFinal);
    	QueryPerformanceFrequency(&freq);
    	tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 			
    	printf("Heap Sort: %f ms", tempoTotal);
    	printf("\n");
    	zerarVetor(vet, vetCopy, tam);
    				    			
    	srand(time(NULL));
    	QueryPerformanceCounter(&tempoInicial);
    	bubbleSort(vetCopy, tam);
    	QueryPerformanceCounter(&tempoFinal);
    	QueryPerformanceFrequency(&freq);
    	tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 			
    	printf("Bubble Sort: %f ms", tempoTotal);
    	printf("\n");
    	zerarVetor(vet, vetCopy, tam);
    				    			
    	srand(time(NULL));
    	QueryPerformanceCounter(&tempoInicial);
    	quickSort(vetCopy, 0, tam-1);
    	QueryPerformanceCounter(&tempoFinal);
    	QueryPerformanceFrequency(&freq);
    	tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 			
    	printf("Quick Sort: %f ms", tempoTotal);
    	printf("\n");
    	
    	zerarVetor(vet, vetCopy, tam);	
    	
    	free(vet);
    	free(vetCopy);
    }
    
    int main()
    {
    	setlocale (LC_ALL, "");
    			
    	//m = 1000   |    k = 10000   |     o = 100000    |    p = 400000  
    					
    	printf("Tempo de Exeução em Milissegundos");
    	
    	printf("\n\nCom 1000 números:\n\n");
    	printf("calculando ... Aguarde...\n\n");
    	calcula(m);
    	
    	printf("\n\nCom 10000 números:\n\n");
    	printf("calculando ... Aguarde...\n\n");
    	calcula(k);
    	
    	printf("\n\nCom 100000 números:\n\n");
    	printf("calculando ... Aguarde...\n\n");
    	calcula(o);
    	
    	printf("\n\nCom 400000 números:\n\n");
    	printf("calculando ... Aguarde...\n\n");
    	calcula(p);
    	
    	printf("\n\n");
    	system("pause");
    	return 0;
    }

     

  2. @arfneto entendo, No caso eu teria que criar a Função, ou apenas substituir?

    quando executo o se código no programa da o seguinte erro, nessa linha.

    for (int i = 1; i <= 3; i += 1)	seletor(i, VFT);

      [Error] 'for' loop initial declarations are only allowed in C99 or C11 mode

      [Note] use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code

  3. alguém tem uma ideia de como posso fazer o código sem o meu menu pra executar todos de uma vez?, eu tentei remover o menu, pra rodar, mais da erro quando executar e compila. o código mostra o tempo de cada Método de ordenação. a quantida eu tava pensado em reduzir também, para M 100, K 1000, 10000

    #include <stdlib.h>
    #include <stdio.h>
    #include <locale.h>
    #include <time.h>
    #include <windows.h>
    
    #define m 1000
    #define k 10000
    #define o 100000
    #define p 400000
    #define true 1
    
    void menu()
    {
    	printf("\n\nEscolha a quantidade de numeros que deseja saber o tempo!  \n");
    	printf("\n 1 - 1000 números");
    	printf("\n 2 - 10.000 números");
    	printf("\n 3 - 100.000 números");
    	printf("\n 4 - 400.000 números");
    	printf("\n 5 - SAIR\n\n");
    	
    }
    
    void menu2()
    {
    	printf("\n\nEscolha o metodo de ordenacao!  \n");
    	printf("\n 1 - Insertion Sort");
    	printf("\n 2 - Selection Sort");
    	printf("\n 3 - Buble Sort");
    	printf("\n 4 - Merge Sort");
    	printf("\n 5 - Quick Sort");
    	printf("\n 6 - Heap Sort");
    	printf("\n 7 - SAIR\n\n");
    	
    }
    
    void Insertion_Sort(int vet[ ],int n)
    {
    	int i,j,aux;
    	for (i=1; i<n; i++)
    	{
    		aux = vet[i];
    		
    		for(j=i-1; j>=0 && vet[j] > aux; j--)
    		{
    			vet[j+1]=vet[j];
    		}
    		vet[j+1]=aux;
    	}
    }
    
    void Merge_Sort(int vetor[], int tamanho){
    	
      int meio = tamanho / 2;
      int i = 0;
      int j = meio;
      int t = 0;
      int aux[tamanho];
      
    	while( i < meio && j < tamanho ){
    	    if( vetor[i] <= vetor[j] )
    	    	aux[t] = vetor[i++];
    	    else
    	    	aux[t] = vetor[j++];
    	    t++;
    	}
     
    	if( i == meio ){
        	while( j < tamanho ){
                aux[t] = vetor[j++];
                t++;
          }
    	}else {
        	while( i < meio ){
                aux[t] = vetor[i++];
                t++;
        	}
    	}
    	
    	for( i = 0; i < tamanho; i++ ){
        	vetor[i] = aux[i];
    	}
    }
    
    int Merge_SortRec(int vetor[], int tamanho)
    {
    	int meio = tamanho / 2;
    
    	if( tamanho > 1 ){
    		Merge_SortRec(vetor, meio);
    		Merge_SortRec(vetor + meio, tamanho - meio);
    		Merge_Sort(vetor, tamanho);
    	}
    }
    
    void selectionSort(int vet[], int tam){
    	
    	int i, j, eleito, menor, pos;
    	
    	for(i=0; i<=tam; i++){
    		eleito = vet[i];
    		menor = vet[i+1];
    		pos = i+1;
    		
    		for(j=i+1; j<=tam; j++){
    			if(vet[j] < menor){
    				menor = vet[j];
    				pos = j;
    			}
    		}
    		
    		if(menor < eleito){
    			vet[i] = vet[pos];
    			vet[pos] = eleito;
    		}
    	}
    }
    
    void bubbleSort(int vet[], int tam){
    	
    	int i, n, aux;
    	int troca;
    	
    	troca = 1;
    	
    	while (n < tam && troca == 1){
    		troca = 0;
    		
    		for(i=0; i<tam; i++){
    			if(vet[i] > vet[i+1]){
    				troca = 1;
    				aux = vet[i];
    				vet[i] = vet[i+1];
    				vet[i+1] = aux;
    			}	
    		}
    		
    		n = n+1;
    	}
    }
    
    void quickSort(int vet[], int esq, int dir) {
        
    	int i, j, pivo, aux;
    	int qtd;
         
        i = esq;
        
        j = dir;
        
        pivo = vet[(esq + dir) / 2];
        
        while(i <= j) {
        	
            while(vet[i] < pivo && i < dir) {
                i++;
            }
            
            while(vet[j] > pivo && j > esq) {
                j--;
            }
            
            if(i <= j) {
                aux = vet[i];
                vet[i] = vet[j];
                vet[j] = aux;
                i++;
                j--;
            }
        }
        
        if(j > esq) {
            quickSort(vet, esq, j);
        }
        if(i < dir) {
            quickSort(vet, i, dir);
        }
    }
    
    void heapSort(int a[], int n) {
    	
    	int i = n / 2, pai, filho, t;
       
    	while(true) {
       	
    		if (i > 0) {
            	
    			i--;
            	
            	t = a[i];
        	} else {
            	
    			n--;
            	
              	if (n <= 0) return;
              	
              	t = a[n];
    			 
              	a[n] = a[0];
          	}
          	
    		pai = i;
    		
          	filho = i * 2 + 1;
          	
    		while (filho < n) {
            	if ((filho + 1 < n)  &&  (a[filho + 1] > a[filho]))
                  	filho++;
    
              	if (a[filho] > t) {
                 	a[pai] = a[filho];
                 	pai = filho;
                 	filho = pai * 2 + 1;
              	} else {
              		// Sai do laco de repeticao
                	break;
            	}
          }
          
    	  a[pai] = t;
       }
    }
    
    int main()
    {
    	setlocale (LC_ALL, "");
    	int vet[p],i,j,op,op2;
     	LARGE_INTEGER tempoInicial, tempoFinal, freq;
     	float tempoTotal;
     	
     	do{
     		menu2();
     		scanf("\n\n%d",&op2);
     		switch(op2){
     			case 1:
     				do{
    					menu();
    					scanf("\n\n%d",&op);			
    					switch(op)
    					{
    						case 1:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<m; i++)
    								{
    				    				vet[i] = rand() % m;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								Insertion_Sort(vet,m);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 2:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<k; i++)
    								{
    				    				vet[i] = rand() % k;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								Insertion_Sort(vet,k);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    							break;
    							
    						case 3:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<o; i++)
    								{
    				    				vet[i] = rand() % o;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								Insertion_Sort(vet,o);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    							break;
    						
    						case 4:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<p; i++)
    								{
    				    				vet[i] = rand() % p;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								Insertion_Sort(vet,p);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    							break;
    						case 5:
    							system ("cls");
    							printf("\n\nFinalizado\n\n");
    							return 0;
    							break;
    					}
    				}while(op != 5);
    			case 2:
    				do{
    					menu();
    					scanf("\n\n%d",&op);			
    					switch(op)
    					{
    						case 1:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<m; i++)
    								{
    				    				vet[i] = rand() % m;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								selectionSort(vet, m);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 2:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<k; i++)
    								{
    				    				vet[i] = rand() % k;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								selectionSort(vet, k);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 3:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<o; i++)
    								{
    				    				vet[i] = rand() % o;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								selectionSort(vet, o);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 4:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<p; i++)
    								{
    				    				vet[i] = rand() % p;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								selectionSort(vet, p);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 5:
    							system ("cls");
    							printf("\n\nFinalizado\n\n");
    							return 0;
    							break;
    					}
    				}while(op != 5);
    			case 3:
    	do{
    					menu();
    					scanf("\n\n%d",&op);			
    					switch(op)
    					{
    						case 1:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<m; i++)
    								{
    				    				vet[i] = rand() % m;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								bubbleSort(vet, m);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 2:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<k; i++)
    								{
    				    				vet[i] = rand() % k;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								bubbleSort(vet, k);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 3:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<o; i++)
    								{
    				    				vet[i] = rand() % o;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								bubbleSort(vet, o);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 4:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<p; i++)
    								{
    				    				vet[i] = rand() % p;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								bubbleSort(vet, p);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 5:
    							system ("cls");
    							printf("\n\nFinalizado\n\n");
    							return 0;
    							break;
    					}
    				}while(op != 5);
    			case 4:
    				do{
    					menu();
    					scanf("\n\n%d",&op);			
    					switch(op)
    					{
    						case 1:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<m; i++)
    								{
    				    				vet[i] = rand() % m;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								Merge_SortRec(vet, m);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 2:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<k; i++)
    								{
    				    				vet[i] = rand() % k;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								Merge_SortRec(vet, k);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 3:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<o; i++)
    								{
    				    				vet[i] = rand() % o;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								Merge_SortRec(vet, o);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 4:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<p; i++)
    								{
    				    				vet[i] = rand() % p;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								Merge_SortRec(vet, p);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 5:
    							system ("cls");
    							printf("\n\nFinalizado\n\n");
    							return 0;
    							break;
    					}
    				}while(op != 5);
    			case 5:
    				do{
    					menu();
    					scanf("\n\n%d",&op);			
    					switch(op)
    					{
    						case 1:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<m; i++)
    								{
    				    				vet[i] = rand() % m;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								quickSort(vet, 0, m-1);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 2:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<k; i++)
    								{
    				    				vet[i] = rand() % k;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								quickSort(vet, 0, k-1);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 3:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<o; i++)
    								{
    				    				vet[i] = rand() % o;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								quickSort(vet, 0, o-1);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 4:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<p; i++)
    								{
    				    				vet[i] = rand() % p;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								quickSort(vet, 0, p-1);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 5:
    							system ("cls");
    							printf("\n\nFinalizado\n\n");
    							return 0;
    							break;
    					}
    				}while(op != 5);
    			case 6:
    				do{
    					menu();
    					scanf("\n\n%d",&op);			
    					switch(op)
    					{
    						case 1:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<m; i++)
    								{
    				    				vet[i] = rand() % m;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								heapSort(vet, m);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 2:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<k; i++)
    								{
    				    				vet[i] = rand() % k;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								heapSort(vet, k);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 3:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<o; i++)
    								{
    				    				vet[i] = rand() % o;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								heapSort(vet, o);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 4:
    							system ("cls");
    							srand(time(NULL));
    							for (i=0; i<p; i++)
    								{
    				    				vet[i] = rand() % p;
    								}
    								printf("Aguarde...");
    								QueryPerformanceCounter(&tempoInicial);
    								heapSort(vet, p);
    					 			QueryPerformanceCounter(&tempoFinal);
    					 			QueryPerformanceFrequency(&freq);
    					 			tempoTotal = (float)(tempoFinal.QuadPart - tempoInicial.QuadPart)/freq.QuadPart;
    					 
    				    			printf("\n\n");
    				    			printf(" Tempo de Exeução em Milissegundos: %f\n", tempoTotal);
    						break;
    						case 5:
    							system ("cls");
    							printf("\n\nFinalizado\n\n");
    							return 0;
    							break;
    					}
    				}while(op != 5);
    			case 7:
    				system ("cls");
    				printf("\n\nFinalizado\n\n");
    				break;
    		 }
    	}while(op2 != 7);
    	printf("\n\n");
    	system("pause");
    	return 0;
    
    }

     

  4. preciso que o programa me mostre a quantidade de trocas feitas no vetor, e a maior sequencia de números impares (Se houver), alguém pra ajudar?

     

     

     

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define tam 10

    int main()
    {
     int numeros[tam];
     
     int i, aux, contador;
    printf("Entre com dez números para o vetor: \n");
     for (i = 0; i < tam; i++) {
     scanf("%d", &numeros);
     }
    printf("Ordem do vetor:\n");
    for (i = 0; i < tam; i++) {
     printf("%4d", numeros);
    }

     for (contador = 1; contador < tam; contador++) {
       for (i = 0; i < tam - 1; i++) {
         if (numeros < numeros[i + 1]) {
           aux = numeros;
           numeros = numeros[i + 1];
           numeros[i + 1] = aux;
         }
       }
     }
    printf("\n Ordem Decrescente do vetor: \n");
    for (i = 0; i < tam; i++) {
     printf("%4d", numeros);
    }
    printf("\n");
    return 0;
    }

    • Curtir 1
  5. Preciso de um vetor com 10 números inteiros aleatórios. Fazer um programa que desloque o menor valor do vetor para a ultima posição. e que mostre como fica o vetor no final do programa. alguém pra ajudar?

     

     

     #include <stdio.h>
     #include <stdlib.h>
     #include <conio.h>
     
     int main(void)
     {
        int vetor[10];
        int x, i;
     
        printf ("digite 10 numeros\n");
        for (i = 0; i < 10; i++) 
        {
           scanf("%d", &vetor);
        }
        i = 1;
        x = vetor[0];
     
        while (i < 10) 
        {
            if (vetor > x)
            {
               x = vetor;
            }
            i++;
        }
     
        printf("\n O maior numero que voce digitou foi %d .\n",x);
     getch ();
        return 0;
     }

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

 

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!