Engineering computing i
This presentation is the property of its rightful owner.
Sponsored Links
1 / 20

Engineering Computing I PowerPoint PPT Presentation


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

Engineering Computing I. Chapter 1 – Part B A Tutorial Introduction continued. Character Input and Output. c = getchar ();. reads the next input character from a text stream. Variable ‘c’. File Copying. File Copying Compact Form.

Download Presentation

Engineering Computing I

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


Engineering computing i

Engineering Computing I

Chapter 1 – Part B

A Tutorial Introduction continued


Character input and output

Character Input and Output

c = getchar();

reads the next input character from a text stream

Variable ‘c’

Chapter 1 - Part B


File copying

File Copying

Chapter 1 - Part B


File copying compact form

File CopyingCompact Form

The parentheses around the assignment, within the condition are necessary!

c = getchar() != EOF

c = (getchar() != EOF)

Chapter 1 - Part B


Exercises

Exercises

Exercise 1-6. Verify that the expression getchar() != EOF is 0 or 1

Exercise 1-7. Write a program to print the value of EOF

Chapter 1 - Part B


Character counting

Character Counting

Auto-increment

Equivalent to:

nc = nc +1;

Chapter 1 - Part B


Line counting

Line Counting

Chapter 1 - Part B


Exercise

Exercise

Write a program named BlankCounting.c to count blanks.

Chapter 1 - Part B


Word counting pseudo code

Word CountingPseudo Code

  • Initialize

    • State = OUT /* start assuming not within a word */

    • nc = nl = nw = 0 /* all counters are cleared*/

  • while (c= character) != EOF

  • {

    • ++nc

    • if c== \nl

      • ++nl

    • if c is a white character – i.e. ‘ ‘, ‘\n’ or ‘\t’

      • State = OUT /* start of the none white character will create a word */

    • else if State == OUT

      • State = IN

      • ++nw

  • }

This*is**a*test!

c

State

nl

nc

nw

Chapter 1 - Part B


Word counting

Word Counting

Chapter 1 - Part B


1 6 arrays

1.6 Arrays

write a program to count the number of occurrences of each digit, of white space characters (blank, tab, newline), and of all other characters

Chapter 1 - Part B


Exercise1

Exercise

  • Write a program to count the number of occurrences of all “vowels”, i.e. ‘a’, ‘e’, ‘i’ , ‘o’ and ‘u’. Use an array of counters.

Chapter 1 - Part B


Engineering computing i

  • A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation.

  • With properly designed functions, it is possible to ignore how a job is done; knowing what is done is sufficient.

  • functions like printf, getchar and putchar have been supplied by C Library

  • Write the function power(m,n) to raise an integer m to a positive integer power n. That is, the value of power(2,5) is 32

Chapter 1 - Part B


Function power m n

function power(m,n)

A function definition has this form:

return-type function-name(parameter declarations, if any)

{

declarations

Statements

return expression;

}

Chapter 1 - Part B


How to call a function

How to Call a Function

Chapter 1 - Part B


1 9 character arrays

1.9 Character Arrays

  • The most common type of array in C is the array of characters

  • write a program that reads a set of text lines and prints the longest

Chapter 1 - Part B


Engineering computing i

Chapter 1 - Part B


Getline read a line into s return length

getline: read a line into s, return length

Chapter 1 - Part B


Copy copy from into to assume to is big enough

Copy : copy ’from’ into ’to’; assume ‘to’ is big enough

Chapter 1 - Part B


How strings are stored

How Strings Are Stored!

"hello\n"

Chapter 1 - Part B


  • Login