Visual Studio. Visual Studio is an extremely complex interactive development environment capable of handling many languages and tools.

Similar documents
Functions. Systems Programming Concepts

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Chapter 5 C Functions

Fundamentals of Programming Session 8

بسم اهلل الرمحن الرحيم

C Functions. 5.2 Program Modules in C

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Subject: Fundamental of Computer Programming 2068

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Multiple Choice Questions ( 1 mark)

Program Organization and Comments

from Appendix B: Some C Essentials

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

Tutorial No. 2 - Solution (Overview of C)

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Chapter 1 Getting Started Structured Programming 1

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 5. Playing with Data Modifiers and Math Functions Getting Controls

Dynamic Memory Allocation

C Programs: Simple Statements and Expressions

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

The Graphing Calculator

Assoc. Prof. Dr. Tansu FİLİK

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

Pointers and scanf() Steven R. Bagley

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

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

1001ICT Introduction To Programming Lecture Notes

Unit 7. Functions. Need of User Defined Functions

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

2. Numbers In, Numbers Out

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

C introduction: part 1

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

C Programming Lecture V

Computers Programming Course 5. Iulian Năstac

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

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

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

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

Programming in C. Part 1: Introduction

IT 374 C# and Applications/ IT695 C# Data Structures

Programming and Data Structure

CSE123. Program Design and Modular Programming Functions 1-1

Introduction to C Language

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

2. Numbers In, Numbers Out

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

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

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

C: How to Program. Week /Mar/05

As stated earlier, the declaration

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

The Hyderabad Public School, Begumpet, Hyderabad, A.P

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

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

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

Precedence and Associativity Table. % specifiers in ANSI C: String Control Codes:

ECET 264 C Programming Language with Applications. C Program Control

Fundamentals of Programming Session 4

Strings and Library Functions

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

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

BSM540 Basics of C Language

Worksheet 4 Basic Input functions and Mathematical Operators

Basics of Programming

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

JTSK Programming in C II C-Lab II. Lecture 3 & 4

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

CS16 Exam #1 7/17/ Minutes 100 Points total

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

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

Programming Assignment #4 Arrays and Pointers

Fundamentals of Programming & Procedural Programming

Computers Programming Course 6. Iulian Năstac

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

AMCAT Automata Coding Sample Questions And Answers

C Programming Multiple. Choice

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Chapter 8 Character Arrays and Strings

ALGORITHM 2-1 Solution for Exercise 4

Programming for Engineers Introduction to C

CSE / ENGR 142 Programming I

Computer Programming in MATLAB

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Topic 6: A Quick Intro To C

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

The Very Basics of the R Interpreter

Fundamentals of Programming & Procedural Programming

Transcription:

Tips and Tricks

Topics Visual Studio Handling Output to the Screen Handling Input from the Keyboard Simple Text Menus When to use a For Loop When to use a While Loop Reviewing a Function

Visual Studio Visual Studio is an extremely complex interactive development environment capable of handling many languages and tools.

How To Get Visual Studio? 1. Visual Studio is available to students at no charge through the DreamSpark program. Go to the link https://www.dreamspark.com/ 2. Create your student account with university email id and login. 3. Go to software catalog and download Visual Studio Community 2015.

Visual Studio Deprecation-as languages develop and are updated over time, some commands are called deprecated, meaning they are not commonly supported. ANSI C is the root form of many versions of C, and some of its commands are deprecated in Visual Studio.

Visual Studio Visual Studio will create a Warning or Error for commands such as Scanf because of deprecation. The textbook, however, still uses these commands to instruct in the original ANSI C

Visual Studio To switch off the Deprecation Warnings in Visual Studio, a line must be added at the very beginning of the program. #define _CRT_SECURE_NO_DEPRECATE

Visual Studio Another feature in Visual Studio is the reporting of errors in the output box.

Visual Studio

Visual Studio Clicking the error message in the output box usually causes the cursor to jump to the location of the error. An easier approach may be to look at the line numbers in the window. However, the line numbers may need to be turned on.

Turn on Line Numbers First, go to Tools on the Main Menu Bar. The Main Menu is always the menu bar at the top of a program

Visual Studio

Visual Studio Next, click Options at the bottom of the menu.

Visual Studio

Visual Studio Next the Options Dialog Box will appear. On the menu tree on the left, look for the words Text Editor and expand the options by selecting the + sign next to the entry.

Visual Studio

Visual Studio Once expanded, many options will appear. Select the C/C++ entry on the tree. On the right side of the dialog box, several options will appear. Click the Line Number option. Click OK

Visual Studio

The Include Statement Reviewing from Chapter 1, the Include statement is useful. #include allows the program to have access to the header files of the system libraries.

Include #include <stdio.h> allows access to the main input and output routines, scanf printf getchar

Include Another useful library is the console input output library. #include <conio.h> _kbhit(); _getch();

Include #include <stdlib.h> allows access to the main library, which contains memory controls Malloc-Allocate a single memory area Calloc-Allocate a set of memory areas

Include #include <math.h> allows access to math routines, such as sin-sine cos-cosine tan-tangent asin-arcsine acos-arccosine atan-arctangent pow-power, x to the power of y sqrt-square Root

The Printf Statement All printf statements use a format string in order to display the output. A format string works by two conditions first, the type of data to be displayed second, the order of the data to be displayed.

The Printf Statement Each type of data has different combinations that can alter how it looks. Formats differ for Integers and Floats

The Printf Statement For Example, an integer can be shown as: %d-simplest display %10d-At least 10 characters are shown, the remainder are spaces %010d, at least 10 characters are shown, the remainder are 0 s %+d, The sign of the integer will always be visible. And these can be combined.

The printf Statement For example, a float can be shown %f just prints the float as is. %6.3f-prints the float in 6 characters, with three characters reserved for the decimal portion. %+6.3f-uses one of the six characters for the sign, and 3 characters for the decimal %016.3f-As with integer, the leading spaces are replaced with zeros As before, these can be combined

Scanf Many times, when Scanf is used, the data terminates at the first non-numeric symbol, such as a blank or the enter key. But the enter key and the space can also be a number.

Scanf One method is to use strings, but that will not be covered until Chapter 5. Another method is to use the following statement fflush(stdin); After a scanf statement is called.

Scanf For example scanf( %c,&mychar); fflush(stdin); will read a single character from the keyboard, and remove any hit enter that is at the end of the input. This can also be used with %d for integers and %f for floats

User Wait Many times, perhaps, it would be best if the program would halt until it is ready to end. This waiting can be readily done by using while loops (chapter 2) combined with various input commands.

User Wait One simple version of user wait is while( q!=getchar()); Which will wait until the lower case q is entered on the keyboard. This requires #include<stdio.h>

User Wait A second form of user wait requires #include <conio.h> And looks something like this printf("press any key to Continue\n"); while(!_kbhit()); _getch();

EOF EOF is a keyword in C, representing End of File, which is a -1. On the keyboard, the EOF would be the <CTRL> key followed by the <Z> key.

Simple Menu Example void SimpleMenu() { int MenuFlag=1; char KeyStroke='\0';

Simple Menu Example while (MenuFlag) { printf("1. Item 1\n"); printf("2. Item 2\n"); printf("3. Item 3\n"); printf("q. Quit\n");

Simple Menu Example scanf("%c",&keystroke); fflush(stdin);

Simple Menu Example if(keystroke=='1') printf("option 1\n"); else if (KeyStroke=='2') printf("option 2\n"); else if (KeyStroke=='3') printf("option 3\n"); else if (KeyStroke=='q') MenuFlag=0; //While Loop will end //after this line executes. else printf("unrecoginized Keystroke\n");

Simple Menu Example }//End of While Loop }//End of Function

Why do we use a loop? A loop is used whenever a task has to be repeated multiple times.

What is a loop? A loop is a special statement in C for controlling a section of code. for loops and while loops

How do we create a loop? A while loop is the simplest form of a loop. A while loop repeats the following statement or statement block so long as its expression is true A while loop requires a condition for the expression int WhileFlag=1; char Input='0'; printf("begin While Loop\n"); printf("enter \'x\' to continue\n"); while(whileflag) { Input=getchar(); if(input=='x') WhileFlag=0; } printf("end While Loop\n\n"); }

How do we create a loop? int WhileFlag=1; char Input='0'; printf("begin While Loop\n"); printf("enter \'x\' to continue\n"); while(whileflag) { Input=getchar(); if(input=='x') WhileFlag=0; } printf("end While Loop\n\n"); } Begin While Loop Enter 'x' to continue x End While Loop

How do we create a loop? A for loop contains all the conditions of its execution within its use. A for loop repeats the following statement or statement block so long as its expression is true. A for loop expression will be changed by either the for loop statement, or the following block. int LoopControl=0; int count=0; printf("begin For Loop\n"); for (LoopControl=0; LoopControl<10; LoopControl++) { printf("loopcontrol is %d,count is %d\n",loopcontrol,count); count=count+1; LoopControl=LoopControl+1; printf("loopcontrol is %d,count is %d\n",loopcontrol,count); } printf("count is %d\n",count); printf("end For Loop\n\n");

How do we create a loop? int LoopControl=0; int count=0; printf("begin For Loop\n"); for (LoopControl=0; LoopControl<10; LoopControl++) { printf("loopcontrol is %d,count is %d\n",loopcontrol,count); count=count+1; LoopControl=LoopControl+1; printf("loopcontrol is %d,count is %d\n",loopcontrol,count); } printf("count is %d\n",count); printf("end For Loop\n\n"); Begin For Loop LoopControl is 0,Count is 0 LoopControl is 1,Count is 1 LoopControl is 2,Count is 1 LoopControl is 3,Count is 2 LoopControl is 4,Count is 2 LoopControl is 5,Count is 3 LoopControl is 6,Count is 3 LoopControl is 7,Count is 4 LoopControl is 8,Count is 4 LoopControl is 9,Count is 5 Count is 5 End For Loop

When do we use a loop? A key question that needs to be asked is when do you need to use a loop, and what kind? If a program contains something that must repeat many times, a loop is used. If a program contains something that must repeat many times, and you do not know how many times, a while loop is used. If a program contains something that must repeat many times, and you know exactly how many times, then a for loop is used.

When do we use a loop? Input 20 integers and say if they are odd or even Does it contain a repetition? Yes Is it a fixed size? Yes Does it contain a condition? No What type of loop would be best? For Loop

When do we use a loop? Input a series of floats and find the sum of them all until the user says to stop. Does it contain a repetition? Yes Is it a fixed size? No Does it contain a condition? Yes What type of loop would be best? While Loop

When do we use a loop? Input a series of characters and print them out to the screen Does it contain a repetition? Yes Is it a fixed size? No Does it contain a condition? No What type of loop would be best? Neither, While Loop or For Loop would work, but a while loop may be a slightly better choice

What is a Program A Program is a series of instructions entered into a computer system to perform a task.

What is a Function? A function is a program within a program, that should perform some sub-task of the program.

Prototypes, Function Declaration, Parameters, Return Types <return type> function Identifier (<Parameter1, parameter2,...parameter n); The return type is a valid data type, for example an int, char, or float. The parameters are variable declarations, for example int x, char y, and float z. For a prototype, a semicolon is added to the end of the function header. An empty parameter list is either blank or of type void.

Calling a Function float Halve(float p) { float result=0.0; result=p*0.5; return result; } void main() { float value=0.0; float half=0.0; value=5.0; half=halve(value); printf( %f\n,half); }

Behavior of the calling of a function (half=result) (p=value) void main() float Halve(float p)