| molecularman (107) | ||||
|
I have an assignment where I was provided some code and I have to write a couple functions. I'll write the assignment and then the code I have so far. Define a function that takes a partially filled array of numbers as its arguments and returns the standard deviation of the numbers in the partially filled array. 1.The numbers in the array will be of type double. The do-while loop inside main() reads in numbers and put them into the array. This do-while loop should continue to read in numbers until either: the user types a negative number, or MAXSIZE has been read. Write the condition for the do-while. 2.Then write a function that will accept an array and print out the contents of the array. This function should accept an array and the number of elements that are to be printed. 3.Then write another function that will accept an array and calculate the standard deviation of the elements of the array. This function should accept an array and the number of elements that are to be printed. This function will also call the average function, which is provided for you. Main Problems: The control variable in the for loop in the printarray function increments one too many times. Here is my code:
here is some output:
| ||||
|
|
||||
| iHutch105 (947) | |||
|
If your array contains five elements, they are indexed 0 - 4. This loop...
...has six iterations: 0, 1, 2, 3, 4, 5. The condition needs to be i < size not i <= size.
| |||
|
Last edited on
|
|||
| scapegote (27) | |
Well, there are several issues. But the one you are asking about it because of this line. for (int i=0; i <= size; i++)you need just just make it "i < size" because the arrays indexes start at s[0]. | |
|
|
|
| molecularman (107) | |
|
Thanks for the quick help guys. Is there a way that I can just display the numbers in the array? I would like to just say "The contents of the array are:" once. By the way, would it be better to say "the elements of the are"?. I think my function to calculate the standard deviation is wrong. scapegote, what other issues do you see? | |
|
|
|
| molecularman (107) | |
| It seems now that I have a big problem with the function that calculates the standard deviation. I'm not sure how to calculate the deviations from the average. I'm trying to use a for loop but I don't know how I would store each deviation. Should I create an array for each deviation? Is that really my only option here? | |
|
|
|
| molecularman (107) | |||
|
I think I'm getting fairly close. I decided I didn't need to create an array. Here is the code for the function that calculates the standard deviation. I think the problem might be how I'm storing the individual deviations from the average for each number. Hopefully I can figure this out or someone can help. I had to skip organic chemistry and this program is due by 5:00 pm.
| |||
|
|
|||
| iHutch105 (947) | |
|
What's the problem with it? You didn't say. | |
|
|
|
| Awais Tahir (17) | |
|
Dear Standard Deviation calculated as squar root((x-(avg))*(x-(avg))/n) | |
|
|
|