Welcome Guest ( Log In | Register )



2 Pages V  < 1 2  
Reply to this topicStart new topic
> Basic C++ Language, Getting started with C++
iGuest
post Mar 10 2008, 09:05 AM
Post #11


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



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
Go to the top of the page
 
+Quote Post
Quatrux
post Mar 11 2008, 08:26 AM
Post #12


the Q
Group Icon

Group: [HOSTED]
Posts: 1,133
Joined: 13-July 05
From: Lithuania, Vilnius
Member No.: 7,059
myCENTs:5.70



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
Go to the top of the page
 
+Quote Post
Umar Shah
post Mar 26 2008, 06:46 PM
Post #13


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 22
Joined: 25-March 08
Member No.: 29,382



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.

Go to the top of the page
 
+Quote Post
Umar Shah
post Mar 26 2008, 06:59 PM
Post #14


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 22
Joined: 25-March 08
Member No.: 29,382



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;

....



Go to the top of the page
 
+Quote Post
Umar Shah
post Mar 26 2008, 07:11 PM
Post #15


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 22
Joined: 25-March 08
Member No.: 29,382



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;
}
Go to the top of the page
 
+Quote Post
Umar Shah
post Mar 26 2008, 07:15 PM
Post #16


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 22
Joined: 25-March 08
Member No.: 29,382



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;
}


Go to the top of the page
 
+Quote Post
Umar Shah
post Mar 26 2008, 07:55 PM
Post #17


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 22
Joined: 25-March 08
Member No.: 29,382



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;
}

Go to the top of the page
 
+Quote Post
Umar Shah
post Mar 26 2008, 08:06 PM
Post #18


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 22
Joined: 25-March 08
Member No.: 29,382



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.
Go to the top of the page
 
+Quote Post
Umar Shah
post Mar 26 2008, 08:26 PM
Post #19


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 22
Joined: 25-March 08
Member No.: 29,382



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.

This post has been edited by Umar Shah: Mar 26 2008, 08:27 PM
Go to the top of the page
 
+Quote Post
Miles
post Mar 27 2008, 07:14 PM
Post #20


Advanced Member
Group Icon

Group: [HOSTED]
Posts: 177
Joined: 25-December 07
Member No.: 27,129



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.
Go to the top of the page
 
+Quote Post

2 Pages V  < 1 2
Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Basic Tips and Tricks in HTML(15)
  2. Your Most Favourite Computer Language(84)
  3. Basic Tutorial: PHP GD(16)
  4. Visual Basic Help(7)
  5. Online Multiplayer Game(16)
  6. What Language Is Best For Game Programming?(27)
  7. VB.NET: Switch Regional Language Automatically(1)
  8. Does Anyone Code Using Turing(3)
  9. Making A Nice Looking Signature In Photoshop Cs(17)
  10. Visual Basic 6 + Crystal Reports 9(6)
  11. Where To Start Learning Programming(17)
  12. What Language Is Linux Written In ?(15)
  13. Visual Basic.NET Help Needed.(8)
  14. C++: Basic Classes(5)
  15. Asterisknow Pbx (voip Telephony)(1)
  1. Phpbb - Installation Tutorial ( For Newbies Based On Astahost Cpane)l(5)
  2. Html Basic Tutorial(9)
  3. Increase Your Knowledge Of Html Language(11)
  4. Linux Basic Command - For Storing Compilation Error To File(1)
  5. Basic Css(6)
  6. An Absolute Basic Guide To Algorithms For Dummies(0)
  7. Basic Html Tutorial(1)
  8. Linux Partitioning Guide (new Users)(1)
  9. Site Language(9)
  10. Which Language Is Easy And Secure Today For Web Development(2)
  11. Basic C Language, Functions(5)
  12. Basic C++ Coding(9)
  13. Making A Programming Language(6)


 



- Lo-Fi Version Time is now: 5th December 2008 - 02:02 AM