Menu
Suggestionsfor Writing Computer Programs (By Nelson Padua-Perez)
Of course, successfully documenting, writing, and maintaining a system of computer programs does not guarantee that the system is beautiful, or that it has utility,. Computer programming is the process of. Such as the machine code of computer programs. High-level languages allow the programmer to write programs in.
Thefollowing document provides information on how to write computer programs.We present fundamental/intrinsic tasks you shouldfollow/complete while developing a computer program.The recommendations in this document apply toany programming language you may use.
As you begin to learn computer programming, you need to keep the following inmind:
Whetheryou use pseudocode, UML, and/or any other design tool, the important idea hereis that you need to first think about what you need to implement before youstart implementing it. Often a personcan spent a significant amount of time writing code that does not solve theassigned problem.It is extremelyimportant to first understand the problem you need to solve,otherwise there will be a significant waste of time and effort. Forexample, if you need to implement a particular algorithm (e.g., a graphalgorithm that computes shortest path), make sure you understand the algorithmand have a couple of examples that you have solved yourself before starting toimplement any code.These examples willhelp you in the process of developing your design and verifying that your codeis working as expected.
This is the most important rule ofcomputer programming!The number onemistake you can make is to try to do too much at once. Incremental code development means adding alittle bit of code, checking that the program is generating the expectedresults, and then moving forward.It isthat simple.If you follow this approachyou will have an easier time at locating where problems are in your code.
Forexample, if your program is working, and you add two lines of code and theprogram stops working, you can bet that the responsible parties are the twolines you just added. Incremental codedevelopment is a simple idea that unfortunately escapes people (I guess thereis a human gene that gets in the way :)).
Keepin mind that as your programming skills develop what we consider a little bitwill vary.For example, beginner programmerscan define a little bit as just adding three or four lines of code, butseasoned programmers could write a larger number.When in doubt, less is better.
Two types of assumptions are common:
Testing is a process that can be tedious, but it is extremely important.You should test your system periodically andas often as possible, rather than waiting to the end once coding is completed(or almost complete).Writing tests mayprevent you from moving faster, but in the long run it will save you timebecause:
Simplify the data sets you use to test your code whenever possible.For example, if you need to implement asorted list, verify the correctness of the list with the most simple data type(e.g., numbers) you can think of.If theproblem originally deals with a list of sorted activities, during testing use alist of integers.It is easier to seewhat could be wrong by using a simpler data type.
You need to organize your tests in a way you can convince yourself (andothers) you are covering all the possible cases.Before you start writing tests analyze thepossible set of cases you can have.
Unlike writing a paper or other tasks you may be familiar with, computerprogramming can be very unpredictable in terms of time requirements.The time it can take to finish a computerprogram can be estimated, but you have to plan and allocate time to deal withunexpected circumstances. If you waituntil the last minute to start on a programming task, you will not have theopportunity to ask for help or to clarify any doubts.
Keepin mind that computer programming requires significant mental effort; time isneeded to rest and being able to focus.Take breaks; don't keep working on code hour after hour.A 10 minute break can help a lot.
As you write code it will help you significantly if you stick with a set ofconventions and style (style that you may have defined yourself).For example, rules for namingvariables/methods/functions if consistently used can help in the process ofwriting code.Remembering which variableyou need to use is easier if you follow a naming convention.
There are two approaches you can use while writing code:
PreventiveProgramming: You are very careful as you write code, considering all possiblescenarios, and making sure you test thoroughly as you move along.
CorrectiveProgramming: You are casual about the code you write assuming any problems canbe detected later on during the testing/debugging phase.
Preventiveprogramming is the preferred choice.Itcould feel you are not moving as fast as you wish, but in the long run you willnot waste time and effort.Smallproblems easy to identify in isolation may become extremely hard problems toidentify later on once the code is part of a larger system.
Often you may add a semicolon, use amethod/function, etc., and get the expected results without actuallyunderstanding why.You need to stop andask why.Do not move forward withoutunderstanding why your system is working.This also applies to scenarios where you are receiving assistance from acolleague, a teaching assistant, etc. Ask them to explain to you any changesthey have done that makes your system works now.
Nowadays languages offer rich libraries thatprovide a lot of support.If you areusing a library to solve a problem and you are not sure how to use the library,experiment with it in isolation before you use it to solve your problem.
Many beginner programmers think that writing code is about writing the codeand then running to someone to find the problems with the code.This is not correct.
Keepin mind that if you are careful in the code development process the number oftimes you will need the debugger is minimal.Also, keep in mind that sometimes a simple output statement could helpyou in the process of finding problems in your code.
If you are planningto be a professional programmer you need to know how to use the fundamentalstools of a debugger.
Acomputer program is a complex piece of engineering.Organization helps keep this complexity undercontrol.Good variable names, goodindentation, appropriate comments all are part of keeping your code wellorganized.
People forget about the code they write.If you look at the code you wrote a couple ofmonths ago you will probably not recognize a large percentage of the code. As you write code keep in mindyou will forget a lot of details and decisions you make.
Youneed to have some sort of backup system; something as simple as copies of yourfiles or more sophisticated system (e.g., version control provided by CVS).Create backups often and if possible indifferent computers.Backups allow youto have a copy of your work if you computer fails and they allow you to go backto a previous version of your system.
Starting a programming assignment can be a daunting task.Make sure you understand the problem you needto solve, and have a design/plan.Don'tbe afraid to start even if you don't have every single detailed thought out;some things you will figure out as you move along in the developmentprocess.
In this tutorial, you will learn how to write a program in the C++ programming language. This will go over 10 steps that will explain a simple C++ program. The topics that each step cover in this tutorial are often used in more complex C++ programs. This is only a simple tutorial designed for new C++ programmers and consequently only covers some of the basic topics in C++. C++ programming is one of the most popular languages and consequently, one of the most useful to know.
The following code is the premise for which the tutorial is written on:
// This is a line comment
/*This is
a multiline
comment */
//This has libraries to use input and output
#include
//This has libraries for string operations
#include
//This has standard c libraries
#include
//This includes time libraries for c
#include
int main() {
std::cout << 'Do you want to guess a number from 0-9?(yes or no)' << std::endl; // prints to the console
std::string response; // declares a string variable named response
getline(std::cin, response); //gets a line from std::cin and stores it in response
std::srand(std::time(0)); // needed once per program run
int random_number = std::rand() % 10; // gets a random number from 0-9
int guess = -1;
int number_of_guesses = 0;
std::cout << random_number << std::endl;
if (response.compare('yes') 0) {
while (guess != random_number) {
std::cin >> guess;
number_of_guesses = number_of_guesses + 1;
}
}
else {
std::cout << 'You're no fun!' << std::endl;
}
if (guess != -1) {
std::cout << 'Number: ' << random_number << std::endl;
std::cout << 'Number of Guesses: ' << number_of_guesses << std::endl;
std::cout << 'Congratulations you guessed the number!' << std::endl;
}
system('pause');
return 0;
}
The following code is the premise for which the tutorial is written on:
// This is a line comment
/*This is
a multiline
comment */
//This has libraries to use input and output
#include
//This has libraries for string operations
#include
//This has standard c libraries
#include
//This includes time libraries for c
#include
int main() {
std::cout << 'Do you want to guess a number from 0-9?(yes or no)' << std::endl; // prints to the console
std::string response; // declares a string variable named response
getline(std::cin, response); //gets a line from std::cin and stores it in response
std::srand(std::time(0)); // needed once per program run
int random_number = std::rand() % 10; // gets a random number from 0-9
int guess = -1;
int number_of_guesses = 0;
std::cout << random_number << std::endl;
if (response.compare('yes') 0) {
while (guess != random_number) {
std::cin >> guess;
number_of_guesses = number_of_guesses + 1;
}
}
else {
std::cout << 'You're no fun!' << std::endl;
}
if (guess != -1) {
std::cout << 'Number: ' << random_number << std::endl;
std::cout << 'Number of Guesses: ' << number_of_guesses << std::endl;
std::cout << 'Congratulations you guessed the number!' << std::endl;
}
system('pause');
return 0;
}