Thursday, March 19, 2009

Blabbling about Pointers

I didn't know what the point of pointers was until yesterday upon reading Prata. I learned that you use pointers to allocate memory with the new operator. I guess there is no other way. Before I found about runtime decisions vs. compile time decisions I thought that there was no point to pointers and you could always just pass by reference. What about runtime decisions vs compile time decisions? well, a runtime decision is a decision that is made while the program is running and a compile time decision is a decision made while the program is being compiled. When you're making a very large program you want to manage memory so that variables or arrays aren't all loaded into memory if they don't need to be loaded into memory. There's a lot of talk about heap or stack. I can't remember where memory allocated by new goes. Any who, maybe you don't want memory space to be allocated until certain conditions are met.

if (GetAnswer() == true;)
{
int * p_number = new int; // allocated memory with new
cout << "You get " << *p_number << " points!"; //dereferencing the pointer to display value and not address } However, you usually don't want to use pointers for programs with just a few variables. Another cool thing about pointers and allocating with the new is dynamic binding. Static binding is when you create an array with a fixed amount of elements, but when you dynamically bind you allocate an array and you make the size of the array a variable. e.g.
Code:

#include <iostream>
using namespace std;

int main()
{
int ArraySize;
cin>> ArraySize;
int * p_numbers = new int [ArraySize];

for (int x = 0; x <= ArraySize; x++){ p_numbers[x] = x; cout<<<>>f;
return 0;
}

You wouldn't be able to complete the same task with normal arrays.



Wednesday, March 18, 2009

CPlusPlus quiz


I was just doing http://www.mycppquiz.com until I realized that I not ready. I must finished Prata's C Primer Plus 5th edition before I try doing that test again.

Monday, March 16, 2009

What's your Fractional Age?


I'm making a relatively [on the relativity of "light coding"] simple program that calculates a persons age with its fractional part (COPY PASTED from coderrach's Blog. [the text before this parenthetical]). I made this program to remind me how much time I have until I'm a year older to remind me that I shouldn't waste time on video games and other things that aren. I just finished! Holy cow, that took a while. I've been working on it for at least 4 hours, thinking of how to solve all the problems.

list of some problems I encountered [don't laugh]
  • Days left until your birthday devided by 365 is not the fractional part of your age; however it is the fraction of a year until your birthday.
  • Amount of days in each month is not 30 [I did not try to code for each month. I used 30.5 (close enough) Though, if you're curious, the average of days in a month out of all the months is 30.42 (rounded)]
  • When birth month was greater than current month, user was a year older.
  • When birth month was less than current month, months left until birthday was not birth month - current
  • "1 exit status. Permission Denied." Dev-C++ failed to let me know that my program was running therefore I was enable to compile. For 10 minutes, I wasted time changing my soure code thinking my code was bad. Arggg!
Here's the code for the program.

Code:

//caclulate your age in years plus its fractional part

#include <iostream>
using namespace std;

float DaysOfYearLeft (int, int, int, int);
int GetYears (int, int, int, int);


int main()
{
float age;
int bday; // birthday
int bmonth;
int byear;
int cday; // current day
int cmonth; // current month
int cyear; // current year
float fyear; // fractional year
float fday; // fractional day
float fage; // fractional age is the sum of fyear and fday


cout<<"This program calculutes your age plus the fractional part of your age.";
cout<<"\n\n Enter your numerical birth day, month, and complete year in that order each\n value seperated by a single space.\n";
cin>>bday; cin>>bmonth; cin>>byear;
cout<<"\n\n Enter the current numerical date day, month, and complete year in that order \neach value seperated by a single space.\n";
cin>>cday; cin>>cmonth; cin>>cyear;

fyear = GetYears (byear, cyear, bmonth, cmonth);
fday = 1 -(DaysOfYearLeft (bday, bmonth, cday, cmonth)/365) ;
fage = fyear + fday;

cout<< "\n\nYour age with its fractional part is "<< fage;
cout<< "\n That is " << GetYears(byear, cyear, bmonth, cmonth) << " years and "<< 365 - DaysOfYearLeft (bday, bmonth, cday, cmonth)<<" days.";
cout<< "\n" << DaysOfYearLeft (bday, bmonth, cday, cmonth) <<" days until your birthday!";


char f;
cin>>f;
return 0;
}

float DaysOfYearLeft (int bday, int bmonth, int cday, int cmonth){

float monthsleft;

if (bmonth > cmonth){
monthsleft = bmonth - cmonth; //months left until next ageplus1
}
else
if (bmonth < cmonth){
monthsleft = 12 - (cmonth - bmonth);
}
// function does not yet account for current month equal to birth month

float daysleft = (monthsleft * 30.5 + bday) - cday;

return daysleft;
}

int GetYears (int byear, int cyear, int bmonth, int cmonth){

int age;


if (bmonth > cmonth){
age= (cyear - byear) - 1;
}
else
if (bmonth < cmonth){
age = cyear - byear;
}

return age;
}
list of problems trying to get the HTML right on this post:

Friday, March 13, 2009

Structures

I just learned about structures yesterday. I'm going to write up what I learned to reinforce some knowledge.

struct Person {char Name[20]; int Age; float Height;};

That's how you declare a structure. According to Prata, you want to externally declare structures so all functions can make use of them. Yes, programmers discourage declaring global variables or externally declaring variables, but recommend the external declaration of structures. This is how you use structures in the main function after having defined the structure before the main:

Person you = {
"CppNoob",
20,
172.0
};

or Person you = {"CppNoob", 20, 172.0}; // all the same

Now you can print you's values

cout<<"Name: " << you.name << " Age: " << you.Age << " Height:" << you.Height;

Saturday, March 7, 2009

Programmers on Blogger Suggestion

There are 61,300 blogger users that are interested in programming. I think Google Blogger should take this fact into consideration and create a source code feature. I find posting my source code somewhat difficult here.

Perhaps Blogger's programming team can get ideas from http://pastebin.com/ or http://www.cplusplus.com/forum I particularly like CPLUSPLUS.com forum's feature. You just put your code between [code][/code] Let me show you.

input:



output (different input but I just wanted to give blogger team and idea of what I want):

Comparing Strings in C++

#include
int compare( const string& str );
int compare( const char* str );
int compare( size_type index, size_type length, const string& str );
int compare( size_type index, size_type length, const string& str, size_type index2, size_type length2 );
int compare( size_type index, size_type length, const char* str, size_type length2 );

Sometimes, you want to compare two different strings for some reason. How do you compare strings in C++?

Easy, use the compare function defined in the string library. Look at all those function prototypes with the same header. I'm pretty sure this is called function overriding or overloading, when the same function name can have different parameters and handle these parameters differently. In this case we have variety of different functions with the same name, different parameters, but they all do the same thing in the end which is compare strings.

int compare( const string& str );

I have no idea what the parameters in the function above mean. Looks like this function has a single parameter: a constant string? I don't know what the ampersand is supposed to tell me! I just realized that I need to be more aware than I am about compare being a member function of the string type(class), therefore in order to use compare you have to create a string object and use a membership operator (e.i. a dot) between the object and the member function.

EXAMPLE SOURCECODE ON USING COMPARE WITH TWO STRINGS OH YAY:

string x = "blah";

string y = "nope";

int t;

t = x.compare(y);

cout<<
see x.compare(y)

Darn. I finally understand. I just didn't think you had to declare what type of information was in the address. Now int compare( const string& str ); makes complete sense. This function accepts a constant string, but

instead of taking in a copy of the string, it takes the address of the string to compare the actual string and not a copy of the string. Fascinating!

if i were to create a function

int funct (int &y){

y = 10;
}

int main () {

int y = 5;

funct(y);

cout y; // psuedo code. add insertion operator.

the y variable would be changed and print 10 instead of 5. if i wouldn't have made the function accept the address of a the variable i'm passing to it then it would just create a copy of the variable, a local variable. try stuff out.
see http://www.cppreference.com/wiki/string/compare
i'm tired.

Friday, March 6, 2009

C Plus Plus Terminology

(order of what first comes to mind)
integers, floating points, boolean, array, classes, objects, instances, polymorphism, virtual functions, compound types, types, operator, member functions, member variables, streams, escape characters, ASCII, loops, token, delimiter, header, constructor, destructor... Check out the cpp's creator's glossary of terms.

Wednesday, March 4, 2009

Wii Internet Capabilities

My cousin has been testing out Wii Internet ever since I bought it for only 5$ dollars, and I was surprised by Wii Internet's capabilities:
(see How To Get Wii Internet if you don't have it)
  • you can go on facebook
  • you can email someone
  • you can chat using web messengers
  • you can watch YOUTUBE videos
I'm not sure if you can watch flash videos... to be continued...

How To Get Wii Internet

  • make sure your wii is connected to your WiFi connection
  • go to wii shops on the wii menu
  • go to start shopping
  • go to buy points
  • go to buy points with visa
  • input visa information to buy points
    • internet app only requires 500 points so buy the minimum 10$ for 1000 pts
  • go back to Start Shopping
  • go to Channels
  • buy internet Channel for 500 pts
  • wait for download to complete

    see Wii Internet Capabilities: what can you do with internet wii?

Monday, March 2, 2009

C Primer Plus (5th Edition) by Stephen Prata

I bought a book at Borders today: C Primer Plus (5th Edition) by Stephen Prata. I've been wanting this book for a long while, but there's a somewhat funny story behind it. Without knowing, I've been borrowing the third edition from the Orange County main library for several days now. As I was reading the third edition, I recognized that it had the same name as that blue C Primer Plus book I saw at the top of amazons best sellers in C++ months ago, but I didn't make the connection. When I looked up the book at Borders.com to see if it was in stock, I saw that they were authored by the same guy, Stephen Prata. "Stephen Prata is a professor of physics and astronomy at the College of Marin in Kentfield, California, where, in addition to astronomy and physics, he does computer science." says Google and the back of the book. My understanding of the language is advancing steadily. Yesterday, I finally learned (upon reading some of the third edition) that cout is an object of the class ostream in the iostream library. [see cplusplus.com on cout for further reference]

C Primer Plus is a mega plus over C++ For Dummies (7 in 1). C++ for Dummies by Jeff Cogswell has so much useless text. I'll try to get some examples when I get my hands on the book. For Dummies likes to get the reader interested, but I don't think that works out when the writer is a computer scientist. Jeff Cogswell's trying to keep my interest just annoyed me. In C Primer Plus, Stephen Prata wastes no text on useless. He surprised me from the very first chapters by already introducing the importance of classes and objects in programming. I have to admit, though, I would be somewhat lost if not for having read C++ For Dummies.