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

Programming in C Language. Please refer to images for details about assignment.

ID: 3906077 • Letter: P

Question

Programming in C Language. Please refer to images for details about assignment. Also attached the text file that program gets numbers from.

Blackboard Learn E Haven Plus in Linkedin 9 GroupMe Assignment: On the course website is a file, hu02-data.txt, that contains the data for this assignment. Each row of the data file consists of a comma-delimited set of numbers. For each set of numbers, produce the sum, arithmetic mean, variance, and counts of the digit characters. Additional requirements specific to this assignment: 1. The data should be stored in an array of type char* in main), with each row of the data being in its own string. The data should be hard-coded in your program; don't read it from the file. 2. Each string will be passed separately to three different functions: (a) a function that calculates the sum of the numbers in the string (b) a function that calculates the mean and variance of the numbers in the string (c) a function that counts the number of each type of digit character in the s 3. C contains a function, atof O, that when given a string of digit characters returns the value that the digits represent as a double. atof O is found in stdlib. 4. TO find the suin of the numbers in a string, write a function with the following function declaration double f (char* s); where s is a comma-delimited string The function returns the sum of the numbers in the string. You can rename the function and function parameters, but the types and quantity of function parameters should No printing will be performed from this function. be as given. 5. To find the arithmetic mean and sample variance of the numbers in a string, write a function with the following function declaration: void f(char* s, doublex meanPtr, double* varPtr); @what is tpm Gi) P?chols4 9)1320-hm2P- RobertsonGp." Document2 w w

Explanation / Answer


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

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

double sum(char *s);
void stats(char *s, double *meanPtr, double *varPtr);
void printDigitCounts(char *s);

int main()
{
char s[4][100] = {"12, 34, 56, 78",
"82.16, 41.296",
"2, -3, 5, -7, 11, -13, 17, -19",
"9.00009, 90.0009, 900.009, 9000.09, 90000.9"};

int i;
double mean, variance;
for(i = 0; i < 4; i++)
{
printf("Calculating for string %s ", s[i]);
printf("Sum: %.6f ", sum(s[i]));
stats(s[i], &mean, &variance);
printf("Mean: %.6f ", mean);
printf("Variance: %.6f ", variance);
printDigitCounts(s[i]);
printf("--------------------- ");
}

}

double sum(char *str)
{
char s[100];
char *token;
double total = 0;
strcpy(s, str);
token = strtok(s, ",");
while(token != NULL)
{
total = total + atof(token);
token = strtok(NULL, ",");
}

return total;
}

void stats(char *str, double *meanPtr, double *varPtr)
{
int n = 0;
char *token;
double total = 0;
double mean;
double var = 0, x, diff;
char s[100];

strcpy(s, str);

token = strtok(s, ",");
while(token != NULL)
{
total = total + atof(token);
token = strtok(NULL, ",");
n++;
}
mean = total / n;
*meanPtr = mean;

strcpy(s, str);
token = strtok(s, ",");
while(token != NULL)
{
x = atof(token);
//printf("%f ", x);
diff = x - mean;
var = diff * diff;
token = strtok(NULL, ",");
}

var = var / (n-1);
var = sqrt(var);
*varPtr = var;
}

void printDigitCounts(char *s)
{
int counts[10] = {0};
int i;
char ch;
int digit;

for(i = 0; s[i] != ''; i++)
{
ch = s[i];
if(ch >= '0' && ch <= '9')
{
digit = ch - '0'; //convert from char to digit but subtracting ascii
counts[digit]++; //increment counter for corrsponding digit
}
}

printf("%d", counts[0]);
for(i = 1; i < 10; i++)
printf(", %d", counts[i]);

printf(" ");
}


output
====
Calculating for string 12, 34, 56, 78
Sum: 180.000000
Mean: 45.000000
Variance: 19.052559
0, 1, 1, 1, 1, 1, 1, 1, 1, 0
---------------------

Calculating for string 82.16, 41.296
Sum: 123.456000
Mean: 61.728000
Variance: 20.432000
0, 2, 2, 0, 1, 0, 2, 0, 1, 1
---------------------

Calculating for string 2, -3, 5, -7, 11, -13, 17, -19
Sum: -7.000000
Mean: -0.875000
Variance: 6.850606
0, 5, 1, 2, 0, 1, 0, 2, 0, 1
---------------------

Calculating for string 9.00009, 90.0009, 900.009, 9000.09, 90000.9
Sum: 99999.999990
Mean: 19999.999998
Variance: 35000.450001
20, 0, 0, 0, 0, 0, 0, 0, 0, 10
---------------------