Quiz (Week 6)

--Originally published at Programming Path

Well… this was a bit large but I did it. I just didn’t made the post because of partials and to be honest, I forgot that we had quiz and I did not publish it, but here I am.

The quiz had 5 problems, so I will post a lot of photos and codes.

The first problem was this:

Type in the following program and run it:

#include 
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;
	cout << endl << "char: \t\t\t%d bits",sizeof(c)*8;  //  \t means tab 
	cout << endl << "short: \t\t\t%d bits",sizeof(s)*8;
	cout << endl << "int: \t\t\t%d bits",sizeof(i)*8;
	cout << endl << "unsigned int: \t\t%d bits",sizeof(ui)*8;
	cout << endl << "unsigned long int: \t%d bits",sizeof(ul)*8;
	cout << endl << "float: \t\t\t%d bits",sizeof(f)*8;
	cout << endl << "double: \t\t%d bits",sizeof(d)*8;
	cout << endl << "long double: \t\t%d bits",sizeof(ld)*8;

This was pretty easy. I just needed to write the same code but in c++. Here is the picture:

quiz5-1

Second problem:

Write a program to read in a character, an integer, and a float, and print out the values, then cast to other types; so that the interaction with the user is something like the following:

	Input a single character, followed by : h
	Input an integer, followed by :  4872
	Input a float, followed by :  182.937
	The character h when cast to an int gives value ?????
	The character h when cast to a float gives value ?????
	The integer 4872 when cast to a char gives value ?
	The integer 4872 when cast to a float gives value ?????
	The float 182.
quiz5-2
quiz5-3
quiz5-4
quiz5-5
Continue reading "Quiz (Week 6)"

Partial exam!

--Originally published at Programming Path

Two days ago I did the first exam of ken’s course. I actually didn’t study, I kind of feel bad about it, but do not regret it. I think it is best to do an exam with your actual knowledge than trying to remember everything the last night, and more with programming. Programming is more like a logical thing, I don’t think it would be a good idea if I memorized the code and then try to apply it to the exam, with different exercise. Even with all that I think the exam wasn’t that bad. It was almost the same as the quizzes we did for the last few weeks. Hope it goes well, not for the notes, but because I really like programming. It’s quite fun.

Have fun with programming.


(WSQ04) Please, don’t joke around

--Originally published at Programming Path

The activity is to run a program where it gives you the sum of a range of numbers, which that range is asked to the user. We needed to be careful if the user inserts the numbers in the wrong order. If that happens then we should put something like “your wrong, please enter it again.

Here is the code:

sum2

Again the code:

#include <iostream>
using namespace std;

int main () {
int l, h, i, r = 0;
cout << “I will sum the numbers in the range you give.” << endl;
cout << endl << “Please enter the lowest number: “;
cin >> l;
cout << “Please enter the highest number: “;
cin >> h;
if (h < l) {
cout << “That number is lower than the first one, please don’t joke around.” << endl;
cout << “Enter the highest number again, please: “;
cin >> h;
}
if (l == 0) {
for (i = 0 ; i <= h; i++) {
r = r + i;
}
}
else {
for (i = l; i <= h; i++) {
r = r + i;
}
}
cout << “The sum from ” << l << ” to ” << h << ” is: ” << r << endl;
return 0;
}

This are the result, with two options:

Wrong order:

I will sum the numbers in the range you give.

Please enter the lowest number: 3
Please enter the highest number: 2
That number is lower than the first one, please don’t joke around.
Enter the highest number again, please: 7
The sum from 3 to 7 is: 25

Right order:

I will sum the numbers in the range you give.

Please enter the lowest number: 3
Please enter the highest number: 7

Continue reading "(WSQ04) Please, don’t joke around"

(Quiz 4) Minimum and double of bla bla bla

--Originally published at Programming Path

The quiz today, #quiz4, was to run a program where the user inputs three numbers (x, y and z) and give you the minimum number and the sum of its squares. We didn’t use any library like the last quiz, but we used functions. These was to return the minimum and the result of the sum.

Here is the code:

Click to view slideshow.

And here as well:

#include <iostream>
using namespace std;

int minimunThree (int, int, int);
int sumSquares (int, int, int);
int main () {
int x, y, z, m, ss;
cout << “Please give me x: “;
cin >> x;
cout << “Please give me y: “;
cin >> y;
cout << “Please give me z: “;
cin >> z;
m = minimunThree (x, y, z);
ss = sumSquares (x, y, z);
cout << endl << “The minimum number is: ” << m << endl;
cout << “The sum of squares is: ” << ss;
return 0;
}
int minimunThree (int x, int y, int z) {
int rm;
if (x < y && x < z) {
rm = x;
}
else if (y < x && y < z) {
rm = y;
}
else if (z < x && z < y) {
rm = z;
}
return rm;
}
int sumSquares (int x, int y, int z) {
int sx, sy, sz, rs;
sx = x * x;
sy = y * y;
sz = z * z;
rs = sx + sy + sz;
return rs;
}

The result is like this:

Please give me x: 9
Please give me y: 4
Please give me z: 12

The minimum number is: 4
The sum of squares is: 241

This covers #Mastery12. If you have any doubts, don’t hesitate in asking.


Your chance to make the first impression

--Originally published at Programming Path

Hello! The task, #WSQ03, that I’m publishing covers the #Mastery12 and #Mastery13.

I’m not that inspired, but I will try to do the best.

The activity was to do a program that choose a random number and the user tries to guess it. To do this, I needed to insert a new library <cstdlib> because this one has the code to generate random numbers. Also, I used do…if…while because I had to put conditions.

Here is picture of the code:

random

Here is just the code:

#include <iostream>
#include <cstdlib>
using namespace std;

int main () {
int a, b, c=0;
srand(time(NULL));
cout << “I picked a number between 1 and 100.” << endl;
cout << “Guess the number I chose: ” << endl;
a = rand()%101;
do {
c = c + 1;
cin >> b;
if (b > a) {
cout << “Wrong! Your number is too high, try again: ” << endl;
}
else if (b < a) {
cout << “That’s not the one. Your number is too low, guess again: ” << endl;
}
else if (b = a) {
cout << “Excellent! You did it! That’s the right number!” << endl;
}
} while (a != b);
cout << “Your total of guesses: ” << c;
return 0;
}

Thanks for reading.


Ask F° and I’ll give you C°

--Originally published at Programming Path

Hello! This is the summery of #WSQ02. The task was to write a program that will ask the user the temperature in Fahrenheit then convert it to Celsius and also if at that temperature the water boils.

This is my code:

temperatura

#include <iostream>
using namespace std;
int main() {
int a, b;
cout <<“Give the temperature in Fahrenheit? “;
cin >> a;

b = ((5*(a-32))/9);
cout << endl << “The temperature in Celcius is ” << b << endl;

if (b >= 100) {
cout << “At this temperature water does boil.”;
}
else {
cout << “At this temperature water DOESN’T boil.”;
}
cout << endl;
return 0;
}

In this program we had to use if.. else it is not that hard to understand, I think is one of the easiest to use. In this example we just need to tell the program that if the number that is given is equal or higher than 100 then the water boils else the water does not boils. That all there is to do, besides the formula to convert F° to C°.

Hope it helped you.


I finished the quiz in time!!

--Originally published at Programming Path

Well, maybe I took me more time because of writing the post, but!!! I did it! I’m proud of myself!

We had to do a program with two specific functions:

  • double square_root(double x) {} ; // returns the square root of x
  • double cube_root(double x) {} ; // returns the cube root of x

The program needs to ask what number do you want to be x and the program will respond with it’s square root and it’s cube root.

For this, we have to add a function to the program and that was the challenge, at least for me, because I didn’t knew how does a function works and even less how to add it to the code. That is why I ask for help to my classmate Sergio. He was very helpful, thank you Sergio!!

This is what I did:

quiz3

and here is outside the photo:

#include <iostream>
#include <cmath>
using namespace std;

double square_root (double);
double cube_root (double);
int main () {
double x, s, c;
cout << “I will give you the square and cube root of x. What is x? “;
cin >> x;
s = square_root (x);
c = cube_root (x);
cout << “Square root: ” << s << endl;
cout << “Cube root: ” << c;
return 0;
}
double square_root (double x) {
return sqrt (x);
}
double cube_root (double x) {
return cbrt (x);
}

So, the first thing I always do is insert the <iostream> and the namspace std. This time I added the library of the math functions which is <cmath>, because we will need it to solve the square and cube roots. I also add the values of them: double square_root (double);
double cube_root (double). What I understood was that we need to add this so

Continue reading "I finished the quiz in time!!"

Easy peasy

--Originally published at Programming Path

Well, I made an exercise of my programming course #WSQ01. It was unexpectedly easy. Of course I didn’t figure it out just by experimenting. I read the book recommended for the course. It is called How to think like a computer scientist. There, I’ve confirmed that it was almost the same commands for adding, subtracting, multiplying and dividing. The only one I couldn’t figure it out by my one was the remainder of the division. So when I read the book, I find the right command.

This was the result for my code, it didn’t take me to long to finished it. Actually, it was fun doing the coding.

mortal

And here it is again:

#include <iostream>
using namespace std;
int main ()
{
int a, b, c, d, e, f, g;
cout << “Insert the first number ” << endl;
cin >> a;
cout << “Insert the second number ” << endl;
cin >> b;

c = a + b;
cout << endl << “Sum of the two numbers = ” << c << endl;
d = a – b;
cout << “Difference of the two numbers = ” << d << endl;
e = a * b;
cout << “Product of the two numbers = ” << e << endl;
f = a / b;
cout << “First divided by second = ” << f << endl;
g = a % b;
cout << “Reminder of integer division = ” << g << endl;

return 0;
}

I really had fun doing this.


Finally! Hello World!

--Originally published at Programming Path

I’ve been trying to run hello world for a week but couldn’t. I’ve followed the instructions my teacher gave me, actually he made a YouTube tutorial and was easy to follow, but still, I couldn’t run “Hello World”!, I started to panic and call my brother for help. He said my code was fine and probably was the programs that were not fully installed. Guess what! It was true. My teacher helped me to install Cygwin correctly and I can run the code now. I’m so happy now!

Here is my code and the result after so much struggling:

hello-world

The code:

#include <iostream>
using namespace std;
int main ()
{
cout << endl << “Hello World ” << endl;
return 0;
}