2. Writing My First
2. Writing My First
So we learn the steps for writing basic programs so we will be writing a lot of programs while
following these
steps.
Let us learn.
See suppose
I am a program.
I know how to add two numbers and you are a user of a program.
Then I will add them then I will give you the output.
That's it.
So from my side if you'll see I'm taking input then processing and giving the output.
Suppose I'm a program for finding the area of a rectangle and you're the user, you want me to find the
area
for that
First of all I should ask you what is the length and what is a breadth.
You will give me length and breadth.
Whatever the formula is, I will process and I will get the result.
Keep yourself at a place of program and other people who are going to use their users.
So now let us see how we can follow these steps for writing a simple program for adding two
numbers.
So first of all I will draw a flowchart for this one then I will take our algorithm using pseudo code.
Then I will write down a c++ program for adding two numbers.
Let us learn see first of on a starting point of a flow chart that is start.
So printing a message.
I will say print Enter two numbers like when i give the example.
I have told you that first of all I should take two numbers from you.
Then you will give me two numbers so I should read two number see.
I'm a program, right? so, read, i will read these numbers, two numbers whatever no. you have given.
I will give names to them.
Now I have two numbers I can add them so I can process so for process rectangle is used.
So a rectangle is used.
So I should add a and b and the result I should store it in some other variables.
so result is in 'c' so it mean for any program for taking input you have to write this and process
Now I have the result ready, I should give the result to you say that some of two numbers is so and so
that
is output so for output again the parallelogram, so draw a parallelogram and give a output here.
This is in quotes because it's a message then comma C so it will be saying "addition is" then the
result
whatever the result is that is there in 'c'. then, next this end of this procedure.
So this is stop.
So for that oval is the shape that we use or ellipses the shape we use so start, and take input, process,
give
This is how the flow chart for adding two numbers. now same thing
So what is pseudocode.
It's not a C language code. so the same pseudo code I will write down.
Now here I will say begin the algorithm begin see already I have explained you that you can write.
Okay then this is the message I'm giving then writing semicolon or not.
That is a and b.
This is over.
Don't take it as equal to otherwise write arrow like this next step is printing additional of number is so-
and-so
So those are steps I have completed them and written them as algorithm.
So this may be becoming little Lengthy or complex so slowly we'll move to it.
Now.
so, First of all I will write down the skeleton of C++ program.
Right.
So here I will write down C out enter two numbers, this will print message on the screen.
So from where? A program will read some numbers or data from the keyboard.
So for that we have object called C in and that is present in this std.
So C in right.
So C in where? see for this I have to use a double greater than symbol for reading anything.
Now when you have to read it in variables A as well as B don't give comma again give this a greater
Now the important thing seeing C++ you cannot directly use the variables unless you have declared
them
ABC three variable are there, a b c three variables and all these variables will have a data type.
So we have to mention the data type means what type of value you are storing in them.
We had a integer type.
So it's not strict it's not stick because that is not C++.
If you miss anything compiler will not compile your program, so you should write on the things that
are
This is over.
This is over.
C assign a plus b, so a plus b I want to do okay additions will be done then it should be stored.
So it should be stored inC so I write down like this C equal to A plus B but don't read it as equal to
read it as assign.
And then right again the double less than and then say C..
Right.
So that's all.
And so here inside the main function we must write down return zero.
That is always you have to write because we have written int here.
So that's all.
Now i will continue and I will show you how to read a name in C++ and give a message.
Now let us write a program or take the name of a user and display a welcome message with the name
right.
So let us assume that I am a program for what I should do I should ask you may I know your name.
Then you will tell the name then I should take the name then I should give a message.
Now let us write on the steps so I'm not showing flowchart all these things.
When you see this you trying to write your name from the keyboard.
Right.
So what is this.
This is a variable name is a variable which you type it should read a word that is name so it should
be string type.
Right.
So which type I should declare it as a string string and variable name is name.
So what is it is a string.
Welcome Mr. So-and-so here I should give a message so far that I should write C out.
Your name.
So for this again write down. see this is enclosed in double quotes.
now the variable name should not be in double quotes it should be directly given.
So this is a simple program for taking name of a user and showing off welcome message.
If your name is having more than one word then it can not read all the words.
Suppose your name is Ravi Kumar so it will just read Ravi right.
Then it will just read Anil and then how to make it read everything.
So instead of seeing we have a function called getline inside this I should write cin variable
name.
This name.
This will read all the words until you hit enter.
So this only perfect for taking the names of people that may be off more than one word.
So this is better.