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

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

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

ET156 Introduction to C Programming

C: How to Program. Week /Mar/05

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Chapter 2 - Introduction to C Programming

Presented By : Gaurav Juneja

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Programming. C++ Basics

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

Fundamentals of Programming

Chapter 1 & 2 Introduction to C Language

Chapter 2: Overview of C. Problem Solving & Program Design in C

BLM2031 Structured Programming. Zeyneb KURT

Fundamental of Programming (C)

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

Data Types and Variables in C language

6.096 Introduction to C++ January (IAP) 2009

Introduction to Computing Lecture 01: Introduction to C

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

CMPE-013/L. Introduction to C Programming

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Procedures, Parameters, Values and Variables. Steven R. Bagley

COMP s1 Lecture 1

C Language, Token, Keywords, Constant, variable

Programming and Data Structures

Syntax and Variables

Programming in C++ 4. The lexical basis of C++

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

A Fast Review of C Essentials Part I

PROGRAMMAZIONE I A.A. 2018/2019

DEPARTMENT OF MATHS, MJ COLLEGE

CS Programming In C

CC112 Structured Programming

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

ET156 Introduction to C Programming

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

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Introduction to the C Programming Language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

ANSI C Programming Simple Programs

AMCAT Automata Coding Sample Questions And Answers

Tokens, Expressions and Control Structures

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

BASIC ELEMENTS OF A COMPUTER PROGRAM

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

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

Lecture 2: C Programming Basic

Creating a C++ Program

VARIABLES AND CONSTANTS

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Programming in C and C++

High Performance Computing

These are reserved words of the C language. For example int, float, if, else, for, while etc.

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

C-Programming. Claude Fuhrer 5 août 2017

Creating, Compiling and Executing

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

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

Lecture 02 C FUNDAMENTALS

C Programming Review CSC 4320/6320

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Storage class and Scope:

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

CS 61C: Great Ideas in Computer Architecture Introduction to C

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

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

H.O.#2 Fall 2015 Gary Chan. Overview of C++ Programming

Computer System and programming in C

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

CpSc 1111 Lab 4 Formatting and Flow Control

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

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

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

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

CMSC 246 Systems Programming

Programming in C and C++

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

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

CSE / ENGR 142 Programming I

Course Information and Introduction

Lecture 3. More About C

Basic Elements of C. Staff Incharge: S.Sasirekha

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Transcription:

Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify t he name of the executabl e. For example, gcc hello.c o hello c. main () used in the book is the same as int main(void) we used in class in the hello_comment.c a. Single line comment b. Multiple line comment c. Where comments should be added i. At the very beginning of the program ii. Variable declaration iii. Function declaration and implementation iv. Non-trivial (small) block of statements v. Code segment added for testing/debugging purpose 4. functions and variables: two components that make a program 5. Variables a. Variable declaration: <type variable_name;> b. Basic data types in C: char, int, float, double c. Variable name: i. Any letter, digits, and _ (underscore) can be in a variable name. ii. The first character must be letter or _. iii. Variable name should have less than 31 characters. iv. Variable names are case sensitive. v. The following keywords are reserved: auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while d. Tips on naming variables: i. Give meaningful names, but not too long (you need to type them) radius, color, exam_1, proj_2, StudentID.

ii. Use upper case letters and _ (occasionally, e.g., between words) to increase readability abadexofvarname, AGoodVarNameEx, a_good_var_name_ex. iii. Add a short comment: float radius; // radius of the circle, read from input iv. Assign safe initial values if possible. float sum = 0; // the sum of my three exam scores int max = 0; // the maximum among some integers. Note that this e. Assign values to variables: 6. Define constants: // this initial value is not safe. i. Direct assignment: radius = 1.5; ii. Declare variable and assign initial value: float sum = 0; iii. Result of some computation: area = 3.14 * radius * radius; iv. Read in from input (see below): scanf() a. Why we need to declare constant as a variable (instead of simply using its value)? b. Two ways to define a constant variable in C: const float PI = 3.14159; const int SIZE = 85; #define PI 3.14159 #define SIZE 85 c. Pay attention to the differences between these two constant variable declarations. 7. Basics on printf() and scanf(): 8. circle.c a. Print out a text message: printf( Hello, world! ): b. Read in a value scanf( %f, &radius); c. Print out the value of a variable printf( %f, radius); d. %: format indicator. It tells functions such as scanf() and printf() what is the data type of the variable to be expected. %f for variable of type float and double %d or %i for variable of type int (d stands for decimal integer) %c for variable of type char

Lab Description 1. Objectives: a. Write very simple programs to practice variable declaration, reading input from standard keyboard and performing simple arithmetic operations.. b. Get familiar with UNIX and its text editors (vi and/or emacs). 2. Pre-lab preparations: Reading assignments are for our textbook (Applied C, Fischer et al.) a. Reading: Chapters 1 and 2 and Sections 3.1 through 3.5 of Chapter 3 b. Read the codes posted for week ; namely, bugs.c and circle.c 3. In-lab description:

Name: Section: 010_ Date: Lab Report 1. Debug the following code, first by observation and then verify it on the computer (by modifying the code, compiling it, and executing it). Enumerate the bugs that you find and correct them. You can correct on the original code. (These code are posted in the blackboard, bugs.c in the documentation section, for your convenience. Remember to comment out unnecessary parts and check file name.) a. include <stdio.h> int main(void) {printf(wednesday\nsep. 8, 2010\n"); return 0; Bugs that you find in the program: b. #include <stdio.h> int main(void) {print(wednesday\nsep. 8, 2010\n"); return 0, c. #include printf( "Wednesday /n " ); printf( "Sep. 8, 2010/n"); return 0;

d. #Include <stdio.h> Int Main() { Printf("Wednesday \n"); Printf("September 8, 2010\n"); Return 0; e. # include <stdio.h> Int main (void) { printf( "Wednesday /n") printf("sep. 8, 2010 /n"); return 0; Total number of bug(s) that you have missed from codes a-e: 2. Where do you expect the following three statements on variable declarations and what are the differences among them? 1) float PI_1 = 3.1416; 2) const float PI_2 = 3.1416; 3) #define PI_3 3.1416

Read the file comment.txt in the documents section on blackboard. Think about how you would like to comment one line, multiple lines, etc. and use your style consistently in the programs for questions 3-5 (and hopefully for all your programs in the future. Of course, your style might change from time to time as you learn more and more in programming.) 3. Write a complete C-code (including the documentation) which asks the user to input the length and width of a rectangle, and outputs its area and perimeter, assume both length and width are integers. 4. Write a complete C-code (including the documentation) to compute a student's average grade over 3 projects. The individual project scores range from 0.0 to 100.0. 5. Modify the hello_comment.c code so it asks the user which section he/she is in and then prints out the following : (e.g. on input 0101) (note, it will be a little tricky to print out the first 0 in 0101. But there are a couple of ways to do that. ) Hello, welcome to ENEE 140 section 0101.