The most essential and basic thing before learning any programming language is the way the solutions are solved in programming. Unlike other courses you red or you may read in the university you will find cp more close to real world problem solving, method comes into your mind and you divide it as you are instructing the computer line by line.
a statement as below
c++ style..............................
a=a+b;
.................................
is an instruction to the computer, to first take the value from a and then take the value from b add them and save the result again in a.
When programming you have to feel like you are instructing the computer.
Now below is a very simple and basic guide on howto solve a problem. Any ways you have been doing the same thing in ITC during your first semster.
- The first part is to understand the problem. You really have to get the idea behind the problem. I suggest you read it five times with fully focused mind and you will get it.
- Now the second part is to divide your problem into small parts so that when you solve them and put them together you get the required result (i.e solution).
- Third part is implementing you have to compile all you have done so far into programming language (like c++ syntax). I suggest you consider this step with the second one.
I suggest you implement the problem in second part when you divide problem into problems. Take first problem solve it on a paper then code it on c++ editor. Take a look at the following example.
- Write a program that inputs a string of keyboard characters and outputs the characters in reverse order.
- PROBLEM UNDERSTANDING
First what we are going to do is understand the problem. First I need to have a string and then i have to invert it or reverse it like if user inputs "ALI" I have to print (infact i will not the program will :) ) "ILA". It is a very simple and easy problem. - DIVIDING
There are two possible divisions of the problem. 1st part is taking a string (that will be an input) and second part is reversing it. - IMPLEMENTING
Now impelmenting the first part . The following code does it all
c++ style........................................................................
#include c++ style........above example cont.....................
#include
using namespace std;
// this all is necessary so you cant divide it :) //
void main()
{
string str;
cin.getline(str, 50);
......................................................................................
It was really very simple as you can see. I included the files as I ever have to so dont ever think about dividing that part. What i did was declared a string not yet intialized it after that I used a method for string "cin.getline(str, 50); " cin.getline takes a string and saves it in the first argument you pass (in this case it was 'str') and the second argument should be number of characters in the string. The output of the code till here is a cursor will blink and will take input of not more than 50 characters.
Now we come to the second part of the problem that is reversing the string.
for(int i=str.length; i(grter-thn)=0; i--)
{
cout str[i];
}
}
.............................................................................
If you still don't get anything write in the comment or any better way of solving a problem please discuss in the comment section. :)
No comments:
Post a Comment