Master File updating problem

This is a discussion on Master File updating problem within the C Programming forums, part of the General Programming Boards category; I'm expecting this output: ACC @ USAGE 101 @ 1302 102 @ 1062 105 @ 1004 159 @ 1031 160 ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    51

    Master File updating problem

    I'm expecting this output:

    ACC @ USAGE

    101 @ 1302
    102 @ 1062
    105 @ 1004
    159 @ 1031
    160 @ 895
    1071 @ 2316
    1176 @ 1914
    1286 @ 1872
    1395 @ 1362
    2999 @ 1702


    Yet.... my program give me the following output...
    ACC == USAGE
    ACC:101 == 1302 KW
    ACC:102 == 1062 KWh
    ACC:105 == 1004 KWh
    ACC:159 == 1031 KWh
    ACC:160 == 895 KWh
    ACC:1071 == 2316 KWh
    ACC:1176 == 1914 KWh
    ACC:1286 == 1872 KWh
    ACC:1395 == 1362 KWh
    ACC:3000 == 1367 KWh <--- weired!!

    Please help me to find out the problem .... if possible correct it...

    thanks!!
    Code:
    #include <stdio.h>
    FILE *om, *nm, *tran;
    
    int main(void)
    {
    	int c_acc_no,kwh_used=0,c_acc_no_t,daily_kwh_used_t,day_t,writeok=0,tran_acc[29],tran_kwh[29],i,j,newacc[10],newkwh[10];
    	char split_c,c_type,name[25],add[30];
    	double total;
    	om=fopen("oldmaster.dat","r");
    	nm=fopen("newmaster.dat","w");
    	tran=fopen("trans.dat","r");//open trans file with read mode
    
    	for(i=0;i<=10;i++)
    	{
    		newkwh[i]=0;
    		newacc[i]=0;
    	}
    
    	//fscanf from transaction file for account number and kilo watts used then store them to array
    	for(i=1;i<30;i++)
    	{
    		fscanf(tran,"%d%c%d%c%d%c",&tran_acc[i],&split_c,&tran_kwh[i],&split_c,&day_t,&split_c);
    		//printf("%d : %d - %d\n",i,array_acc[i],array_kwh[i]);
    	}
    
    	for(j=1,i=2;i<=30;i++)//FOR loop until i equal 30
    	{
    		printf("%dL: %d - %d\n",i,tran_acc[i-1],tran_acc[i]);
    
    		//if account number from tran_acc[n-1] (pervious acc num) is same with current tran_acc[n]
    		//then add pervious kilo watt and current kilo watt to newkwh[i-1]
    		//else store pervious(n-1) to newacc (ready to 
    		if(tran_acc[i-1]==tran_acc[i])
    		{
    			newkwh[j]+=(tran_kwh[i-1]);
    		}
    		else
    		{
    			printf("NOT EQUAL %d != %d\n\n",tran_acc[i-1],tran_acc[i]);
    			newkwh[j]+=(tran_kwh[i-1]);
    			newacc[j]=tran_acc[i-1];
    			j++;
    		}
    	}
    
    	for(i=1;i<=10;i++)
    		printf("ACC:%d == %d KWh\n",newacc[i],newkwh[i]);
    
    		fcloseall();//close all file
    		printf("Completed\n\n\n\t\t\tNew Master File Updated !!!\a\n");
    }
    ================
    here the trans.dat file
    ================
    101#545#2#
    101#512#5#
    101#245#1#
    102#130#4#
    102#821#2#
    102#111#3#
    105#236#2#
    105#256#1#
    105#512#3#
    159#160#3#
    159#384#4#
    159#487#5#
    160#320#1#
    160#225#7#
    160#350#3#
    1071#996#1#
    1071#653#5#
    1071#667#7#
    1176#992#6#
    1176#472#5#
    1176#450#3#
    1286#970#5#
    1286#680#4#
    1286#222#3#
    1395#204#4#
    1395#394#1#
    1395#764#3#
    2999#701#5#
    2999#666#6#
    2999#335#1#

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    An array of size 3 can be indexed using 0, 1, or 2, but not 3.

    You are looping through newacc and newkwh sometimes starting at 0, and other times at 1. And since their size is 10, an index of 10 is invalid.

    Re-evaluate your loop variables.

    gg

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    thanks a lot codeplug!! i already figure out my problem... my problem is the array declaration...

    How to make the coding run faster and optimize ?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How to make the coding run faster and optimize ?
    First determine if it's worth the effort. Profile your speed and if it exceeds your acceptable performance, take a look at your algorithm and see if you can come up with something faster.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    Code:
    for(i=0;i<=10;i++)
    {
    	newkwh[i]=0;
    	newacc[i]=0;
    }
    Ok.. i think this coding is not effecient... is there any other better way to do so other than this {0,0,....0}?

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    There is nothing inefficient with that code. Since you're using a constant to control your loop, it will be extremely easy for the optimizer to unroll it.

    Unfortunately, you still have the same problem as before. An array declared as arr[10] can be indexed as arr[0] through arr[9].
    So your for loop should be setup like this:

    for(i=0; i<10; i++)

    Not:

    for(i=0; i<=10; i++)

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by megablue
    Code:
    for(i=0;i<=10;i++)
    {
    	newkwh[i]=0;
    	newacc[i]=0;
    }
    Ok.. i think this coding is not effecient... is there any other better way to do so other than this {0,0,....0}?
    Sure, just initialize the first value, the rest are set to zero automaticly.
    Code:
    int foo[SIZE] = {0}; /* all elements are zero */
    int bar[SIZE] = {1}; /* first is 1, the rest are zero */
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    .
    Join Date
    Nov 2003
    Posts
    307
    If you're going to do this for production - don't use arrays.
    We have 700,000 transformers which would make a pretty big array.

    You have set the array size to the number of expected records.
    What happens if you get the number of records wrong, or over time the number changes?

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Assuming that "trans.dat" is sorted by 'account' and you are only interested in the sum of each 'kwh' amounts for a given 'account', something like this might work.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       FILE *file = fopen("trans.dat", "r");
       if ( file )
       {
          struct sRecord { int account, kwh, day; } line, current = {-1};
          int items;
          do {
             items = fscanf(file, "%d#%d#%d#", &line.account, &line.kwh, &line.day);
             switch ( items )
             {
             case 3:
                if ( line.account == current.account )
                {
                   current.kwh += line.kwh;
                }
                else
                {
                   if ( current.account >= 0 )
                   {
             default:
                      printf("ACC: %4d = %4d kWh\n", current.account, current.kwh);
                   }
                   current = line;
                }
             }
          } while ( items == 3 );
          fclose(file);
       }
       return 0;
    }
    
    /* my output
    ACC:  101 = 1302 kWh
    ACC:  102 = 1062 kWh
    ACC:  105 = 1004 kWh
    ACC:  159 = 1031 kWh
    ACC:  160 =  895 kWh
    ACC: 1071 = 2316 kWh
    ACC: 1176 = 1914 kWh
    ACC: 1286 = 1872 kWh
    ACC: 1395 = 1362 kWh
    ACC: 2999 = 1702 kWh
    */
    [The hideous use of the default case was intentional for my own amusement.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21