d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C Code Help > Sub Forum Is Rather Dead
Add Reply New Topic New Poll
Member
Posts: 9,775
Joined: Jul 8 2008
Gold: 50.00
Sep 2 2014 10:04am
im stumped here :\

Here is my code:
Code
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>

int* get_grades (int* num);

float calculate_average (int grades[], int num_grades);

double calculate_median (int grades[], int num_grades);

float standard_deviation(int grades[], int num_grades);

void display_average (float ave);

void display_stddev (float dev);

void print_Median(double med);

int main ()
{
int num_grades;
int* grades = get_grades (&num_grades);
if (num_grades == 0)
printf ("no grades\n");
else
{
float ave = calculate_average (grades, num_grades);
double med = calculate_median (grades, num_grades);
float dev = standard_deviation (grades, num_grades);
display_average (ave);
print_Median (med);
display_stddev (dev);
}
return 0;
}

int* get_grades (int* num)
{
assert (num != NULL);
int* grades = malloc (10 * sizeof (int));
if (grades == NULL)
{
printf ("out of resources - terminating\n");
exit(0);
}
else
{
int size = 10;
*num = 0;
int grade;
printf ("enter a grade - negative to terminate: \n");
scanf ("%d", &grade);
while (grade >= 0)
{
if (*num == size)
{
int* temp = malloc ((2 * size) * sizeof (int));
int i;
for (i = 0; i < size; i++)
temp[i] = grades[i];
free (grades);
grades = temp;
size *= 2;
}
grades[*num] = grade;
(*num)++;
printf ("enter a grade - negative to terminate: ");
scanf ("%d", &grade);
}
}
assert (grades != NULL);
return grades;
}

float calculate_average (int grades[], int num_grades)
{
assert (num_grades > 0);
int sum = 0;
int i;
for (i = 0; i < num_grades; i++)
sum += grades[i];
return (float)sum/num_grades;
}

void display_average (float ave)
{
assert (ave >= 0);
printf ("average: %f\n", ave);
}

double calculate_median(int grades[], int num_grades)
{
double temp;
double* sorted = malloc(sizeof(int)*num_grades);
int i,j;
for (i = 0; i < num_grades; i++)
{
sorted[i] = grades[i];
}
for (i = num_grades - 1; i > 0; i--)
{
for (j = 0; j < i; j++)
{
if (sorted[j] > sorted[j+1])
{
temp = sorted[j];
sorted[j] = sorted[j+1];
sorted[j+1] = temp;
}
}
}

if((num_grades%2)==0)
{
return (double)(sorted[num_grades/2]+sorted[num_grades/2-1])/2;
}
else
{
return (double) sorted[num_grades/2];
}
}

void print_Median(double med)
{
printf ("median: %f\n", med);
}

float standard_deviation(int grades[], int num_grades)
{
float mean=0.0, sum_deviation=0.0;

int i;

for(i=0; i<num_grades;++i)
{
mean+=grades[i];
}

mean= mean/num_grades;

for(i=0; i<num_grades;i++)
{
sum_deviation+=(grades[i]-mean)*(grades[i]-mean);
return sqrt(sum_deviation/num_grades);
}

}

void print_stddev (double dev)
{
printf ("Standard deviation: %.2f\n", dev);
}


my output calculates everything but stops working after it prints the median. the debugger says this:
enter a grade - negative to terminate: 1
enter a grade - negative to terminate: 2
enter a grade - negative to terminate: 3
enter a grade - negative to terminate: 4
enter a grade - negative to terminate: -1
average: 2.500000
median: 2.500000
[Inferior 1 (process 3544) exited normally]

any thoughts?
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Sep 2 2014 10:42am
You've declared the function prototype for display_stddev() but the function itself doesn't actually exist.

This post was edited by SelfTaught on Sep 2 2014 10:44am
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Sep 2 2014 11:50am
It shouldn't compile then. It's probably in an another file in the project and the bug is most likely there.

You should work on a naming convention. What's the difference between display_a() and print_a()? Why are some values calculated with calculate_f() and some with f()?
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll