Arrays, simple yet useful

#TC1017 #WSQ10

This programm was very easy, since the formulas are well known by now, we have used them a lot in other classes. And arrays are my favorite part of C++ so far. I think they are very intuitive to use, and make soving problems like these, with a big set of numbers, quite easy to solve.

wsq10.PNG

 

Source Code below: [GitHub Link: https://github.com/diegodamy/WSQ10 ]

#include <iostream>
#include <math.h>
using namespace std;

float STDV (int size2, float st){
double stdv;
return stdv = sqrt(st/(size2-1));

}

float ST(int list2[], int size2, float average){
double st;
for(int i = 0; i <size2; i++){
st += pow((list2[i] – average),2);
}
return st;
}

float GetAverage (int list[], int size){
float sum = 0.0;
float result = 0.0;

for ( int n = 0; n <size; n++){
sum += list[n];
}
return result = sum / size;
}

int main(){
int array [10];
double average;
double st;

cout << “Please enter ten numbers:” << endl;

for (int i = 0; i<10; i++){
cin >> array [i];
}

average = GetAverage (array, 10);
cout << “Average is ” << average;

st = ST(array,10,average);

cout << endl << “Standard deviation is: ” << STDV (10,st);
}

——————————–

Photo Credit: <a href=”https://www.flickr.com/photos/50318388@N00/23640476465/”>mag3737</a&gt; via <a href=”http://compfight.com”>Compfight</a&gt; <a href=”https://creativecommons.org/licenses/by-nc-sa/2.0/”>cc</a&gt;

WSQ 10 List of Numbers

In this WSQ I had to create a program that ask for numbers and then show them as a list, the total, average and standard deviation of them.

So I used a for loop with a array to ask for the numbers, the array was the quantity of numbers I would like to use for that program, so I could modify it for whatever I want to.

Then, I created the functions Total, Average and Std. Dev., the Total function could be used for the Average and the Average could be used for the Standard Deviation.

Captura de pantalla 2016-04-05 a las 8.38.21 p.m.

When it runs looks like this:

Captura de pantalla 2016-04-05 a las 8.38.44 p.m.

As you can see, this is an “easy” program but you have to make it easy to write, using functions, loops and arrays.

BRkj4jM_700wa_0

TC101

WSQ10

WSQ10: Lists

In this wsq we had to get the values of the total, the average and the standard deviation from ten numbers. So firstly we had to do a list of the given numbers by using an array. Ken help us to do it and it turned just well, because he taught us how to do it simplier and clean and not the large one.

Here is my code finished:Wsq10

Listas

2016-03-22

 

En este programa use dos funciones, que después mandé a llamar c:

1.- para obtener la media o promedio de los números (10 a ingresar por el usuario). Se hace sumando todos los números y dividiendo entre el número que son

2.-La otra función es para la desviación estandar.

El for que aparece seguido de las funciones es para obtener los 10 números, mientras el usuario los vaya ingresando, esos números se van guardando en una lista, “mlist” sin borrarse o sin tomar el número del anterior. Para eso es el “.append” después de el nombre de la lista.  (En la página del link, se verá como mutar* listas)

 

Finalmente imprimo todos los resultados, la lista (para que el usuario vea lo que ingresó, si acaso se le olvidó), la suma de esos números, el promedio y su desviación estandar.

2016-03-22 (2)

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.

    Continue reading “Lists”

WSQ10 ‘Lists’

 

For this one, we were in charge of letting the user sumbit 10 numbers and play with them. The point of this WSQ was to append the 10 numbers that the user sumbits to a list, and us to program different operations with those numbers. In this WSQ, I learned a bunch of codes and strategies. For example:

  • x = [] has to be empty in order to let the user fill the x with the 10 digits he submits.
  • x.append(float(input)) so the program fills x with the digits.
  • import statistics so I am able to use statistics functions since the WSQ requested standar deviation. This is easier than to search for the formula and manually code it.

bulbasaur

At any case this is how the code looks and runs , and here is the GitHub to analyse it closer. I made the comparision with a random webpage to double check it was correct.

WSQ10.png

This blog is sponsored (I wish) by Jack Dolgen’s “I Will Come For You”. As part of How I Met You Mother’s soundtrack, this song is full of feelings. This song is played during one of the saddest moments of the show, episode 19 of season 6. A kid needs a hoop…

I will come for you.png

 

WSQ10

#include <iostream>
#include <cmath>

using namespace std;

int suma (int numeros[], int x){
int suma=0;
for(int i=1; i<=x; i++){
suma=suma+numeros[i];
}
return (suma);
}

float DE (int numeros[], int p, int x){
float des;
float var =0.0;

for(int i=1; i<=x; i++){
var=var+ pow(p-numeros[i],2);
}
cout << var<< endl;
var=var/n-1;
des=sqrt(var);
return (des);
}

int main (){
//int num;
int n, prom, s;
int nums[n];

cout << “cuantos numeros quieres insertar?” << endl;
cin >> n;
cout << “Inserta “<< n << “numeros” << endl;

for (int i=1; i<=n; i++){
cin >> nums[i];
}

s=suma(nums, n);
prom = s/n;
cout <<“El promedio de esos numeros es: “<< prom << endl;
cout << “desviacion estandar es ” << DE (nums, prom, n)<< endl;
return 0;
}