Question

In: Computer Science

PRELAB - Week of September 16 You’ve created a general-purpose function for allocating an array with...

PRELAB - Week of September 16

You’ve created a general-purpose function for allocating an array with elements of any data type:

array = createArray(numElems, elemSize);

and which supports the following function for obtaining its number of elements:

size = getArraySize(array);

and that’s all you’ll need for this pre-lab. All you’re asked to do for next week is to be able to:

1. Open a file

2. Read an integer giving the number of employee records in the file

3. Allocate an array of employee records (structs) to hold the records in the file

4. Read the records into the allocated array where an employee record is declared as:

typedef struct {

intempID, ssn, position;

float salary;}

Record;

To get comfortable with structs you’ll probably want to loop through the array and print the four members of each record–but you’d probably do that anyway just to verify that you get the results you expect from your test file!

Solutions

Expert Solution

Hi, Please go through code and output.

CODE:

#include <stdio.h>
#include <stdlib.h>

typedef struct {
int empID, ssn, position;
float salary;}
Record;

Record * createArray(int numElems,int elemSize) // create array and return pointer
{
   Record * array = malloc(numElems * elemSize);
   return array;

}

int getArraySize(Record * array) // return array size
{
   int size = 0;
   FILE * fp = NULL;
   fp = fopen("employee.txt","r"); // open file
   if(fp == NULL)
   {
       printf("File no available!\n");
       return -1;
   }
   else
   {
       fscanf(fp, "%d", &size); // read record size from file
       fclose(fp);
   }
   return size; // return records size
}

int main()
{
   Record * array = NULL;
   int size = 0;
   int temp = 0;
   int i =0;
   size = getArraySize(array); // get size
   if(size == -1)
   {
       return 0;
   }
   array = createArray(size, sizeof(Record)); // create array pointer

   FILE * fp = NULL;
   fp = fopen("employee.txt","r"); // open file for read
   if(fp == NULL)
   {
       printf("file can not open!\n");
       return 0;
   }
   else
   {
       fscanf(fp, "%d", &temp); // bypass first element
       for(i=0; i<size; i++) // read data from file and store in array
       {
           fscanf(fp, "%d %d %d %f",&array[i].empID, &array[i].ssn, &array[i].position, &array[i].salary);
       }
       for(i=0; i<size; i++) // print data
       {
           printf("EmpID - %d Ssn - %d Position - %d Record - %lf\n",array[i].empID, array[i].ssn, array[i].position, array[i].salary);
       }
       free(array); // free array
       array = NULL; // assign null
   }
}

OUTPUT:

file: employee.txt

10
1 21 11 11.1
2 22 12 12.1
3 23 13 13.1
4 24 14 14.1
5 25 15 15.1
6 26 16 16.1
7 27 17 17.1
8 28 18 18.1
9 29 19 19.1
10 30 20 20.1

OUTPUT:


Related Solutions

Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function...
Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function that, given an arbitrary 2-d array as input, returns its perimeter sum, which is defined as the sum of all the elements along its perimeter. You must name your function perimeter_sum
Identify and discuss the FOUR (4) main components’ function in Von Neumann architecture for any general-purpose...
Identify and discuss the FOUR (4) main components’ function in Von Neumann architecture for any general-purpose computer based.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT