0

Here my question is calculating the sum of infinite no.of integers where the input is taken from a file.The file contains infinite no.of integers in a line delimited by space.And also I need to display invalid input if those input of integer contain any other characters or symbols.I had tried this code and output well Here is my code....

void main()
{
  int i=1,j,a[100000],total=0,r=0;
  char discard,buffer[1024];
  FILE *fp;
  char filename[100];
  scanf("%s",filename);
  fp=fopen(filename,"r");
  do
  {
    fscanf(fp,"%1024s%c",buffer,&discard);
    r+=sscanf(buffer,"%d",&a[i]);
    total+=a[i++];
  } while(discard!='\n');
  if(r==i-1)
  {
    printf("\n%d",total);
  }
  else
    printf("\n Invalid Input");
}

The code is executing well.But the problem here is the code exceeding my time constraint.Please help me so that i could get a better code

Tezirg
  • 1,629
  • 1
  • 10
  • 20
Megha M
  • 105
  • 1
  • 10
  • Show the first 3-4 lines of your input file. – Jabberwocky Jun 10 '16 at 10:23
  • 9
    Summing an unlimited number of integers will always take an infinite amount of time, hence the violation of your time constraint. – Christian Neverdal Jun 10 '16 at 10:23
  • 1
    @ChristianJonassen the OP must have an amazing harddrive that can contain a file with an infinite number of lines... ;-) – Jabberwocky Jun 10 '16 at 10:24
  • first of all, range of the `int` type variables in c is from -32768 to 32767 so you can not store a value greater than 32767 in a variable of type `int`. – viveksinghggits Jun 10 '16 at 10:26
  • My input file can be 1 2 3 4 5 6 7 8 9 0 1 23 3 5 – Megha M Jun 10 '16 at 10:27
  • Actually I had to upload this code some where where I dont know how much of input it gives.Its all the machine analysis.I had uploaded this code and it is giving me feedback: 5 tests with Time Limit Exceeded So I need to minimize – Megha M Jun 10 '16 at 10:29
  • @MeghaM thanks, but put please edit your question and put it there. – Jabberwocky Jun 10 '16 at 10:34
  • @MeghaM Your input file is finite, yet you say that the input is infinite. What is actually the case? – fuz Jun 10 '16 at 10:47
  • Is there a reason why you need to store the numbers is an array if you are just interested in the sum? – Lundin Jun 10 '16 at 10:50
  • My input file can be larger and larger.I had just plot an example it couls be more then that. – Megha M Jun 10 '16 at 11:04
  • Can I reduce compilation time by removing the array a[100000] above?? – Megha M Jun 10 '16 at 11:12

3 Answers3

1

You can read the file in chunks, speeding up the reading into integers. For hints, look at the fread() example in this link (search for e.g. 'sentinel' in the linked page): How do I process a text file in C by chunks of lines? Also, look at the comments to that answer, e.g. good idea to do malloc instead of stack allocation, maybe use binary mode, heed comment about EOF etc.

Community
  • 1
  • 1
Erik Alapää
  • 2,585
  • 1
  • 14
  • 25
0
  1. Get file size
  2. Allocate dynamic memory buffer via malloc
  3. Read all file into allocated memory buffer.
  4. Make other actions from memory buffer.
Beka
  • 97
  • 5
  • but there is no problem with memory.If I had a problem with memory I could have some other error which I have faced in the earlier statges – Megha M Jun 10 '16 at 10:46
0

I don't know if this will improve performance a lot, but the code is more readable and I also removed the use of stack buffers

void main()
{
  int value = 0, total = 0, r = 0;
  char discard;
  FILE *fp;
  char filename[100];
  scanf("%s",filename);
  fp = fopen(filename,"r");
  do
  {
    r = fscanf(fp, "%d%c", &value, &discard);
    if (r != 2 || (discard != ' ' && discard != '\n'))
    {
      printf("\n Invalid Input");
      return;
    }
    total += value;
  } while(discard != '\n');
  printf("\n%d", total);       
}

(Note that it's untested, but I'm confident)

Tezirg
  • 1,629
  • 1
  • 10
  • 20