HI, CAN SOMEBODY PLEASE HELP ME WITH GETTING THE LAST PART OF THIS PROGRAM WHICH
ID: 3607558 • Letter: H
Question
HI, CAN SOMEBODY PLEASE HELP ME WITH GETTING THE LAST PART OF THIS PROGRAM WHICH IS:
SUMMARY REPORT :
Mean atomic weight = xx.xx
Median atomic weight = xx.xx
The element with the minimum density is sssssssssss
The element with the maximum atomic weight is sssssssssss
A7: ELEMENT DATA BASE
(using input/output files, for loops, if statements, and an array of structures)
INTRODUCTION:
Your company requires you to design a computer based element selection system that will keep track of various elements used for the development of composite materials. The computer based system must be able to extract information from a known reference. The reference is the input file called elements. The input file contains the name of the element, the two-letter symbol, the atomic number, the atomic weight, the density in g/cm3, and the crystal structure. The input file is space delimited.
ASSIGNMENT:
Write a C program that will read the data from the input file into an array of structures. The program will do the following:
Sort the elements by atomic number.
Compute the mean atomic weight of the elements in the data base.
Compute the median atomic weight of the elements in the data base. See the note below.
Determine the element with the maximum atomic weight.
Determine the element with the minimum density.
Your program will print to the computer screen and print to an output file called element_report. The output format is given below.
OUTPUT FORMAT:
***********************************************************
ELEMENT DATA TABLE
Element Symbol Atomic Atomic Density Crystal
Number Weight g/cm^3 Structure
-----------------------------------------------------------
sssssssssss sss xx xxx.xx xx.xx sss
. . . . . .
. . . . . .
. . . . . .
sssssssssss sss xx xxx.xx xx.xx sss
-----------------------------------------------------------
SUMMARY REPORT:
Mean atomic weight = xx.xx
Median atomic weight = xx.xx
The element with the minimum density is sssssssssss
The element with the maximum atomic weight is sssssssssss
***********************************************************
Note: Assuming that the values in an array have been sorted, the median is the value in the middle of the group of values. When the number of data values is odd, then the median is the value in the middle of the array and the array subscript is computed by floor(n/2). When the number of data values is even, then the median is the average of the two middle positions of the array and the array subscripts are computed by floor(n/2)-1 and floor(n/2).
After the array has been sorted, to compute the median atomic weight for this problem first compute a new subscript variable k by:
k = floor(n/2)
where n is the number of elements in the array.
Next, compute the median value in the array by applying the following: If the number of elements modulus 2 (n%2) is not equal to 0, assign median the atomic weight at location k, else assign median the average of the two middle atomic weights at location k-1 and location k.
Explanation / Answer
#include<stdio.h>
#include <stdlib.h>
#include <math.h>
// Structure element definition
typedef struct element
{
// Data member declaration
char name[20];
char symbol[4];
float atomicNumber;
float atomicWeight;
float density;
char crystalStructure[4];
}Ele;// End of structure
// Function to read the file and store the data in appropriate structure element and return number of records
int readFile(Ele el[])
{
// Initializes the record counter to zero
int counter = 0;
//File pointer for numbers.txt file declared and open it in read mode
FILE *fileR = fopen("numbers.txt", "r");
//Checks if file is unable to open then display error message
if (fileR == NULL)
{
puts("Could not open files");
exit(0);
}//End of if
// Extracts a character from numbers.txt file and stores it in structure variable and Loops till end of file
while (!feof(fileR))
{
// Reads data from file and stores it in structure variables
fscanf (fileR, "%s", el[counter].name);
fscanf (fileR, "%s", el[counter].symbol);
fscanf (fileR, "%f", &el[counter].atomicNumber);
fscanf (fileR, "%f", &el[counter].atomicWeight);
fscanf (fileR, "%f", &el[counter].density);
fscanf (fileR, "%s", el[counter].crystalStructure);
// Increase the record counter by one
counter++;
}// End of while loop
// Close the file for keyword
fclose(fileR);
// Returns number of record in the file
return counter;
}// End of function
// Displays the data
void display(Ele el[], int len)
{
// Loop variable
int x;
printf(" ******************************************************************************************** ");
// Displays the heading
printf(" ELEMENT DATA TABLE ");
printf("Element Symbol Atomic Atomic Density Crystal ");
printf(" Number Weight g/cm^3 Structure ");
printf(" -------------------------------------------------------------------------------------------- ");
// Loops till number of records
for(x = 0; x < len; x++)
{
// Displays structure contents
printf("%-16s",el[x].name);
printf("%-16s",el[x].symbol);
printf("%-16.2f",el[x].atomicNumber);
printf("%-16.2f",el[x].atomicWeight);
printf("%-16.2f",el[x].density);
printf("%-10s",el[x].crystalStructure);
printf(" ");
}// End of for loop
}// End of function
// Function to calculate and return mean
float calculateMean(Ele el[], int len)
{
// Initializes total to zero
int x, total = 0;
// To store mean value
float mean;
// Loops till number of records
for(x = 0; x < len; x++)
// Calculates total atomic weight
total += el[x].atomicWeight;
// Calculates mean
mean = (total / (float)len);
// Returns the mean result
return mean;
}// End of function
// Function to calculate and return median
float calculatemedian(Ele el[], int len)
{
// To store median
float median;
// To store current index position and previous index position
int loc, prev;
// Checks for even number of records
if(len % 2 == 0)
{
// Calculate the middle index position
loc = len / 2;
// Calculates previous index position to the middle
prev = (len / 2) - 1;
// Calculates the median
median = ((el[prev].atomicWeight + el[loc].atomicWeight) / 2.0);
}// End of if
// Otherwise
else
{
// Calculate the middle index position
loc = len / 2;
// Calculates the median
median = el[loc].atomicWeight;
}// End of else
// Returns the median result
return median;
}// End of function
// Function to sort the records
void sortElements(Ele el[], int len)
{
// Loop variable
int x, y;
// Creates a temporary object
Ele temp;
//Loops till length of the array
for(x = 0; x < len; x++)
{
//Loops till length of the array minus x minus one times
for(y = 0; y < len - x - 1; y++)
{
//Checks if current index position of the array is greater than the next index position of the array
if(strcasecmp(el[y].name, el[y + 1].name) > 0)
{
//Swapping process
temp = el[y];
el[y] = el[y + 1];
el[y + 1] = temp;
}//End of if condition
}//End of inner for loop
}//End of outer for loop
}// End of function
int minDensity(Ele el[], int len)
{
// Stores the first index position density as minimum
int min = el[0].density;
// Initializes index position to zero
int x, pos = 0;
// Loops from one to length
for(x = 1; x < len; x++)
{
// Checks if the current index position density is greater than the minimum
if(min > el[x].density)
{
// Update the minimum to current index position density
min = el[x].density;
// Update the index position to x value
pos = x;
}// End of if condition
}// End of loop
// Returns the index position
return pos;
}// End of function
int maxWeight(Ele el[], int len)
{
// Stores the first index position weight as maximum
int max = el[0].atomicWeight;
// Initializes index position to zero
int x, pos = 0;
// Loops from one to length
for(x = 1; x < len; x++)
{
// Checks if the current index position weight is less than the maximum
if(max < el[x].atomicWeight)
{
// Update the maximum to current index position weight
max = el[x].atomicWeight;
// Update the index position to x value
pos = x;
}// End of if condition
}// End of for loop
// Returns the index position
return pos;
}// End of function
// main function definition
int main()
{
// Creates an array of objects
Ele elements[100];
int x, counter = 0;
float mean;
float median;
// Calls the function to read data from file
counter = readFile(elements);
// Displays the record before sorting
printf(" Before Sorting ");
display(elements, counter);
// Calls the function to sort the records
sortElements(elements, counter);
// Displays the record after sorting
printf(" After Sorting ");
display(elements, counter);
// Calls the function calculate mean
mean = calculateMean(elements, counter);
printf(" Mean atomic weight = %.2f", mean);
// Calls the function calculate median
median = calculatemedian(elements, counter);
printf(" Median atomic weight = %.2f", median);
// Calls the function to display minimum density product name
printf(" The element with the minimum density is %s", elements[minDensity(elements, counter)].name);
// Calls the function to display maximum atomic weight product name
printf(" The element with the maximum atomic weight is %s", elements[maxWeight(elements, counter)].name);
}// End of main function
Sample Run:
Before Sorting
********************************************************************************************
ELEMENT DATA TABLE
Element Symbol Atomic Atomic Density Crystal
Number Weight g/cm^3 Structure
--------------------------------------------------------------------------------------------
XYZ XX 30.00 22.00 7.00 YY
ABC AA 12.00 14.00 4.00 BB
PQR PP 10.00 11.00 2.00 QQ
MNO MM 8.00 10.00 7.00 NN
After Sorting
********************************************************************************************
ELEMENT DATA TABLE
Element Symbol Atomic Atomic Density Crystal
Number Weight g/cm^3 Structure
--------------------------------------------------------------------------------------------
ABC AA 12.00 14.00 4.00 BB
MNO MM 8.00 10.00 7.00 NN
PQR PP 10.00 11.00 2.00 QQ
XYZ XX 30.00 22.00 7.00 YY
Mean atomic weight = 14.25
Median atomic weight = 10.50
The element with the minimum density is PQR
The element with the maximum atomic weight is XYZ
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.