Data ordering C

Asked

Viewed 35 times

1

I am doing an ordering job where the program should read a text file with several lines and do the ordering of values. are 6 files, one with 10000 records, another with 100000 records, another with 1000000 records, another with 10000000, another with 50000000 and another with 100000000. The program is running and doing the normal ordering with the files of 10000 records and 100000 record, but when I run the of 1000000 up it does not give any kind of error it simply opens and closes at the same time.

code with 100000 records:

#include <stdio.h>
#include <conio.h>

void main()
{
FILE *arq;
 int a, b, c, d, e, f;
 double linhas[100000], aux;
 int i, j;

 arq = fopen("random100000s.txt", "rt");

 if (arq == NULL) 
 {
  printf("Problemas na abertura do arquivo\n");
  return;
 }
 i = 0;
 while (!feof(arq))
 {
  fscanf(arq, "%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
  linhas[i] = ((a*365) + (b*30) + (c) + (d*0.041667) + (e*0.00069444) + 
  (f*0.0000115741));
 i++;
 }

 for(i = 0; i <= 100000; i++)
 {
  aux = linhas[i];
  for(j = i; (j > 0) && (aux < linhas[j - 1]); j--)
   linhas[j] = linhas[j - 1];
   linhas[j] = aux;
 }


 for(i = 0; i <= 100000; i++){
  printf("\n%lf", linhas[i]);
 }

 fclose(arq);
}

code with 1000000 records:

    void main()
{
FILE *arq;
 int a, b, c, d, e, f;
 double linhas[1000000], aux;
 int i, j;

 arq = fopen("random1000000s.txt", "rt");

 if (arq == NULL) 
 {
  printf("Problemas na abertura do arquivo\n");
  return;
 }
 i = 0;
 while (!feof(arq))
 {
  fscanf(arq, "%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
  linhas[i] = ((a*365) + (b*30) + (c) + (d*0.041667) + (e*0.00069444) + 
  (f*0.0000115741));
 i++;
 }

 for(i = 0; i <= 1000000; i++)
 {
  aux = linhas[i];
  for(j = i; (j > 0) && (aux < linhas[j - 1]); j--)
   linhas[j] = linhas[j - 1];
   linhas[j] = aux;
 }


 for(i = 0; i <= 1000000; i++){
  printf("\n%lf", linhas[i]);
 }

 fclose(arq);
}

The first wheel and presents correctly already the second no. NOTE: .txt files are all with the same format The only difference is the amount of records.

Can anyone help??

  • It starts that the loop is wrong: i = 0; i <= 1000000, will try to read 1000001 records, is the famous error "off by one". It would have to be i < 1000000. If you have difficulty seeing the problem, test a loop that counts up to 10.

  • in vdd excuse, the Arq has 1000001 records, so much so that the first code are 100001 records and it worked.

  • sera can’t be vector size which is very large v[1000000]?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.