Thursday, September 8, 2011

References

#include <iostream>

using namespace std;

int main()
{
string name = "Eric L";
string& refName = name;
cout << refName << endl;

refName = "Changed!";
cout << refName << endl;
cout << name << endl;
return 0;
}




References

Test of string

Test of string

#include <iostream>

using namespace std;

int main()
{
string a = "Hello";
string b = "World!";
string c = a + b;
cout << c << endl;

cout << "c.size() = " << c.size() << endl;

for(unsigned i = 0; i < c.size(); i++){
cout << "c[" << i << "] : " << (char)c[i] << endl;
}

return 0;
}




Sunday, September 4, 2011

Pointer and Array

Pointer and Array



#include <iostream>


using namespace std;

int main()
{
char array[] = {'A', 'B', 'C', 'D', 'E'};
char* PointerToArray = array;
char* PointerToChar = &array[0];

cout << "array = " << array << endl;
cout << "PointerToArray = " << PointerToArray << endl;
cout << "PointerToChar = " << PointerToChar << endl;

cout << endl;
cout << "array[2] = " << array[2] << endl;
cout << "PointerToArray[2] = " << PointerToArray[2] << endl;

cout << endl;
cout << "array + 2 = " << array + 2 << endl;
cout << "PointerToArray + 2 = " << PointerToArray + 2 << endl;
cout << "PointerToChar + 2 = " << PointerToChar + 2 << endl;


return 0;
}



VISUAL C++ 2010 EXPRESS: Free tools to create applications on Windows using Visual C++


Visual C++ 2010 Express is part of the Visual Studio 2010 Express family, a free set of tools that Windows developers at any level can use to create custom applications using basic and expert settings. Visual C++ is a powerful language that is designed to give you deep and detailed control when you build either native Windows (COM+) applications or .NET Framework managed Windows applications.





link:

- Visual C++ 2010 Express

Saturday, September 3, 2011

Size of data

Size of data


#include <iostream>


using namespace std;

int main()
{
cout << "sizeOf(bool) = " << sizeof(bool) <<endl;
cout << "sizeOf(short) = " << sizeof(short) <<endl;
cout << "sizeOf(int) = " << sizeof(int) <<endl;
cout << "sizeOf(long) = " << sizeof(long) <<endl;
cout << "sizeOf(float) = " << sizeof(float) <<endl;
cout << "sizeOf(double) = " << sizeof(double) <<endl;
cout << "sizeOf(char) = " << sizeof(char) <<endl;
cout << "sizeOf(void) = " << sizeof(void) <<endl;
cout << "sizeOf(NULL) = " << sizeof(NULL) <<endl;
cout << "sizeOf(void *) = " << sizeof(void *) << " - (pointer)" << endl;

return 0;
}


Please notice that the size of data type are machine/system dependance!


Get integer value of char


The function int(c) returns the integer value associated with a character variable c.

Get integer value of char

#include <iostream>


using namespace std;

int main()
{
for (char i = 'A'; i <= 'Z'; i++){
cout << i << " : " << int(i) << endl;
}

for (char i = 'a'; i <= 'z'; i++){
cout << i << " : " << int(i) << endl;
}
return 0;
}



Thursday, September 1, 2011

Bubble Sort

Bubble Sort


#include <iostream>

#include <cstdlib>

using namespace std;
void bubblesort();
void swap(int *p1, int *p2);

int data[10];

int main () {

//Prepare data
for (int i = 0; i < 10; i++) {
data[i] = rand() % 100;
}
cout << "data before bubble sort:" << endl;
for (int i = 0; i < 10; i++)
cout << data[i] << " ";

bubblesort();

cout << "data after bubble sort:" << endl;
for (int i = 0; i < 10; i++)
cout << data[i] << " ";

return 0;
}

void bubblesort() {
int i, j, min;
int size = sizeof(data)/sizeof(int);

cout << endl << "size = " << size << endl;

for(i = 0; i < size - 1; i++) {

min = i;
for (j = i + 1; j < size; j++){
if (data[j] < data[min])
min = j;
}

if (i != min){
int tmp = data[i];
data[i] = data[min];
data[min] = tmp;
}

}
}