Basic C++ Language - Getting started with C++

Pages: 1, 2
free web hosting
Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > C and C++

Basic C++ Language - Getting started with C++

iGuest
help in c++
Basic C++ Language

Hello mr

Iam ali atwi student in master 2 physics in university du maine france , I want help in c++

I want a function to generate 0 1st time ( that I used in other function ) then when we generate this function again it give me 1 and so on (0 and 1 ... 0 and 1)

it look like ran2() function that genrate a random variable betwwen 0 and 1 when I generate a function it give me a value betwwen 0 and 1 and after I genrate the function next time it give me other random varibale betwwen 0 and 1 which is different from the 1st one.

So I want a function look like that that genrate 0 then 1 then 0 and so on

Thank you for your time and your help

Ali atwi

-reply by ALI ATWi

Reply

Quatrux
This is really an easy task to accomplish, if you know mathematics, when to get this result you only need to get 0 or 1, so for example we get 0 when the number is even or the other way around and we get 1 when it's odd or the other way around, this could be done in a loop or any other way by using modulus %, just read about it what it does, on some other languages it's just mod, for example:

CODE
// Even_odd.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    for (int i=0; i<20; i++)
        cout << i%2 << ",";

    cout << "\n\n";

    return 0;
}


The result will be something like this: 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,

Hope this helped biggrin.gif

 

 

 


Reply

Umar Shah
QUOTE(the empty calorie @ May 15 2005, 06:23 AM) *
I keep seeing things for C++..But I would like to learn just plain C.



Learning C should be precondition to all programming.
Anyway What you wnat to learn?

to Start with we can write a simple hello world program.

CODE
#include<stdio.h>

int main(int argc, char * argv[])
{
printf("Hello World!\n");
return 0;
}


the above example just prints "Hello World!" on the screen.
"\n" represents a newline character that makes the cursor go to the begining of the next line.

the return 0 signals the OS that the program ended normally.


CODE
#include<stdio.h>

int main(int argc, char * argv[])
{
int i;
i=10;
printf("The value of i is %d\n",i);
return 0;
}


the next example just prints "10" on the screen.
"\n" represents a newline character that makes the cursor go to the begining of the next line.

again the return 0 signals the OS that the program ended normally.


Reply

Umar Shah
QUOTE(Umar Shah @ Mar 27 2008, 12:16 AM) *
Now that we uinderstood how to print a number and a string we should next try to understand how to control the flow of a program with the if construct.

CODE
#include<stdio.h>

int main(int argc, char * argv[])
{
int i=10;

if(i<10)
{

printf("the value is less than 10!\n");
}
else if(i==10)
{
printf("the value is exactly 10!\n");
}
else
{
printf("the value is greater than 10!\n");
}
return 0;
}


the above example will prints "the value is exactly 10!" on the screen.
since the condition i=10 is true, the other printfs wont be called.


Suppose we know a value will take some fixed set of values then we can use switch case as follows:

CODE
#include<stdio.h>

int main(int argc, char * argv[])
{
int i;
i=10;
switch (i){
case 1:
printf("The value is one\n");
break;
case 2:
printf("The value is two\n");
case 3:
break;
printf("The value is three\n");
break;
default:
printf("The value is not one two or three\n");
break;

}
return 0;
}


Now in the above example the control of the flow is determined by the value of i that switch interprets.
If the value is 1, 2, or 3 the appropriate printf is called.
if none matches then the flow will pass to default and that printf is called.

if we want to group some values then we can do that by omitting the break statements as:

CODE
....
case 5:
case 6:
case 7:
printf("The value is five six or seven\n");
break;

....




Reply

Umar Shah
Now that we know about if we can start to see how a loop works.

to begin , we shall start with a while loop.
CODE

int main(int argc, char argv[])
{

int i=0;
while(i<10)
{
printf ("Iteration no: %s\n",i);
i = i +1;
}
return 0;
}


the above program starts off with a variable i =0.

it loops through while construct and prints
Iteration no: value_of_i
for each iteration.

i = i + 1;
increments value of i by 1 ;

this can also achieved by
CODE

i++;


or also by

CODE

i+=1;


while(i<10) is true untill i gets value of 10 and then the program control passes to the line immedately following the closing brace of while.

this can also be achieved by a for loop as

CODE

int main(int argc, char argv[])
{

int i=0;
for(i=0;i<10;i++)
{
printf ("Iteration no: %s\n",i);
}

return 0;
}

Reply

Umar Shah
another form of the loop is do while
this can be shown in the following example

CODE

int main(int argc, char argv[])
{

int i=0;
do
{
printf ("Iteration no: %s\n",i);
i = i +1;
}
while(i<10);
return 0;
}



Reply

Umar Shah
Next thing we would want to do is talk about arrays

An array is an ordered list of values.

CODE

#include<stdio.h>
int main(int argc, char *argv[])
{
int arr[10];
int j;

for(int j=0;j<10;j++)
{
arr[j] = j * 3;
}

for(int j=0;j<10;j++)
{
printf("the %d th value of arr is \n",j,arr[j]);
}

return 0;
}



the above example demonstrates initializing an integer array of size 10 with multiples of 3 in the first loop.

the next loop prints these vales in that order in which they were stored.

Next example demonstrates a two dimensional array.


CODE

#include<stdio.h>
int main(int argc, char *argv[])
{
int arr[10][10];
int i,j;
for(i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
arr[i][j] = i * j ;
}
}

for(i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
printf("the arr[%d][%d] has value :%d \n",i,j,arr[i][j]);
}
}

return 0;
}


Reply

Umar Shah
Next we can try to understand a basic philosophy that everything is done in functions.

lets see how we can use some of the previous examples to understand basics of functions.


CODE

#include<stdio.h>
void printArr(int arr[]);
int main(int argc, char *argv[])
{
int array[10];
int j;

for(int j=0;j<10;j++)
{
array[j] = j * 3;
}

printArr(array);
return 0;
}

void printArr(int arr[])
{
int j;
for(j=0;j<10;j++)
{
printf("the %d th value of arr is \n",j,arr[j]);
}
}


the reult of this program is sam, it assigns and then prints the values of array;

but the function of printing values is carried out by a separate function called printArr which does not return any value but carries out the task of printing the values.

Reply

Umar Shah
CODE

#include<stdio.h>
void printArr(int arr[]);
int getMultiple(int a, int cool.gif;
int main(int argc, char *argv[])
{
int array[10];
int j;

for(int j=0;j<10;j++)
{
array[j] = getMultiple(j, 3 ) ;
}

printArr(array);
return 0;
}

void printArr(int arr[])
{
int j;
for(j=0;j<10;j++)
{
printf("the %d th value of arr is \n",j,arr[j]);
}

int getMultiple(int a, int cool.gif
{
return a * b;
}
}


in this example the vales are assigned by calling the function getMultiple.
this function returns the product of the arguments passed to it.

Reply

Miles
Very good tutorial, I already have experiance in both C and C++, and these tutorials certainly should do better than what I done, that being trial and error and looking at already programmed programs for assitance, I'm glad Umar Shah has decided to expand on this.

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Pages: 1, 2
Recent Queries:-
  1. c language basic - 5.89 hr back. (1)
  2. basic operation of c language - 6.07 hr back. (2)
  3. basic idea of c programming language - 15.34 hr back. (1)
  4. c language for bucle - 24.21 hr back. (1)
  5. basic idea about c language - 38.36 hr back. (1)
  6. c while statement grammar - 42.72 hr back. (1)
  7. basic datypes in c language - 50.26 hr back. (1)
  8. c language main shouldnt end with semicolon - 50.69 hr back. (1)
  9. basic c language - 71.71 hr back. (1)
  10. basic c language program - 74.63 hr back. (1)
  11. basic example of c language - 81.20 hr back. (1)
  12. looping in c language - 128.08 hr back. (1)
  13. basic questions c language - 172.12 hr back. (1)
  14. basic is c language - 192.39 hr back. (1)
Similar Topics

Keywords : basic, c, language, started, c

  1. Basic C++ Coding
    (9)
  2. Basic C Language, Functions
    (5)
    In a complicated and/or big C programs, a programmer might need to use the same peice of code more
    than one time. However repeating the code can be both time consuming and frustrating. Thats why we
    use functions. Definition: A function is a small subprogram having its own name and datatype. It
    is used to carry out certain operations more than one time without the need of repetition. One of
    the functions we all know is the main functions; void main(). I will explain the void later.
    Syntax: Each function is composed of 3 parts: Name of the function Info type of the....
  3. Which Language Is Easy And Secure Today For Web Development
    is PHP MYSQL is easy and Secure ? (2)
    Web development Application now a day using PHP MySQL Mostly as i observed on discussion topics on
    Forums friends switch to PHP MYSqL, Did PHP MYSQL is Easy and Secure for todays environment what
    you are say about this Hacking tracking stolen password are very much popular now a days his website
    hacked by X person so many time comes to news please emphasis on security issue when you go to
    develop any web tools . what is the right way to tight security on PHP what thing i kept in mind
    which logic i use that any one can not hack webtools. when i was use java/j2ee languag....
  4. Site Language
    Other languages permitted (9)
    Hi. I'm thinking of transferring my website to astahost, but I stumbled upon a problem. In your
    TOS, it is stated that hosted websites must be in English. My website, which is Joomla based, is a
    community site with two languages, Portuguese and English, with Portuguese being the default
    language. So, I'd like to know if these rules are final or if a Portuguese website could be
    accepted. I'm not expecting you to just open an exception because I asked you to, but it would
    be cool if you could expose some conditions by which I could abide to get my wish. I'....
  5. Linux Partitioning Guide (new Users)
    A very basic guide to partitioning in linux designed for new users (1)
    QUOTE Partitioning Guide This guide is designed for users New to Linux. 1. All Linux users
    run as just that 'a user'. If you are moving from Windows it is most likely that you ran
    Windows as an 'Administrator' (root). In Linux, any task which alters the Operating System
    (OS) of a Linux distro requires 'root' privileges. You will need the 'root' password
    to do this. Basically, this gives you temporary access to the core of your OS. This is what makes
    Linux far more secure than Windows. 1. That being said you will now understan....
  6. Basic Html Tutorial
    Made it myself, hope you like it. (1)
    HTML stands for hyper text markup language. It is a basic coding language used on almost every
    website. There are some tags in HTML which must be used, whilst others are enhancing, but not
    essential. Below I shall list the essential tags and there uses: This tells the browser that
    the language between the tags is going to be HTML so it knows how to read it. Inbetween those tags
    come These tags tell the browser that all the content that you wish to be displayed is in there.
    They are the only essential tags for HTML! The list is short but if you only use them your ....
  7. An Absolute Basic Guide To Algorithms For Dummies
    (0)
    I really want to learn about algorithms but i dont know how it works at all. Can anyone suggest a
    site, or more preferably a book that can introduce me to algorithms?....
  8. Basic Css
    (6)
    can someone help me learn CSS or give me a good link to learn it....
  9. Linux Basic Command - For Storing Compilation Error To File
    (1)
    Ex: Compiling a cpp file using a basic command " g++ filename.cpp " and to run the program use
    ./a.out , Then to store the compilation error to text file use this command. g++ test.cpp >
    log.txt log.txt contains the compile time errors. ....
  10. Who Is No. 1 Actor In India (all Language Including Bollywood)?
    (1)
    Who is No. 1 Actor in India (All language including Bollywood)? My choice Amithab Bachan....All
    time hero.......
  11. Some Usefull Linux Basic Commands And Utilities. Please Add To This List If You Know One.
    (0)
    Let me give some usefull linux commands and utilities. Please add to this list if you know.
    Work with tar files. To make tar archive use $ tar -cvf filename.tar filename
    To extract tar archive use $ tar -xvf filename.tar To extract tar archive
    with gz use $ tar -xzvf filename.tar.gz Connect to remote system through ssh
    $ ssh name@ip followed by passwd e.g. ssh project@172.16.0.14 passwd: List the
    file in current directory $ ls -l list the running process ....
  12. How Do I Get Started?
    (3)
    I am interested in learning Java for programming since I would like to program GUIs for my
    applications. I have learned very basic C and Qbasic programming if that helps. I am looking for
    beginner tutorials that I can do in Notepad, not any integrated development environments like
    NetBeans or anything like that. I understand that it is necessary to install the Java Developer Kit
    and I have done that. What is my next step now? Is this programming language easy to learn?....
  13. Increase Your Knowledge Of Html Language
    (11)
    For Creating good website you have to be master in HTML Language. It is a scryptic Language. If you
    want to Learn this language than visit the link http://zwqa.page.tl/Increase-you-HTML-Knowledge.htm
    ....
  14. Html Basic Tutorial
    <!-- For beginners only --> (9)
    Knowledge HTML stands for H yper T ext M arkup L anguage. You cannot create an HTML file
    using a rich-text editor, such as Microsoft Word or Wordpad. HTML To write a basic HTML, you will
    need to start with this: CODE The html > tag tells the browser that this is an HTML page.
    To close any tag, the same tag will be repeted but with the "/" sign. For example, CODE   
         Page title    Did you notice the /title >, /head > & /html > tags? That's how we
    close the tag. The HEAD > Tag A head > tag will include the meta >, titl....
  15. Phpbb - Installation Tutorial ( For Newbies Based On Astahost Cpane)l
    phpBB 2.0.22 installation Tutorial with basic steps. (5)
    Introduction!! Providing a comprehensive 'How To' tutorial guide, on the
    installation of phpbb 2.0.22 forum on your Astahost/cPanel account. Tutorial includes Downloading
    of files Uploading using cPanel File Manager CHMOD using cPanel File Manager MySQL database
    creation PhpBB initial configuration This guid will help everyone from beginners, with images and
    an in depth guide of what to do. Step 1 To start with we need to download PhpBB ( latest stable
    version is PhpBB 2.0.22 ). This can be done by visiting the PhpBB Downloads page which shou....
  16. Asterisknow Pbx (voip Telephony)
    A basic run down & some questions (1)
    I was wondering if anyone here ever used (or uses) Asterisk? If you don't know what Asterisk is,
    it is a VoIP server (Voice over IP). Basically, it's like Vonage's servers in your home
    (beats the $24.99 month phone bill from Vonage, plus your local phone bill, plus internet bill).
    Anyways, nothing beats a POTS right? Well, features such as Voicemail, call forwarding, voice
    options (menus), etc can come with a price tag from your phone company. You already have copper
    phone lines coming into your house so why should you pay extra for these services when you ca....
  17. C++: Basic Classes
    classes, objects, access labels, members, inline functions (5)
    This tutorial assumes that you have a basic knowledge of C++. You know how to use built-in types,
    like ints, doubles, chars, etc. You should know some types that are part of the STL, like vector,
    etc. Those types that come in the STL are just C++; you can create your own types just like those!
    Non built-in types are referred to as classes . To create a class, you just use the keyword class
    , the name of the class, and curly brackets. CODE //A class named MyClass class MyClass { };
    In fact, that is all we need to create variables, pointers, or references to t....
  18. Visual Basic.NET Help Needed.
    (8)
    I have a project to submit this wednesday, and i have to make it a working program. Not a big one ..
    but just sumthing small that includes : Splitter, Progress Bar & Track Bar. I thought of making
    sumthing like the picture below, I would enter the text and hit the button .. the text would come in
    BOTH the labels. Then i move the track bar on the left to increase the font or/and the track bar on
    the right to change the color ( RBG only 3 colors ). The progress bar at the bottom will show the
    progress every time i move either of the track bars to a level. Nothing fanc....
  19. What Language Is Linux Written In ?
    (15)
    I''ve been wondering lately, what language is Linux written in? I'm interested in
    this, because my friend and I are considering writing our own distro. I know, it sounds like a bit
    of a feat, but one guy wrote MEPIS by playing with the Debian source code. I think the two of us
    could do something similar. /tongue.gif' border='0' style='vertical-align:middle' alt='tongue.gif'
    /> ....
  20. Where To Start Learning Programming
    Please advise me on which language to (17)
    I am a beginer to programming i wish to know from which language should i start programming C, C++
    Is there any problem if i jump directly to vb or like please respond where can i find good beginer
    tutorials Thank you....
  21. Visual Basic 6 + Crystal Reports 9
    how to distribute (6)
    I have made a software with Visual Basic 6 and Crystal Reprots 9. Now I have to distribute it on the
    client side but the problem is coming in distributing the Crystal Reports. I have tried Setup
    Factory but it is not working. Then I have used Visual Studio Installer from Microsoft and
    downloaded Crystal Report Merge Modules from Business Objects website. It is working properly but
    now the setup file size is very big because it carries a lot of files with it. Is there any other
    installer software which can automatically select the required files to be distribute with the ....
  22. Making A Nice Looking Signature In Photoshop Cs
    Some basic Photoshop skills required... (17)
    Here's a guide you can use to help you make a signature image in Photoshop CS. Before we begin
    you need to pick out a good "render" for your signature. A render is basically an image that takes
    the focus in your signature, the render I will use currently looks like this First off I hit
    Ctrl+A and then Ctrl+C, then I go to File> New, and I set my image proportions to 410 wide, and 165
    high. Then I hit Ctrl+V and right click the eraser tool I select the Magic eraser tool, I hold down
    Ctrl and then left click my render. I move my render all the way to the right, th....
  23. Does Anyone Code Using Turing
    A student programming language (3)
    Does anyone use Turing here? It's a Delphi/Pascal based programming language developed by
    University of Toronto and is now owned by Holt Software in Toronto. Sample: var name:string var
    input:string put "Please enter your name" get name:* cls put "Please enter your message: " ..,
    input cls put "Your name is: ", name put "Your message : ", input It should accept your name and
    show your name and message. xboxrulz....
  24. VB.NET: Switch Regional Language Automatically
    When the field being edited gets FOCUS (1)
    Switch the language of the Textbox Control automatically upon receiving focus Hi guys, This
    tutorial sparked off from my own ventures to incorporate a particular feature in my own software -
    which uses mixed language (English & Thai) on a couple of screens to store user data. Certain
    fields, apart from First & Last Names & parts of address were to be entered in Thai. Now the current
    language can be easily switched by pressing Alt-Left Shift (or whatever key combination you've
    set your system to) - but when you consider the same key-combination has to be repe....
  25. What Language Is Best For Game Programming?
    I need some help (27)
    ok I am trying to get into programming and then into games. So i was wondering if anyone knows a
    good language except c++. I have started to learn it but the problem is its not user friendly for a
    first time programmer. So I would appreciate it if you can maybe name some languages and there weak
    points and strong points i would aprreciate it.....
  26. Online Multiplayer Game
    What language to learn in order make it? (16)
    If you look at e.g. THIS LINK What language would you use to make something like that?
    /wink.gif' border='0' style='vertical-align:middle' alt='wink.gif' /> ....
  27. Visual Basic Help
    need help useing visual basic (7)
    i was just wondering, when you are useing the program Visual Basic is there any way to add some
    animation to a form, and if so what is a good(cheep) animation program i could get?....
  28. Basic Tutorial: PHP GD
    basic tuts (16)
    With php gd is the image function library of php. The functions include in this library enable php
    users to creating image up to manipulating photos. PHP gd can create with file extension of jpg,
    gif, swf, tiff and jpeg2000. Installation see http://www.php.net/manual/en/ref.image.php .
    Astahost hosting service has enable gd library, so won’t need any extra works except in coding.
    Lets get started, creating images from php set the canvas: CODE   $img = imagecreate(250, 80)
    imagecreate this function create and set a blank canvas with the wide of 250 pixels a....
  29. Your Most Favourite Computer Language
    (84)
    what do you think??....
  30. Basic Tips and Tricks in HTML
    (15)
    Here is some quick links for basic html coding... A quick and easy way to create your first web
    page! -> The easiest HTML guide for beginners You'll learn how to create tables from real
    examples. -> How to create TABLE? You will learn how to create frames from a real example.
    You'll see how to create a borderless frame, how to specify the target frame, etc. -> How to
    build FRAMES? Follow a few easy steps to add sound to your web pages. -> How to add sound to a
    web page? This page tells you how to automatically load a visitor to another web page.....

    1. Looking for basic, c, language, started, c






*SIMILAR VIDEOS*
Searching Video's for basic, c, language, started, c
Similar
Basic C++ Coding
Basic C Language, Functions
Which Language Is Easy And Secure Today For Web Development - is PHP MYSQL is easy and Secure ?
Site Language - Other languages permitted
Linux Partitioning Guide (new Users) - A very basic guide to partitioning in linux designed for new users
Basic Html Tutorial - Made it myself, hope you like it.
An Absolute Basic Guide To Algorithms For Dummies
Basic Css
Linux Basic Command - For Storing Compilation Error To File
Who Is No. 1 Actor In India (all Language Including Bollywood)?
Some Usefull Linux Basic Commands And Utilities. Please Add To This List If You Know One.
How Do I Get Started?
Increase Your Knowledge Of Html Language
Html Basic Tutorial - <!-- For beginners only -->
Phpbb - Installation Tutorial ( For Newbies Based On Astahost Cpane)l - phpBB 2.0.22 installation Tutorial with basic steps.
Asterisknow Pbx (voip Telephony) - A basic run down & some questions
C++: Basic Classes - classes, objects, access labels, members, inline functions
Visual Basic.NET Help Needed.
What Language Is Linux Written In ?
Where To Start Learning Programming - Please advise me on which language to
Visual Basic 6 + Crystal Reports 9 - how to distribute
Making A Nice Looking Signature In Photoshop Cs - Some basic Photoshop skills required...
Does Anyone Code Using Turing - A student programming language
VB.NET: Switch Regional Language Automatically - When the field being edited gets FOCUS
What Language Is Best For Game Programming? - I need some help
Online Multiplayer Game - What language to learn in order make it?
Visual Basic Help - need help useing visual basic
Basic Tutorial: PHP GD - basic tuts
Your Most Favourite Computer Language
Basic Tips and Tricks in HTML
advertisement




Basic C++ Language - Getting started with C++



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE