Lecture
This presentation is the property of its rightful owner.
Sponsored Links
1 / 57

Lecture 8 Array and Struct ( ตัวแปรชุด และ ข้อมูลแบบโครงสร้าง) PowerPoint PPT Presentation


  • 42 Views
  • Uploaded on
  • Presentation posted in: General

Lecture 8 Array and Struct ( ตัวแปรชุด และ ข้อมูลแบบโครงสร้าง). OBJECTIVE 1. ทราบการกำหนดตัวแปรอาร์เรย์ (Define Array Variable) 2. ทราบถึงการนำไปใช้งาน 3. สามารถนำตัวแปรแบบอาร์เรย์ไปประยุกต์ใช้งานได้ 4. ทราบถึงการกำหนดตัวแปรแบบโครงสร้าง. อาร์เรย์ (Array) คืออะไร

Download Presentation

Lecture 8 Array and Struct ( ตัวแปรชุด และ ข้อมูลแบบโครงสร้าง)

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Lecture 8 array and struct

Lecture 8

Array and Struct

( )


Lecture 8 array and struct

OBJECTIVE

1. (Define Array Variable)

2.

3.

4.


Lecture 8 array and struct

(Array)

TM1 OldTm1 Oldtm1 TM1 20

. 631

TM1 20 ()

1. 1

2. ...


Lecture 8 array and struct

[]

0 1 2 3 4 ... 19

GradeTM1[20];


Lecture 8 array and struct

RESERVED

RESERVED

RESERVED

RESERVED

RESERVED

#define MAXC 5

char t[MAXC];


Lecture 8 array and struct

RESERVED

RESERVED

RESERVED

RESERVED

RESERVED

#define MAXC 5

char t[MAXC];

t[0]

t[1]

  • Arrays .

  • t[2]( thirdelement of the arrayt.)

t[2]

t[3]

t[4]


Lecture 8 array and struct

8.1

[]

int oldTM1[20];

char GradTM1[20];

oldTM1 20

0 1 2 3 4 ... 19

oldTM1[20];


Lecture 8 array and struct

()

void main()

{

const int size=4;

int a[size];

}

a[4]

void main()

{

char a[10];

}

a[10] 10 ( 1 10 )


Lecture 8 array and struct

8.2

#include <iostream.h>

void main()

{

int a[4]={1,2,3,4};

}

a[0] = 1a[1] = 2a[2] = 3a[3] = 4

void main()

{

int a[]={1,2,3,4};

}

complier 4


Lecture 8 array and struct

#include <iostream.h>

void main()

{

int a[2][4]={{1,2,3,4},{5,6,7,8}};

}

a[0][0] = 1 , a[0][1] = 2 , a[0][2] = 3 , a[0][3] = 4

a[1][0] = 5 , a[1][1] = 6 , a[1][2] = 7 , a[1][3] = 8


Lecture 8 array and struct

#include <iostream.h>

void main()

{

char A[10]={S,o,m,p,o,n,g};

}

A[0] = S , A[1] = o , A[2] = m , A[3] = p

A[4] = o , A[5] = n , A[6] = g , A[7] =

A[8] = , A[9] =


Lecture 8 array and struct

A

S

o

m

p

o

n

g

\o

A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

A[6]

A[7]

#include <iostream.h>

void main()

{

char A[]=Sompong;

}

\o

\o


Lecture 8 array and struct

A

S

o

m

p

o

n

g

\o

A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

A[6]

A[7]

\o array

#include <iostream.h>

void main()

{

char A[7]=Sompong;

}

\o

\o


Lecture 8 array and struct

A

S

o

m

p

o

n

g

\o

A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

A[6]

A[7]

8 !!!

#include <iostream.h>

void main()

{

char A[8]=Sompong;

}

\o


Lecture 8 array and struct

A

S

o

m

p

o

n

g

\o

A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

A[6]

A[7]

#include <iostream.h>

void main()

{

char A[8]={Sompong};

}

\o


Lecture 8 array and struct

A

S

o

m

p

o

n

g

A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

A[6]

7

#include <iostream.h>

void main()

{

char A[7]={S,o,m,p,o,n,g};

}


Lecture 8 array and struct

float !!!

#include <iostream.h>

void main()

{

floatA[3]={10.12 ,5.1214 , -14.121 };

}

A

A[0]

A[1]

A[2]


Lecture 8 array and struct

double !!! double 8 byte float 4 byte !!!!!

#include <iostream.h>

void main()

{

doubleA[3]={10.12 ,5.1214 , -14.121 };

}

4 byte

4 byte

4 byte

A

A[0]

A[1]

A[2]


Lecture 8 array and struct

?????

????


Lecture 8 array and struct

#include <iostream.h>

int main()

{

double a[4];

cout << a[0] << a[1] << a[2] << a[3];

return 0;

}

Array 555!!!!


Lecture 8 array and struct

#include <iostream.h>

int main()

{

float a[4]={ 1.11 , 2 , 0.5 , 8.11 };

cout << a[0] << a[1] << a[2] << a[3];

return 0;

}

!!!


Lecture 8 array and struct

#include <iostream.h>

int main()

{

double a[4]={ 1 , 2 , 0.5 , 8.11 };

for(int i ; i<4 ; i++)

{

cout << a[i] <<endl ;

}

return 0;

}

i<4

i <=4

i<=4 a[4] a[3] C/C++


Lecture 8 array and struct

?????

????


Lecture 8 array and struct

#include <iostream.h>

void main()

{

float a[3];

a[0] = 12.1;

a[1] = 0.00011;

a[2] = -12.1;

cout << a[0] << a[1] << a[2] << endl;

}

!!!


Lecture 8 array and struct

#include <iostream.h>

void main()

{

char name[10];

cin >> name;

cout <<Your name is : << name << endl;

}

cin 10 10 ()


8 3 string

8.3 string

  • gets() ( library stdio.h)

  • strcpy() ( library string.h)

  • strcat() ( library string.h)

  • strlen() ( library string.h)

  • strcmp() ( library string.h)


Lecture 8 array and struct

gets()

  • #include <iostream.h>

  • #include <stdio.h>

  • void main( void )

  • {

    • char line[81];

    • cout<< "Input a string: ";

    • gets( line );

    • cout<<"The line entered was: <<line;

  • }

  • Output

  • Input a string: Hello!

  • The line entered was: Hello!


Strcpy strcat

strcpy() strcat()

  • #include <iostream.h>

  • #include <stdio.h>

  • #include <string.h>

  • void main( void )

  • {

    • char string[80];

    • strcpy( string, "Hello world from " );

    • strcat( string, "strcpy " );

    • strcat( string, "and " );

    • strcat( string, "strcat!" );

    • printf( "String = %s\n", string );

  • }

  • Output

  • String = Hello world from strcpy and strcat!


Strlen

strlen()

  • #include <string.h>

  • #include <stdio.h>

  • #include <conio.h>

  • void main( void )

  • {

    • char buffer[61] = "How long am I?";

    • int len;

    • len = strlen( buffer );

    • printf( "'%s' is %d characters long\n", buffer, len );

  • }

  • Output

  • 'How long am I?' is 14 characters long

14


Strcmp

strcmp()

#include <string.h> #include <stdio.h> #include <conio.h>

char string1[] = Hello";

char string2[] = hello";

void main( void )

{

char tmp[20];

int result;

/* Case sensitive */

printf( "Compare strings:\n\t%s\n\t%s\n\n", string1, string2 );

result = strcmp( string1, string2 );

if( result > 0 )

strcpy( tmp, "greater than" );

elseif( result < 0 )

strcpy( tmp, "less than" );

else

strcpy( tmp, "equal to" );

printf( "\tstrcmp: string 1 is %s string 2\n", tmp );

}

ASCII


Strcmp1

strcmp()

  • result = strcmp( string1, string2 );

    • string1<string2 <0

    • string1=string2 =0

    • string1>string2 >0


Stricmp

_stricmp()

#include <string.h> #include <stdio.h> #include <conio.h>

char string1[] = Hello";

char string2[] = hello";

void main( void )

{

char tmp[20];

int result;

/* Case sensitive */

printf( "Compare strings:\n\t%s\n\t%s\n\n", string1, string2 );

result = _stricmp( string1, string2 );

if( result > 0 )

strcpy( tmp, "greater than" );

elseif( result < 0 )

strcpy( tmp, "less than" );

else

strcpy( tmp, "equal to" );

printf( "\tstrcmp: String 1 is %s string 2\n", tmp );

}


8 4 array

8.4 Array

#include <iostream.h>

#include <math.h>

int cube(int x[]); //declare function

int main(void)

{int a[]={1,2,3};

int ans;

ans=cube(a);

cout<<"Result is "<<ans<<endl;

return 0;

}

int cube(int x[])

{

int result; //local variable

result = x[0]+x[1]+x[2];

return result;

}


Lecture 8 array and struct

?????

array

Pass by value() Pass by reference()

2... Lecture 7


Lecture 8 array and struct

array

#include <iostream.h>

#include <math.h>

void cube(int x[]); //declare function

int main(void)

{int a[]={1,2,3};

int ans;

cout<<"Result is "<<a[0]<<a[1]<<a[2]<<endl;

ans=cube(a);

cout<<"Result is "<<a[0]<<a[1]<<a[2]<<endl;

return 0;

}

void cube(int x[])

{

x[0]=4;

}

Pass by value() Pass by reference()

2... Lecture 7


Lecture 8 array and struct

(Struct)

.,.,. ,,

1.

2.


Lecture 8 array and struct


Lecture 8 array and struct

!!!


Lecture 8 array and struct

struct

struct {

}Object;

struct children{

int age;

char sex;

char tel[9];

int height;

}Sompong;


Lecture 8 array and struct

struct

struct children{

int age;

char sex;

char tel[9];

int height;

}Sompong;

void main(void)

{

cin >> Sompong.age;

cout << Sompong.age;

}

Sompong Object struct Children struct childern Object struct


Lecture 8 array and struct

Memory Map Object

struct children{

int age;

char sex;

char tel[4];

int hei;

}Sompong;

Sompong Object struct Children struct childern Object struct

children

Somong

hei

tel

hei

age

age

sex

tel

tel

tel


Lecture 8 array and struct

struct Object

struct {

};

struct children{

int age;

char sex;

char tel[9];

int height;

};


Lecture 8 array and struct

struct

struct children{

int age;

char sex;

char tel[9];

int height;

};

void main(void)

{

children Sompong;

cin >> Sompong.age;

cout << Sompong.age;

}

Object Sompong


Lecture 8 array and struct

class

class {

};

class children{

int age;

char sex;

char tel[9];

int height;

};


Lecture 8 array and struct

class

class children{

int age;

char sex;

char tel[9];

int height;

};

void main(void)

{

children Sompong;

cin >> Sompong.age;

cout << Sompong.age;

}

Run !!


Lecture 8 array and struct

class

struct default Private

class children{

public:

int age;

char sex;

char tel[9];

int height;

};

void main(void)

{

children Sompong;

cin >> Sompong.age;

cout << Sompong.age;

}


Lecture 8 array and struct

(ASCII)


Lecture 8 array and struct

(ASCII)


T he e nd

THE END


Inclass lab1

  • total

  • i=0

  • data[i]

  • i<total

    • i=i+1

    • 3.

  • 5. i=0

  • 6. data[i]>data[i+1]

  • data[i] data[i+1]

  • 5

  • i=i+1

  • i = total 1 7.

  • 6.

  • 7. i=0

  • 8. data[i]

  • 9. i<total

  • i=i+1

  • 8.

  • 10.return

Inclass Lab1


Inclass lab2

Inclass Lab2

Array


Inclass lab3

Inclass Lab3

5


Inclass lab4

Inclass Lab4

3 x 3


Inclass lab5

Inclass Lab5


Inclass lab6

Inclass Lab6

1. 5

Please Enter 5 number :

a[0]=2 (Enter)

a[1]=3 (Enter)

a[2]=7 (Enter)

a[3]=1 (Enter)

a[4]=8 (Enter)

Mininum is : a[3]=1 ()

Test Driver

( 5 )


Lecture 8 array and struct

#include <iostream.h>

#include <iomanip.h>

using namespace std;

int main()

{

const int MAX_ITEMS = 8;

float x[MAX_ITEMS];

float average;

}


  • Login