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.