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
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;
}
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
constunsigned max_count = 100; /* whatever your max is */
int xs[ max_count ]; /* the array */
unsigned count = 0; /* current number of elements in use */
...
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.
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' );