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

26
Introduction Chapter 1 1/22/16

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.

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

Introduction

Chapter 11/22/16

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

Check zyBooks Completion

Click on the boxes for each section.

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

Assignment• See my website for the new assignment.

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

Today's Topics

OutputParts of a C ProgramCommentsInputAlgorithmsErrors and Warnings

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

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.

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

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");

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

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

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

#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;}

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

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

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

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.

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

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

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

return Statement

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

operating system.

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

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.

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

Input

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

Input Makes Program Useful Programs so far do one calculation

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

doginput.c

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

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.

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

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.

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

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.

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

#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;}

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

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.

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

Questions

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

programs that have input or output?

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

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.

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

Errors and Warnings

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

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;

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

Errors and Warnings

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

wrong. Try to get rid of it.

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

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.