
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  CSCE 150A -- Problem Solving Using Computers
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[pdftex]{beamer}

\input{beamerPreamble}
\usepackage[T1]{fontenc}
%\usepackage{tikz}\text
%\usetikzlibrary{shapes.callout}

\title[CSCE150A]{Computer Science \& Engineering 150A\\
    Problem Solving Using Computers -- Laboratory}
\subtitle{Lecture 15 - Reviews}

\author[~]{Shuai Xie\vskip 1\baselineskip (Adapted from Derrick Stolee, Lin Liu \& Shuai Xie )} %

\date{Spring 2010}
%\usepackage{listings}
\usepackage{multicol}
\begin{document}
%\lstset{language=C}
\begin{frame}
  \titlepage
  \begin{center}
     \vfill
     \email{sxie@cse.unl.edu}
  \end{center}
\end{frame}


%\section{Data Type}


\begin{frame} %-----------------------------------------------------------|
  \frametitle{\texttt{Variable}}
  \framesubtitle{~}

\vspace{1em}

\begin{itemize}
  \item no: *, \#, \&, \%, ...
  \item no: space
  \item cannot start with numbers
  \item no: reserved words, e.g. int double char
  \item ...
  \item Bo*ys, \#a, angle's, char

\end{itemize}

\end{frame} %-------------------------------------------------------------|




\begin{frame} %-----------------------------------------------------------|
  \frametitle{\texttt{Data Type}}
  \framesubtitle{~}

\begin{tabular}[h]{c|c|c|c}
	Var Name & Stores & Scanf & Printf \\
	\hline
	\texttt{int} & Integers & \%d & \%d \\
	\hline
	\texttt{double} & Decimals & \%lf & \%f \\
	\hline
	\texttt{char} & Letters & \%c & \%c \\
\end{tabular}

\vspace{1em}

\begin{itemize}
  \item char grade = 'A';
  \item \%10d \qquad \%7.2f
  \item Type cast
     \begin{itemize}
     \item int i=3, j=4;
     \item int k; k=i/j; (k=0)
     \item double l;
     \item l=(double)i/j (l=0.75);
     \end{itemize}
\end{itemize}

\end{frame} %-------------------------------------------------------------|

%\section{Operators}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{Some Operators}
  \framesubtitle{~}

 \begin{itemize}
 	\item Math operators: +\quad $-$\quad * \quad/ \quad\%(remainder)
 	\vskip 1\baselineskip
 	\item Relation and equality operators:\quad $<$ \quad $>$ \quad $<$$=$ \quad $>$$=$ \quad $=$$=$ \quad $!$$=$\\
 	\vskip 1\baselineskip
  \item logical operators: \quad $\&$$\&$ \quad $\mid$$\mid$ \quad $!$
  \vskip 1\baselineskip
 	\item Notes: order of operators;  using "( )". 	
 	


\end{itemize}
\vspace{2in}
\end{frame} %-------------------------------------------------------------|


%\section{Math}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{\texttt{Math}}
  \framesubtitle{~}

 \begin{itemize}
 \item Math function: abs(x),\quad fabs(x),\quad sin(x),\quad cos(x),\quad pow(x,y),\quad sqrt(x)
 \vskip 1\baselineskip
 \item \#include <math.h>
 \vskip 1\baselineskip
 \item Math expression: \\
  \vskip 1\baselineskip
 $\left|\frac{1}{\sqrt{1-x^2}}\right|$ $=>$ \texttt{fabs(1.0/sqrt(1-pow(x,2)))}\\
 \end{itemize}
\vspace{2in}
\end{frame} %-----------------------------------------------------------|





%\section{Loops}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{while loop}
  \framesubtitle{~}

  \texttt{int i=1, total=0;}
  \vskip 0.8\baselineskip
	\texttt{while ( i <=10 )}\\
	 $\{$ \\
	 \qquad \texttt{total = total + i;}\\
	 \qquad \texttt{i++;}\\
	  $\}$\\
\end{frame} %-------------------------------------------------------------|

%\section{do-while}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{do while loop}
  \framesubtitle{~}
\vskip 0.5\baselineskip
 \begin{itemize}
 \item Repetition: At least once
\vskip 0.5\baselineskip
\texttt{do}\\
\texttt{$\{$}\\
\qquad\texttt{executable1;}\\
\qquad\texttt{$\dots$}\\
\qquad{change variable in conditional statement;}\\
\texttt{$\}$ while ( check condition );}\\
\vskip 1\baselineskip

 \item Similar to while loop
 \item Execute loop body at least once
 \item Don't miss ";"
 \end{itemize}

\vspace{2in}
\end{frame} %-----------------------------------------------------------|



\begin{frame} %-----------------------------------------------------------|
  \frametitle{do while loop}
  \framesubtitle{~}

  \vskip 1\baselineskip	
  \texttt{int i=1, total=0;}\\
   \vskip 0.8\baselineskip
	\texttt{do}\\
	 $\{$ \\
	 \qquad \texttt{total = total + i;}\\
	 \qquad \texttt{i++;}\\
   $\}$ \texttt{while ( i >= 10 );}\\

\end{frame} %-------------------------------------------------------------|

\begin{frame} %-----------------------------------------------------------|
  \frametitle{for loop}
  \framesubtitle{~}

\vskip 1\baselineskip
	\texttt{for ( i = 1; i < 10; i++ )}\\
	$\{$ \\
	\qquad \texttt{total = total + i;}\\
	 $\}$\\
\vskip 1\baselineskip

\end{frame} %-------------------------------------------------------------|



\begin{frame} %-----------------------------------------------------------|
  \frametitle{Switch statement}
  \framesubtitle{~}

\footnotesize
\texttt{switch ( controlling\_variable ) }\\
$\{$\\
\quad \quad \texttt{ case 1:}  \\
\quad \quad \qquad \texttt{ executable group 1 ;}  \\
\quad \quad \qquad \texttt{ break ;}  \\
\quad \quad \texttt{ case 2:}  \\
\quad \quad \qquad \texttt{ executable group 2 ;}  \\
\quad \quad \qquad \texttt{ break ;}  \\
\quad \quad \qquad \texttt{ \dots}  \\
\vskip 0.5\baselineskip
\quad \quad \texttt{ default:}  \\
\quad \quad \qquad \texttt{ executable group last ;}  \\
$\}$\\

\begin{itemize}
\item \texttt{controlling\_variable: int, char}
\item \texttt{char variable:  'c' ;}
\item \texttt{break;}
\item \texttt{other cases $-$$-$ default :}
\end{itemize}

\vspace{2in}
\end{frame} %-------------------------------------------------------------|




\begin{frame} %-----------------------------------------------------------|
  \frametitle{switch}
  \framesubtitle{~}
  \small
\texttt{int windlevel ; }\\
\texttt{$\dots$}\\
  \texttt{switch ( windlevel ) }\\
$\{$\\
\quad \quad \texttt{ case 2:}  \\
\quad \quad \qquad \texttt{ printf("Level 2: Breeze") ;}  \\
\quad \quad \qquad \texttt{ break ;}  \\
\quad \quad \texttt{ case 7:}  \\
\quad \quad \qquad \texttt{ printf("Level 7: Strong wind") ;}  \\
\quad \quad \texttt{ case 10:}  \\
\quad \quad \qquad \texttt{ printf("Level 10: Storm wind") ;}  \\
\quad \quad \qquad \texttt{ break ;}  \\
\vskip 0.5\baselineskip
\quad \quad \texttt{ default:}  \\
\quad \quad \qquad \texttt{ printf("More detail see our website") ;}  \\
$\}$\\

\vspace{2in}
\end{frame} %-------------------------------------------------------------|




\begin{frame} %-----------------------------------------------------------|
  \frametitle{Function: \texttt{Prototype}}
  \framesubtitle{~}
\#include $<$stdio.h$>$\\
\vskip 0.4\baselineskip
\quad \texttt{void PrintAlarm(void);}\\
\quad \texttt{int Cube(int x);}\\
\quad \texttt{double EllipseArea(int x, int y);}\\
\vskip 0.4\baselineskip
int main(void)\\
$\{$ \dots
\vskip 0.5\baselineskip
\begin{itemize}
  \item \texttt{Between preprocessor directive and main function }
	\item \texttt{Returntype $-$ void, int, double, char, etc}
	\item \texttt{Argument $-$ void, single, multiple}
	\item \texttt{";"}
\end{itemize}
\end{frame} %-------------------------------------------------------------|


\begin{frame} %-----------------------------------------------------------|
  \frametitle{Function: \texttt{Definition}}
  \framesubtitle{~}

\texttt{ReturnType FuncName(type Arg 1,\dots)}\\
$\{$\\
   \texttt{local variable declarations;}\\
   \texttt{executable statements;}\\
   \texttt{return statement;}\\
$\}$\\

\begin{itemize}
  \item \texttt{After the main funciton}
  \item \texttt{$\approx$ prototype, without ";"}
	\item "$\{$", "$\}$" $-$ scope
	\item \texttt{Local variables disappear outside scope}
	\item \texttt{Output}
	\begin{itemize}
	    \item \texttt{Return statement}
	    \item \texttt{Match returntype}
	\end{itemize}
\end{itemize}
\end{frame} %-------------------------------------------------------------|


%\section{Function}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{Function}
  \framesubtitle{~}

\#include $<$stdio.h$>$\\
\vskip 1\baselineskip
\small
\texttt{double CountArea(double x, double y);} $\setminus$$\setminus$Prototype \\
\vskip 1\baselineskip
\texttt{int main(void)}\\
$\{$\\
\texttt{$\dots$}\\
\texttt{area = CountArea(x, y);} $\setminus$$\setminus$Call the function \\
\texttt{$\dots$}\\
$\}$\\
\vskip 1\baselineskip
\texttt{double CountArea(double x, double y)} $\setminus$$\setminus$Definition \\
$\{$\\
   \texttt{return (x * y);}\\
$\}$\\

 \normalsize
\vspace{2in}
\end{frame} %-----------------------------------------------------------|




\begin{frame} %-----------------------------------------------------------|
  \frametitle{Function with Output Parameters}
  \framesubtitle{~}

\#include $<$stdio.h$>$\\
\vskip 1\baselineskip
\small
\texttt{void CountPro(int x, int y, int $*$zp);} $\setminus$$\setminus$Prototype \\
\vskip 1\baselineskip
\texttt{int main(void)}\\
$\{$\\
\texttt{int a=4,b=5,c=6;}\\
\texttt{CountPro(c,a,\&b);} $\setminus$$\setminus$Call the function \\
\texttt{printf("The sum is $\%d$",c);}\\
\texttt{return 0;}\\
$\}$\\
\vskip 1\baselineskip
\texttt{void CountPro(int x, int y, int $*$zp)} $\setminus$$\setminus$Definition \\
$\{$\\
   \texttt{$*$zp = x * y;}\\
$\}$\\

 \normalsize
\vspace{2in}
\end{frame} %-----------------------------------------------------------|



\begin{frame} %-----------------------------------------------------------|
  \frametitle{Declaring and Referencing Arrays}
  \framesubtitle{~}

\begin{itemize}
\item An array is a single variable that references several data elements in a row.
\vskip .5\baselineskip
\item To define, declare the \emph{name of the array} and the \emph{number of cells} associated with it.\\
\vskip .5\baselineskip
\textbf{Type ArrayName[Size];}\\
\vskip .5\baselineskip
\item Example: \qquad double x[8];
\vskip .5\baselineskip
Creates 8 memory cells of type {\tt double} with the name {\tt x}; \\
\vskip .5\baselineskip
These memory cells will be adjacent to each other in memory.\\
\end{itemize}
\vspace{2in}
\end{frame} %-------------------------------------------------------------|





\begin{frame} %-----------------------------------------------------------|
  \frametitle{Array declaration and Initialization}
  \framesubtitle{~}

\begin{itemize}

\item ...
\vskip .5\baselineskip
\texttt{\#define SIZE 10}\\
\texttt{\dots}\\
\texttt{int array[SIZE];}\\
\texttt{for(i=0; i < SIZE; i++)}\\
 $\{$ \\
	\texttt{scanf("\%d", \&array[i]);}  \\
 $\}$ \\

\item ...

\texttt{for(i=0; i < SIZE; i++)}\\
 $\{$ \\
	\texttt{printf("\%d", array[i]);}  \\
 $\}$ \\


\end{itemize}
\vspace{2in}

\end{frame} %-----------------------------------------------------------|




\frame{
\frametitle{string}
\begin{itemize}
	\item A list of characters is a \emph{string}.
	\item We use arrays of type {\tt char}:
\end{itemize}
	\begin{center}
		{\tt char str[] = "My string!";}
		
		\begin{tabular}[h]{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}
			\hline
			`M'& `y' & ` ' & `s' & `t' & `r' & `i' & `n' & `g' & `!' & `$\setminus$0'\\
			\hline
			77 & 121 & 32 & 115 & 116 & 114 & 105 & 110 & 103 & 33 & 0\\
			\hline
		\end{tabular}
	\end{center}

\begin{itemize}
	\item The final `$\setminus$0' character signifies the end of the string.
	\item \emph{Note: there may be more entries in the array, but they will be ignored!}
    \item int size = strlen(str);  (size=10, and include<string.h>)
\end{itemize}
\vspace{2in}
}

\frame[containsverbatim]{
\frametitle{Dealing with Character Values}
\begin{itemize}
\item Upper-case letters {\tt 'A'} through {\tt 'Z'} appear as contiguous numbers, in order.
\item Lower-case letters {\tt 'a'} through {\tt 'z'} appear as contiguous numbers, in order.	
\item You can treat a {\tt char} type (including string index) and {\tt char} literal (such as {\tt 'a'}) as numbers!
	\begin{itemize}
		\item You can use comparisons: {\tt == \quad != \quad <= \quad >= }
		\item You can use operations: {\tt + \quad - \quad / \quad * }
	\end{itemize}
\vspace{2in}
\end{itemize}
} %frame

\frame[containsverbatim]{
\frametitle{Storing Strings}
\begin{itemize}
\item You can use static arrays to store strings.

\end{itemize}
\begin{verbatim}
char string1[200];
char string2[100] = "Length determined by 100";
\end{verbatim}

\begin{itemize}
\item Then, you can output strings with {\tt printf}:

	{\tt scanf("\%s",string1);}
	
	{\tt printf("My string2 is: \%s$\setminus$n",string2);}
\end{itemize}

\vspace{2in}
} %frame










\end{document} 