Homework Help please...

Feb 25, 2008 at 12:03am
Hi All,

I have no programing knowledge so I need help solving the following problem:

The breakdown is as follows:
1)Write a program that can average a series of numbers that are held in an array.

2)Extend the program so that it can compute the difference between the average and each number.

3)Extend the program so that it sums the differences in #2.

4)Extend the program so that it computes the square of the sum of the differences, divides the result by ‘n-1,’ where n is the # of numbers in the array, and then finally computing the square root, which is the standard deviation.

5)Encapsulate the entire stddev process in a separate function and have the mainline invoke the function.

6)Move the stddev function into a separate file called, ‘stddev.c,’ and create a header file called, ‘stddev.h,’ which is included by both stddev.c and stddev.h
Feb 25, 2008 at 3:37am
I'm sorry to say that I think it unlikely that you will find anyone who wants to do your homework for you. You should have paid attention in class.

If you give it your best shot and post back with what you've got, we'll help you refine it.
Feb 25, 2008 at 4:53am
Thanks Duoas. These are self study courses, so there are no classes. Anyways, here's what I have got so far:

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

int main(int argc, char *argv[])

{

int x1, y2, z3;
float average;
float sum;
x1 = 20;
y2 = 50;
z3 = 100;
sum = x1 + y2 + z3;
average = sum/3;
printf("Your sum of x1, y2 and z3 is %f\n", sum);
printf ("The average of x1, y2 and z3 %f\n", average);
system("PAUSE");
return 0;
}
Last edited on Feb 25, 2008 at 4:53am
Feb 25, 2008 at 7:54am
Ah, self-study. My hat's off to you.

This should work over arrays, so I would change
int x1, y2, z3;
into
int xs[] = { 20, 50, 100 };

Now use a "for" loop to iterate through the array to sum all the numbers. There are a lot of examples on google for using arrays and loops.

As a suggestion, make a big array, use a loop to get the numbers from the user, and use another variable to keep track of how many elements of the array are actually being used:
1
2
3
4
const unsigned max_count = 100;  /* whatever your max is */
int xs[ max_count ];  /* the array */
unsigned count = 0;  /* current number of elements in use */
...


Let us know how it goes.
Feb 25, 2008 at 9:18pm
For a beginner, that's not a bad start.

Some people prefer to use cin.get(); instead of system("pause"); to maintain cross-platform compatibility, and minimize usage of system resources, but I'd say you're off to a good start.
Last edited on Feb 25, 2008 at 9:19pm
Feb 25, 2008 at 9:27pm
Yes, it saddens me that teaching system("pause") is so entrenched. For homework, it doesn't really matter much.

When dealing with human input, the best thing to remember is that humans will always press ENTER when they are done. Hence, always assume that input is of the form:
<zero or more characters><enter>

Hence, instead of pause or get, use
cin.ignore( numeric_limits<streamsize>::max(), '\n' );

Enjoy.
Topic archived. No new replies allowed.