PDA

View Full Version : Akalic Shows You How to Make Shitty Programs



Akalic
01-25-2011, 01:20 AM
What is all this about?

This is what happens when I have a light semester and an overwhelming urge to make random people on the Internet to suffer. Well... not really suffer, but I assume that learning how to program takes some level of pain and suffering. No longer will copy and pasting various functions you find on Google cut it, you will be able to actually write your own code (and damn good code, no O(n^3) programs here).

Why should I listen to what you're blabbing about?

I'm a third year Computer Science student and I know how to code. What allows me to make that claim? Numerous all-nighters, professors grading on a binary scale (100 or 0, nothing else) and still passing, and being able to code in Scheme without losing 95% of my brain functionality. You name any programming language and I'll be able to set up the integrated development environment (IDE), learn the syntax, and bust out the program within the day.

How will you help me?

I will post various programming books that I have "purchased" over my years weekly. These books will start out as simple introductory books on easy to learn languages and, hopefully, will evolve to my books on more intense topics. And given a significant interest in this thread (at least one or two victi... I mean, students) I will also post assignments for the week's topic, usually a quick program to flex your skills with a language or topic.


January 24, 2011

Books of the week: C++ for Dummies (http://www.speedyshare.com/files/26470790/C_For_Dummies_5th_Edition.pdf) and C for Dummies (http://www.speedyshare.com/files/26470789/C_For_Dummies_2nd_Edition.pdf)

As far as first languages go, C and C++ are ideal. Both share nearly exactly the same syntax (the wording of the language), making learning both a breeze. C++ is an easy to learn language with many free IDE programs, I highly suggest DevC++ (http://www.bloodshed.net/devcpp.html) for your first IDE program. Another plus is that C and C++ can be compiled on the same IDE. C++ is an Object Oriented Programming (OOP) language, meaning that you can create classes and objects which makes many programming assignments (especially Graphical User Interface (GUI) programs, which are technically anything that isn't text-based). C is an older language where C++ gets much of its structure from, but it does not have any OOP support or any fancy features that C++ has. Some would think this is a bad thing, but in fact it is a massive plus for the language. By not having things like memory allocation, type casting, and networking done automatically by either the IDE or the language itself, you learn about the inner workings of programs and how to code with some appreciation that you don't have to use malloc() for every variable you wish to keep around.

March 7, 2011

Books of the week: Clean Code: A Handbook of Agile Software Craftsmanship (http://www.speedyshare.com/files/27274283/Clean.Code.A.Handbook.of.Agile.Software.Craftsmans hip.pdf) and 97 Things Every Programmer Should Know (http://www.speedyshare.com/files/27274285/97-Things-Every-Programmer-Should-Know_Collective-Wisdom-From-The-.pdf)

This week I deviate from teaching you new languages in order to make you better at what you already have a basic grasp of. Clean Code is a great handbook at teaching you how to write your code in a clean and efficient way so others can read and understand your code without losing their minds in the process. No longer will you name variables a, b, and c. You will learn that making detailed names of variables, even the tiny integer variables for loops, is the proper way to write your code. 97 Things is a collection of essays from programmers in the field that tell about some horror stories that they have experienced in their line of work. Learning how to program from text books is fine and dandy, but in order to become a master of the art of programming, you have to learn by experience. This book lets you do the same learning but without the pain and suffering of looking for a logic error in a program 3000 lines long.

Assignments
Assignments will be listed from easiest to hardest. Most assignments can be completed in any of the languages I offer through my books, but if I decide to teach a language that is very specific (like Scheme) I will mark the assignments that cannot be done in that language. If my wording of the assignment leaves you with questions or suggestions, please refer to the Contact Me section to get on my ass about it.

Write a program that prints out "Hello World" five times, each on a new line, using a for, while, or do-while loop.

Write a program that prints out "3 + 4 - 2 *25 = " (whitespaces included), and calculates the answer, and finally prints the answer on the same line.

Write a program that prints out a two column table using the following data

Players: Bob, John, Jill, Tommy, Shasha, Derick
Score: 32, 12, -2, 300, 0, 42
Use the '\t' escape character to format the table to look like this:

Players <-- \t -> | Score
____________________
Name <--- \t ---> | Score
Write a program that uses various sorting algorithms to place a 15 slot array of random, positive integers (0-100) in DESCENDING order. When you run the program, it will print out the unsorted array and three options below it, "Bucket Sort," "Quick Sort," and "Bubble Sort." Each option will be numbered 1-3 and the program will be waiting for you to input an integer. Once you input an integer, it will check if it is valid, if it is not (like you put in a 5) it will clear the console and reprint the array and options. Once you are sure the input is correct, run the sorting algorithm assigned to that number. After the program sorts the array, it will clear the screen and print the sorted array with a message, "Generate new array? (Y/N)." If Y, clear the console and generate a new random array with the same options. If N, exit.

As for the algorithms, lets assume you name your array ran_arr Bucket sort requires that you find the largest number in ran_arr and make another array of that length (temp_arr). And then you assign every integer in ran_arr into temp_arr[integer]. This means if there is a 5 in ran_arr, you put a 5 in the 5th slot in temp_arr. Once temp_arr has all 15 integers stored, it will print itself, ignoring the numerous null slots it contains. Bubble sort and Quick sort are too bitchy for me to write, so here's some queer robots. (http://www.youtube.com/watch?v=vxENKlcs2Tw) Be aware that Quick sort uses recursion, so make sure to read about that before taking this on.

[ADVANCED] Write a program that asks the user for an integer and preform a Fibonacci sequence up to that integer:
Enter Number: 4
Fib Seq: 0, 1, 1, 2 Make sure to print the correct number of commas and whitespaces. This assignment will use recursion, not loops, be sure to read up on recursion functions and how to correctly compute the answer using a helper function and the correct base case.

[ADVANCED][For extra points, write this in C using structures] Write a program that simulates a library with books that can be checked in or out. The library should be able to print out a list of books it contains with its current status (checked in/out) and end the list with the sum of all the books, the sum of all books checked in and all books checked out. Use this format
BOOK_NAME - STATUS
BOOK_NAME - STATUS
...
XX Books in collection: XX in stacks, XX Out


[ADVANCED] Write a program that finds all the prime numbers up to n, where n is a positive integer that the program will ask as input when it is ran. The program will save the output of prime numbers in a file called “list.txt” in the same directory as the program. The file itself will have a header that will name the program that created it and what it lists.
Ex. list.txt

Created by prime.exe
List of prime numbers up to 22
1
2
3
5
7
11
13
17
19


[GOD TIER][JAVA OR C++ ONLY] Make Pong. The definition of the game itself should be easy for you to learn, but there will only be one paddle controlled by the mouse. This means you will draw the paddle based on the mouse’s position, no clicking required. The player will be playing against the wall which makes up the entire right side of the playing field. Of course the ball must bounce off the wall at the same angle it hit, meaning that if the ball hits the far right wall going downward and towards the wall at a 30 degree angle, it will change its velocity to be downward and away from the wall at 30 degrees (Hint, think of the movement of the ball as two separate numbers, the movement in the X axis and the movement in the Y axis. So when the ball hits the wall, depending on which axis that wall follows, you change the respective movement number by a factor of -1). The score will be the number of times the player can hit the far right wall, the game ends when the player misses the ball with the paddle and the ball passes the far left boundary. The speed of the ball will be handled by a standard class of the language, use Google to find the right classes to use. Another restriction is that contact with objects (ie. The paddle and walls) do not affect the ball’s speed, however, the ball’s speed will be changed every 5 points the player scores. The manner of change in speed is up to you, but I recommend either a flat increase in speed or using a random number generator to choose the increase in speed (I recommend setting the RNG to choose between a small variance of numbers so the player isn’t boned when his game makes the ball go faster by 10^22 frames). And for extra credit you can create a highscore list that is saved into a file and loaded to print the list.

DPK
01-25-2011, 01:44 AM
Nice of you too teach others :D
Ill try some of the begginer stuff out when i get some time

Luls
01-25-2011, 02:20 AM
If my computer wasn't shit and I had space to download, I'd give it a go

Spectrus
01-25-2011, 04:52 AM
Hmm... I'll keep an eye on this.

Akalic
01-25-2011, 04:52 AM
Updated with some basic assignments.

Spectrus
01-25-2011, 11:49 PM
Below are my attempts at the assignments, if you intend on doing this yourself... DO NOT READ THE CODE FOUND BELOW! IT MAY SPOIL IT!

Questions:

Q. What is the easiest way to pause and wait for user input before closing?
A. Solved. Use "cin.get()".
Q. How do you display a string and a variable using 1 cout command?
A. Solved. Use this syntax: cout << "string" << variable.
Q. How do you create a variable that contains a string?
A. This one's rather embarassing. Dev-Cpp wasn't making the word 'string' bold when I used it as a variable type, as it was when I used int. I assumed it wouldn't work. It did. Thanks Jo3Bingham. Like this: string varName;
HELP ME WITH RECURSION! PLEASE! (:p)


1. Hello World



//Print "Hello World" five times, using a while loop.

#include <iostream>

using namespace std;

int main() {

int n = 0; //Variable to increment.

while (n < 5) {
cout << "Hello World.\n";
n++;
}
cin.get();
return 0;
}


2. Math


//Calculating 3 + 4 - 2 * 25 and printing it out.

#include <iostream>

using namespace std;

int main() {

int nMath = 3 + 4 - 2 * 25; //nMath is the answer to our problem.

cout << "3 + 4 - 2 * 25 = " << nMath;

cin.get();
return 0;
}


3. Name/Score Table


//Print a 2 column table using \t command. Exciting.

#include <iostream>

using namespace std;

int main() {

int n = 0; //An integer used to increment.
int nScore[] = {32, 12, -2, 300, 0}; //An array of the user's scores.
string szName[] = {"Bob", "John", "Jill", "Tommy", "Shasha", "Derick"}; //The user's names.

cout << "Name:\t| Score:\n"; //The title of the table.

while (n < 5) {
cout << szName[n] <<"\t| " << nScore[n] << "\n"; //Will display a new line for each user in the same format as the title.
n++;
}

cin.get();
return 0;
}

Working on more...

Visc
01-26-2011, 03:33 PM
Ok here are my attempts:


1. Write a program that prints out "Hello World" five times, each on a new line, using a for, while, or do-while loop.



#include <iostream>
using namespace std;

int main()
{
for(int counter (0); counter != 5; ++counter)
{
cout << "Hello world!\n";
}
cout << "Press Enter to exit";
cin.get();
}


2. Write a program that prints out "3 + 4 - 2 *25 = " (whitespaces included), and calculates the answer, and finally prints the answer on the same line.



#include <iostream>
using namespace std;

int main()
{

cout << "3 + 4 - 2 * 25 = " << 3 + 4 - 2 * 25 << "\n";
cout << "Press Enter to exit";
cin.get();
}


3. Why Derick scored nothing? There is 6 players and 5 scores :P

Write a program that prints out a two column table using the following data
Code:
Players: Bob, John, Jill, Tommy, Shasha, Derick
Score: 32, 12, -2, 300, 0

I'm on the first chapters so my skill is low :P But i'm practicing ^^ Gonna solve rest when i finish next chapters. If it's possible please add syntax highlighting to forum and few more sections (for programmers/designers/linux users etc.)

Akalic
01-26-2011, 08:55 PM
I am a moron

Edit: Will be adding more C++ projects and I am currently making a guide on how to program C the only way I consider pure, in a Unix environment. After I post the explanation for Windows users (which I assume most of you are) on how to install Cygwin and use it's terminal environment to develop code.

Visc
01-26-2011, 11:08 PM
Visc, your first assignment works and prints everything perfectly, but I need to make you aware of your use of ++. When you put ++ in front of the variable, it increments the variable BEFORE the first looping. In the same sense, putting ++ AFTER the variable (var++) the variable will increment after the first loop.


Ex. for(int x = 0; x < 5; x++)
cout << "Using ++ after";

for(int y = 0; y < 5; ++y)
cout << "Using ++ before";

The first for loop will loop 5 times while the second will only loop 4. If you do not understand the way that ++ (and the subraction version, --) works, you will hit very annoying "Off-By-One" errors in larger projects.

Edit: Will be adding more C++ projects and I am currently making a guide on how to program C the only way I consider pure, in a Unix environment. After I post the explanation for Windows users (which I assume most of you are) on how to install Cygwin and use it's terminal environment to develop code.

I have to disagree. In case of for loop it doesn't matter, since incrementation is made AFTER the body of loop. In your example results will be the same. So it makes no difference where we place incrementation, after or before the variable. There is a difference in another loop(not only in loop):
For example:



#include <iostream>
using namespace std;

int main()
{

int counter (0);
while(counter < 1)
{

cout << ++counter + 1 << "\n";

}
counter = 0;
while(counter < 1)
{

cout << counter++ + 1 << "\n";

}


cout << "Press Enter to exit";
cin.get();
}

Now results will be different :D

Akalic
01-26-2011, 11:39 PM
Bleh, this is what happens when I spent the entire day getting Cygwin to work. You're right, I'm wrong. Even the master sucks donkey dick sometimes...

Visc
01-26-2011, 11:57 PM
I'm still nooba :P I remembered this because today I was reading about for loop :P Btw. do you know any good linux IDE ? Now I'm using Kdevelop but it's not very handy (my computer is too slow for Eclipse).

Good thread, keep it up :)

Akalic
01-27-2011, 01:38 AM
Emacs. EMACS!! And use g++ to compile.

Visc
01-27-2011, 12:33 PM
Emacs/Vi are a bit to rough for me :P By now I tried netbeans (slowest one), eclipse (nice one, but a bit overloaded. Minus - it's not available in debian repo). Probably ill stick with geany - it's fast, simple and by now fits to all my needs.

DarkstaR
01-27-2011, 03:30 PM
Even the master sucks donkey dick sometimes...

I never suck donkey dick, thanks. You, however, often do. When I'm home from school I may make an attempt to show you all how clean code really looks :D

Akalic thinks hes better than me but he am sux :(

Akalic
01-28-2011, 12:32 AM
Dark I have more coding skill in my fucking stool that you have in your head. The only thing you have over me is an obsession with Tibia.

DarkstaR
01-28-2011, 05:14 AM
Hahahahaha your funny idiot ;D

Efren
01-28-2011, 05:54 AM
words means nothing, prove it :)

@topic, I pretty like this guides thanks for sharing, also żdo you have any advanced book to share?

Akalic
01-28-2011, 06:13 AM
Will be posting more on Monday. The topics covered will be basic hacking and Unix-base programming if I can find the book for it online (only have the actual one).

DarkstaR
01-28-2011, 06:49 AM
words means nothing, prove it :)

@topic, I pretty like this guides thanks for sharing, also żdo you have any advanced book to share?

He's the one who needs to prove it, he doesn't have 2 pretty successful bot projects going for him

:(
poor mexican.

Efren
02-05-2011, 07:07 PM
Here are some tips:
I really recommend to read and understand everyones source codes, You will learn from them and oviously increase your knowledge, If you want to be a programmer also remember to make everything by yourself but apply everything you have learned before.

Assigment1.cpp


#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[]) {
/*
* Write a program that prints out "Hello World" five times,
* each on a new line, using a for, while, or do-while loop.
* (im using "for" loop.)
*/

for(int n=0; n<5; n++){
cout << "hello world \n";
}
system("PAUSE");
}


Assigment2.cpp


#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[]) {
/*
* Write a program that prints out "3 + 4 - 2 *25 = " (whitespaces included),
* and calculates the answer, and finally prints the answer on the same line.
*/

int answer = 3+4-2*25;

cout << "3 + 4 - 2 *25 = " << answer << "\n";
system("PAUSE");
}


Assigment3.cpp


#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[]) {
/*
* Write a program that prints out a two column table using the following data
* ----------------------------------------------------
* Players: Bob, John, Jill, Tommy, Shasha, Derick
* Score: 32, 12, -2, 300, 0, 42
* ----------------------------------------------------
*/

string Players[] = {"Bob", "John", "Jill", "Tommy", "Shasha", "Derick"};
int Score[] = {32, 12, -2, 300, 0, 42};

cout << "Players" << "\t| Score \n";
cout << "----------------------\n";
for(int n=0; n<6; n++){
cout << Players[n] << "\t| " << Score[n] << "\n";
}
cout << "----------------------\n";
system("PAUSE");
}


Assigment4.cpp


Working on this (Seriously i don't know how to do this xD! )


Assigment5.cpp


After Assigment4 is completed

Spectrus
02-09-2011, 06:55 AM
Bumping this.

Akalic, would you mind if I stickied this? (If yes, I'll change the title to something without "shitty" in it.) ;)

Perhaps a hint for the 4th and 5th problems?
Perhaps new books?
Perhaps I love you for sharing your knowledge. ;);)

Akalic
03-07-2011, 05:08 AM
If it means I lose some of my freedom to use the words I choose to use, then no, I do not wish this to be stickied. In other news, I'm a massive bag of douche, in the process to upgrade my computing capability, I forgot to backup my bookmarks. So the friendly little icon for this forum that I used to click on somewhat regularly was gone. To make up for my apparent hatred for doing good, I will be posting a huge stash of shit for you peeps once I redownload my library that I also lost in the transition.

Luls
03-07-2011, 07:48 PM
I'm a massive bag of douche

And sig! :rolleyes:

Akalic
03-08-2011, 01:35 AM
Added two more books and completely changed the way I listed assignments. More to come as I finish restocking my library (My torrents need SEEEEEEEEEEEEEEEEEEEEDS).

Also, Luls. Suck my dick.

Luls
03-08-2011, 03:51 AM
I like your new avatar :)

What is that guy from? o.o

Akalic
03-08-2011, 06:16 AM
Ed Edd 'n Eddy.

Akalic
03-09-2011, 10:13 PM
Added a sorting algorithm assignment. And I do hope that it'll show how Bubble sort is a fucking abomination of logic.

Spectrus
03-11-2011, 12:04 AM
Hahaha, I shouldn't try to interfere with the way people decide to present themselves. Mind if this gets stickied? I won't change anything. Glad to see you're back, and it appears I'm going to have lots of reading over the next few days. Thanks mate.

Akalic
03-11-2011, 12:36 AM
Go for it.

Spectrus
03-11-2011, 12:42 AM
Done. I've read over your post and already I'm overwhelmed by the assignments, haha. Need to read those books. Don't know how to use an algorithm.

Akalic
03-11-2011, 12:57 AM
An algorithm is just a set of rules to solve a problem. Technically using a for loop to check an array for a value is an algorithm, the one's that I listed are just very well known and useful algorithms. So not only do you learn about algorithms, you learn more about arrays and how to use them for effectively.

Akalic
03-16-2011, 06:10 AM
Don't like the fact that I haven't posted this week's books? Tough, suck my giant black dick 'cause I don't give a shit. It's my Spring Break and I am set on never being sober for longer than it takes to write this post. Peace bitches.

Luls
03-16-2011, 12:30 PM
I am set on never being sober for longer than it takes to write this post.

That sounds like a goal that almost any given member of my family could set, and achieve.

Spectrus
05-20-2011, 11:03 AM
Been lazy lately so haven't really been nagging to keep this updated. Decided to take on another one of your challenges today and I feel I've accomplished it.


//Attempt at determining prime numbers up to user input.

#include <iostream>
#include <fstream>

using namespace std;

char primeCheck(int num);

int main()
{
int userInput;
ofstream txtFile;

cout<<"Hello, please enter a positive integer: ";
cin>>userInput;
txtFile.open("list.txt");
txtFile<<"Created by prime.exe"<<endl;
txtFile<<"Prime numbers up to "<<userInput<<endl;

for(int n=0;n<userInput;n++)
{
if (primeCheck(n) == 'y')
{
txtFile<<n<<endl;
}
}

char f;
cin>>f;
}

char primeCheck(int num)
{
char returnVal = 'y';

for(int a=2;a <= num/2;a++)
{
if (num % a == 0)
{
returnVal = 'n';
break;
}
}

return returnVal;
}

Spectrus
06-03-2011, 11:42 AM
Bump!

Let's learn algorithms! Except quick sort, as you'll see. Fuck you. Later. Maybe. God.


#include <iostream>
#include <cstdlib>

void BubbleSort(int a[]);
void QuickSort(int a[]);
void BucketSort(int a[]);

int main()
{
int arr[15];
int response;
srand( time(NULL) );
while(true)
{
std::cout<<"Here is your array:";
for(int n=0;n<15;n++)
{
arr[n] = rand() % 101;
std::cout<<" "<<arr[n];
}
std::cout<<"\nWhat would you like to do?"<<std::endl<<"(1)\tBubble Sort"<<std::endl<<"(2)\tQuick Sort"<<std::endl<<"(3)\tBucket Sort"<<std::endl<<">";
std::cin>>response;
switch(response)
{
case 1:
{
BubbleSort(arr);
continue;
};
case 2:
{
QuickSort(arr);
continue;
}
case 3:
{
BucketSort(arr);
continue;
}
default:
{
std::cout<<"Invalid input."<<std::endl;
continue;
}
}
}
}

void BubbleSort(int arr[])
{
std::cout<<"Bubble Sort: ";
bool flag = true;
int a = 0;
int tempVal;
while(flag)
{
flag = false;
a++;
for (int i = 0; i < 15 - a; i++) {
if (arr[i] > arr[i + 1]) {
tempVal = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tempVal;
flag = true;
}
}
}
for (int a = 0; a < 15; a++) { std::cout<<arr[a]<<" "; }
std::cout<<std::endl;
}

void QuickSort(int arr[])
{
//FUCK YOU.
}

void BucketSort(int arr[])
{
std::cout<<"Bucket Sort: ";
int tempVal;
int highVal = 0;
for(int a = 0; a < 15; a++) { if (arr[a] > highVal) { highVal = arr[a]; } }
int temparr[highVal + 1];
for(int a = 0; a < highVal; a++) { temparr[a] = -1; }
for(int a = 0; a < 15; a++)
{
tempVal = arr[a];
temparr[tempVal] = tempVal;
}
for(int a = 0; a < highVal + 1; a++) { if (temparr[a] != -1 ) { std::cout<<temparr[a]<<" "; } }
std::cout<<std::endl;
}

sion
08-31-2011, 09:51 PM
is this thread still alive? i would like to get a copy of the c++ for dummys.
ive got a project that i've been thinking of for a while, and need alot of basic knowledge in order to make it.

Spectrus
09-01-2011, 05:44 AM
Unfortunately, it's not still alive (despite my best efforts). It seems not many people are interested in programming.

Efren
09-10-2011, 12:59 AM
std::cout<<"Here is your array:";

Why don't you use the "using namespace std;" ?

DarkstaR
09-10-2011, 01:09 AM
In that case, it would have been helpful. Generally, though, people who write their own libraries stay away from the "using" keyword because it can lead to ambiguous symbols between libraries.

Spectrus
09-10-2011, 07:05 AM
In that case, it would have been helpful. Generally, though, people who write their own libraries stay away from the "using" keyword because it can lead to ambiguous symbols between libraries.

For this reason. Perhaps it would have been helpful there, but using it regularly can lead to bad habits which can lead to problems further on.

thorekz
12-29-2011, 06:19 PM
if your looking for C++ for dummies you can read it from here http://larripa.net/unixthings/C++%20For%20Dummies,%205th%20Edition.pdf
im currently trying to learn some
good luck

Akalic
05-03-2013, 05:18 PM
Oh hey, this is still here...