Site hosted by Angelfire.com: Build your free website today!
Master Menu - Links the Master Page


Columns . . . :                     Edit                         GOLFP/SRCFILE
 SEU==>_______________________________________________________             C07
 FMT C* .....C*. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 
        *************** Beginning of data *************************************
/*

  Write a program which uses arrays to solve the following problem. 
  The company you are working for has just taken over a smaller company. 
  the accounting department would like to have a combined file of 
  employee ID numbers and department codes for the two groups. The file
  employee.dat contains both files you just need to reorganize the data
  in ascending order as described below. 

  Output: The ID's are in ascending order. Following the last employee's
  data is a dummy record. It has an ID of 0000 and no department code. 
  Following this is the data for the employees of the larger company, in 
  the same format as the data for the smaler company. Write a new .dat file 
  consisting of an ordered listing of employee ID numbers and department
  codes followed by a 1 if from the smaller company and a 2 if from the 
  larger company, i.e. 3 columns. Create the output in a file called 
  empjrb.dat.

		Written by: Browning ©
		      Date: 09/20/99
  -------------------------------------------------------------------------

Contents of Employee.dat :

1033 A
1051 A
1115 B
1322 F
2318 C
3066 A
4262 B
5150 D
6212 A
8119 B
9813 C
0000
1010 G
1111 D
2222 E
3333 F
4444 A
5555 H
6666 B
7777 D
8888 E
9999 A
---------------------------------------------------------------------------*/

#include 
int main ( void )
   {
FILE *oldRec;     /*Employee.dat*/
FILE *newRec;     /*Empjrb.dat*/
/*-------------------------------------------------------------------------*/
int empID[11];                        /*Identifiaction code array          */
int empDC[11][2];                     /*The array stores 11 strings each
                                        being made up of 2 characters      */ 
int x = 0;                            /*Counter variable                   */
int F;                                /*Variable to continue reading file
                                        after 0 is initally found          */
int count = 0;                        /*A transfer variable holding the                                                          number of
                                        elements contained in the array    */
int id;                               /*Stores the currnet ID              */
char dc;                              /*Stores the currnet Dep. Code       */
/*-------------------------------------------------------------------------*/
                 newRec = fopen("a:\empjrb.dat", "w");
                 oldRec = fopen("a:\employee.dat", "r");
/*-------------------------------------------------------------------------*/
    if (oldRec == NULL)
                        {  printf("File not found on A:\\n");  }
/*-------------------------------------------------------------------------*/
    fscanf(oldRec, "%i", &empID[x]);  /* read & store the first variable   */
         
          while(empID[x] > 0 )        /*Perform a loop unitl 0 is found    */
               {
                 fscanf(oldRec, "%s", &empDC[x]);         
                 x++;
                 fscanf(oldRec, "%i", &empID[x]);
               }
/*-------------------------------------------------------------------------*/
count = x;                            /*Transfer the number of employees 
                                        held in the array to the variable
                                        count                              */
x = 0;                                /*Re-initialize the array variable   */
/*-------------------------------------------------------------------------*/
 F = fscanf(oldRec, "%i %c", &id, &dc);
      do     {                        /*Sort the data                      */
               if(id < empID[x] || x >= count)
                 {
                   fprintf(newRec, "%i %c 2\n", id, dc);
                   F = fscanf(oldRec, "%i %c", &id, &dc);
                 }
          else if(empID[x] < id)
                 {
                   fprintf(newRec, "%i %s 1\n", empID[x], empDC[x]);
                   x++;  
                 }
                                                             
              }while (F != EOF);
/*-------------------------------------------------------------------------*/
   fclose(oldRec);
   fclose(newRec);
return;
   }
/*-------------------------------------------------------------------------*/


Copyright ©1998 - 2001 J. Browning - All rights reserved.
Master Menu - Links the Master Page