I can post this question twice and will award 3000 points to whomever gets a wor
ID: 3569449 • Letter: I
Question
I can post this question twice and will award 3000 points to whomever gets a working compiling program! Just post a working half here and comment so I can post the other one and post the other half afterwards.
Problem Description using C:
Big Jim is opening a new pizza restaurant and needs a program that reads a data file containing his employee information and prints a summary report of employees and salary. His data file contains two kinds of transactions that his reporting program must process: an employee insertion transaction and a reporting transaction.
An insertion transaction starts with the tag INSERT and the rest of the line in the text file contains the employee data Big Jim needs for adding an employee: an employee id, full name, age, and salary. A reporting transaction is a line in the text file containing only the tag REPORT.
Your program must read the provided data file line-by-line. When your program encounters an insertion transaction, you must dynamically allocate space to create a new employee struct and store a pointer to the new struct in your dynamically-allocated employees pointer array. If your employee pointer array is full, you must call realloc to allocate additional space in the pointer array to store 5 more pointers. Your array will initially start at a capacity of 0 which means in order to store the first employee, you must allocate space to store 5 pointers (note that you can also use realloc for the initial allocation). Every time you realloc your pointer array, your program should inform the user of the array growth by printing a notification in the following format:
When your program encounters a reporting transaction, Big Jim wants a list of all employees in a simple tabular layout along with summary information after the table containing total # of employees, total salary, and average salary. Below is an example (your program MUST follow the same format):
If a report is requested and there are no employees, your program should output:
The format of an insertion transaction in the file is a pipe-delimited set of fields:
INSERT||||
is an int
is a string (you must dynamically allocate as a char * of appropriate size) is an int
is a double
The format of a reporting transaction is simply:
REPORT
Your program must use a header file (employee.h) for the definition of the employee struct. Use a typedef in your header file for simplicity and readability. You must keep a dynamically allocated array of pointers in which to store references to your dynamically created employee structs. Dont try to use a statically allocated array of employee pointers. One of the primary goals of this assignment is to acquaint you with the functions necessary for the dynamic growth of data at runtime.
extra: if you can add:
Add support for a 3rd transaction type in the data file: deletion of an employee record.
A deletion transaction starts with the tag DELETE and is always followed by the employee id of the record to delete. To implement deletion, search your employee array (looking through each struct via its pointer) to see if the id matches the one you need to delete. If you find a match, free the employee record appropriately (including the employee name string member) and set the pointer to that struct in the employee pointer array to NULL.
Explanation / Answer
#include<stdio.h>
#define SIZE 50
struct student {
char name[30];
int rollno;
int sub[3];
};
void main() {
int i, j, max, count, total, n, a[SIZE], ni;
struct student st[SIZE];
clrscr();
printf("Enter how many students: ");
scanf("%d", &n);
/* for loop to read the names and roll numbers*/
for (i = 0; i < n; i++) {
printf(" Enter name and roll number for student %d : ", i);
scanf("%s", &st[i].name);
scanf("%d", &st[i].rollno);
}
/* for loop to read ith student's jth subject*/
for (i = 0; i < n; i++) {
for (j = 0; j <= 2; j++) {
printf(" Enter marks of student %d for subject %d : ", i, j);
scanf("%d", &st[i].sub[j]);
}
}
/* (i) for loop to calculate total marks obtained by each student*/
for (i = 0; i < n; i++) {
total = 0;
for (j = 0; j < 3; j++) {
total = total + st[i].sub[j];
}
printf(" Total marks obtained by student %s are %dn", st[i].name,total);
a[i] = total;
}
/* (ii) for loop to list out the student's roll numbers who
have secured the highest marks in each subject */
/* roll number who secured the highest marks */
for (j = 0; j < 3; j++) {
max = 0;
for (i = 0; i < n; i++) {
if (max < st[i].sub[j]) {
max = st[i].sub[j];
ni = i;
}
}
printf(" Student %s got maximum marks = %d in Subject : %d",st[ni].name, max, j);
}
max = 0;
for (i = 0; i < n; i++) {
if (max < a[i]) {
max = a[i];
ni = i;
}
}
printf(" %s obtained the total highest marks.", st[ni].name);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.