CSC 270 Survey of Programming Languages

Similar documents
BSM540 Basics of C Language

Introduction to Computer Programming

Introduction to Computer Programming

Fundamental of Programming (C)

CSE 303 Lecture 8. Intro to C programming

CSCI 2132 Software Development. Lecture 8: Introduction to C

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Chapter 11 Introduction to Programming in C

Comments. Comments: /* This is a comment */

CpSc 1111 Lab 4 Formatting and Flow Control

Fundamentals of Programming Session 4

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

printf( Please enter another number: ); scanf( %d, &num2);

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

What we have learned so far

Chapter 11 Introduction to Programming in C

Control Flow, Functions and Basic Linkage

Fundamentals of Programming

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C: How to Program. Week /Mar/05

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Chapter 1 & 2 Introduction to C Language

Fundamentals of Programming Session 14

Repetition Structures II

Chapter 11 Introduction to Programming in C

Hacking C Code - Local Machine

Chapter 2 - Introduction to C Programming

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark]

Chapter 11 Introduction to Programming in C

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Structured programming. Exercises 3

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Engineering 12 - Spring, 1999

Fundamental of Programming (C)

Chapter 11 Introduction to Programming in C

Data types, variables, constants

Chapter 11 Introduction to Programming in C

Programming and Data Structures

The C language. Introductory course #1

Array Lesson 1 Outline

Computers Programming Course 7. Iulian Năstac

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Chapter 5: Control Structures

Week 2 C Fundamentals; Formatted Input/Output; int and float Types

Use of scanf. scanf("%d", &number);

Topic 6: A Quick Intro To C

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Lecture 9 - C Functions

Arrays. Here is the generic syntax for an array declaration:

Structured programming

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

C Programming

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

BSM540 Basics of C Language

Programming for Electrical and Computer Engineers. Loops

Absolute Value. if Lesson 1 CS1313 Spring

Crude Video Game Simulator Algorithm

Formatted Input/Output

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Programming Language A

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

CC112 Structured Programming

Chapter 3 Structured Program Development

EL2310 Scientific Programming

Question 2. [2 points] Which of the following is a correct statement to obtain user input? (Assume that fleems is an int variable.

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Computer Programming: Skills & Concepts (CP) Variables and ints

Lecture 5: C programming

AMCAT Automata Coding Sample Questions And Answers

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Introduction to C. Systems Programming Concepts

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Computer Programming

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

while Loop Example #1

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

Introduction to Computer Programming

C Overview Fall 2015 Jinkyu Jeong

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

Computer Programming Lecture 14 Arrays (Part 2)

CS Spring 2018 Homework #5

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

C Programming Basics

WARM UP LESSONS BARE BASICS

For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -VARIABLE AND DATA TYPES- SPRING 2015, SEON-JU AHN, CNU EE

Transcription:

CSC 270 Survey of Programming Languages C Lecture 1 : Getting Started: in C #include int { A First Program <stdio.h> main(void) header makes input and output available to us printf("this is my first C program.\n"); open and close braces mark the beginning and end statements 1

A First Program What Does It Do? printf("this is my first C program.\n"); Prints the message This is my first C++ program. Ends the program Ends the line Identifier Rules An identifier must begin with a letter or an underscore _ C is case sensitive upper case (capital) or lower case letters are considered different characters. Average, average and AVERAGE are three different identifiers. Numbers can also appear after the first character. 2

Identifier Rules (continued) Identifiers can be as long as you want but names that are too long usually are too cumbersome. However, C only considers the first 6 (external identifiers) or first 31 (internal identifiers) significant. Identifiers cannot be reserved words (special words like int, main, etc.) Average3.cpp #include <stdio.h> int { main(void) int value1, value2, value3; float sum, average; printf("what is the first value? "); scanf("%d", &value1); Read printf("what is the second value? "); scanf("%d", &value2); Indicates that we are reading an integer 3

printf("what is the third value? "); scanf("%d", &value3); sum = value1 + value2 + value3; average = sum / 3; scanf needs the & before the identifier printf("average = %f\n", average); payroll.c #include <stdio.h> int { main(void) float rate, hours, gross; printf("what is your hourly pay rate? "); scanf("%f",&rate); printf("how many hours did you work? "); scanf("%f", &hours); 4

gross = rate * hours; printf("your gross pay is $%f\n", gross); Print a float value Comments In C, anything beginning with /* and ending with */ is considered a comment. 5

payroll.c #include <stdio.h> /* * This program calculates the gross pay for an * hourly worker. * Inputs - hourly pay rate and number of hours * worked * Output - Gross pay */ int main(void) { float rate, hours, gross; /* Get the hourly rate */ printf("what is your hourly pay rate? "); scanf("%f",&rate); /* Get the number of hours worked */ printf("how many hours did you work? "); scanf("%f", &hours); /* Calculate and display the gross pay */ gross = rate * hours; printf("your gross pay is $%f\n", gross); 6

Character Data All of our programs so far have used variables to store numbers, not words. We can store one or more characters by writing: char x, s[10]; x can hold one and only one character s can up to nine characters. For now, we use character data for input and output only. A program that uses a character variable #include <stdio.h> /* A very polite program that greets you by name */ int main(void) { char name[25]; /* Ask the user his/her name */ printf("what is your name? "); scanf("%s", &name); /* Greet the user */ printf("glad to meet you, %s\n.", name); 7

if and if-else (continued) The general form is: if (expression) statement; or if (expression) statement; else statement; IsItNeg.c #include <stdio.h> /* * Tell a user if a number is negative * or non-negative */ int main(void) { float number; /* Ask the user for a number */ printf("please enter a number? "); scanf("%f", &number); 8

// Print whether the number is negative or not if (number < 0) printf("%f is a negative number\n", number); else printf("%f is NOT a negative number\n", number); // Print the warning if appropriate if (speed > 55) { printf("**be CAREFUL!**"); printf("you are driving too fast!\n"); 9

Declaring Constants There are two ways of defining constants in C: using #define and const. #define is a compiler preprocessor which replaces each occurrence of the constant's name with its value: The general form of the constant declaration is: #define ConstantName ConstantValue Let's take a look at a few examples: #define withholding_rate 0.8 #define prompt 'y' #define answer "yes" #define maxpeople 15 #define inchperft 12 #define speed_limit 55 Declaring Constants The general form of the constant declaration is: const datatype ConstantName = ConstantValue, AnotherConstantName = AnotherConstantValue; Let's take a look at a few examples of constants: const float withholding_rate = 0.8; const char prompt = 'y', answer = "yes"; const int maxpeople = 15, inchperft = 12; speed_limit = 55; 10

Counting Loops We use a for loop to write counting loops In C, it looks like this: for (count = start; count <= finish; count++) statement or for (count = start; count <= finish; count++) { statements Counting Loops (continued) for (count = start; count <= finish; count++) statement variable used to count times through the loop initial value of the counter final value of the counter 11

HelloAgain.cpp #include <stdio.h> /* * Hello again - this is a better way to write * "Hello, again" five times */ int main(void) { int i; for (i = 1; i <= 5; i++) printf("hello, again\n"); The Interest Program #include <stdio.h> /* * Calculate the interest that the Canarsie * Indians could have accrued if they had * deposited the $24 in an bank account at * 5% interest. */ int main(void) { const int present = 2016; int year; const float rate = 0.05; float interest, principle; /* Set the initial principle at $24 */ principle = 24; 12

/* * For every year since 1625, add 5% interest * to the principle and print out * the principle */ for (year = 1625; year < present; year++) { interest = rate * principle; principle = principle + interest; printf("year = %d\tprinciple = %f\n", year, principle); Output from the Compound Interest Program What will our output look like? year = 1625 principle = 25.200001 year = 1626 principle = 26.460001 year = 1627 principle = 27.783001 year = 1628 principle = 29.172152 year = 2011 principle = 3806008832.000000 year = 2012 principle = 3996309248.000000 year = 2013 principle = 4196124672.000000 year = 2014 principle = 4405931008.000000 year = 2015 principle = 4626227712.000000 This does not look the way we expect monetary amounts to be written! 13

%d and %f The specifiers %d and %f allow a programmer to specify how many spaces a number will occupy and (in the case of float values) how many decimal places will be used. %nd will use at least n spaces to display the integer value in decimal (base 10) format. %w.df will use at least w spaces to display the value and will have exactly d decimal places. Changing the width Number 182 182 182 182-182 -182-182 Formatting %2d %3d %5d %7d %4d %5d %7d Print as: 182 182 ``182 ````182-182 `-182 ```-182 14

Changing the width (continued) Number 23 23 23 23 11023 11023-11023 -11023 Formatting %1d %2d %6d %8d %4d %6d %6d %10d Print as: 23 23.23 23 11023.11023-11023..-11023 Changing The Precision Number Formatting Prints as: 2.718281828 %8.5f `2.71828 2.718281828 %8.3f ```2.718 2.718281828 %8.2f ````2.72 2.718281828 %8.0f ````````3 2.718281828 %13.11f 2.71828182800 2.718281828 %13.12f 2.718281828000 15

The revised Compound program #include <stdio.h> /* * Calculate the interest that the Canarsie * Indians could have accrued if they had * deposited the $24 in an bank account at * 5% interest. */ int main(void) { const int present = 2000; int year; const float rate = 0.05; float interest, principle; /* Set the initial principle at $24 */ principle = 24; /* * For every year since 1625, add 5% * interest to the principle and print out * the principle */ for (year = 1625; year < present; year++) { interest = rate * principle; principle = principle + interest; printf("year = %d\tprinciple = %15.2f\n", year, principle); 16

for (year = 1625; year < present; year++) { interest = rate * principle; principle = principle + interest; printf("year = %d\tprinciple = %15.2f\n", year, principle); The output from the Revised Compound Program Our output now looks like this: year = 1625 principle = 25.20 year = 1626 principle = 26.46 year = 1627 principle = 37.78 year = 1628 principle = 29.17 year = 1996 principle = 1830755328.00 year = 1997 principle = 1922293120.00 year = 1998 principle = 2018407808.00 year = 1999 principle = 2119328256.00 17

While Loops The most common form of conditional loops are while loops. In C, they have the form: while (condition) statement; or while(condition) { statements keepasking.c #include <stdio.h> /* A simple example of how while works */ int main(void) { int number; /* Get your first number */ printf("hi there. Pick a positive" " integer >>"); scanf("%d", &number); 18

/* Keep reading number as long as they are positive */ while (number > 0) { printf("pick another positive" " integer>>"); scanf("%d", &number); printf("%d is not a positive integer\n", number); Magic Number Problem The magic number game involves guessing a number and with each wrong guess, the player is told too high or too low. The goal is to guess the number in the smallest number of tries. We need a method for having the computer pick a number at random for the player to guess. We will need to learn about how to use library functions to provide us with the magic number. 19

do.. while loops You may have noticed that we asked the user twice for same information - the number (s)he is guessing. Some loops really require that the condition be at the end - not at the beginning. In Java, we have the do.. while loop, whose syntax is: do { statement(s) (condition) The Magic Number Program The main loop in the magic number program is: do{ /* Let the user make a guess */ printf("guess: "); scanf("%d", &guess); /* Let the user make another guess */ if (guess > magic) printf(".. Wrong.. Too high\n\n"); else if (guess < magic printf(".. Wrong.. Too low\n\n"); tries++; while (guess!= magic); 20

exit() exit() allows the user to let a program terminate if the program detects an unrecoverable error. The statement #include <stdlib.h> has to be included. A non-zero status value should be returned when the program terminates abnormally. 21