April 24, 2009
I am jumping beyond my level with C++ at this point: pointer variable. I was just giving my first (not really, my second) glance on it. But the powerful pointer is really amazing and attractive - wondering when....
Okay I hope what I am going to write is 99% correct - if not, please leave a comment and discuss with me.
What is pointer? Well, I would just simply treat pointer as "pointer" in English. In English it means something is used to indicate something. In programming, we can say, "pointer is a tool to point at an object".
In another word, in terms of C++ and programming theory, that first, we know a variable is treated as directly reference to a value, and so a pointer, we can say, it's indirectly reference to a value.
Try to picture pointer as something has to do with "address", the sample code below will give you a very brief relationship among the pointer, variable and the value.
1 2 3 4 5 6 7 8 9 10
| #include <iostream>
using namespace std;
int main()
{
int a = 11;
int * aPtr = &a;
cout < < "\nAt address " << aPtr << ", the value " << *aPtr << " is stored.\n";
} |
Outcome:
At address 0x0053AD78, the value 11 is stored.
(more...)
Filed under:C/C++ Author:John Wong
No Comments »
March 13, 2009
I want to where my level stands, so I opened the book a few days ago, and tried to write it in C++ using what I know so far on control structures.
TASK
Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a C++ program that will input the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful. After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls.
You know I had been busy the whole week and tired after all these exams and lack of sleeping. I just spent a few minutes on Monday I believe, and failed the program I attempted to write. I found my thinking was wrong because I didn't read the task carefully, and did not analyze the entire task thoroughly.
I did some thinking today as I was walking home. I got stuck at one point: the ending - find the average of the combined miles per gallon from all the previous results. I need to store these results somewhere and use them later. I got some ideas and I believed I am on the correct route, but just too exhausted to continue thinking on the problem. By the way, I just woke up and is about to sleep again.
So far my thinking is simply, 1) what are the variables use in the program (I probably missed one or two in the drafting) 2) how to obtain the combined average (the concept about getting the results from the previous results without using tool like pointers, just simple loops) , 3) i know this is a loop, by solving #2 i can continue to formalte the structure of the loops.
So this is what I am thinking on
1 2 3 4 5 6 7 8 9
| int main()
{
// variables //
//initialization //
//do-while loops (i think) to get the result for each mpg until a sentinle value is receive to end the loop//
// if-else loops to do the usual non-zero division referring to the do-while loops //
// another loops on finding the combined miles per gallon //
} |
I think I am getting something from this draft now.... oh well
Filed under:C/C++ Author:John Wong
1 Comment »
March 1, 2009
Yesterday I was re-reading about the sentinel-control-structure, and I came back to my problem on why the author initialize the counter from 0 or from 1 (in two different cases).
For the discussion thread, please look at here
http://forum.codecall.net/c-c/14435-sentinel-control-structure.html
I will first let you guys see what are the two cases:
First case (definite-counter-control structure)
Task: You are ask to develop a program that will find the average of 10 students' test grades.
Code sample provide by the book
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| // average grade
#include <iostream>
using namespace std;
int main()
{
int total, // sum of grades
gradeCounter, // number of grades entered
grade, // individual grade
average; // average of grades in this class
// now we should first re-set some of the values
total = 0; // make sure total always starts with zero
gradeCounter = 1; // starting from 1, ends at 10
// enter all ten grades
while ( gradeCounter < =10 ) {
cout << "Enter grade: ";
cin >> grade;
total = total + grade;
gradeCounter = gradeCounter +1;
}
cin.get();
// get all ten and finally calculate this
average = total / 10;
cout < < "Class average is " << average << endl;
cin.get();
return 0;
} |
Second case (sentinel-control-structure)
Task: You are going to develop a simple program that will find the average of an arbitary number of test grades. In this case, the number of test grades being enter is vary in any case.
Code provide by the book
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| // average grade
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int total, // sum of grades
grade, // one grade
gradeCounter; // number of grades entered
float average; // class average
//initialization
total = 0;
gradeCounter =0;
// PROCESSING
cout < < "Enter grade, -1 to end the program: ";
cin >> grade;
cin.get();
while ( grade !=-1 ) {
total = total + grade;
gradeCounter = gradeCounter +1;
cout < < "Enter grade, -1 to end the program: ";
cin >> grade;
}
cin.get();
// termination
if ( gradeCounter !=0 ){
average = static_cast< float > ( total ) / gradeCounter;
cout < < "Class average is " << setprecision( 2 )
<< setiosflags ( ios::fixed | ios::showpoint )
<< average << endl;
}
else
cout << "No grade were entered" << endl;
cin.get();
return 0; // program ended successfully
} |
(more...)
Filed under:C/C++ Author:John Wong
No Comments »
February 27, 2009
Well we all know control structures are basically if, if/else, while, do, switch, and a few other methods that you can use to specify a sequences of task that the program may carries out.
I started reading the How to Program C++ book, a gift from my teacher. The book is a college level, well-written, even though it is quite old, published in 1998. But the detail it contained was amazing...
Just a few quick notes on control structures:
1. algorithms - a procedure for solving a problem
2. pseudocode - plain-English statements that allow a programmer to develop algorithms
3. total of only 7 control structures
[1] sequence
[2] selections -> if, if/else, switch
[3] repetition -> while, do/while, for
4. it's always good to initialize counter and total to zero
5. counter = 1;
6. counter =0;
7. conditional operator -> (?:)
8. counter-control-structure = definite repetition
9. sentinel-control-structure = indefinite repetition
10. use top-down, stepwise refinement to develop the algorithms
Filed under:C/C++ Author:John Wong
No Comments »
February 17, 2009
It's just for fun, i will integrate a similar one into my hello world program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| // Let me trying to run another calculation program
// no more salary now
// let gets this people vs bandwidth
#include <iostream>
using namespace std;
// let us do a gloabl variables
int visitorNumber, visitBotNumber, bandwidth, dataSize, transferTime;
signed int dataBWR = 4;
// let us begin the main body of this program
int main()
{
// asks some questions
int x;
cout << "How many visitors so far?\n"<< endl;
cin >> x;
cin.get();
int y;
cout << "How many bot visitors?\n"<< endl;
cin >> y;
cin.get();
int z;
cout << "What is the total amount of data size being uploaded?\n"<< endl;
cin >> z;
if (z >10000)
bandwidth = (x + y) + (dataBWR * z);
else
bandwidth = (x + y) * (dataBWR);
cin.get();
cout << bandwidth;
cin.get();
return 0;
} |
Filed under:C/C++, College Notes, PHP&MYSQL, Ubuntu, Uncategorized Category, WordPress Mods, phpBB Mods (abandoned) Author:John Wong
2 Comments »
February 15, 2009
It sounds simple, isn't it? Well, let me try to play around with it. So I started with visual c++ 2008, but now I changed my mind to Dev C++ because that's what I used when I am in school (of course, @ my free period !!)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| // This is my N-times writing a hello-world program in C++
// Guess what, I am having fun with it !!
// So let's go....
// Author Jwxie518
#include <iostream>
#include <string>
using namespace std;
int main()
{
// first let me define a set of strings
string first, second, third, yourName, yourAge;
string myName = "John Wong's Smart Program";
// next, I want to print something out in the console
cout < < "Welcome to John Wong's Smart Program!\n"<<endl;
cout << "I am here to entertain you, please follow the instructions:\n"<<endl;
cout << "What is your name?\n";
getline (cin, yourName);
cout << "Hello, " << yourName <<" !\n";
cout << yourName << ", my name is " << myName;
cin.get();
cout << "How old are you?\n";
getline (cin, yourAge);
cout << yourAge << "? I can't believe it! Cool!\n";
cin.get();
cout << "So we are done with the introudction, I guess?\n"<<endl;
cin.get();
return 0;
} |
Well, I will keep making this hello-world program larger and funnier ^_^
Filed under:C/C++ Author:John Wong
1 Comment »