Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Submit the source code (your filename.c file) electronically and on paper as a h

ID: 3811967 • Letter: S

Question

Submit the source code (your filename.c file) electronically and on paper as a hard copy. Include copy of the results as comment lines in the completed program. All source code must be in a filename.c format. No other formats will be accepted. Phase I General Preprocessing items: Use mnemonic variables of 3 characters or more. Use void main (void) or void main in your program and leave off the return 0. Use double as the choice for all floating point values. Add heading items using as printf statement. Watch for instructions that specify a symbolic constant and assign that symbolic constant in all capital letters and using the #define. Thank you. The "xxxx" in a sample output will show locations that the computer program will transfer values. Phase II General procedures: A line of information in the program should be no longer than 80 characters. All user-defined function problems should include the student's last name as part of their function name. All user- defined function written for the class must have a prototype. Prototypes should be placed before the main. The user-defined functions will be located in the program after the main program. Note: Our class must have all of its results, variables, outputs stored in memory Problem page 38 from book. Tasks Send the Output from the Drogram to a data file or text file. our rules Use the book program, but make all variables 3 characters or more and use variables that make sense Use your last name as part of the array name. We will do that from this point on Use void main(void) for our programs and leave off the return(0) for our programs Use the type double for floating point numbers in our programs. Print program, print data file, upload program, and upload datafile Send the output from the program to a data file or text file. (Use the filename: lastname arraytext firstinitial.c) The rules for the use of an output file (data file or text file) The data file must have the student's lastname keyword included in the filename. The data file should have the extension txt The cprogram file must open the data with a statemer The cprogram file must close the datafile with a statement required: Electronic submission and paper copy of program code and Electronic submission and paper copy of text files

Explanation / Answer

lastname_arraytext_initial.c

#include <stdio.h>
#include <math.h>

#define MAX_ITEM 8

void main(void)
{
//Opening file for writing
FILE *fptr = fopen("lastname_arraytext.txt", "w");
//Checking if file is opened or not. Program will continue only if file is opened
if (fptr == NULL)
{
printf("Error opening file..Terminating ");
return;
}
// Declaring variables
double array_lastname[MAX_ITEM],mean,standard_deviation,sum_of_elements,sum_of_squares;
int i;
//Prompting user for input
printf("Enter %d numbers separated by blanks or <returns> ",MAX_ITEM);
//Taking input from user
for(i = 0; i < MAX_ITEM; i++)
scanf("%lf",&array_lastname[i]);
//Initializing values to zero
sum_of_elements = 0;
sum_of_squares = 0;
// Calculating sum of elements and sum of squares of elements
for(i = 0; i < MAX_ITEM; i++)
{
sum_of_elements += array_lastname[i];
sum_of_squares += array_lastname[i] * array_lastname[i];
}
//Calculating mean
mean = sum_of_elements / MAX_ITEM;
//Calculating standard deviation
standard_deviation = sqrt(sum_of_squares / MAX_ITEM - mean * mean);
//If file is opened, writing values to file
fprintf(fptr,"The mean is %.2lf. ",mean);
fprintf(fptr,"The standard deviation is %.2lf. ",standard_deviation);
fprintf(fptr," The table of differences between each item and the mean ");
fprintf(fptr,"Index Item Difference ");
for(i = 0; i < MAX_ITEM; i++)
fprintf(fptr,"%3d %9.2f %9.2f ",i+1,array_lastname[i],array_lastname[i] - mean);
//Closing file after writing
fclose(fptr);

}

lastname_arraytext.txt:

The mean is 4.50.
The standard deviation is 2.29.

The table of differences between each item and the mean
Index Item Difference
1   1.00   -3.50
2   2.00   -2.50
3   3.00   -1.50
4   4.00   -0.50
5   5.00   0.50
6   6.00   1.50
7   7.00   2.50
8   8.00   3.5