Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,171,232 members, 7,880,879 topics. Date: Friday, 05 July 2024 at 08:31 AM

C++ Tutorial For Beginners... Part 1... - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / C++ Tutorial For Beginners... Part 1... (19184 Views)

C++ Tutorial Download Link / Java Tutorial For Beginners / Free C++ Tutorial Videos (2) (3) (4)

(1) (2) (3) (4) (5) (6) (Reply) (Go Down)

Re: C++ Tutorial For Beginners... Part 1... by Nobody: 6:39pm On Oct 13, 2014
haxorDelite:
following you bro keep it up, but that "cout" am lost there oo
Re: C++ Tutorial For Beginners... Part 1... by Nobody: 9:52pm On Oct 13, 2014
zyzxx:
bro u are a Gud teacher.
Av really learn a lot thanks
Following u bumper to bumper

THANKS BRO.....
I REALLY APPRECIATE...

1 Like

Re: C++ Tutorial For Beginners... Part 1... by Nobody: 10:20pm On Oct 13, 2014
Hey guys, did you find your first code interesting? well, it was fun to me cause i debugged my code and it worked properly, you can't challenge the experts for now, our journey just began.
i wanted to start with java, then moved to Android app programming where you can create cool apps compatible with Android devices, but i opted to start with Cplusplus..and i can see we are having fun, aren't we?

well, i cant leave you guys with just one example and with over 200 views and going to make the next program even more interesting...

am a lover of physics, so today example we will deal with a problem in physics...

PROBLEM 2:

The Force of attraction between two charges is directly proportional to the product of its charges and is inversely proportional to the square of their distance.... write a program that can find the magnitude of the force? using cpp...
( please i did physics a long time ago, i cant really remember the law but i hope i stated it correctly< if not, pls feel free to correct me) thank you...

SOLUTION...

First, am sorry if your not a physics student but i only created this problem to show you how we can also apply cpp to physics. i bet you will find this interesting... don't be overwhelmed by the grammar, just hold my hands and follow my lead...

Outline......

( before i proceed let me update the current scoreline.... men 5 - women 0... yeah,, ladies you haven't contributed anything yet,, later you yell what a man can do a woman can do it better) ... lol lets get back to business,,, upload shortly....

1 Like

Re: C++ Tutorial For Beginners... Part 1... by Nobody: 11:09pm On Oct 13, 2014
please guys, i need more comments, more sharing, more questions and suggestions... thank you
note: am using blue text format because am a chelseafc fan lmao.........


OUTLINE....

hmm, just as we did to the first problem, we repeat again... writing an outline helps a lot in building programs....

let me break that problem down, the formula is

F= K(Q1*Q2)/R^2 OR
F= K(Q1*Q2)/(R*R)

WHERE

F= FORCE (NEWTON) (N)
K=CONSTANT ( assume is on air) (9.0 x 109 N • m*m / C*C)
Q1= CHARGE OF THE FIRST FIRST PARTICLE unit Coulombs (C)
Q2= CHARGE OF THE SECOND PARTICLE unit coulombs (C)
R= RADIUS/ DISTANCE BETWEEN CHARGES...( m^2)

*1 Aim: Program able to find the magnitude of the force of attraction between two bodies..
*2 Header files: suitable header files includes

#include <math.h> ( because we might need some mathematical operations)
#include <cstdio>
#include < iostream> ( because we require our program to get input/information from the user..

*3 NATURE: we need to ensure that the units are correct and complete, we can solve this problem breaking down into numerator(constant multiplying the product of charges and denominator( radius raise to power 2)...

*4 INPUTS : this program will requires 3 inputs from the user, note ( k is a constant and wont change, it will just sit there laughing grin lmao), ( but the main input are (Q1,Q2 , AND R).


*5 OUTPUT(S): this program will require just one major output which is "F=force"

*6 MATHEMATICAL SIGNS REQUIRED: from the nature of the problem, we can find four (4) signs namely ( Equal sign , pair of bracket, multiplication and as well division..)

*7 VARIABLE TYPE : " double" WILL BE VERY SUITABLE, DON'T USE "int" ( to avoid truncation error)...

*8 UNITS: it is important to ensure the units are correct.... to make this program simple lets assume no conversion where to be needed)......



what you see is just a mere outline, gives a feasibility study of what our program entails....
i will post solution as soon i get five (5) comments from the new or existing followers.......










5 Likes 5 Shares

Re: C++ Tutorial For Beginners... Part 1... by haxorDelite(m): 9:32am On Oct 14, 2014
Ezechinwa:


my brother, thank you jare, , this is my first out of many write ups on nairaland... i really appreciate it alot...

cout is a command to display stuff on screen/ output device...

for example,

cout << " anything you write here will show on screen ";

my main format is this, you must write the double arrow like dis << for cout and like dis >> for cin...

irrespective of the content, you must end it with semicolon ;

also see,

int nairaland ; // variable
nairaland = 4 ;

cout << nairaland ;

notice: from the second example, the output will not be nairaland but 4, becus nairaland is just a variable holding a valve which is 4...

conclusion... when displaying comments add ","... but when displaying the valve of a variable put that variable inside...

remember cpp is case sensitive, i mean capital letter is not the same as small letter...

example

int williams ;

williams is not the same as Williams...

thank you for following.. i do appreciate....
God Bless you for me.. Keep up the tut we're following bumper to bumper
Re: C++ Tutorial For Beginners... Part 1... by swscorpio: 4:22pm On Oct 14, 2014
Ezechinwa:
please guys, i need more comments, more sharing, more questions and suggestions... thank you
note: am using blue text format because am a chelseafc fan lmao.........


OUTLINE....

hmm, just as we did to the first problem, we repeat again... writing an outline helps a lot in building programs....

let me break that problem down, the formula is

F= K(Q1*Q2)/R^2 OR
F= K(Q1*Q2)/(R*R)

WHERE

F= FORCE (NEWTON) (N)
K=CONSTANT ( assume is on air) (9.0 x 109 N • m*m / C*C)
Q1= CHARGE OF THE FIRST FIRST PARTICLE unit Coulombs (C)
Q2= CHARGE OF THE SECOND PARTICLE unit coulombs (C)
R= RADIUS/ DISTANCE BETWEEN CHARGES...( m^2)

*1 Aim: Program able to find the magnitude of the force of attraction between two bodies..
*2 Header files: suitable header files includes

#include <math.h> ( because we might need some mathematical operations)
#include <cstdio>
#include < iostream> ( because we require our program to get input/information from the user..

*3 NATURE: we need to ensure that the units are correct and complete, we can solve this problem breaking down into numerator(constant multiplying the product of charges and denominator( radius raise to power 2)...

*4 INPUTS : this program will requires 3 inputs from the user, note ( k is a constant and wont change, it will just sit there laughing grin lmao), ( but the main input are (Q1,Q2 , AND R).


*5 OUTPUT(S): this program will require just one major output which is "F=force"

*6 MATHEMATICAL SIGNS REQUIRED: from the nature of the problem, we can find four (4) signs namely ( Equal sign , pair of bracket, multiplication and as well division..)

*7 VARIABLE TYPE : " double" WILL BE VERY SUITABLE, DON'T USE "int" ( to avoid truncation error)...

*8 UNITS: it is important to ensure the units are correct.... to make this program simple lets assume no conversion where to be needed)......



what you see is just a mere outline, gives a feasibility study of what our program entails....
i will post solution as soon i get five (5) comments from the new or existing followers.......









Wow!!! This is awesome,i am really enjoying this ur tutorial.U r a genius,pls keep this tread alive,am following bumper to bumper.Tnx a million.
Re: C++ Tutorial For Beginners... Part 1... by Nobody: 5:43pm On Oct 14, 2014
You guys rock...
( let me share a little fact about myself, Am a final year student in a federal uni in Nigeria, Have been programming for roughly 2 years now, all have learnt about programming is just via reading few books & practising on my personal laptop... it wasn't easy, but it ws worth the effort....

pls don't get me wrong, am not saying going to tutor centers like Niit, afrihub etc is bad, no.. but am only saying, if you dont have the money to learn, there are free tools, books etc you can read and improve... @javanian your the best, i learnt a lot on nl )

if you are enjoying CPlusplus.. wait till i show u Java.... for me, Java is the best programming language ever.. )

once again, i really appreciate the likes and the shares,, it may be little for now, but it shows that am touching someone's life out there...

note... if your a good graphic designer, send me a pm,,, on facebook or tweet me @xbox360fan... lets work together... and develop andriod apps...


The solution to the next problem is coming up soon.. as soon as i get home... so stay tuned... God bless Nairaland...

3 Likes 1 Share

Re: C++ Tutorial For Beginners... Part 1... by haxorDelite(m): 7:13pm On Oct 14, 2014
I got a question, can one build a web app with c++
Re: C++ Tutorial For Beginners... Part 1... by Nobody: 9:01pm On Oct 14, 2014
[color=#006600]

hope you guys didn't miss me that much, lol, well, so far, we have dealt with only one example... but now, we have another tactical problem on our hands...

we have just an outline, so lets apply it...
tools to solve this problem..

1. get a calculator / use your mobile phone.
2. switch on your pc, and open ur compiler.
3. get a cup of tea, relax and enjoy..

note:: look carefully at the constant K, it has a valve of 9 * 10 ^ 9.. prononced ( nine times ten raise to the power of 9).., we also assumed that our user won't enter a wrong unit, hence no conversion needed....
therefore K=constant= 9,000,000,000 n*m*m/c*c....

pls if that valve isnt correct, you can correct me
... i crammed dat formula since my sec school days.. lol..

note: as young programmers, it is a very good habit to have a small book, where u can write very important codes for future use ...

also note: if you only read this tutorial without practising it yourself , then your bound to forget... as programmers, you must practise what you learn... it will sink into your memory.. thank you.


having these apparatus in place, let's proceed....

solution...

// let's introduce suitable header files below...Part 1

1 #include <iostream>
2 #include <cstdio>
3 #include <math.h>






// let's start program flow, declaring main...Part 2

4. using namespace std ;
5. int main ()
6. {





// we have declared the start of program flow, its
//time to declared all our variables.... Part 3


7. double F ;
// output force

8. double Q1 ;
// input first charge

9. double Q2;
// input second charge

10. double R ;
// Input distance





// lets declare the constanst K.. Part 4.

11. double K = 9000000000;
// this valve will remain permanent... remember its a constant..







// Part 5 .. lets ask the user some questions

12. cout << " What is the valve of the first charge ( in columbs) : " ;
13. cin >> Q1;

14. cout << " What is the valve of the second charge ( in columbs) : " ;
15. cin >> Q2;

16. cout << "What is the valve of the distance ( in meters) : " ;
17. cin >> R;





// Let's break it first into numrator..Part 6.

18. double numurator ;
// variable for the numerator

19. numurator = ( Q1 * Q2 * K) ;
// your done with this aspect.. move now to denominator.






// Let's break the other part... denominator .. part 7...

20. double denominator ;
// variable for lower part of formula..

21. denominator = ( R * R) ;
// your done with this aspect..







// part 8... lets compile part 6 and 7...

22. F = ( numurator) / ( denominator) ;
// vola.. that's your force..





// finally, part 9.. produce results..

23. cout << " From the given information provided by you, the force of attraction between two bodies is : ";

24. cout << " Force is equal to : " ;
25. cout << F ;
26. cout << " unit is in ( Newton) " ;



// sign out...

27. system ( "PAUSE"wink ;
28. return 0;

}

2 Likes 3 Shares

Re: C++ Tutorial For Beginners... Part 1... by Nobody: 9:06pm On Oct 14, 2014
haxorDelite:
I got a question, can one build a web app with c++

yes it can, but its not suitable for this kind of tasks.. most common tools are java.. php.. but c++ , not dat in use..
Re: C++ Tutorial For Beginners... Part 1... by Nobody: 9:14pm On Oct 14, 2014
Ezechinwa:
[color=#006600]

hope you guys didn't miss me that much, lol, well, so far, we have dealt with only one example... but now, we have another tactical problem on our hands...

we have just an outline, so lets apply it...
tools to solve this problem..

1. get a calculator / use your mobile phone.
2. switch on your pc, and open ur compiler.
3. get a cup of tea, relax and enjoy..

note:: look carefully at the constant K, it has a valve of 9 * 10 ^ 9.. prononced ( nine times ten raise to the power of 9).., we also assumed that our user won't enter a wrong unit, hence no conversion needed....
therefore K=constant= 9,000,000,000 n*m*m/c*c....

pls if that valve isnt correct, you can correct me
... i crammed dat formula since my sec school days.. lol..

note: as young programmers, it is a very good habit to have a small book, where u can write very important codes for future use ...

also note: if you only read this tutorial without practising it yourself , then your bound to forget... as programmers, you must practise what you learn... it will sink into your memory.. thank you.


having these apparatus in place, let's proceed....

solution...

// let's introduce suitable header files below...Part 1

1 #include <iostream>
2 #include <cstdio>
3 #include <math.h>






// let's start program flow, declaring main...Part 2

4. using namespace std ;
5. int main ()
6. {





// we have declared the start of program flow, its
//time to declared all our variables.... Part 3


7. double F ;
// output force

8. double Q1 ;
// input first charge

9. double Q2;
// input second charge

10. double R ;
// Input distance





// lets declare the constanst K.. Part 4.

11. double K = 9000000000;
// this valve will remain permanent... remember its a constant..







// Part 5 .. lets ask the user some questions

12. cout << " What is the valve of the first charge ( in columbs) : " ;
13. cin >> Q1;

14. cout << " What is the valve of the second charge ( in columbs) : " ;
15. cin >> Q2;

16. cout << "What is the valve of the distance ( in meters) : " ;
17. cin >> R;





// Let's break it first into numrator..Part 6.

18. double numurator ;
// variable for the numerator

19. numurator = ( Q1 * Q2 * K) ;
// your done with this aspect.. move now to denominator.






// Let's break the other part... denominator .. part 7...

20. double denominator ;
// variable for lower part of formula..

21. denominator = ( R * R) ;
// your done with this aspect..







// part 8... lets compile part 6 and 7...

22. F = ( numurator) / ( denominator) ;
// vola.. that's your force..





// finally, part 9.. produce results..

23. cout << " From the given information provided by you, the force of attraction between two bodies is : ";

24. cout << " Force is equal to : " ;
25. cout << F ;
26. cout << " unit is in ( Newton) " ;



// sign out...

27. system ( "PAUSE"wink ;
28. return 0;

}



I implore you to test this codes... i will explain where neccesary... thank you.
Re: C++ Tutorial For Beginners... Part 1... by swscorpio: 9:28pm On Oct 14, 2014
Ezechinwa:




I implore you to test this codes... i will explain where neccesary... thank you.

Noted.i will do that soon and give u a result.Tnx Man!
Re: C++ Tutorial For Beginners... Part 1... by swscorpio: 9:35pm On Oct 14, 2014
Please bro,which type of programming language use a word " echo " whenever we want it to display that output,i use to c it on Linux programs.Tnx
Re: C++ Tutorial For Beginners... Part 1... by Nobody: 9:39pm On Oct 14, 2014
swscorpio:
Please bro,which type of programming language use a word " echo " whenever we want it to display that output,i use to c it on Linux programs.Tnx


its used for php.... bro...

2 Likes 1 Share

Re: C++ Tutorial For Beginners... Part 1... by haxorDelite(m): 10:35pm On Oct 14, 2014
Ezechinwa:


yes it can, but its not suitable for this kind of tasks.. most common tools are java.. php.. but c++ , not dat in use..
owk thank you
Re: C++ Tutorial For Beginners... Part 1... by Nobody: 11:05pm On Oct 14, 2014
haxorDelite:
owk thank you

ur welcome my brother...
Re: C++ Tutorial For Beginners... Part 1... by Nobody: 11:06pm On Oct 14, 2014
GUYS PLS COME UP WITH A PROBLEM, LETS BREAK IT DOWN TOGETHER.....

3 Likes 1 Share

Re: C++ Tutorial For Beginners... Part 1... by zyzxx(m): 8:20am On Oct 15, 2014
U rock bro
Re: C++ Tutorial For Beginners... Part 1... by swscorpio: 8:36am On Oct 15, 2014
Ezechinwa:
GUYS PLS COME UP WITH A PROBLEM, LETS BREAK IT DOWN TOGETHER.....

Hi bro,Pls is that possible to write a program that will detect every person that login to my system(Redhat 6)?
Re: C++ Tutorial For Beginners... Part 1... by Nobody: 9:53am On Oct 15, 2014
swscorpio:


Hi bro,Pls is that possible to write a program that will detect every person that login to my system(Redhat 6)?

Its very possible, most websites use such facility, for example gmail, Facebook... Etc ( user authenticity) ,,, I will make more research and share whatever I find...

By d way, did you try out the second example?...

1 Like

Re: C++ Tutorial For Beginners... Part 1... by Nobody: 9:55am On Oct 15, 2014
zyzxx:
U rock bro


Thanks... Bro... Did u run d code
Re: C++ Tutorial For Beginners... Part 1... by zyzxx(m): 10:39am On Oct 15, 2014
Ezechinwa:


Thanks... Bro... Did u run d code
not yet sir. But i will very soon

1 Like

Re: C++ Tutorial For Beginners... Part 1... by swscorpio: 3:43pm On Oct 15, 2014
Ezechinwa:


Its very possible, most websites use such facility, for example gmail, Facebook... Etc ( user authenticity) ,,, I will make more research and share whatever I find...

By d way, did you try out the second example?...

Yeah i tried it bro, it works fine it will display me a black dialog box asking:
what is the value of the first charge < in columbs>
what is the value of the second charge < in columbs>
what is the value of the distance < in meters >

And i do inserted the values, but i couldn't see the final answer that is the value of force(F) in Newton.
Re: C++ Tutorial For Beginners... Part 1... by swscorpio: 4:00pm On Oct 15, 2014
Ezechinwa:
..... Part 3..... continues....

LET'S begin... relax... do not cram but practise,,, And Encourage me...

INTRODUCTION TO CPP...


As promised earlier, am going to skip unrelevant stuffs about CPP... but for reference sake you can read up the following online.. you can use "google.com"

1. history of cpp
2. important of learning cpp
3. why you require a compiler, functions of a compiler.. etc.

as i posted earlier the tools required, am now going to post the mindset/techniques needed to venture in programning with Cpp...

1. Using comments "//" to make your codes readable and traceable .. let me explain...

there are many ways to put comments in your programs, but as a beginner using just "//" will do just fine...

for example.. we intend writing a program that sums two numbers??

imaging the following "scenarios "
(
case 1

int a;
int b;
int c;

c=a+b;
...

)
(
case 2

// program that adds two numbers

int a ; // first number
int b; // second number
int c; // variable rep output

c=a+b; // output of sum off two numbers.
...
)

from the about cases, but programs will run correctly, but case 1 is done by a dirty programmer and it will be hard to understand his work...

but case 2, is a well mapped program and can easily be readable and tracable as well...

it is important to note that anything writing after the // " would be ignored by the compiler ( also called Whitespaces)..


Nb.. its 4:24am, so am off to bed, i promise to update from here tomorrow.... once again thanks...

to be continued...





On the above example; I run it and it work so well,this is the one i run;i just add some little thing,cos it give me errors before adding the first 2 line and the last one:

int main ()
{

// program that adds two numbers

int a; // first number
int b; // second number
int c; // variable rep output

c= a+b; // output of sum off two numbers
}
Re: C++ Tutorial For Beginners... Part 1... by Nobody: 11:01pm On Oct 15, 2014
wow.. over 400 views.. i think we are making a huge progress... well , you have seen just a little stuff of what cpp is made off... ( lol, am made of black sha)...

we have been able to solve few examples... precisely 2... as i promised earlier, i can't show you everything about this amazing program, am just working on the basics.. ..

i think we can move further from here... once again, thnks for the sharing, comments, replies.. etc...


i will take a break for today , the next session will be on ... " Conditional statements in Cpp"....

stay tuned...

3 Likes

Re: C++ Tutorial For Beginners... Part 1... by Nobody: 11:04pm On Oct 15, 2014
pls, drop more comments, and ask more questions...
let's build Nigeria together, or aren't we programmers??

1 Like

Re: C++ Tutorial For Beginners... Part 1... by haxorDelite(m): 7:01am On Oct 16, 2014
Bro can we combine javascript with it, for example => after inserting something in a field it opens automatically before one press opens
Re: C++ Tutorial For Beginners... Part 1... by Nobody: 11:49am On Oct 16, 2014
haxorDelite:
Bro can we combine javascript with it, for example => after inserting something in a field it opens automatically before one press opens

Yes sir... U can combine javascript with cpp... Take a look at the Link below for a better Explanation : http://www.codeproject.com/Articles/2352/JavaScript-call-from-C
Re: C++ Tutorial For Beginners... Part 1... by picasso4u(m): 12:22pm On Oct 17, 2014
Ezechinwa:



note: from now, i will update this trend using a red color format...i proceed..
So far, we have looked into few foundations of C++, like the proper implementation of semicolons, brackets etc, in case your just tuning in, you haven't missed that much, just go back and read from the beginning..

Part 3... continues..

4. Header files and directives...

hmm, you want to write a program ( maybe solve a problem, lets say mathematical in nature) , it is important to add "HEADER FILES" using a directive "#include < header file> ..
don't get overwhelmed, let me explain what i mean, there are lots of "Header files" in cplusplus ( and you are not required to know all of them ), these files have a function to play in the program your building..
furthermore, the files are added to the program using a directive #include. you still don't get it?, OK, consider the two concepts we are discussing here namely "directives" and " header files" relate them to this example, assuming your giving an assignment in mathematics ( " to find the area of a square"wink, of course you will go home and apply the formula "AREA= LENGTH * WIDTH" then return the result which is "AREA" to your teacher isn't it? now see ,

the "HEADER FILE" = FORMULA ( which is Area= length * width)
the Directive( #include) = YOU "applying the formula to the problem" ...

I will share with you few header files for beginners below,
"#include <stdio.h> " //will make the compiler to include the contents of standard header file
"#include <iostream>" // basically tells the compiler: i need input and output..
"#include <math.h> // to preform some mathematical operations, you need this library..

note:: please bear with me, i will get my laptop repaired soon, for now, am typing with a phone.. thank you..
Ezechinwa, u re really doing a good job here. keep it up and God will increase ur knowledge.
what is d function of d quotation mark("wink b4 d directive
Re: C++ Tutorial For Beginners... Part 1... by Nobody: 1:01pm On Oct 17, 2014
picasso4u:

Ezechinwa, u re really doing a good job here. keep it up and God will increase ur knowledge.
what is d function of d quotation mark("wink b4 d directive

thnks bro... that wasn't part of the program, it was just an illustration of what the header files can do..
Re: C++ Tutorial For Beginners... Part 1... by Nobody: 1:24pm On Oct 17, 2014
hello great minds... we are back again , what you saw in my previous posts were programs that can't make decisions...

so, later on today, i will show you how to develop programs that can make decision...

i haven't used variable types like boolean & string... so far, i will find problems needing such in future....

so later tonight... we move futher, for now i don't think my boss will find it funny seeing me posting rather dan working.. lol.. so, later guys...

1 Like 1 Share

Re: C++ Tutorial For Beginners... Part 1... by picasso4u(m): 3:41pm On Oct 17, 2014
Ezechinwa:
hello great minds... we are back again , what you saw in my previous posts were programs that can't make decisions...

so, later on today, i will show you how to develop programs that can make decision...

i haven't used variable types like boolean & string... so far, i will find problems needing such in future....

so later tonight... we move futher, for now i don't think my boss will find it funny seeing me posting rather dan working.. lol.. so, later guys...
u are really a life saver bro. I comment only when I see d need to. I have really seen d need in did thread. thumps up

3 Likes 2 Shares

(1) (2) (3) (4) (5) (6) (Reply)

Programming Challenge For Beginners N20000 / Nigerian Ethical Hackers In Here ---> / How Much Do Web Developers Earn In Nigeria?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 93
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.