Subscribe to Stuck in an Infiniteloop        RSS Feed
-----

Frequently Asked Programming Questions - Part I

Icon 1 Comments
We get a lot of programming questions here. They come in waves; they come in packs. The analytically minded among us have noticed patterns among threads (start/end of a semester, extra credit, AP tests, etc...). There are also a lot of "repeats": the exact same question, a variation on that question, or a permutation imposed by the giver to make it harder to Google a program for it (but at the core, the same deal with a few modifications). Allow me to climb out of this hole to explain the purpose of this post: to provide a link to the most commonly asked questions (and perhaps a few less common ones?). Having that said, I have no idea how many parts this is going to have. It's more of a "write as I think of it" type of blogging. Each will have 2-3 problems: each "Section" will contain the problem, the purpose of the assignment and a few examples of how to go about it. There might also be links to tutorials or snippets here (use 'em if you got 'em!).

1. Drawing [insert any shape here] in the console

Purpose: How to use embedded loops.

Posted Image

The idea is for "you" to learn that each time the outer loop executes, the inner loop executes 'x' amount of times:

for(int i = 0; i < 5; i++){
     for(int j = 0; j < x; j++){
     }
}



A "Right triangle":

Quote

*
**
***
****
*****


What's the pattern? Each line has one more asterisk then the line before it. First iteration has one, second has two, so on and so forth, so:

#include <iostream>
using namespace std;

void drawRightTriangle(int size){
	for(int i = 0; i < size; i++){ //the number of "lines"
		for(int j = 0; j <= i; j++){ //each line needs to have one more then the line before it
			cout << "*";
		}
		cout << endl;
	}
}

int main(){
	drawRightTriangle(5);
	return 0;
}




Taking the same concept, let's draw a square. The definition of a square is that the sides are the same:

Quote

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *


5x5. Each line must have the number passed to the function (or hard-coded if you like):

void drawSquare(int side){
	for(int i = 0; i < side; i++){
		for(int j = 0; j < side; j++){
			cout << "* ";
		}
		cout << endl;
	}
}



Figuring out the pattern/relationship is the key for shape creation. Moving on!


2. Functions (or methods if you're British Java)!

Purpose: Delegation of code functionality! Have a segment of code that is constantly repeated? Throw it into a function! Programmers are lazy, the less typing the better.

In a nutshell;, when a function is called, "control" is passed from the caller (main() usually) to the function that is called. All of it executes until it hits its return (can be explicit, examples to follow).

A visual:
Attached Image


A programming example:

#include <iostream>
using namespace std;

void function(){
	cout << "Hi! I'm in a function!" << endl;
	return; //not necessary for void function, implicit
}

int main(){
	cout << "Hi! I'm in main()!" << endl;
	function();
	cout << "Back in main() :-(" << endl;
	return 0;
}



Notice the ordering of statements (executed sequentially, barring any type of control structure). These type of statements are really helpful, oh wait look:

3. How Do I Debug this: Tracer Statements

Purpose: This is less of a question and more of "you need this!". Tracer statements are lines where you print some data to a log, the console, whatever. This is the most basic form of debugging, your bread and butter some might say. It allows you to see what the computer "sees"/is dealing with at any point in time.

Why is this important? Assumption is the mother of all fuck ups. You may think you know what the value of a variable is or where control branches off based on your logic, but sometimes, it isn't what you expect and fiddling around without verification is a complete waste of time.

Let's assume you are new to static variables (just for the sake of argument, they can be sneaky):

#include <iostream>
using namespace std;

int sneakyFunction(){
	static int value = 1;
	return ++value;
}

int main(){
	int temp = sneakyFunction();
	if(temp == 2) cout << "Hooray! This is what I want! (first check)" << endl; //this will execute
	temp = sneakyFunction();
	if(temp == 2) cout << "Hooray! This is what I want! (second check)" << endl; //this won't execute
	return 0;
}



One could assume that the variable goes out of scope and thus, each if conditional should execute. The second doesn't, so let's put a tracer statements inside sneakyFunction to get a "snapshot" of the variable 'value':

#include <iostream>
using namespace std;

int sneakyFunction(){
	static int value = 1;
	cout << "value's value: " << value << endl;
	return ++value;
}

int main(){
	int temp = sneakyFunction();
	if(temp == 2) cout << "Hooray! This is what I want! (first check)" << endl; //this will execute
	temp = sneakyFunction();
	if(temp == 2) cout << "Hooray! This is what I want! (second check)" << endl; //this won't execute
	return 0;
}




Let's take another example. We'll remove the static modifier on value and deal with another "possibly" sneaky issue: the placement of the increment operator:

#include <iostream>
using namespace std;

int sneakyFunction(){
	int value = 1;
	return value++;
}

int main(){
	int temp = sneakyFunction();
	if(temp == 2) cout << "Hooray! This is what I want! (first check)" << endl; //this will execute
	temp = sneakyFunction();
	if(temp == 2) cout << "Hooray! This is what I want! (second check)" << endl; //this won't execute
	return 0;
}



Here, the increment doesn't happen until after the variable is returned (so, technically it doesn't happen at all). To illustrate what temp is assigned to, let's through a tracer statement before the first conditional:

int main(){
	int temp = sneakyFunction();
	cout << "temp's value: " << temp << endl;
	if(temp == 2) cout << "Hooray! This is what I want! (first check)" << endl; //this will execute
	temp = sneakyFunction();
	if(temp == 2) cout << "Hooray! This is what I want! (second check)" << endl; //this won't execute
	return 0;
}




Now that we can "See" temp is one, we can adjust accordingly.



-------------
This concludes part one.

I'll be on the hunt for more FAPQ in the forums (these first three are ones that just came to mind at a moment's notice). Happy Coding!

1 Comments On This Entry

Page 1 of 1

alias120 

11 May 2010 - 02:37 AM
Glad to see some of the FAPQ being answered here, when I first started programming refrences like these were invaluable. Keep up the good work KYA.

-alias
0
Page 1 of 1

December 2020

S M T W T F S
  1234 5
6789101112
13141516171819
20212223242526
2728293031  

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    2 user(s) viewing

    2 Guests
    0 member(s)
    0 anonymous member(s)