\documentclass{beamer} %include lhs2TeX.fmt \author{Piyush P Kurur\\ Office no: 224\\ Dept. of Comp. Sci. and Engg.\\ IIT Kanpur} \newcommand{\Token}[2][]{\node(#2#1)[token]{#2};} \newcommand{\Symbol}[2][]{\node(#2#1)[symbol]{#2};} \newcommand{\Point}[1]{\node(#1)[point]{#1};} \title{Fundamentals of Computing: Lecture 6} \date{August 7, 2009} \begin{document} \begin{frame} \maketitle \end{frame} \begin{frame} \frametitle{Summary of previous lecture: Statement} \begin{itemize} \item Simple statemets are expressions followed by |;| \item Compound statements are a sequence of statements enclosed in a pair of braces. \item Conditionals; |if| and |if else| statements. \item Loops; while loop and for loop. \end{itemize} \end{frame} \begin{frame} \frametitle{Arrays} \begin{block}{Declaration} type variable[integer]; type variable[]={values}; \end{block} Eg, \begin{spec} int x[100]; double mass[100]; int u[] = {10,11,12,13,14} \end{spec} \end{frame} \begin{frame} \frametitle{Character type} \begin{spec} char foo = 'A'; char foo = '\65'; char foo = 65; \end{spec} \pause \begin{block}{Character escapes} \begin{itemize} \item |'\n'| is new line, \pause \item |'\t'| is tab, \pause \item |'\65'| is the character with ascii value 65, \pause \item |'\\'| is a back slash character. \end{itemize} \end{block} \end{frame} \begin{frame} \frametitle{Strings} \begin{itemize} \item Strings are character array. \item Strings are terminated with a null character |'\0'|. \end{itemize} \begin{spec} char mesg[] = { 'h','e','l','l','o','\0'} char mesg[] = "hello"; \end{spec} \end{frame} \begin{frame} \frametitle{The |printf| and |scanf| function} \begin{spec} printf(fmtstring,e_1,e_2...e_n); scanf(fmtstring,&v1,&v2,...,&v_n); \end{spec} Eg. \begin{code} #include int main() { double h,w; double area,per; printf("Enter the height and width: "); scanf("%g %g",h,w); area = h * w; per = 2 * (h + w); printf("For a %g x %g rectangle\n\t",h,w); printf("Area = %g\n\tPerimeter = %g\n", area, per); } \end{code} \end{frame} \begin{frame} \frametitle{Format specifier for |printf| and |scanf|} \begin{itemize} \item All formating specifier is a |%| followed by a character, \item To print a literal |%| use |%%|, \item Depending on what follows |%| we have the following meanings \begin{tabular}{ll} |d|, |x|, |o| & integer in decimal, hex or octal\\ |f| & float\\ |g| & generic floating point, i.e. float or double.\\ |c| & character \end{tabular} \end{itemize} For more formating characters check out manpage of scanf. \end{frame} \end{document} \begin{frame} \frametitle{How to design loops?} \begin{definition} An invarient of a loop is a condition that is true always during the execution of the loop. \end{definition} Eg. \begin{spec} int sum = 0; int i = 0; while( i < n) { i++; sum += i; } \end{spec} A nice invarient is $sum = \sum_{j=0}^i i$. \end{frame}