WSQ07 – Lists

--Originally published at The Talking Chalk

Later I will include a proper explanation of the code.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
;
double number[10], sum,average, variance, sdev, forvar;
  cout<<“Please give ten numbers.”<<endl;
for(int count=0; count<10; count++)
{
cin>>number[count];
sum+=number[count];
average=sum/10;
forvar+=pow((number[count]-average),2);
variance=forvar/10;
sdev=sqrt(variance);
}
cout<<“The sum of the given numbers is “<<sum<<endl;
cout<<“The average of the given numbers is “<<average<<endl;
cout<<“The standard deviation of the given numbers is “<<sdev<<endl;
}

Mastery Topics 6 – Calling a function

--Originally published at The Talking Chalk

mas2aAs seen in the example form Mastery Topics 2. You should only write the function outside the main fucntion and finally, in the main fucntion, write the name of the function while stablishing what are the parameters for it (making sure the type of each parameter is the adequate for the function. remember that int and doubles can be easily converted, but char cannot become an int or a double and viceversa).


Mastery Topics 4 – Output

--Originally published at The Talking Chalk

As easy as it may see, after importing the library and use namespace std, to give an output you only have to write an int main() and follow between the braquets a cout (comes out), two arrows towards the left (<<), the text within quoting marks and following yet another two arrows with endl or \n, both are correct as long as you end with a semicolon.


Mastery Topics 3 – Basic types and their use.

--Originally published at The Talking Chalk

A type determines what the variable is and what can contain.

Baisc types are int, double and char.

Char is for variables with text, however, you have to denotate the number of characters for it, as if you print a Char=”Hola”, it will only print the first letter.

Int is for integer numbers and double for float numbers. If you declare an int variable’s value as a float number (ex: int x=2.4), decimals will be chopped out; similarly, when you give a double variable’s value an integer number, a decimal space is added to it (ex: double x=1, will be interpreted as 1.0)

 


Mastery Topics 2 – C++ good style coding covnentions

--Originally published at The Talking Chalk

To have a proper coding style you have to optimize the space of the text of the code, use comments, remove innecesary codes (or make them shorter).

A simple example is the code I used for the WSQ05, which, looking at it again, sucks on space optimization.

mas2

This way is how it should be coded

mas2a

Use of comments to describe what thing does that part of the code does, remove spaces that are not necessary, yet leaving those that may distinguish which part of the code are similar to others. (inputs and outputs, functions)


Mastery Topics 1- Comments

--Originally published at The Talking Chalk

A comment is a piece of text in a cod that is meant to be readed for humans, not programs. Their main use is to facilitate the programmer to understand the code.

#include <iostream>
using namespace std;
int main()
{
cout<<“Hi”<<endl;//This is a comment
cout<<“Hellow world”<<endl;  /*which can be used to say that
this will print out
“Hello world”*/
}mas1


WSQ06-Factorial and the delusion of the exclamation mark.

--Originally published at The Talking Chalk

In math, you can represent a factorial number as n! but in c++ you cannot just type a ! following a number to get its factorial, as ! is mostly used to express that something is not something (as in !=).

I decided to still use a do and while number as I did not comprehended completely” the for” loops.

The main problem was to have a proper way to express the formula of a factorial number with conditionals and loops. I had to replace int variables in a loop so they could grew bigger until they reached the number the user asked for. Hading a counter, the factorial, and the multiply which would be the factorial of that try of the loop is what made it succesful.

So, if you were not related with the “for” (which you should be) or want to know another form to get a factorial number in c++. follow this technique.

#include <iostream>
using namespace std;
int main()
{
int value, factorial, count=1, multiply=1;
do
{
cout<<“Could you give an integer positive number, please?”<<endl;
cin>>value;
}
while(value<=0);
if(value>1)
{
do
{
factorial = (multiply)*(count+1);
multiply = factorial;
count = count+1;
}
while(count<value);
}
else{
cout<<“The factorial of this number is 1.”<<endl;
}
cout<<“The factorial of this number is “<<factorial<<“.”<<endl;
}

double u es cue zero one…NO! IT’S THE WSQ05

--Originally published at The Talking Chalk

This surely was easy. I have already the power of the functions in my veins, flowing and flowing like a rainbow through the cloudy skies.
Here comes dat Chalk.
int waddup(waddup)
{
return waddup
}
Here comes dat Chalk.
Watch him coding.
Watch his bash.
Watch him coding.
Watch his bash.
Jokes aside, I only had to créate a function for each task and return in each the designated mathematical operation of the two parameters.
#include <iostream>
#include <string>
using namespace std;
int sum(int x ,int y)
{
return x+y;
}
int abst(int x ,int y)
{
return x-y;
}int mult(int x ,int y)
{
return x*y;
}
int divid(int x ,int y)
{
return x/y;
}
int remain(int x ,int y)
{
return x%y;
}
int main()
{
int number_one = 0;
int number_two = 0;
 cout << “Enter an integer value:” << endl;
cin>> number_one;
 cout <<“You chose: ” << number_one << “.” << endl;
 cout << “Enter another integer value: \n”;
cin>> number_two;
cout << “You chose: ” << number_two << “.” << endl;
 cout << “The sum of these values is equal to: ” << sum(number_one, number_two) << “.” << endl;
cout << “The difference of these values is equal to: ” << abst(number_one, number_two) << “.” << endl;
cout << “The product of these values is equal to: ” << mult(number_one, number_two) << “.” << endl;
cout << “The division of these values with no decimals is equal to: ” << divid(number_one, number_two) << “.” << endl;
cout << “The remainder of these values is equal to: ” << remain(number_one, number_two) << “.” << endl;
 return 0;
}

WSQ04-between we win

--Originally published at The Talking Chalk

I am sure most of us know about the story of a Young Carl Gauss giving the sum of all numbers within 1 and 100 to his teacher in a class after he asked for the value of the sum. Since I was a kid, I liked to think that one day I could do something as astonishing as what the Young Gauss did… I did not until now, but hey! I made a code that can do what gauss did and more!

It was not a big deal, only had to create a do-while loop so the lowest number would be added a 1 until it reached the biggest number, as well as the count would sum 1.

Finally, the result would be half of the biggest multiplied by itself plus one minus the original lowest number plus 1 multplied by the loest number minus 1, all divided by 2.

I should have used just a for loop…

#include <iostream>
using namespace std;
int main()
{
int lowest, biggest, count, sum;
cout<<“Provide us a number.”<<endl;
cin>>lowest;
do
{
cout<<“Now provide a number bigger than the first you gave”<<endl;
cin>>biggest;
}
while(biggest<=lowest);
do
{
lowest=lowest+1;
count=count+1;
}
while(lowest!=biggest);
sum=biggest*(biggest+1)/2-((lowest-count-1)*((lowest-count-1)+1))/2;
cout<<“The sum of all numbers between the range is “<<sum<<endl;
return 0;
}