METU Dept' of Computer Eng' Summer 2002 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

METU Dept' of Computer Eng' Summer 2002

Description:

Lecture 08. METU Dept. of Computer Eng. Summer 2002 ... by Ahmet Sacan. Mon July 24, 2002. Copying Arrays ... char name[6] = 'ahmet'; Printing char arrays ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 26
Provided by: ahm4
Category:
Tags: metu | ahmet | computer | dept | eng | summer

less

Transcript and Presenter's Notes

Title: METU Dept' of Computer Eng' Summer 2002


1
Lecture 08
Mon July 24, 2002
  • METU Dept. of Computer Eng. Summer 2002
  • Ceng230 - Section 01
  • Introduction To C Programming
  • by Ahmet Sacan

2
Copying Arrays
  • an array cannot be directly assigned to another
    array.
  • int x100, y100
  • x y / ILLEGAL /
  • You must copy values one-by-one
  • for(i0 ilt100 i)
  • xi yi

3
Perfect your skills at
  • using arrays in loops, esp. in for-loops.
  • for(i0 iltARR_SIZE i)
  • scanf("d", arri)
  • printf("d", arri)

4
Array Initialization
  • int myscores3 90, 95, 56
  • int ttt33 0,1,0, 2,0,0, 1,0,2
  • int ttt33 0,1,0,2,0,0,1,0,0
  • char myName5 'a', 'h', 'm', 'e', 't'

5
options...
  • Can omit size when initializers are specified.
  • int myScores 90, 95, 56
  • when less initializers than the size, the rest
    filled with zeros. (?)
  • int myScores3 90,95 /same as 90,95,0
    /
  • int goals6 1,2,0 /same as 1,2,0,0,0,0
    /

6
character arrays
  • a character array may be initialized using a
    double-quoted string.
  • char myName6 "ahmet" / why 6? /
  • char myName6 'a','h','m','e','t','\0'

7
errors...
  • more initializers than size
  • int scores3 90,100,50, 20
  • accessing unallocated blocks
  • int scores3 90,100,50
  • ...
  • scores4 3

8
Arrays as Arguments
  • Write a function that takes in a float-array of
    size 10, and returns the average of these 10
    numbers.

9
Arrays as arguments
  • float avg( float x10 )
  • int i float sum 0
  • for(i0 i lt 10 i)
  • sum xi
  • return sum/10
  • void main()
  • float arr10
  • ...
  • a avg( arr )
  • ...

10
Goal
  • Write a function that takes a float-array of any
    size, and returns the average of the elements.

11
  • float avg( float x, int size )
  • int i float sum 0
  • for(i0 i lt size i)
  • sum xi
  • return sum/size
  • void main()
  • float arr10
  • ...
  • a avg( arr , 5)
  • ...

12
when...
  • ... array dimension is greater than 1, all but
    first dimension size must be specified.
  • define MAXCOLS 5
  • int PrintMatrix(int aMAXCOLS, int rows)

13
more on char
  • we'll call a null ('\0') terminated character
    array a string.
  • char mystr16 "hello"
  • is equivalent to
  • char mystr16 'h', 'e', 'l', 'l', 'o','\0'

14
char array initialization
  • to initialize a character array, you can use a
    double-quoted string only when you are declaring
    the array.
  • char lastName32 "sacan"
  • lastName "ekmen" / INVALID /
  • make sure that array is long enough to hold the
    string. Leave one extra character for '\0' - null
    character.
  • char name6 "ahmet"

15
Printing char arrays
  • Like regular arrays, you may use a for-loop to
    print individual elements
  • Or, use s, so that printf takes care of it for
    you...
  • printf("s", arr)

16
Goal
  • Write a function that prints a character array,
    char-by-char.

17
  • void PrintCharArr(char str , int maxSize)
  • int i
  • for(i0 iltsize i)
  • if(stri '\0')
  • break
  • printf("c", arri)

18
Reading Input-Line into char-array
  • Use the special function fgets, defined in
    stdio.h, to read a line of input.
  • The fgets() function reads characters from the
    stream into the array pointed to by s, until n-1
    characters are read, or a newline character is
    read and transferred to s, or an end-of-file
    condition is encountered. The string is then
    terminated with a null character.
  • fgets(str, MAX_SIZE, stdin)

19
Goal
  • Read a line of input into an array and print the
    array character by character.

20
  • define MAX_SIZE 256
  • void main()
  • int i
  • char strMAX_SIZE
  • printf("enter string ")
  • fgets(str, MAX_SIZE, stdin)
  • for(i0 iltMAX_SIZE i)
  • if(stri '\0')
  • break
  • printf("c", stri)

sample Run enter string trshx trshx
21
Goal
  • Write a function that takes a character-array as
    input, and its maximum size, and returns the
    length of the string contained in the array.

22
  • int GetStrLength(char str , int maxLen)
  • int i
  • for(i0 iltmaxLen i)
  • if(stri '\0')
  • return i
  • return 0 / not a null-terminated array /

23
Back to Non-char arrays.
24
Goal
  • Write a function that takes does matrix
    production.
  • Function takes three matrices and their sizes,
    put the product of the first two into the third.

25
  • define MAXCOLS
  • void MatProd(float x MAXCOLS, int xR,
    xC, float y MAXCOLS, yC, float z
    MAXCOLS, int zA, zB)
  • int i,j,k
  • for(i0 i lt xR i)
  • for(j0 j lt xC j)
  • zij 0
  • for(k0 k lt yC k)
  • zij xik ykj
Write a Comment
User Comments (0)
About PowerShow.com