Lists

Hello,

Today I´m going to show you a new code. Every new code has a new level of complexity and this one is not the exception.

Instructions:

Create a program that asks the user for 10 numbers  (floating point). Store those numbers in a list. Show to the user the total, average and standard deviation of those numbers.

Well, as you cans see, we are going to apply some of the information that we used in the past, like the sum and the average of a range of numbers, but, now we have to use the deviation. Let me explain what is the deviation of a range of numbers.

The Standard Deviation is a measure of how spread out numbers are. The formula is easy: it is the square root of the Variance. And the variance is the average of the squared differences from the Mean. 

This is the formula:

(http://geographyfieldwork.com/StandardDeviation1.htm)

Now, knowing what is a standard Deviation we can the the code and this is my code:

2016-03-21 (1)2016-03-21 (2)2016-03-21

First, we need create a function for the sum of the numbers.

Second, we have to create another function for the average of all the range of numbers.

The last two mentioned steps doesn’t have any problem because we analized how to do it in the last posts. On the other hand, for the deviation you only need to follow the rules of the formula and the rules of math, it isn’t hard.

NOTES:

  • Use float.
  • As you can see I’m using another kind of loop, it is called FOR LOOP and basically it is like a while loop but in other order.

Syntax of a for loop:

for ( init; condition; increment )
{
   statement(s);
}
  1. The init step is executed first, and only once.

    step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

  2. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
  3. After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
  4. The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.

(http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm)

  • The last point I want to highlight is that in the code we use ”  []  ” . When you see it next to a variable it means that the same variable turns into a kind of differents variables. What does it mean? If you have ” for (int i = 0; i < 5; i++) ” and ” x[i] ” you will have 4 different values to enter in the same variable.

 

Thank you for watch my post. See you in the next post.

Top image by: http://flickr.com/photos/mmorgan8186/5946796450

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CC BY-SA 4.0 Lists by sercho93 is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.