Jump to content

Recommended Posts

  • Replies 68
  • Created
  • Last Reply

Top Posters In This Topic

Posted

The string is a speciel type of array. It's an array of chars. Therefor, you can use it as array or as single variable.

char s[20];
cin>>s;
cout<<s;

This code will get a string, and then print it.

You can give a value to the intire string only when you declare it:

char s[15]="hello world"; //Right
s="whats up";                 //Wrong

 

If you want to give a value to a string after you declare it, do it like this:

strcpy(s,"whats up");

strcpy is a function. Inside the (), there are the functions parameters. Even if The function doesn't have any parameters, you still need to put the (), like this: "function()". Inside the (), the parameters are seperated by commas.

 

The strcpy function is inside "string.h", so you must put the line "#include<string.h>" at the top of the program, like this:

#include<iostream.h>
#include<string.h>
void main()
{
 char s[15]="hello world";
 cout<<s;
 strcpy(s,"whats up");
 cout<<s;
}

 

You can also use the string like an array, like this:

char s[10]="hello";
cout<<s[2];

the output will be "l", because that's the char at the index 2 (remember, the indexes begin from 0).

 

The last char of the string is '\0', or 0 in ascii. This is the char that tells the computer where is the string's end. You can also use it.

#include<iostream.h>
void main()
{
 char s[20];
 cout<<"\ngive me a string: ";
 for(int i=0;s[i];i++)
   cout<<s[i]<<endl;
}

This program is a little complicated, so I will explain. The program get an input string, and put all the chars in the sting in a column. The i goes from the begining of the string - index 0 - untill s is false. If you remember, false in C++ is 0, so '\0', the end of the string, is false. so the loop will stop when it reach to the end of the string.

 

That will be all for today. Class dismissed.

Posted

You can make an array with more then one dimention. A 2D array is called matrix. You do it like this:

int mat[5][5];
mat[1][3]=7;
cout<<mat[2][4];

 

You can make more then two dimention arrays:

int a[2][5][7],b[4][3][10][6];

There is a limit, but it is very high, so you will probebly never reach it.

 

A matrix of chars, is basicly an array of strings:

char s[5][20];
cin>>s[2];

The firest number is the number of string in the array, and the second number is the maximum chars in each string.

 

If you want to fill a matrix with for loops, you need to make a loop inside loop. The indexes of the loops must be different, so the loops will work right. You use i for the outer loop, and j for the inner loop:

#include<iostream.h>
void main()
{
 int mat[5][5];
 for(int i=0;i<5;i++)
   for(int j=0;j<5;j++)
    {
     cout<<"enter number "<<i<<","<<j<<":";
     cin>>mat[i][j];
    }
 for(int i=0;i<5;i++)
  {
   cout<<endl;
   for(int j=0;j<5;j++)
     cout<<mat[i][j];
  }
}

 

That will be all for today. Class dismissed.

Posted

Just to note a coupla things you missed:

 

You got down the standard operators alright (+, -, *, /) and their shorthand forms (++,+=, etc) but you missed a very essential and important set of operators - the bitwise operators.

 

Here they are:

 

& - Bitwise AND, performs a logical AND bit by between the two given variables

Returns 1 ONLY if both inputs are 1

AND Table:

a b Out

1 1 1

1 0 0

0 1 0

0 0 0

example

9 & 11 = 1001&1011=1001 = 9

| - Bitwise OR, performs a logical OR bit by between the two given variables

Returns 1 if either one, or both inputs are 1

OR Table:

a b Out

1 1 1

1 0 1

0 1 1

0 0 0

example

9 | 11 = 1001|1011=1011 = 11

^ - Bitwise XOR, performs a logical XOR bit by between the two given variables

Returns 1 if the inputs are different, also called a diffrential amplifier

XOR Table:

a b Out

1 1 0

1 0 1

0 1 1

0 0 0

example

9 ^ 11 = 1001&1011=10 = 2

~ - NOT, complements (inverts) every bit in the variable

Returns 1 if input is 0, 0 if input is 1

NOT Table:

a Out

1 0

0 1

example

~11 = ~1011 = 0100 = 4

 

Shorthand style follows the same rules for these operators, so you can do things like &=, |=, ^= etc

Note these are NOT the comparison operators, which are differentiated by doubling up the operators (&& for comparison and, || for comparison or, ! for comparison not)

 

Oh, if you feel like a brain teaser, have fun with this little piece of code :(

 

x^=y

y^=x

x^=y

 

Those three lines will actually swap the contents of x and y without needing a temporary third variable. The question is, How Does It Work? Enjoy!

Posted

Thanks roofus.

 

Variables can change during the program, but you can also declare values that can not be changed. Why do you want to do that?

Let's say you need to use pi alot in your program. pi is the relation circuit of the circlre to it's diameter. The value of pi is about 3.141592654. Since you don't want to write that long number any time you use pi, you declare a constant.

 

The first way to declare a constant, is a macro:

#include<iostream.h>
#define PI 3.141592654 // constant are usualy writed with capital letters.
void main()
{
 cout<<"pi="<<PI;
}

#define name value

 

You can make string macroes, int macroes, char macroes ect.

 

What macro does is scaning your program from the point you declared it, and replacing each "PI" with "3.141592654".

 

Another way to decalre a constant, is a const variable:

const int PI=3.141592654;
cout<<PI;

If you add "const" before the variable decleration, the value you assigned it at the decleration is unchangeable.

 

The third way is to use the keyword "enum":

enum
{
 MINUTESISHOUR=60
};

enum can only be used for integer constants. You can make many contants in single enum command with the comma:

enum
{
 HOURSINDAY=12,
 MINUTESISHOUR=60
};

 

You can also use constants for values you will want to change during the programing.

 

That will be all for today. Class dismissed.

Posted
IDK if ill ever be able to understand that >_<

the stuff gets less intimidating as you work with it more.

 

programming isn't hard, its just tedious.

this is what it takes to calculate a score in my poker game(shrunken down to prevent page stretching):

/////////////////////////////////

int getscore(drawhand *thishand)

/////////////////////////////////

{

int score;

int lowesti=1;

int e=1;

drawhand sorted;

 

inithand(&sorted);

bool asorted[6];

for(int a=0; a<=5;a++)

{asorted[a]=false;}

 

 

 

 

for(int s=1;s<=5;s++)

{

lowesti=1;

while(asorted[lowesti]==true)

{lowesti++;}

 

for(int s0=1;s0<=5;s0++)

{

if((thishand->playercards[s0].num<thishand->playercards[lowesti].num)&&(asorted[s0]==false))

{

lowesti=s0;

}

}

 

e=1;

while(e<=5)

{

if((thishand->playercards[e].num==thishand->playercards[lowesti].num)&&(asorted[e]==false))

{asorted[e]=true;sorted.playercards=thishand->playercards[e];e=20;}

else

{e++;}

 

}

 

 

}

 

if(((sorted.playercards[1].num==10)&&(sorted.playercards[2].num==11)&&(sorted.playercards[3].num==12)&&(sorted.playercards[4].num==13)&&(sorted.playercards[5].num==14))&&((sorted.playercards[1].suit==sorted.playercards[2].suit)&&(sorted.playercards[2].suit==sorted.playercards[3].suit)&&(sorted.playercards[3].suit==sorted.playercards[4].suit)&&(sorted.playercards[4].suit==sorted.playercards[5].suit)))

{

score=600000000;

}

 

else if((((sorted.playercards[1].num==(sorted.playercards[1].num+0))&&(sorted.playercards[2].num==(sorted.playercards[1].num+1))&&(sorted.playercards[3].num==(sorted.playercards[1].num+2))&&(sorted.playercards[4].num==(sorted.playercards[1].num+3))&&(sorted.playercards[5].num==(sorted.playercards[1].num+4)))&&((sorted.playercards[1].suit==sorted.playercards[2].suit)&&(sorted.playercards[2].suit==sorted.playercards[3].suit)&&(sorted.playercards[3].suit==sorted.playercards[4].suit)&&(sorted.playercards[4].suit==sorted.playercards[5].suit))))

{

score=480000000;

score+=(sorted.playercards[5].num*pow(15,5));

 

}

 

else if(((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[2].num==sorted.playercards[3].num)&&(sorted.playercards[3].num==sorted.playercards[4].num))||((sorted.playercard

s[2].num==sorted.playercards[3].num)&&(sorted.playercards[3].num==sorted.playercards[4].num)&&(sorted.playercards[4].num==sorted.playercards[5].num)))

{

score=420000000;

if((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[2].num==sorted.playercards[3].num)&&(sorted.playercards[3].num==sorted.playercards[4].num))

{score+=(5*(sorted.playercards[1].num*pow(15,5)));}

else if((sorted.playercards[2].num==sorted.playercards[3].num)&&(sorted.playercards[3].num==sorted.playercards[4].num)&&(sorted.playercards[4].num==sorted.playercards[5].num))

{score+=(5*(sorted.playercards[2].num*pow(15,5)));}

 

}

 

else if((((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[2].num==sorted.playercards[3].num))&&((sorted.playercards[4].num==sorted.playercards[5].num)))||(((sorted.playerc

ards[1].num==sorted.playercards[2].num))&&((sorted.playercards[3].num==sorted.playercards[4].num)&&(sorted.playercards[4].num==sorted.playercards[5].num))))

{

score=360000000;

if(((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[2].num==sorted.playercards[3].num))&&((sorted.playercards[4].num==sorted.playercards[5].num)))

{score+=(sorted.playercards[1].num*pow(15,5));score+=(sorted.playercards[4].num*

pow(15,4));}

else if(((sorted.playercards[1].num==sorted.playercards[2].num))&&((sorted.playercards[3].num==sorted.playercards[4].num)&&(sorted.playercards[4].num==sorted.playercards[5].num)))

{score+=(sorted.playercards[1].num*pow(15,4));score+=(sorted.playercards[3].num*

pow(15,5));}

 

}

 

else if((sorted.playercards[1].suit==sorted.playercards[2].suit)&&(sorted.playercards[2].suit==sorted.playercards[3].suit)&&(sorted.playercards[3].suit==sorted.playercards[4].suit)&&(sorted.playercards[4].suit==sorted.playercards[5].suit))

{

score=300000000;

score+=(sorted.playercards[1].num*pow(15,1));

score+=(sorted.playercards[2].num*pow(15,2));

score+=(sorted.playercards[3].num*pow(15,3));

score+=(sorted.playercards[4].num*pow(15,4));

score+=(sorted.playercards[5].num*pow(15,5));

 

}

 

else if(((sorted.playercards[1].num==(sorted.playercards[1].num+0))&&(sorted.playercards[2].num==(sorted.playercards[1].num+1))&&(sorted.playercards[3].num==(sorted.playercards[1].num+2))&&(sorted.playercards[4].num==(sorted.playercards[1].num+3))&&(sorted.playercards[5].num==(sorted.playercards[1].num+4))))

{

score=240000000;

score+=(sorted.playercards[5].num*pow(15,5));

}

 

else if(((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[2].num==sorted.playercards[3].num))||((sorted.playercard

s[2].num==sorted.playercards[3].num)&&(sorted.playercards[3].num==sorted.playercards[4].num))||((sorted.playercard

s[3].num==sorted.playercards[4].num)&&(sorted.playercards[4].num==sorted.playercards[5].num)))

{

score=180000000;

if((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[2].num==sorted.playercards[3].num))

{score+=(sorted.playercards[1].num*pow(15,5));}

else if((sorted.playercards[2].num==sorted.playercards[3].num)&&(sorted.playercards[3].num==sorted.playercards[4].num))

{score+=(sorted.playercards[2].num*pow(15,5));}

else if((sorted.playercards[3].num==sorted.playercards[4].num)&&(sorted.playercards[4].num==sorted.playercards[5].num))

{score+=(sorted.playercards[3].num*pow(15,5));}

 

}

 

else if(((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[3].num==sorted.playercards[4].num))||((sorted.playercard

s[2].num==sorted.playercards[3].num)&&(sorted.playercards[4].num==sorted.playercards[5].num))||((sorted.playercard

s[1].num==sorted.playercards[2].num)&&(sorted.playercards[4].num==sorted.playercards[5].num)))

{

score=120000000;

if((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[3].num==sorted.playercards[4].num))

{score+=(sorted.playercards[3].num*pow(15,5));score+=(sorted.playercards[1].num*

pow(15,4));score+=(sorted.playercards[5].num*pow(15,3));}

 

else if((sorted.playercards[2].num==sorted.playercards[3].num)&&(sorted.playercards[4].num==sorted.playercards[5].num))

{score+=(sorted.playercards[4].num*pow(15,5));score+=(sorted.playercards[2].num*

pow(15,4));score+=(sorted.playercards[1].num*pow(15,3));}

 

else if((sorted.playercards[1].num==sorted.playercards[2].num)&&(sorted.playercards[4].num==sorted.playercards[5].num))

{score+=(sorted.playercards[4].num*pow(15,5));score+=(sorted.playercards[1].num*

pow(15,4));score+=(sorted.playercards[3].num*pow(15,3));}

 

}

 

else if((sorted.playercards[1].num==sorted.playercards[2].num)||(sorted.playercards[2

].num==sorted.playercards[3].num)||(sorted.playercards[3].num==sorted.playercards

[4].num)||(sorted.playercards[4].num==sorted.playercards[5].num))

{

score=60000000;

if(sorted.playercards[1].num==sorted.playercards[2].num)

{

score+=(sorted.playercards[1].num*pow(15,5));

 

score+=(sorted.playercards[3].num*pow(15,2));

score+=(sorted.playercards[4].num*pow(15,3));

score+=(sorted.playercards[5].num*pow(15,4));

}

else if(sorted.playercards[2].num==sorted.playercards[3].num)

{

score+=(sorted.playercards[2].num*pow(15,5));

 

score+=(sorted.playercards[1].num*pow(15,2));

score+=(sorted.playercards[4].num*pow(15,3));

score+=(sorted.playercards[5].num*pow(15,4));

 

}

else if(sorted.playercards[3].num==sorted.playercards[4].num)

{

score+=(sorted.playercards[3].num*pow(15,5));

 

score+=(sorted.playercards[1].num*pow(15,2));

score+=(sorted.playercards[2].num*pow(15,3));

score+=(sorted.playercards[5].num*pow(15,4));

 

}

else if(sorted.playercards[4].num==sorted.playercards[5].num)

{

score+=(sorted.playercards[4].num*pow(15,5));

 

score+=(sorted.playercards[1].num*pow(15,2));

score+=(sorted.playercards[2].num*pow(15,3));

score+=(sorted.playercards[3].num*pow(15,4));

}

 

}

 

else

{

score=0;

score+=(sorted.playercards[1].num*pow(15,1));

score+=(sorted.playercards[2].num*pow(15,2));

score+=(sorted.playercards[3].num*pow(15,3));

score+=(sorted.playercards[4].num*pow(15,4));

score+=(sorted.playercards[5].num*pow(15,5));

}

 

thishand->score=score;

 

return score;

}

 

it may take a while (this took me 2 hours), but it not really that hard.

You just have to learn to think like a computer. and since computers are stupid, it isn't hard.

Posted

Functions are code parts that you can call. Functions are used to split the program into simple parts, making it easyer to write and to read, and/or to save codes: you can call a function any time you want.

 

You declare a function like this:

#include<iostream.h>

void func1()
{
 //bla bla bla
}

void main()
{
 //yada yada yada
 func1();//calling the function
}

 

First, the program will run "yada yada yada". Then, by calling func1, the program will run "bla bla bla". So:

#include<iostream.h>
void func1()
{
 cout<<"bla";
}
void main()
{
 cout<"yada";
 func1();
}

Will output "yadabla"

 

functions ca allso have parameters between the ():

void f(int a,int b,char c,char s[])

Then you can use those variables in the function.

Few notes:

You can't put two parameters after the same type(int, char ect.)

In arrays, you don't put the number of cells inside the []. In matrixes, you don't put the number in the first [], but you put it in the others. I will explain the reasons later.

 

If you change the parameter inside the function, it wont effact whatever you put there when you call the function. So:

#include<iostream.h>
void changenum(int a)
{
 a++;
}
voin main()
{
 int a=5;
 changenum(a);
 cout<<a;
}

will output "5".

The "a" inside the function is not the "a" inside the main program. You can call it any other legal name.

if you put "&" before the name of the parameter, The cangews you do to it will affact the outside:

#include<iostream.h>
void changenum(int &a)
{
 a++;
}
voin main()
{
 int a=5;
 changenum(a);
 cout<<a;
}

will output "6".

 

Functions can allso return values:

#include<iostream.h>
int multipile(int a,int b)
{
 return a*b;
}
voin main()
{
 cout<<multipile(4,5);
}

will output "20". Nonvoid functions must have a return command. The return can allso be put in a void function. The function will end itself when it reach to a return command, so:

#include<iostream.h>
void func()
{
 cout<<"first";
 return;
 cout<<"second";
}
void main()
{
 func();
}

will return "first"

 

That will be all for today. Class dismissed.

Posted

You can make variables accessible for all functions includeing the main program ("void main()": it's a function) by making them global.

When you declare a variable inside a function, it's accessible only inside that function. However, if you declare a variable outside of functions, it's accessible to the entire program (all the program after the decleration, of course):

#include<iostream.h>
int num=0;
void func()
{
 num++;
}
void main()
{
 num++;
 func();
}

 

"const" variables and "enum"s are allso following that rule. "#define"s, however, is accessible to the entire program after the command, since it's precompiling action.

 

That will be all for today. Class dismissed.

Posted

Sometimes you don't want to know what number you will use. You want to generate a random number. There are some functions that generates a random number. To use those functions, you need to include "stdlib.h":

#include<iostream.h>
#include<stdlib.h>
void main()
{
 randomize();
 cout<<random(10);
}

the output will be any number between 0 and 10, includeing 0 not includeing 10.

We can think about those functions like a lottory. Randomize mix up the balls, and random pick a ball. Use randomize only once, before the first random, or you will get the same number any time you use random. If you don't use randomize, you will get the same numbers every time you run the program.

 

That will be all for today. Class dismissed.

Posted

Bit off topic, but does anyone know that the topic title is spelt wrong. Maybe someone in charge could change it.

 

Not that important I know, but someone may as well change it.

Posted

This subject is complicated, so don't be too shy to ask questions.

 

Pointers are very powerfull tools in programing. Wo what are pointers?

Every variable has an address. The address tells the computer where in the memory the variable is located. A pointer is a variable that can store such addresses.

 

To declare a pointer, add '*' in before it's name:

int *p;

p is a pointer to integer.

 

If you add '&' before a variable name, you get the variables address. You can put that address inside a pointer:

int a,*p;
p=&a;

 

To use the value in the address the pointer holds, put '*' before the pointers name:

int a=0,*p;
p=&a;
*p++;

a is now 1.

 

You can create variable on the fly using pointers like this:

int *p;
p=new int;

"new int" find an empty place in the memory, and return it's address. You can to it to other types of variables:

char *c;
float *f;
c=new char;
f=new float;

 

You can release the memory like this:

int *p;
p=new int;
delete p;

 

That will be all for today. Class dismissed.

Posted
I checked on google. There are many results for both spells.

the correct spelling is tutorial, not toturia.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...