Tuesday, May 1, 2012

Conversion table of numbers from char to int, for problem 7 of chapter 7.

This is just a handy list I will be leaving here. Who knows, I might even have a use for it later.

  • 0 in char is 48 in int
  • 1 in char is 49 in int
  • 2 in char is 50 in int
  • 3 in char is 51 in int
  • 4 in char is 52 in int
  • 5 in char is 53 in int
  • 6 in char is 54 in int
  • 7 in char is 55 in int
  • 8 in char is 56 in int
  • 9 in char is 57 in int

And this is the code I used to get the values:

File contents of SourceCode.cpp
 #include <iostream>  
 using namespace std;  
   
 int main(){  
 char charone = '0';  
 int intone = charone, counter = 1;  
   
 while(counter <= 10){  
 cout << endl;  
 cout << charone << " in char is " << intone << " in int";  
 cout << endl;  
 intone++;  
 charone = intone;  
 counter++;  
 }  
   
 return 0;  
 }  

Hating emacs and debugging...

My hate for emacs got me to find a really nice linux IDE called Eclipse CDT. It was quite heavy to download, but worth it. It's almost like Microsoft Visual Studio Express 2010. So far, debugging in Eclipse has been a breeze. Still better in my humble opinion than emacs.


PS: Thank you hate. You really are useful sometimes.

Wednesday, April 25, 2012

Final script updates for ultimate laziness...

So, here is now my new, and most recent, "cal" batch script and the new "execute program" batch script:

File contents of "cal.bat":
 @echo off   
 echo Compiling and linking the C++ source code,  
 echo and preparing the executable for debugging...  
 g++.exe -Wall -g -o executable SourceCode.cpp  
 pause  

File contents of "execute program.bat":
 @echo off  
 executable.exe  
 pause  


PS: The little "pause" command at the end of each file freezes the Command Prompt window until I press any key, which makes me able to see the contents of the window without it automatically closing right after the compilation or the execution of the program was complete...


Let the double-click heaven begin!!

ideone.com - Best. Site. Ever.

First I got rid of linux by installing MinGW and MS Visual Studio Express, and now I'm basically getting rid of my whole computer as long as I have a device I can type stuff into a website. I don't need a computer at all to test my programs! ideone.com rocks.

PS: For some reason I feel like making and advertising right now...

Sunday, April 22, 2012

Back in buisness...

I know, I know, I've been awfully lazy lately. I just wish I had a huge break by now. I still don't want to do anything at all, but I don't want a bad grade either. Well, here's some stuff I've written down a bit in my notebook. Here is a list of what has been defined in class so far and in which directive they reside so that every time I start programming, if I need to look down something to see where it was defined, I can just take a look at this list and include the directive I need in my source code. I still need to update some of it though.

I. List of primitive types:
    This list should be enough.


II. List objects, operators and member functions from <iostream>:


Objects:

  1. cout
  2. cin
Operators:
  1. >> - extraction operator
  2. <<  - insertion operator
Member funcions:
  1. setf(... :: ...)
  2. precision(n)


III. List of classes, operators and member functions from <fstream>:


Classes:

  1. ifstream
  2. ofstream
Operators:
  1. >> - extraction operator
  2. <<  - insertion operator
Member funcions:
  1. setf(... :: ...)
  2. precision(n)


IV. List of manipulators from <iomanip>
  1. setw(n)
  2. setprecision(n)
V. List of functions from <cctype>
  1. toupper(char_variable) - Returns an int value
  2. tolower(char_variable) - Returns an int value
  3. isupper(char_variable) - Returns a boolean vaue
  4. islower(char_variable) - Returns a boolean value
  5. isalpha(char_variable) - Returns a boolean value
  6. isdigit(char_variable) - Returns a boolean value
  7. isspace(char_variable) - Returns a boolean value
------------End of List-------------

Those are basically the lists. I'll probably be updating those so that I just have to search within this text in order to find what I need to include in my source code. Oh, and before I forget, I need to write these couple of important sentences down:

  1. Every Variable is an Object, but not every Object is a Variable (i.e. the objects are a proper subset of the variables).
  2. Every Type is a Class, but not every Class is a Type. (i.e. the classes are a proper subset of the types.)
  3. An object is an element of a Class.
  4. A variable is an element of a Type that is not a Class.
I think that will be it for this post...


Edit (April 25): I think I'm missing a list...

Friday, March 23, 2012

Integer division for the win!

Integer division is SO useful. I can't believe I made the Arabic to Roman numerals converter using that. The program is even able to write numbers from 0 to 3000 (instead of 1000 to 3000) which is amazing! I can't believe I'm so excited for something that silly, haha!

By the way, just a reminder to myself: Integer division and modular arithmetic are NOT the same.


I need to get that into my head...

Thursday, March 22, 2012

Ugh... stupid "break;" >.>

Programming is frustrating... a little bit. I spent like 10 minutes looking for a really small error I couldn't get to find by myself, until I used a debugger to get to it. I just missed a "break;" inside my switch statement and my string variables went crazy changing.

Note to self:
      Write more carefully. It's better to take my time writing the program, than to write it as fast as I can and, later, lose a bunch of time looking for mistakes.


Update:
     UGH! Found another one. I can't believe this. Next time I'm seriously going to take my time and my patience to write one.

Saturday, March 17, 2012

What sorcery is this!? Computers can divide by zero!

I can't believe it! Including the directive <cmath> to my source code actually allows the computer to calculate infinite values. I had a small error with the Leibniz's Pi approximation series where I ended up basically dividing 1 by zero and multiplying that by 4, but this is what I got:

 Please enter the number of terms you would  
 like to sum from the Leibniz's Pi approximation series: 0  
   
 The sum of the 0 term(s) results in  
 the Pi approximation equal to: inf.  

There's no floating point exception!

Tuesday, March 13, 2012

632 lines of code!

I thought my head was going to explode! This program was huge! Most of it copy paste though... it repeated a lot, but it took me some time to see the pattern. I can't leave this for the last week. I got to work on Lab 3 during the weekend. Programming in C++ seemed easier to write than what it actually is. Silly things that help like indenting and formatting stuff with variables and such help so much but it takes so much time. I still like to write C++ code though, but this program seriously took some of my time.

... I want a Raspberry Pi.  =|

Thursday, March 8, 2012

I never thought I'd say this...

... but, I think I'm starting to like programming a LOT. I mean, my grade wasn't what I really wanted on my first midterm. I don't blame myself though, I had a lot of stuff to do that week, and my reading-code skills aren't that great right now, but is just that whenever I try to solve a problem by writing a program, and the more I do it, the clearer my thoughts become. These are the kinds of problem I love doing. And, on top of that, this is really practical stuff. I'm pretty sure that the time will come when I can translate everything that I know so far of math to C++. And what's good about it is that I can use it to solve some complex stuff for some people by asking just a little bit of input that I know they can give. Gosh, the more I think about it, the more I like to write code. :|

I got to stop thinking now though, I have to sleep.

Monday, February 20, 2012

Yes! Found a debugger for Windows!

Microsoft Visual Studio 2010 Express is my solution. It does everything and the GUI is even beautiful. It's a little weird for the compilation, but it does its work. Got to practice in it a lot though. But I'm so glad I found one that just works.

Bummer!!! I thought I had it right!

Dang, I think I got something mixed up when I wrote all my codes. Now I got to check all of them.

Note to self: when g++ compiles stuff, at least in what just happened, when there's an int divided by a double variable it results in an int variable. At least that's what happens here in Windows. It screwed up my Moore's Law source code. Bummer. Got check the g++ of Linux though. I really don't know if the compilers work exactly the same, but it seems pretty well like it.


Update: Yeah, it's the same stuff in Linux. I can still compile happily in Windows. I'm so glad that hasn't been taken away from me. 

Testing code for the Babylonian Algorithm

So, here I was happy until I saw in the lab that I had to compute the square root of a number. Thing is, I'm still to lazy to grab a calculator and put some numbers in it.

So, I searched in Google how to compute it in C++ and I found that I had to include a new source code called "cmath". I tried it and it worked. Here's the source code for testing the sqrt() function from the cmath source code:

Source Code:
 #include <iostream>  
 #include <cmath>  
 using namespace std;  
   
 int main()  
 {  
   
 long double number_to_calculate_square_root_of;  
 char ans;  
 cout << endl;  
 cout << "Greetings.\n";  
   
 do  
 {  
 cout << "Please enter the number you would like\n";  
 cout << "to calculate the square root of: ";  
 cin >> number_to_calculate_square_root_of;  
 cout << "The square root of that number is: " << sqrt(number_to_calculate_square_root_of) << ".\n";  
   
 cout << endl;  
 cout << "Would you like to repeate this process again?";  
 cout << "Press 'Y' for yes and 'N' for no:";  
   
 }while(ans == 'y' || ans == 'Y');  
   
 cout << endl;  
 cout << "Thank you for testing.";  
 return 0;  
 }  


PS: I'm definitely going to use this for the Babylonian Algorithm just to check that the algorithm is actually approaching the square root of the inputted number.

PPS: My professor did mention this, but he told me he did...


Note to self: Pay MORE attention.

Sunday, February 19, 2012

Batch File and Linux Script - Final Version

Here are the contents of the final batch (*.bat) and linux script file for compiling my source codes.


Batch File:
 @echo off   
 echo Compiling and linking C++ source code,
 echo and preparing the executable for debugging...
 g++.exe -Wall -g -o executable SourceCode.cpp    
 echo Done  


Linux Script:
 echo "Compiling and linking C++ source code,"   
 echo "and preparing the executable for debugging..."   
 g++ -Wall -g -o executable SourceCode.cpp    
 echo "Done"


PS: I really like their similarities. They are almost the same!
PPS: I got to remember to look for a debugger for the Windows OS later.

MinGW - The Solution! Windows Batch - Updated!

Success! MinGW compiles and links the source code and prepares the executable for debugging! Here's the new batch file:

@echo off  
echo "Compiling and linking C++ source code,"  
echo "and preparing the executable for debugging..."  
"C:\MinGW\bin\g++.exe" -Wall -g -o executable SourceCode.cpp   
echo "Done"   

PS: I can't believe it! No more jumping between operating systems! Now I'm just missing a debugger! I hate Emacs!


PSS: Before I totally forget. By installing MinGW in windows I had to add a new path for the environment variables. (In Windows 7 I can find that by clicking on Start > Right clicking on Computer > Properties > Advanced System Setting > Environment Variables. Then, once I found the "Path" variable, I had only to add a semi-colon at the end of the line ";" and add the desire path, which in this case was "C:\MinGW\bin". I did this because I had an error with libgmp-10.dll. The g++ from the MinGW directory could not find it when I tried to compile my source code. And now that I recall... setting that environment variable that allows me to run g++ directly through the command prompt window, without having to specify its directory. I should update this *.bat file once more.

Windows Batch File - Done!

It works! Here is the content of the windows batch file (*.bat) I recently created:

@echo off  
echo "Compiling and linking C++ source code..."  
"C:\Dev-Cpp\bin\g++.exe" -o executable SourceCode.cpp
echo "Done"

Note: It's basically the same stuff as in linux, the only difference is that I need to turn the echo off for the currently running codes. If I don't do this, the line "C:\Dev-Cpp\bin\g++.exe" -o executable SourceCode.cpp  will show up in the command prompt window.


PS: It doesn't seem possible to prepare the executable for debugging in Windows. I just read something about MinGW, got to check it out... It's a pain to have to reboot every time I need to make programs.

I can compile in Windows!

Yes! I heard someone mention in the math department that they were programming in something called Bloodshed. I checked it out, and it turns out the program was called DevC++ by Bloodshed. Looks like someone compiled the g++ for the Windows OS. I tried it with one of my problems and voila, it compiled and run perfectly.

Now I just need to make a script... I'm still too lazy to keep writing the same commands all over again in Windows.

Wednesday, February 15, 2012

Now that I remember...

I remember clearly that in one class the professor mentioned that he had to recompile our source codes in order to run the program. Does that mean that the executables created by us won't work on his computer? If so, and assuming he is also using linux, how is it possible for other machines to run software not compiled by them? It doesn't make sense. I download and run free software all the time, and I'm pretty sure I'm not getting any source code. I have to ask this on next class. This might come in the next exam if he asks about compilation. I mean, there must be something I can do when I compile my source code so that the executables work in any (or perhaps a broad range) of computers.

Precision Error!

... I actually typed this into my C++ source code and I'm too lazy to type it again. So, I'll just copy and paste it in here.

Here's the problem 1 of Lab #2:
A metric ton is 35,273.92 ounces.
1. Write a program that will read the weight of a package of breakfast cereal in ounces and output the weight in metric tons as well as the number of boxes needed to yield one metric ton of cereal.
2. Your program should allow the user to repeat this calculation as often as the user wishes.

And here's my "precision error" on the digits after the dot:

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);  

The value weight_in_metric_tons is REALLY small if the value of package_of_breakfast_cereal_in_ounces is really small too. If this is the case, setting the precision to 2 will only fetch me the first two digits after the dot revealling 0.00 as the value for weight_in_metric_tons which is false. I should let the computer handle the precision in that calculation. I should test more numbers really close to zero though, to see if I get 0.000000000 (or something like that) as a result.

Note to self:
Practice more.

Friday, February 10, 2012

Script - Updated

I just updated my "cal" script (which stands for Compile And Link) for deleting the .o file and the executable just before doing everything.

rm SourceCode.o
rm executableecho "Compiling the C++ source code..."
g++ -Wall -c SourceCode.cpp
echo "Compilation done"
echo "Now linking..."
g++ -o Executable SourceCode.o
echo "Done"

And here's the most compact version of the script:

rm executable
echo "Compiling, Linking and preparing the C++ source code for debugging..."
g++ -Wall -g -o executable SourceCode.cpp
echo "Done"

Now the compiler compiles, links and prepares the C++ source code for debugging. There's no .o file create by the compiler using this last script.

Monday, January 30, 2012

Before I forget...

I'm starting to have some questions about how data is stored. I mean, how is it physically possible to store data into a device? And, moreover, how is it possible to read it? For some reason I really want to understand the whole mechanism. I think I'm going to ask the professor about it next class. But I should get to work on another things right now...

Script - Done!

Okay, so, I asked the professor past week about creating a script for automatically compiling a C++ source code and linking it and he told me to just press the up arrow on my keyboard until I find the command on my terminal. Thing is, I'm too lazy to do that. I prefer to just type 5 symbols to get everything compiled and linked. So, here's the simple script I needed:

 echo "Compiling the C++ source code..."
 g++ -Wall -c SourceCode.cpp
 echo "Compilation done"
 echo "Now linking..."
 g++ -o Executable SourceCode.o
 echo "Done" 

Note to self: I'm "echoing" stuff because the g++ commands do not appear in the terminal window when running the script. I just want to know what the script is doing when I run it. And I hope compiler errors show up if anything happens.

Wednesday, January 25, 2012

My first weird error...

So, today I was just having fun with my first progam on my computer class, when suddenly I got a really weird output with it. Here it is:


[user@M110-07 Terminal]$ ./FirstProgram
Press return after entering a number.
Enter the number of pods:
100000000000000
Enter the number of peas in a pod:
If you have 2147483647 pea pods
and 134514000 peas in each pod, then
you have -134514000 peas in all the pods.


According to my professor, we need 47 bits for processing 100000000000000 in C++. Since the CPU is only 32bit, the processor can't process that number with C++. That's why  With Maple (or any other math-oriented software) is possible, but not with C++.

I just thought I'd leave here as a reminder.


Friday, January 20, 2012

First question.

I barely started my programming class today, and a question just popped into my mind:

  • How is it that we can control machines through inputs? What makes it possible?

Edit:
 Well, looking back at it, I don't think I made the right question. More precisely, "How is it that computer can compute stuff like adding numbers and processing the truth value of a boolean statement given the truth values of its components?"

There's still stuff that I don't get like how cpus work.