PICTURE OF ACTOR

So in this eight week class I started with doing the survey of mid semester where I gave ideas in order to improve this course and this and this WSQ07. I started reviewing in creating and calling functions in C++. #Mastery06, #Mastery07, #Mastery16 Use of recursion for repetitive algorithms, #Mastery17 When to use what type of repetition in a program and #Mastery18 Creation and use of Arrays/ Vectors in C++.

What I did for this numeric program is solving the problem to the user by writing 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.Futhermore, Once you have this working, change it so that users keep giving you values until they signal “no more values”. How would you implement this and in particular for the C++ group, how to you deal with an unknown size to your array during compilation?

The quantity of the value depends of the quantity of the type float variable that has only 32 bits of leght, therefore you need a new library in order to increase the value of numbers called Biginteger.hh but I am going to add it in the next WSQ08 called Yo soy 196. Next, to deal with an unknown size of my array during compilation we need to ask the user the number of that size and save it in a variable n.

The resources I need it to solve this program are here:

ken bauer

Similar code made by Eduardo Torres

C Programming Tutorial: Functions (Call By Value, Reference,passing Arrays to function)

The following photograph shows the solution to this problem:

wsq7v2

wsq7v3

wsq7v4wsq7v5

wsq7v6

Picture of author

So at first I wrote the same structure of the program just did the same as what i did in Hello World: Second Class, Second Blog (Blog of the second class 12/01/17) and Hello World.cpp,  #WSQ01 Post Fun with Numbers 16/01/17 and WSQ1.cpp#WSQ02 Post Temperature 23/01/17 and WSQ02.cpp#WSQ03 Post Pick a Number 23/01/17 and WSQ03.cpp#WSQ04 Post Sum of Numbers 23/01/17 and WSQ04.cpp#WSQ05 Six Tutorial On To Functions 12/02/17 and WSQ05.cpp and #WSQ06 Factorial Calculator 12/02/17 and WSQ06.cpp where i explained the application of the #MasteryTopic01 that it is for comments that could be very useful when debugging and #MasteryTopic04 that is basic output for data. Also I did some other mastery topics.

What i first put in the code was the library to call all the fuctions <iostream> of inputs and outputs of data in languague C++ #MasteryTopic06 Calling Functions and #MasteryTopic08 Importing and using libraries.

Then i had to write a command that everytime goes with the instructions of input and outputs of data that is call std but with this command helps writing these std out of the main because the machine factorizes them. Futhermore, you need a library to use all math expresions.

#include <iostream>//Library to call all the
//fuctions of inputs and outputs of data in languague
//C++ #MasteryTopic01

#include <cmath>//Library to call all the
//fuctions of MATH of data in languague
//C++ #MasteryTopic01

using namespace std;//In C++ we need a command
// that everytime goes with the instructions of input and outputs
//of data that is call std but with this command helps writing
//these std out of the main because the machine factorizes them

Next I initialize the begin of my program int main()


int main() //Begin of the program

After that I had to declarate int variables n and tam. Futhermore, I declarated a decimal variable called summa, averagefinal, standardfinal and list[tam]. This is where I am using the #Mastery18 Creation and use of Arrays/Vectors where I am declarating a float variable list as an array with a size of the variable tam because in an array you need to have a size in the variable. Futhermore, where you have to pass the array as a parameter, you have to write in this case float *list.

int n; //Declarate int variable n
int tam; //Declarate int variable tam
float summa; //Declarate decimal float variable summa
float averagefinal; //Declarate decimal float variable averagefinal
float standardfinal; //Declarate decimal float variable standardfinal
float list[tam]; //Declarate decimal float variable list[tam]. This is where I am using the #Mastery18 Creation and use of Arrays/Vectors
//where I am declarating a float variable list as an array with a size of the variable tam because in an array you need to have a size in
//the variable. Futhermore, where you have to pass the array as a parameter, you have to write in this case float *list .

Next, I use the #WSQ7 command of out data in form of text. This is to ask the user to give
for the number of the elements in the array. Therefore, this is the unknown size

cout<<"Give me the number of the elements in the array:"; //#WSQ5 command of out data in form of text. This is to ask the user to give
//for the number of the elements in the array.

After that I need this command allows enter data for inputs. In this case this enters the value of variable ‘n’

cin>>n; // This command allows enter data for inputs. In this case this enters the
//value of variable 'n'

After that, I need THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF «numbertotal» TO MAKE THE TASK OF THE total VALUE OF THE NUMBER WITH THE HELP OF THE VARIABLE float ‘summa’ with the parameters n and list (array) #Mastery06 and #Mastery07.

summa=numbertotal(n,list);//THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF "numbertotal" TO MAKE THE TASK OF
//THE total VALUE OF THE NUMBER WITH THE HELP OF THE VARIABLE float 'summa' with the parameters n and list (array) #Mastery06 and
//#Mastery07

After this last action, the program escapes from the main function to the float numbertotal in order to make the task of the total value of the numbers with the help of the float and int variable where one is an array. This is the structure of a function where you have subrutines in order to make different tasks, in this case we have a FLOAT function with the name «numbertotal» and with int parameter n,and float parameter *list where I am using the #Mastery18 Creation and use of Arrays/Vectors in C++ in order to have float results inside this function. Therefore, I am using the array in order to save each number entered by the user with combining it with a loop.


float numbertotal(int n, float *list) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a FLOAT function with the name "numbertotal" and with int parameter n,and float parameter
//*list where I am using the #Mastery18 Creation and use of Arrays/Vectors in C++ in order to have float results inside this function.
//Therefore, I am using the array in order to save each number entered by the user with combining it with a loop.

Next, Inside of the float function ‘numbertotal’ I have to declarate decimal variable total initialized in 0. (This is a local variable).

float total=0; //Declarate decimal variable total initialized in 0.

Futhermore, I need a #Mastery14 use of loops with for. With this command for is utilized as a loop FOR execute all the instruccions that has this loop until that does not complete that the variable i will be less or equal than n while it is increasing one in one in this variable i inicialized from the value 1. This is FOR repiting all the statements n times.

for(int i=1; i<=n; i++) //With this command for is utilized as a loop FOR execute all the
//instruccions that has this loop until that does not complete that the variable i
// will be less or equal than n while it is increasing one in one in this variable i
//inicialized from the value 1

Moreover, you need a command of out data in form of text. In this text we show the authentic output of the value entered of i in the terminal interface #MasteryTopic04 and we show the user each numer given depending on n.

cout<<"Give me "<< i <<" float number:"; //command of out data in form of text
//In this text we show the authentic output of the value entered of i in the terminal interface #MasteryTopic04

Next, Inside of this function we need this command that allows enter data for inputs. In this case this enters the value of variable list[i where this i is the space of each value entered by the user from 1 to n I am using the #Mastery18 Creation and use of Arrays/Vectors in C++ ]

cin>>list[i]; // This command allows enter data for inputs. In this case this enters the
//value of variable list[i where this i is the space of each value entered by the user from 1 to n I am using the #Mastery18
//Creation and use of Arrays/Vectors in C++ ]

Then, In this instruction of the for loop, it tells we have an accumulator that tells the value of the variable total plus the variable list[i where this i is the space of each value entered by the user from 1 to n I am using the #Mastery18 Creation and use of Arrays/Vectors in C++ ] is given to the variable total. In this case we have a variable whose function is save the value of the operation list until the loop for finishes with all the total of integers in the range you provide saved in variable total.

total=total+list[i];//In this instruction of the for loop, it tells we have an accumulator that tells the value of
//the variable total plus the variable list[i where this i is the space of each value entered by the user from 1 to n I am
//using the #Mastery18 Creation and use of Arrays/Vectors in C++ ] is given to the variable total. In this case we
//have a variable whose function is save the value of the operation list until the loop
//for finishes with all the total of integers in the range you provide saved in variable total

After that we have a return statement. With this command you return the value of the previus operations of «numbertotal» function inside of the variable float total in order to call it in THE MAIN FUNCTION.

return total; //With this command you return the value of the previus operations of "numbertotal" function inside of the
//variable float total in order to call it in THE MAIN FUNCTION.

Next, the program goes back to the main function and I need a command of out data in form of text. In this text we show the authentic output of the value entered of summa in the terminal interface #MasteryTopic04.

cout<<"The total of the numbers is: "<<summa<<endl; //command of out data in form of text
//In this text we show the authentic output of the value entered of summa in the terminal interface #MasteryTopic04

After that, we need THIS ASSIGNATION that HELPS TO CALL THE FUNCTION OF «averageofnumbers» TO MAKE THE TASK OF THE average VALUE OF THE NUMBER WITH THE HELP OF THE VARIABLE float ‘averagefinal’ with the parameters n and summa #Mastery06 and #Mastery07.

After this last action, the program escapes from the main function to the float ‘averageofnumbers’ in order to make the task of the average value of the numbers with the help of the float and int variable. This is the structure of a function where you have subrutines in order to make different tasks, in this case we have a FLOAT function with the name «averageofnumbers» and with float parameter summa and int parameter n in order to have float results inside this function.

float averageofnumbers(float summa, int n)//This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a FLOAT function with the name "averageofnumbers" and with float parameter summa and int parameter
//n in order to have float results inside this function.

Inside of the float function ‘averageofnumbers’ I have to declarate a decimal variable average. (This is a local variable).

float average; //Declarate decimal variable average

Next, we need to use this math operation between ‘summa’ and ‘n’ and stands for do the division between those values and saved it in float variable ‘average’

average=(summa)/n; //This is a math operation between 'summa' and 'n' and stands for do the division between
//those values and saved it in float variable 'average'

After that, we have a return statement. With this command you return the value of the previus operations of «averageofnumbers» function inside of the variable float average in order to call it in THE MAIN FUNCTION.

return average; //With this command you return the value of the previus operations of "averageofnumbers" function inside of the
//variable float average in order to call it in THE MAIN FUNCTION.

Next, the program goes back to the main function and I need a command of out data in form of text. In this text we show the authentic output of the value entered of averagefinal in the terminal interface #MasteryTopic04

cout<<"The average of the numbers is: "<<averagefinal<<endl; //command of out data in form of text
//In this text we show the authentic output of the value entered of averagefinal in the terminal interface #MasteryTopic04

Then, we have THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF «standarddop» TO MAKE THE TASK OF THE standard deviation VALUE OF THE NUMBER WITH THE HELP OF THE VARIABLE float ‘standardfinal’ with the parameters list (array) averagefinal and n #Mastery06 and #Mastery07.

 standardfinal=standardop(list,averagefinal,n); //THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF "standarddop" TO MAKE THE TASK OF
//THE standard deviation VALUE OF THE NUMBER WITH THE HELP OF THE VARIABLE float 'standardfinal' with the parameters list (array)
// averagefinal and n #Mastery06 and #Mastery07

After this last action, the program escapes from the main function to the float numbertotal in order to make the task of the total value of the numbers with the help of the float and int variable where one is an array.This is the structure of a function where you have subrutines in order to make different tasks, in this case we have a FLOAT function with the name «standardop» and with int parameter n,and float parameter *list where I am using the #Mastery18 Creation and use of Arrays/Vectors in C++ in order to have float results inside this function. Therefore, I am using the array in order to save each number entered by the user with combining it with a loop.

float standardop(float *list,float averagefinal, int n) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a FLOAT function with the name "standardop" and with int parameter n,and float parameter
//*list where I am using the #Mastery18 Creation and use of Arrays/Vectors in C++ in order to have float results inside this function.
//Therefore, I am using the array in order to save each number entered by the user with combining it with a loop.

Inside of the float function ‘standardop’ I have to declarate decimal variable prevariance initialized in 0. (This is a local variable).

Next, Inside of the float function ‘standardop’ I have to declarate decimal variable variance. (This is a local variable).

Next, Inside of the float function ‘standardop’ I have to declarate decimal variable standard. (This is a local variable).

float standardop(float *list,float averagefinal, int n) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a FLOAT function with the name "standardop" and with int parameter n,and float parameter
//*list where I am using the #Mastery18 Creation and use of Arrays/Vectors in C++ in order to have float results inside this function.
//Therefore, I am using the array in order to save each number entered by the user with combining it with a loop.
{
float prevariance=0; //Declarate decimal variable prevariance initialized in 0.
float variance; //Declarate decimal variable variance
float standard; //Declarate decimal variable standard

Futhermore, I need a #Mastery14 use of loops with for. With this command for is utilized as a loop FOR execute all the instruccions that has this loop until that does not complete that the variable i will be less or equal than n while it is increasing one in one in this variable i inicialized from the value 0. This is for repeting all the statements n times.

for(int i=1; i<=n; i++) //With this command for is utilized as a loop FOR execute all the
//instruccions that has this loop until that does not complete that the variable i
// will be less or equal than n while it is increasing one in one in this variable i
//inicialized from the value 0

Inside of the for loop we nees This math operation between ‘prevariance’ and ‘list[i where this i is the space of each value entered by the user from 1 to n I am using the #Mastery18 Creation and use of Arrays/Vectors in C++]’ and stands for do the summa between those values where in the part of the list[i] you have to make de difference with the average final and elevated to power 2 and saved it in float variable ‘prevariance’. This is part of the formula to calculate the standar deviation formula that I saw in this link.

s1

s2

s3

s4

Link of the page


prevariance =prevariance + ((list[i]-averagefinal))*((list[i]-averagefinal)); //This is a math operation between 'prevariance' and
//'list[i where this i is the space of each value entered by the user from 1 to n I am
//using the #Mastery18 Creation and use of Arrays/Vectors in C++]' and stands for do the summa between
//those values and saved it in float variable 'prevariance'


After that, we need this is math operation between ‘prevariance’ and ‘n’ and stands for do the division between those values and saved it in float variable ‘variance’

variance=(prevariance)/n; //This is a math operation between 'prevariance' and 'n' and stands for do the division between
//those values and saved it in float variable 'variance'

Then, we nees this  math operation between ‘standard’ and ‘variance’ and stands for do the square root between those values and saved it in float variable ‘standard’

standard=sqrt(variance); //This is a math operation between 'standard' and 'variance' and stands for do the square root between
//those values and saved it in float variable 'standard'

Finally, we have a return statement. With this command you return the value of the previus operations of «standardop» function inside of the variable float standard in order to call it in THE MAIN FUNCTION.

return standard; //With this command you return the value of the previus operations of "standardop" function inside of the
//variable float standard in order to call it in THE MAIN FUNCTION.

Next, the program goes back to the main function and I need a command of out data in form of text. In this text we show the authentic output of the value entered of standardfinal in the terminal interface #MasteryTopic04

cout<<"The standard deviation of those numbers is: "<<standardfinal<<endl; //command of out data in form of text
//In this text we show the authentic output of the value entered of standardfinal in the terminal interface #MasteryTopic04

And i also resolved it by doing this video tutorial that i made reafirming these parts at this link :#WSQ07 Eight Tutorial Lists 03/03/17 and WSQ07.cpp

Finally with the command ./a. out tells the terminal to run de C++ file:

s5

This code for solution will be here and in Github:

Link in Github:WSQ07.cpp

If you do not want the WSQ07.cpp there is it in here with comments. The comments are very useful to detect errors and create new things #Mastery01 #Masterytopic01 #MasteryTopic01 #MASTERY01 #MASTERYTOPIC01 :

Also in the last class I learned how to present my C++ in a better ‘readable’ way after doing it #Mastery02 #Masterytopic02 #MasteryTopic02 #MASTERY02 #MASTERYTOPIC02:

#include <iostream>//Library to call all the
//fuctions of inputs and outputs of data in languague
//C++ #MasteryTopic01
#include <cmath>//Library to call all the
//fuctions of MATH of data in languague
//C++ #MasteryTopic01

using namespace std;//In C++ we need a command
// that everytime goes with the instructions of input and outputs
//of data that is call std but with this command helps writing
//these std out of the main because the machine factorizes them


float numbertotal(int n, float *list) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a FLOAT function with the name "numbertotal" and with int parameter n,and float parameter
//*list where I am using the #Mastery18 Creation and use of Arrays/Vectors in C++ in order to have float results inside this function.
//Therefore, I am using the array in order to save each number entered by the user with combining it with a loop.
{
float total=0; //Declarate decimal variable total initialized in 0.

for(int i=1; i<=n; i++) //With this command for is utilized as a loop FOR execute all the
//instruccions that has this loop until that does not complete that the variable i
// will be less or equal than n while it is increasing one in one in this variable i
//inicialized from the value 1
{
cout<<"Give me "<< i <<" float number:"; //command of out data in form of text
//In this text we show the authentic output of the value entered of i in the terminal interface #MasteryTopic04
cin>>list[i]; // This command allows enter data for inputs. In this case this enters the
//value of variable list[i where this i is the space of each value entered by the user from 1 to n I am using the #Mastery18
//Creation and use of Arrays/Vectors in C++ ]

total=total+list[i];//In this instruction of the for loop, it tells we have an accumulator that tells the value of
//the variable total plus the variable list[i where this i is the space of each value entered by the user from 1 to n I am
//using the #Mastery18 Creation and use of Arrays/Vectors in C++ ] is given to the variable total. In this case we
//have a variable whose function is save the value of the operation list until the loop
//for finishes with all the total of integers in the range you provide saved in variable total
}

return total; //With this command you return the value of the previus operations of "numbertotal" function inside of the
//variable float total in order to call it in THE MAIN FUNCTION.

}


float averageofnumbers(float summa, int n)//This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a FLOAT function with the name "averageofnumbers" and with float parameter summa and int parameter
//n in order to have float results inside this function.

{
float average; //Declarate decimal variable average
average=(summa)/n; //This is a math operation between 'summa' and 'n' and stands for do the division between
//those values and saved it in float variable 'average'

return average; //With this command you return the value of the previus operations of "averageofnumbers" function inside of the
//variable float average in order to call it in THE MAIN FUNCTION.


}

float standardop(float *list,float averagefinal, int n) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have a FLOAT function with the name "standardop" and with int parameter n,and float parameter
//*list where I am using the #Mastery18 Creation and use of Arrays/Vectors in C++ in order to have float results inside this function.
//Therefore, I am using the array in order to save each number entered by the user with combining it with a loop.
{
float prevariance=0; //Declarate decimal variable prevariance initialized in 0.
float variance; //Declarate decimal variable variance
float standard; //Declarate decimal variable standard

for(int i=1; i<=n; i++) //With this command for is utilized as a loop FOR execute all the
//instruccions that has this loop until that does not complete that the variable i
// will be less or equal than n while it is increasing one in one in this variable i
//inicialized from the value 0
{
prevariance =prevariance + ((list[i]-averagefinal))*((list[i]-averagefinal)); //This is a math operation between 'prevariance' and
//'list[i where this i is the space of each value entered by the user from 1 to n I am
//using the #Mastery18 Creation and use of Arrays/Vectors in C++]' and stands for do the summa between
//those values and saved it in float variable 'prevariance'

}
variance=(prevariance)/n; //This is a math operation between 'prevariance' and 'n' and stands for do the division between
//those values and saved it in float variable 'variance'

standard=sqrt(variance); //This is a math operation between 'standard' and 'variance' and stands for do the square root between
//those values and saved it in float variable 'standard'


return standard; //With this command you return the value of the previus operations of "standardop" function inside of the
//variable float standard in order to call it in THE MAIN FUNCTION.

}


int main() //Begin of the program
{

int n; //Declarate int variable n
int tam; //Declarate int variable tam
float summa; //Declarate decimal float variable summa
float averagefinal; //Declarate decimal float variable averagefinal
float standardfinal; //Declarate decimal float variable standardfinal
float list[tam]; //Declarate decimal float variable list[tam]. This is where I am using the #Mastery18 Creation and use of Arrays/Vectors
//where I am declarating a float variable list as an array with a size of the variable tam because in an array you need to have a size in
//the variable. Futhermore, where you have to pass the array as a parameter, you have to write in this case float *list .


cout<<"Give me the number of the elements in the array:"; //#WSQ5 command of out data in form of text. This is to ask the user to give
//for the number of the elements in the array.
cin>>n; // This command allows enter data for inputs. In this case this enters the
//value of variable 'n'

summa=numbertotal(n,list);//THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF "numbertotal" TO MAKE THE TASK OF
//THE total VALUE OF THE NUMBER WITH THE HELP OF THE VARIABLE float 'summa' with the parameters n and list (array) #Mastery06 and
//#Mastery07
cout<<"The total of the numbers is: "<<summa<<endl; //command of out data in form of text
//In this text we show the authentic output of the value entered of summa in the terminal interface #MasteryTopic04


averagefinal=averageofnumbers(summa,n);//THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF "averageofnumbers" TO MAKE THE TASK OF
//THE average VALUE OF THE NUMBER WITH THE HELP OF THE VARIABLE float 'averagefinal' with the parameters n and summa #Mastery06 and
//#Mastery07
cout<<"The average of the numbers is: "<<averagefinal<<endl; //command of out data in form of text
//In this text we show the authentic output of the value entered of averagefinal in the terminal interface #MasteryTopic04

standardfinal=standardop(list,averagefinal,n); //THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF "standarddop" TO MAKE THE TASK OF
//THE standard deviation VALUE OF THE NUMBER WITH THE HELP OF THE VARIABLE float 'standardfinal' with the parameters list (array)
// averagefinal and n #Mastery06 and #Mastery07
cout<<"The standard deviation of those numbers is: "<<standardfinal<<endl; //command of out data in form of text
//In this text we show the authentic output of the value entered of standardfinal in the terminal interface #MasteryTopic04




}

 

 

Anuncio publicitario

7 comentarios en “#WSQ07 Lists 03/03/17 and WSQ07.cpp

Deja una respuesta

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Salir /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Salir /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

Conectando a %s