Arrays Speaker: 楊濬仲 Advisor: 黃俊龍. What is array?

24
Arrays Speaker: 楊楊楊 Advisor: 楊楊楊
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    229
  • download

    2

Transcript of Arrays Speaker: 楊濬仲 Advisor: 黃俊龍. What is array?

Arrays

Speaker: 楊濬仲Advisor: 黃俊龍

What is array?

Why we need array? If we don’t want use array, we have to declare

a lot of variables to maintain our elements.

I’m “A”

I’m “B”

I’m “Z”

I’m “?”

Why we need array? So, if we use the array to maintain the similar

elements, the world will become simple and logically.

I’m “man[

0]”

I’m “man[

2]”

I’m “man[1

1]”I’m

“man[n]”

Yada!!!!!

Declaring a series of variables 鄉民的最愛 : ” 要怎樣就怎樣,要多大就多大”

Type short int int long int float double long double unsigned short int unsigned int unsigned long int …

• Size (Up to your OS)– short int a[1];– Int b[5];– long int c[10];– float d[100];– double e[1000];– long double f[10000];– unsigned short int

g[100000];– unsigned int– unsigned long int– …

As BIG as your

OS setup

Initial array When initial one array, we must assign a size

for array.Ex. Int array[10];

Then we can assign one value for the array by the index.Ex. array[5] = “5”;

Or you can initial all value for the array at declare state.Ex. Int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7,

8, 9 };

Initial array Remember, array index is start from 0, in

other words, if you declare int a[3] = {1,2,3}; , than a[0] = 1, a[1] = 2, a[2] = 3, and there is no a[3].

But why we can access a[3]?

Arrays Of More Than One Dimension Q: Can we use more than one dimension? A: Sure, you can use numbers of dimension as

more as you want. If you understand what are you writing.Ex. int Score [4][3] ; int IDontKnow [10][20][30][40]

[50]; Similarly, you can initialize

the values in the declare statement.Ex. int Score[4][3] = {

{1, 2, 3}, {4, 5, 6},{7, 8, 9}, {10, 11, 12}

}

1 2 3

11

5 6

7

10

8

12

9

4

Arrays and Nested Loops Arrays of more than one dimension are usually

handled by nested for loops.Ex.

Arrays as Parameters for functions Q: Should we pass all elements as parameters

for functions ? 國台辦 : Ridiculous, 荒謬

( 不過你很行還是可以啦 = =“)

Think about call on functioncall(a[0],a[1],a[2],…..,a[10000], ….);

When we can terminate the endless code?

Arrays as Parameters for functions Now, you have a smart way to handle your

arrays when passing arrays into functions.

Prototype for call by arrayint * functionName(int [], int [][3]); int * functionName(int [], int *[3]); int * functionName(int [], int **);

Return a Array Pass a array

Arrays as Parameters for functions

Some tips You can’t copy a array just by arrayName

Ex.

You should only copy elements one by one.

Some tips The value in the incorrect memory location

would be corrupted with unpredictable consequences.

The value would corrupt the memory and crash the program completely! On Unix systems this leads to a memory segmentation fault.

Appendix

Outline How to read continued input ?

while(scanf (“%d %d”,&a,%b) != EOF) {…

}for(;scanf(“%d”,&a) == EOF;){

…}

How to get function The C Library Reference

http://www.acm.uiuc.edu/webmonkeys/book/c_guide/

Random numbers #include <stdlib.h> int rand(void)

Return a random number 0≦value≦RAND_MAX RAND_MAX In 32bits compiler: (7FFFFFFF)16 In Turbo C: (7FFF)16

Ex. srand(time(NULL)); ( set up random with seed)

printf("%d\n",rand());

System call #include<stdlib.h> int system(const char *string);

The String means the OS’s command

Ex. System(“PAUSE”); EX. System(“cls”);

Practice & Demo procedure today

Procedure

Practice •~9:00•Finish today practice

Demo with bonus •~9:00•You can demo hw1 with bonus before 9:00 p.m.

General Demo •9:00 ~•We just demo your hw1

Practice Write a program can generate not repeatable

random numbers to fill with 5 * 5 array

Hints Not repeatable control: Use a array to

checkout whether a number appear.

Random number: Use rand() % 25 + 1 to make sure your number is locating at your ideal range.

Homework1 Bonus (10%)

Two Christmas Trees!