4
I have a problem when passing data from my file (integer numbers) to the vector.
The purpose of the program below is to check the performance of the sorting algorithms (Mergesort, Bubble Sort, Quicksort) but whenever I put a number of data above 42, the locking program gives crash.
Below are the algorithms to generate the.txt file and the other to sort the vector respectively.
PS: The code is a bit messy, sorry.
TXT Generator Code:
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
int main (void)
{
FILE *x;
x = fopen("ex.txt","wt");
if(x == NULL)
{
printf("Erro");
return(-1);
}
int i,n,y;
printf("Quantos nums serao gerados? ");
scanf("%d",&n);
system("cls");
clock_t inicio = clock();
for(i=0; i<n; ++i){
y= (rand()%100);
fprintf(x,"%d\n", y);
//printf("%d\n",y);
}
fclose(x);
clock_t fim = clock();
double gasto = difftime(fim,inicio)/CLOCKS_PER_SEC;
printf("\ntempo gasto: %f segundos\n", gasto);
return(0);
}
Vector ordering code:
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
int qtd;
void quicksort(int *P, int tam);
void mergesort(int p1[],int i,int j);
void merge(int p2[],int i1,int j1,int i2,int j2);
void mergesort(int p1[],int i,int j)
{
clock_t inicio = clock();
int meio;
if(i<j)
{
meio=(i+j)/2;
mergesort(p1,i,meio);
mergesort(p1,meio+1,j);
merge(p1,i,meio,meio+1,j);
}
clock_t fim = clock();
double gasto = difftime(fim,inicio)/CLOCKS_PER_SEC;
printf("\ntempo gasto: %f segundos\n", gasto);
}
void merge(int p2[],int i1,int j1,int i2,int j2)
{
int aux[qtd];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1 && j<=j2)
{
if(p2[i]<p2[j])
aux[k++]=p2[i++];
else
aux[k++]=p2[j++];
}
while(i<=j1)
aux[k++]=p2[i++];
while(j<=j2)
aux[k++]=p2[j++];
for(i=i1,j=0;i<=j2;i++,j++)
p2[i]=aux[j];
}
void quicksort(int *P, int tam) {
clock_t inicio = clock();
if (tam < 2) {
return;
}
int pivo = P[tam/2];
int i3, j3;
for (i3 = 0, j3 = tam - 1; ; i3++, j3--) {
while (P[i3] < pivo){
i3++;
}
while (P[j3] > pivo){
j3--;
}
if (i3 >= j3) {
break;
}
int aux2 = P[i3];
P[i3] = P[j3];
P[j3] = aux2;
}
quicksort(P, i3);
quicksort(P + i3, tam - i3);
clock_t fim = clock();
double gasto = difftime(fim,inicio)/CLOCKS_PER_SEC;
printf("\ntempo gasto: %f segundos\n", gasto);
}
int main () {
FILE *x;
char c,arq[100];
int i,y,op,co;
int p[qtd];
printf("Digite o nome do arquivo: ");
gets(arq);
system("cls");
x = fopen(arq,"r");
for (c = getc(x); c != EOF; c = getc(x))
if (c == '\n')
qtd = qtd + 1;
fclose(x);
x = fopen(arq,"r");
fseek(x, 0, SEEK_SET);
while(!feof(x)){
for(i=0;i<qtd;i++){
fscanf(x,"%d",&y);
p[i]=y;
}
}
fclose(x);
clock_t inicio = clock();
do{
printf("Menu\n");
printf("1. Bubblesort\n");
printf("2. Mergesort\n");
printf("3. Quicksort\n");
scanf("%d",&op);
system("cls");
clock_t fim = clock();
double gasto = difftime(fim,inicio)/CLOCKS_PER_SEC;
switch(op){
case 1:
int aux, k, j;
for(k=qtd-2;k>=0;k--){
for(j=0;j<=k;j++){
if(p[j]>p[j+1] ){
aux=p[j];
p[j]=p[j+1];
p[j+1]=aux;
}
}
}
printf("Vetor ordenado: \n");
for(k=0;k<qtd;k++){
printf("%d\n",p[k]);
}
printf("\ntempo gasto: %f segundos\n", gasto);
system("pause");
system("cls");
return 0;
break;
case 2:
mergesort(p,0,qtd-1);
printf("\nVetor Ordenado: \n");
for(i=0;i<qtd;i++){
printf("%d\n",p[i]);
}
system("pause");
system("cls");
return 0;
break;
case 3:
quicksort(p,qtd);
printf("Vetor ordenado: \n");
for (co = 0; co < qtd; co++) {
printf("%d\n", p[co]);
}
system("pause");
system("cls");
return 0;
break;
default:
printf("Entrada invalida!\n");
printf("\ntempo gasto: %f segundos\n", gasto);
system("pause");
system("cls");
return 0;
break;
}
}while(op != 4);
return 0;
}
Why do you have
return 0;
and soon afterbreak;
?– Victor Stafusa
on what part of the error? in file generation? while trying to read the file? while trying to sort the array?
– Christian Beregula
I tested your random number generation and it’s okay.
– Rafael Coelho
A hint, if you have repeated instructions on all cases on your switch, put those instructions only once off the swith.
– Rafael Coelho
Your biggest problem with code is that you use the global variable Qtd and use it to determine the vector size. You can declare the vector after knowing the amount of it.
– Rafael Coelho