WSQ – 06, Factorial calculator

--Originally published at The Clueless Programmer

So what this wsq asked us, was to do a program that got the factorial of a number, which to people that don´t know (like me before I looked it up on Wikipedia) is the product of all the numbers that precede a number. Example, the factorial of 4 is 24 (4*3*2*1). And then it asked us to continually asked the user if he wanted to get the factorial of another number until he said no. When I first got into this assignment I didn´t know what to do or where to start because I hadn´t understood the whole concept yet, but I got some help from this webpage and from the blogpost of one of my classmates, and then I knew what to do.

So here´s the program:

imagen1

So as you can see first I name the variables and explain the program and then I start the recursion function, I started it after I introduced the program because I don´t want that to keep popping of over and over. Then I set my control variable to 1, it is important to do it here so that the value resets everytime the program starts again.

After this I create an “if” in which, if the value is negative or zero, the program says that has no factorial and sets the repetitive contition to “y”, so the program starts over. Then I start another loop that multiplies a variable equaled to the number with the variable equaled to one, later substracting one from the first variable while this is bigger than than one.

The program is basically over at this point, the only thing left would be to print the value to the user and then ask him if he wants to do it again. The answer to this question

Continue reading "WSQ – 06, Factorial calculator"

WSQ – 05, the number 1 but with a function

--Originally published at The Clueless Programmer

So this WSQ was easy because I esencially had already done this assignment. All I had to do was return to the first WSQ and just expand it by adding a function that did all of the operations that happened in the main program. So here you have the code:

#include <iostream>
using namespace std;
int sdpir (int a, int b, int res)
{
res=a+b;
cout<<“The sum of the integers is “<<res<<endl;
res=a-b;
cout<<“The difference of the integeres is “<<res<<endl;
res=a*b;
cout<<“The product of the integers is “<<res<<endl;
res=a/b;
cout<<“The integer based division is “<<res<<endl;
res=a%b;
cout<<“The remainder integer of the division is “<<res<<endl;
}
int main()
{
int num1, num2, res;
cout<<“Please enter a number “<<endl;
cin>>num1;
cout<<“The number you entered is “<<num1<<endl;
cout<<“Please enter a second number “<<endl;
cin>>num2;
cout<<“The number you entered is “<<num2<<endl;
sdpir(num1, num2, res);
}

So as you can see the first thing I do is name a functionthat calls 3 ints, two that I will be using to make opperations and one that is going to print the result. As you can see I just do the five opperations and print them all in the same variable, this is optional but I do it like this to reduce the variables and make it easier.

After the function is done I just go to the main program in which I basically ask the user to give me two numbers, then I execute the function and done, I have all the results I wanted in a “smaller” code.

Thanks for reading this awesome blog (it is pretty bad actually) again and as always I hope that I could´ve been of some help. Program on.


Quiz week 6

--Originally published at The Clueless Programmer

So for the quiz on this week we had to do a bunch of programs, so expect this blog post so be filled with codes, don´t be scared though, they´re not so hard. The excercises we had to do came out of this web page: http://www.utdallas.edu/~ivor/cs1315/clabs96.html

Excercise 1: They gave us a program and we had to make it run. We added some stuff at the beginning and then we had to add a library and change the couts to printfs.

#include <iostream>
#include <stdio.h>

using namespace std;
int main()
{ /* PROGRAM TO PRINT OUT SPACE RESERVED FOR VARIABLES */
char c;
short s;
int i;
unsigned int ui;
unsigned long int ul;
float f;
double d;
long double ld;
cout << endl
<< “The storage space for each variable type is:”
<< endl;
printf(“char: \t\t\t%d bits”,sizeof(c)*8); // \t means tab
printf(“short: \t\t\t%d bits”,sizeof(s)*8);
printf(“int: \t\t\t%d bits”,sizeof(i)*8);
printf(“unsigned int: \t\t%d bits”,sizeof(ui)*8);
printf(“unsigned long int: \t%d bits”,sizeof(ul)*8);
printf(“float: \t\t\t%d bits”,sizeof(f)*8);
printf(“double: \t\t%d bits”,sizeof(d)*8);
printf(“long double: \t\t%d bits”,sizeof(ld)*8);
}

Excercise 2: We had to ask the user to input a char, an int and a float value and then we had to print each value as the other two. Like give the char and the float value of an int.

#include <iostream>
using namespace std;
int main()
{
char c1;
int n1;
float f1;
cout<<“Write a single character: “;
cin>>c1;
cout<<“Write an integer: “;
cin>>n1;
cout<<“Write a float: “;
cin>>f1;
int n2=int(c1);
float f2=float(c1);
cout<<“The character “<<c1<<” when cast to an int gives value: “<<n2<<endl;
cout<<“The character “<<c1<<” when cast to a float gives value: “<<f2<<endl;
char c2=char(n1);
float f3=float(n1);
cout<<“The integer “<<n1<<” when cast to a char gives value: “<<c2<<endl;
cout<<“The integer “<<n1<<” when cast to a float gives value: “<<f3<<endl;
int n3=int(f1);
char c3=char(f1);

Continue reading "Quiz week 6"

Quiz 4

--Originally published at The Clueless Programmer

Tricky quiz today, seemed easy at first but it turned out pretty hard. Well played. Here is the code:

#include <iostream>
using namespace std;
int minimumThree(int x, int y, int z)
{
int j;
if(x<y)
{
if(x<z)
{
j=x;
}
else
{
j=z;
}
}
else if(y<z)
{
j=y;
}
else
{
j=z;
}
if((x==y)&&(y==z))
{
j=0;
}
return j;
}
int sumSquares(int x, int y, int z)
{
int sum;
sum=(x*x)+(y*y)+(z*z);
return sum;
}
int main()
{
int n1, n2, n3, a, b;
cout<<“This program will tell you the minimun of the three numbers you write and the sum of squares of those three numbers”<<endl;
cout<<“Give me your first number: “;
cin>>n1;
cout<<“The second one: “;
cin>>n2;
cout<<“The third one: “;
cin>>n3;
a= minimumThree(n1, n2, n3);
b= sumSquares(n1, n2, n3);
if (a==0)
{
cout<<“Your numbers are the same, there is no minimum”<<endl;
cout<<“The sum of squares is: “<<b<<endl;
}
else
{
cout<<“The minimum number is: “<<a<<endl;
cout<<“The sum of squares is: “<<b<<endl;
}
}

imagen1

And there is the code ran with all the possible solutions the may give.

So the first thing you want to do is create your functions. Here is the first tricky part, because your functions can´t print, and I hadn´t done that yet, but it is not that hard. As you can see in the first function I do a bunch of ifs to find the smallest of the three numbers, while everytime that I found a small number, I save ir in a variabla “j” that I named independently at the start, and that I return at the end. I save “j” as 0 when the three numbers are the same.

The sum of squares is pretty simple as well, I do the same trick of saving the independent variable and then returning it.

Continue reading "Quiz 4"

WSQ-04, sum of numbers

--Originally published at The Clueless Programmer

In this blog post I will talk about the fourth assignment (fifth if you count the quiz) which I just finished, the one assigned for week four. In this assignment we were asked to make a program that would ask you to write two numbers, and then it would output the integer sum (the sum of every number in between those two numbers) including the numbers you wrote. So up with the program:

imagen1 #include <iostream>
using namespace std;
int main()
{
int n1, n2, sum=0, acum=0;
cout<<“We will calculate the sum of the integers in the range you provide”<<endl;
cout<<“Please give us the lower bound: “;
cin>>n1;
cout<<“Please give us the upper bound: “;
cin>>n2;
acum=n1;
if(n1==n2)
{
cout<<“Both your numbers are the same”<<endl;
}
else if(n1<n2)
{
do
{
sum=sum+acum;
acum=acum+1;
} while(acum<=n2);
}
else
{
acum=n1;
do
{
sum=sum+acum;
acum=acum-1;
} while(acum>=n2);
}
cout<<“The sum from “<<n1<<” to “<<n2<<” (inclusive) is “<<sum<<endl;
}

So okay as you can see we don´t need any special libraries to run the programs, we just need to claim our variables and then ask them to give us both of the numbers. The way I did the program makes it so that it doesn´t matter if you write fist the big number or the low number, the result will be the same.

Next thing you wanna do is start with the ifs so that those little things won´t mess with your program. The first one avoids the numbers that are the same. Then the other are basically the same. You state a new variable with a value of zero and sum to it the first number, written as another variable, and then you sum one to this variable while the variable is lower or the same as the second one. What this does

Continue reading "WSQ-04, sum of numbers"

Quiz week 3

--Originally published at The Clueless Programmer

This post has the answer to the problem we have to do for the quiz, which asked us to make a couple of functions that would give the square and the cube root of a number given by the user. So if you are struggling with the problem in class right now (I see a bunch of you) you can just copy mine.

imagen1

 

#include <iostream>
#include <cmath>
using namespace std;
double square_root (double x, double square)
{
square=sqrt(x);
cout<<“The square root of “<<x<<” is “<<square<<endl;
}
double cube_root (double x, double cube)
{
cube=cbrt(x);
cout<<“The cube root of “<<x<<” is “<<cube<<endl;
}
int main()
{
double num, raiz=0, cubica=0;
cout<<“Write the number you want to know the square and the cube root of: “;
cin>>num;
if (num>=0)
{
square_root(num, raiz);
cube_root(num, cubica);
}
else
{
cout<<“That number has imaginary square roots”<<endl;
cube_root(num, cubica);
}
}

There you go. I have to give an explanation but I still don´t quite know what I did, so I´ll do my best:

I include the libraries (the math one has the roots), then I create the functions, I tell them what I want them to print and then I start the program (If you think my function explanation is vague it is because I don´t know how to explain it). You can make you function in a different way, mine is just an example. And then the program should be fairly easy by now. I just add an “if” so if they write a negative number it tell them that it does not have a square root, but it does have a cube root.

So yeah that´s it, thanks for reading, have fun.


Third assignment – Pick a number

--Originally published at The Clueless Programmer

Okay so this assignment really tested me out, specially because I didn´t know anything about generating a random number or how to do it, but by reading the book (and asking a lot of questions) I figures it out. Let´s go.

The tasks asks us to make a program that thinks of a random number and then starts asking us to guess the number, telling us if we are lower or higher, once we guess it, it gets us out.

imagen1

#include <iostream>
#include <stdlib.h>//Library for the rand command
using namespace std;
int main()
{
int n1, n2;
char cond=’n’;
srand(time(NULL));//This makes the number always random
n1=rand()%101;//I use the 101 because it takes every number until the number you write
do
{
cout<<“Try to guess the number I´m thinking of”<<endl;
cin>>n2;
if (n2>n1)
{
cout<<“Your number is higher than the one I´m thinking of”<<endl;
}
else if(n2<n1)
{
cout<<“Your number is lower than the one I´m thinking of”<<endl;
}
else
{
cout<<“Congrats! You guessed it”<<endl;
cond=’s’;
}
} while(cond==’n’);
}

So okay you are confused, you see a lot of tricky stuff right? But don´t worry it will make sense, eventually.

First what you want to do is include the library that has the command for the random number generator. Then after you name your variables, you write that srand stuff, which basically what it does is change the random command so that everytime you enter the number is different. Then you write the rand command, I write 101 instead of 100 because it takes the number before the oe you write, so 0-100. Then we enter the do-while, which basically means it ill do something at first and then continue to do it while the condition isn´t fulfilled. I do the ifs and else in which I close out

Continue reading "Third assignment – Pick a number"

Second assignment – Temperature

--Originally published at The Clueless Programmer

Here I go again, clueless programmer on the go hitting you up with another awesome blog post. That´s what I would say if I indeed made awesome blog posts. Anyway, let´s get right to it.

This time the assignment was more difficult than the last one, but still, I managed. This was the first program that asked us to use an “if” conditional (a conditional that only applies if you trigger the if).

imagen1

#include <iostream>
using namespace std;
int main()
{
int n1, n2;
cout<<“Write the temperature in Fahrenheit degrees: “;
cin>>n1;
n2=(5*(n1-32))/9;
cout<<n1<<” Fahrenheit degrees convert to “<<n2<<” Celsius degrees”<<endl;
if (n2>=100)
{
cout<<“Water boils at this temperature”<<endl;
}
else
{
cout<<“Water doesn´t boil at this temperature”<<endl;
}
}

There you have the program!

Basically what I do is ask you for a temperature in Fahrenheit degrees, which I save on a variable, then on another variable I save that Fahrenheit temperature converted into a Celsius one. Then I put the ifs down, if the conversion is bigger or the same as a hundred, water boils at that temperature, if it is lower, it doesn´t.

Simple stuff right? (I can bet karma will hit me up with a really difficult assignment really soon, but I´ll enjoy my time until then). So yeah hope you learned something, as always thanks for reading me and hang in there, program on.

 


First assignment

--Originally published at The Clueless Programmer

I see you came back for more of my programming magic, welcome. Ok what I have for you today is certainly a more difficult program than the “Hello World” one, but was still fairly easy to program.

Basically the assignment asked us to make a program that asks the user to input two numbers, and the sums, substracts, multiplies and divides them giving us all the different results. This is an example of the program with two numbers I chose randomly:

sin-titulo

#include <iostream>
using namespace std;
int main()
{
int num1, num2, res;
cout<<“Please enter a number “<<endl;
cin>>num1;
cout<<“The number you entered is “<<num1<<endl;
cout<<“Please enter a second number “<<endl;
cin>>num2;
cout<<“The number you entered is “<<num2<<endl;
res=num1+num2;
cout<<“The sum of “<<num1<<” + “<<num2<<” is “<<res<<endl;
res=num1-num2;
cout<<“The difference of “<<num1<<” – “<<num2<<” is “<<res<<endl;
res=num1*num2;
cout<<“The product of “<<num1<<” * “<<num2<<” is “<<res<<endl;
res=num1/num2;
cout<<“The integer based division of “<<num1<<” divided by “<<num2<<” is “<<res<<endl;
res=num1%num2;
cout<<“The remainder integer of “<<num1<<” divided by “<<num2<<” is “<<res<<endl;
}

As you can see, the program asks you to give it two numbers and then, because it is super smart, does the operations for you, so if one day you don´t feel like doing simple math then all you have to do is turn on your computer and open this program! Way cooler than a calculator am I right?

I basically named the variables first, then asked the user to input two numbers, the parts where I printed a lot of useless information was just to try out some output stuff, they are not really necessary. Then you just do the operations, saving them on the variable, and print them, easy. I was able to program this easily because of the previous knowledge I had acquired on a course I took

Continue reading "First assignment"

Hello World (Probably not the first one you´ve seen)

--Originally published at The Clueless Programmer

Finally did it. It took effort, sweat, blood and every inch of intelligence I have (and maybe some help from my professor), but I finally did it, I set up my programming tools (APPLAUSE). Okay probably it wasn´t that hard to begin with but it sure seems hard when you don´t know what you´re doing and your terminal just keeps giving you ERROR messages like it is enjoying itself.

Once you accomplish that, the “Hello World” program everyone is talking about turns out to be really easy (It is even easier when all you did was copying everything that the professor did in his explanation).

But anyways, drumrolls:

sin-titulo

There it is, majestic as always. Basically what you do is include the iostream so that your message can be shown, the “using namespace std” on the next line is optional but it makes your life easier by abstaining you from writing “std” before every line so I´ll take it. Then you open the program, give an output message and end the line. Yeah, it is as simple as I explained it, maybe even more.

So thank you for reading my weird explanation, hope I helped you find whatever it is that you are pursuing in life, and yeah of course with the program too.

See you soon, program on.