Introduction Chapter 1 1/22/16. Check zyBooks Completion Click on the boxes for each section.

Post on 18-Jan-2018

217 views 0 download

description

Assignment See my website for the new assignment.

Transcript of Introduction Chapter 1 1/22/16. Check zyBooks Completion Click on the boxes for each section.

Introduction

Chapter 11/22/16

Check zyBooks Completion

Click on the boxes for each section.

Assignment• See my website for the new assignment.

Today's Topics

OutputParts of a C ProgramCommentsInputAlgorithmsErrors and Warnings

Outputprintf function is used to display output.Must include the i/o library in the program. #include <stdio.h>Now the printf function can be used.printf(“%d tons\n", 16);Output:16 tons%d is format for integers. Where tons is printed.

Displaying Text• \n is a new line character• If it is not there, the next output will

continue on the same line.

printf("Without Dennis Ritchie "); printf("there would be no\n"); printf("Steve Jobs.\n");

A Simple C ProgramA program is needed to figure out how many

pets we have, given the number of dogs and cats that includes. Write a program to set the number of cats and dogs. Output the number of pets.

The instructions in a program are called code.Here is the code:

Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

#include <stdio.h>//Program to add up the number of pets

int main(void){ int cats = 4; int dogs = 3;

int pets = cats + dogs;

printf("We have %d pets\n" , pets);

return 0;}

A Simple C Programmain function

• Where the execution begins. int main(void){ }• Block begins and ends with brace{ … } • Statements within block end with a ;

Tell processor what to do.

Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Assignment Statementint cats = 4;• int cats declares a variable.

o A variable is a memory location.o Memory is a place to store values temporarily.

• cat = 4; sets cats to 4.• int pets = cats + dogs;o Does the addition on the right first.o Assigns the answer to pets.

CommentsDocuments the programComments

• Begin line with double slash //Ends with the end of the line.

• Span multiple lines between slash-star combination./* . . . . . . */

Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

return Statement

return 0;• Ends the program and gives control back to

operating system.

Questions

• What happens when a “\n” is put in the output?• Where do the statements in a C program

belong?• Give an example of an assignment

statement.

Input

Input Makes Program Useful Programs so far do one calculation

Example in dogyrs.c More useful if you can input human_yrs

doginput.c

scanf

scanf("%d", &human_yrs);

scanf – function name“%d” - input format, same as printf

formats& -- “address of” operator&human_yrs – where data will be stored.

scanf

Execution pauses until user enters a value and hits <enter>

Value input stored in variable(s)Prompt is recommended

printf to tell user input is expected. I'll put one in doginput.c

• #include <stdio.h> makes scanf available.

Find the Number of People

Write a program to figure how many people are touring Yosemite Park today. Ask for the number of people one tour bus can hold and the number of buses in the park. Output the total number of passengers.

#include <stdio.h>

int main(void){//Ask for the number of people on each bus int peopleOnBus; printf("People on one bus?"); scanf("%d", &peopleOnBus);//Ask for the number of buses int noBuses; printf("Number of Buses?"); scanf("%d", &noBuses);//Find total number of people int people = peopleOnBus * noBuses;//Output total number of people printf("%d people\n", people);

return 0;}

Login and Password

If you don't have a login, send me email. Contact me if you forget your password. Change your password to something easy

to remember. ssh -Y onyx

Login again passwd

Nothing shows while you are typing.

Questions

• What statement is used to handle input?• What preprocessor directive must be in

programs that have input or output?

Number of Meals Needed

A coding conference provides three meals for each participant. Write a program to input the number of participants then output the total number of meals needed.

• Write an algorithm with a partner• Along with your partner, code the program

using vim.– Print it and hand in a copy with both

names on it.

Errors and Warnings

Errors

• Syntax Error Violation of programming language rules. Caught by compiler. Program will not compile.

• Logic Error Error while program runs. For example, incorrect computation.

distance = rate/time;

Errors and Warnings

• Error prevents program from compiling• Warning means something might be

wrong. Try to get rid of it.

Questions

What kind of error is it?1.The compiler prints an error message because a semicolon(;) is missing.2. A program to convert Fahrenheit to Celcius gives incorrect results.3. The program compiles but the compiler gives a message about a possible problem.