Input and Output in C - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Input and Output in C

Description:

You have seen the programming environment of C in our last meeting. ... text ampersand(&) comma(,) double quotes(') variable conversion specifier(%f) ... – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 25
Provided by: wind402
Category:

less

Transcript and Presenter's Notes

Title: Input and Output in C


1
Input and Output in C
  • By
  • Shiela Marie B. Pelayo

2
You have seen the programming environment of C in
our last meeting. I am very sure your questions
are How can information be displayed on the
monitor? How can information or values be
manipulated in Turbo C? Lets first see if
you get the correct vibes about the program that
you have seen in our last meeting by answering
the anticipation guide in the next slide.
3
Directions The class shall have 3 members per
group. The group shall answer the anticipation
guide. On the space in front of each of the
numbers, place an X" to indicates where you
stand in regard to the statement that
follows Agree Disagree _____ ______ 1.
printf() and scanf() use format
conversion
specifiers _____ ______ 2.scanf() is used to
scan text or picture ______ ______ 3.
printf() displays text or information on the
monitor ______ ______4. C language has print
and input
statements ______ ______5. C language has
printf and scanf
functions Be ready to share your answers to
the class.
4
Hands-on1
Note Do not panic when you see error messages.
Follow the directions and answer the questions
after the program. I designed the activities that
way to emphasize important points.
How to debug errors Error1 Unable to create
file noname00.cpp -Save the program properly in
the labserve folder. Dont forget to change
directory. Error2 Statement missing -Check
all the C statements. Put a semi-colon at the end
of the line/s. Error3 Declaration syntax
error -Put a semi-colon at the end of the
variable declaration statement
5
You have different answers in the anticipation
guide. Lets now find out which answers are
correct by doing the hands-on activities. The
PrintF Activity ? 1. Make the subfolder named
PRINTF in the 3rd term folder. 2. Click Start
button and select Turbo C IDE 3.After viewing
TurboC window, click File menu and select
New. 4.Type the code below in the coding area
includeltstdio.hgt includeltconio.hgt void
main() print("Welcome to C programming.") getch
() 5. Click Compile Menu and select Compile
or simply press AltF9.
6

Questions Did you see errors on the monitor?
__________________ How are you going to debug
the code?________________ (Tip Check the line in
blue color. Edit it.) What is the output?
_____________________________ 5. View the output
by pressing CtrlF9 or select the Run command
in the Run menu 6. Save the file as print.cpp
in the PrintF subfolder. 7. Make an exe file.
Press F9.
7
The ScanF Activity ? 1. Make the subfolder
named SCANF in the 3rd term folder. 2. Type the
code below in the coding area
includeltstdio.hgt includeltconio.hgt void
main() char fname printf("Please enter your
first name") scan("s",fname) printf("Welcome
to C s", fname) getch() 3. Click
Compile Menu and select Compile or simply press
AltF9.
8
Questions Did you see errors on the monitor?
__________________ How are you going to debug
the code?________________ (Tip Check the line in
blue color. Edit it.) What is the output?
_____________________________ 4. View the output
by pressing CtrlF9 or select the Run command in
Run menu. 5. Save the file as scan.cpp in the
SCANF subfolder 6. Make an exe file. Press
F9. Are things getting clearer now? Do you have
an idea now on what statement you are going to
use to get and display information on the
monitor? To give you more ideas on how these
things are done, please go to the next
slide.
9
What's up
You have seen simple examples of C programs and
their output. You might be wondering now how
strings are displayed on the monitor and how
computer get information from the user in C. To
satisfy your curiosity, it is a must to study
first the basic functions that are used in
displaying and getting information from the
user. 1. Please open the link http//www.geocitie
s.com/learnprogramming123/Clesson4Beginner.htm 2.
Check/read the function of printf(), scanf(),
and be familiar with the conversion
specifiers. 3. Answer the questions in the next
slide. Type your answers on the space provided
for each number.
10
3.1 What is the function of printf()? 3.2 What
is the function of scanf()? 3.3 What are the
different conversion specifiers? Be ready to
share your answers to the class. Aside from
printf(), scanf(), and conversion specifiers, you
have to know also the different relational and
logical operators, and the types of identifiers
in order to make a better C program. Lets
check them in the next slide.
11
Cs data types
Variable Type Keyword Range
Character char -128 to 127
Integer int -32768 to 32767
Long Integer long -2,147,483,648 to 2,147,483,647
Single-precision floating pt float 1.2E-38 to 3.4E381
Double-precision floating pt double 2.2E-308 to 1.8E3082
12
Arithmetic Operators
Operator Action
Addition
- Subtraction
Multiplication
/ Division
Modulus Operation
Exponentiation
Increment
-- Decrement
  • Examples
  • 10 31 how? 10/33 remainder 1
  • S is the same as SS1
  • S-- is the same as SS-1

13
Identifiers Identifiers are names that are used
to referenced variables, functions, labels and
user- defined objects. They consist of a letter
or an underscore followed by any combinations of
letters, digits or underscores. They can be as
long as 127 characters. C has standard
identifiers and user-defined identifiers. User-Def
ined Identifiers start with a letter and may
contain any combination of letters and digits. It
can be of any length but only the first eight
characters will be recognized. Ex sum, num1,
ave Examples of Standard Identifiers are
printf(), scanf(),getch().
14
  • The many things that you have read are the basic
    concepts in Turbo C. They will make your
    programming activities easier.
  • Let us see if you understand the lesson on
    identifiers by answering the exercises below.
  • Syntax Rules and Valid Identifiers
  • An identifier must consist of letters, digits
    and underscores only.
  • An identifier must not begin with a digit.
  • A C reserved word cannot be used as an
    identifier.
  • An identifier defined in a C library must not be
    redefined.
  • Directions Classify the identifiers. Type V on
    the blank if the identifier is valid otherwise,
    type I.
  • ___ 1. quiz ___ 7. 2answer
  • ___ 2. quiz2 ___ 8. username
  • ___ 3. quiz 3
  • ___ 4. sum_1
  • ___ 5. quiz1
  • ___ 6. main

15
Hands-on2
  • I am sure you also want to see mathematical
    computation in Turbo C. All you need to do is to
    remember the syntax of the statements and apply
    math concepts. Lets do them now.
  • The program is intended to ask the user 2
    numbers. After entering 2 numbers, the program
    shall compute and display the sum.
  • The Sum Activity
  • Make the subfolder named SUM in the 3rd term
    folder.
  • 2. Click File menu and select New.
  • 3. Type the code below in the coding window
  • / This program shall compute the sum of two
    numbers. (The symbols, asterisk and slash, are
    used for comments.) /
  • includeltstdio.hgt
  • includeltconio.hgt
  • void main()

16
int num1,num2,sum printf("Input your first
number") scanf("d", num1) printf("\nInput
your second number") scanf("d",
num2) sumnum1num2 /The sum of the values
of num1 and num2 shall be saved in the
variable sum./ printf("\nThe sum of d and d is
d., num1,num2,sum) getch() 4. Click
Compile Menu and select Compile or simply press
AltF9. 5. View the output by pressing CtrlF9 or
select the Run command in Run menu. 6. Save the
program as sum.cpp in the Sum subfolder. 7.Press
F9 to make an EXE file. 8. Update your index.
17
  • \n is an escape sequence. It is called the
    newline character, and it means, move to the
    start of the next line.
  • Comprehension Questions
  • Diretions Please answer the questions below as a
    group. Type the answers on the space provided
    after each question.
  • What are the variables in the program?
  • 2. What is the data type of the variables?
  • 3. What is the format modifier/conversion
    specifier for int data type?
  • 4. What mathematical operator is used in the
    program?
  • 5. Why does the statement, printf("\nThe sum of
    d and d is d., num1,num2,sum) contain 3
    d?

18
What you have seen is just a simple example of
solving mathematical problem in Turbo C. I am
sure making your own Turbo C program sounds
challenging to you. Its time to show your
programming skills by doing the average
program. You always want to know the average
that you get from examinations. Instead of using
your calculator in getting the average, all you
need to do is to make a simple program in Turbo
C. Lets read the details in the next slide.
19
Hands-on3
  • Make the subfolder named AVERAGE in the 3rd term
    folder.
  • Read the programming problem below
  • Make a C program that shall get 3 quiz grades
    from the user. The program should display the
    quiz grades as well as the average.
  • Sample output
  • Please enter quiz1 grade 85
  • Please enter quiz1 grade 90
  • Please enter quiz1 grade 88
  • You have entered 85, 90 and 88.
  • The average is 87.67.
  • 3. Save the program as average.cpp in the AVERAGE
    subfolder. Make an EXE file by pressing F9.

20
  • Before you make the program, take note of the
    following
  • The variable for average must use the f
    conversion specifier.
  • ex scanf(f, ave)
  • ave is the variable used for average here.
  • 2. The number of decimal places can be formatted
    in printf().
  • ex printf(The answer is .3f ., answer)
  • output The answer is 123.456.
  • The value of the variable answer was formatted
    in three decimal places using .3f .

21
Once the program is finished, get 1/8 sheet cross
wise. Write your name, section, date , and title
of the activity which is Average. Evaluate the
output using the rating scale below 10/10 The
output is correct. All requirements are
present. 6/10 The program has a minor
error. 4/10 The program has many errors. No
output was seen in the run mode.
22
  • Assignment
  • Write your answers in ½ cross wise of paper.
  • Do the flowchart of The Average Activity.
  • 2.Write a C program that shall ask for a radius
    in centimeter(cm) from the user. The program must
    compute the area of the circle.
  • Output
  • Please enter a radius in cm 25 (enter)
  • The area of the circle is 1963.5 sq cm
  • 3. Please study the sample C programs and the
    Introduction to C lesson. Seatwork2 shall be
    given next meeting.

23
Let's review
Which of the following are needed by printf() and
scanf()? text ampersand() comma(,) double
quotes() variable conversion
specifier(f) Type your answers in the correct
boxes.
Printf()
Scanf()
Be ready to share your answers to the class.
24
Valuing
  • Please answer the question below in ¼ sheet of
    pad paper. Use 2 to 3 statements only.
  • Inputs can be your preparations to achieve the
    desired outputs. Outputs can be your goals or
    dreams in life.
  • As a student, what inputs must be done to
    achieve the desired outputs in life?
  • 2. Submit the paper to the teacher.
  • 3. Be ready to share your answers to the
    class.
Write a Comment
User Comments (0)
About PowerShow.com