Programming the right way?
- C and C++ are the most basic programming languages, and are often referred as god in the programming world.
- Knowing to program the right way, and efficiently is very important.
Tips for efficient C/C++ Programs:
1)Always write your programs using proper spacing, and write in a structured manner.
2)Make a rough note of your program.
- Write what you want in the output in a paper or notepad, and concentrate on it.
- Think logically, and make variables for each thing you require. For example if you are going to make a program for printing a line for 5 times, then first thing which should come to your mind is the loop. You will need a loop to display a line for 5 times. Start writing your code with the variables.
- Think what happens each time inside the loops you are going to create. Iterate the loops and see where you need to correct it. Scribble everything which comes to your mind on a paper, and then code it.
- This is the part which is the most important part in creating programs. You may know all the syntax, but you just don't know what to use and how to create the program?
- Think about the problem in parts. Divide the problem in a number of parts or "modules".
- See what you require in the problem. Do you need a lot of elements or things to store data? Do you need a lot of elements with the same data type? Do you need to do something MORE THAN ONE TIME? What are the things you need from the user? What will you print on the screen when the program runs? What will be your interface? What options will you give to the user for the problem? Think about all this very carefully.
- Think iteratively. You need to think what will happen after every line is executed.
- Check for all the available functions and operators in the C/C++ language. You can do the same thing with multiple structures. In some cases, using arrays will be more simple then structures and classes.
4)Use proper syntax
- Although I don't need to tell this, but this is where, most people have problems with this.
- Always make a habit to use clrscr() and getch() in your programs. Many times your program runs correctly, but you simply cant see whats happening because you have not written getch().
- Use smaller names for variables, it helps you to remember the variable names, and write faster. Instead of name, use n, instead of total_marks, use tot_m
- Make functions for lines of code that are going to be needed frequently. For example you can make a function to input the data of a matrix.
5)Use Error Tracing
- If you are using the Turbo C/C++ compiler, you have an option of tracing your program. Tracing means to see how the program is running line by line.
- If you have compiled your code without any errors, then you can use tracing.
- Use F7 key to start tracing your code. Use F7 for going to the next line. It will execute your code line by line, and you can see where you are getting problem. This helps most in finding out the logical errors, and solving infinite loops.
These where some tips to code efficiently. If you have any doubts, just leave a comment below.