
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  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 13 - Array}

\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{Assignment}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{Announcement: CodeLab assignment}
  \framesubtitle{~}

\begin{itemize}
	\item The 7th CodeLab assignment\\
	\vskip 1\baselineskip
	 \qquad "CodeLab Assignment-7"\\
	 \qquad Due:  23:59 Apr 23\\
	      \vskip 1\baselineskip
\end{itemize}

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


\section{Arrays}

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


\begin{itemize}
\item Simple data types use a single memory cell to store a variable.
\item Sometimes, we want to \emph{group} information together.
\item For example: a program processes exam scores for a class could:
\end{itemize}
\begin{enumerate}
\item Declare 54 variables:\\ {\tt double student1,student2,student3,$\dots$} student54;
\vskip .5\baselineskip
\item Store all of the scores  together in continuous memory cells\\
Access them as a group\\
Reference them through the same variable name
\end{enumerate}
\qquad double student[54];
\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{\texttt{index}}
  \framesubtitle{~}

 \begin{itemize}
\item Access individual data elements with [index] brackets.
\vskip .5\baselineskip
\item By specifying the \emph{array name} and identifying the element desired, we can access a particular value.
\vskip .5\baselineskip
\begin{itemize}
\item {\tt x$[$0$]$} (read as x sub zero) references the 0th element of the array {\tt x};
\item {\tt x$[$1$]$} is the next element in the array, followed by {\tt
x$[2]$, $\ldots$, x$[$n-1$]$.}
\end{itemize}
\vskip .5\baselineskip
\item The array subscript value must be in the range from zero to one less than the number of memory cells in the array.\\
\quad double student[54]; \\
\quad index: 0, 1, 2, ..., 53\\
\quad Element "student[54]" is invalid and may cause serious memory problem.
\end{itemize}
\vspace{2in}
\end{frame} %-----------------------------------------------------------|

\section{Using arrays}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{Parallel Arrays}
  \framesubtitle{~}

\begin{itemize}
\item We can define two arrays and have the ith element in each array correspond to the same thing, such as the
following:
\vskip .5\baselineskip
\texttt{int id[50];}\\
\texttt{double gpa[50];}\\
\vskip .5\baselineskip
\item {\tt id$[$i$]$} and {\tt gpa$[$i$]$} correspond to the {\tt i}$^{th}$ student.
\vskip .5\baselineskip

\end{itemize}
\vspace{2in}

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


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

\begin{itemize}


\item Array name can not be the same as the variable name.\\
int a; \\
double a[10]; \\
\textbf{wrong!}\\
\vskip .5\baselineskip
\item When declare array, array size should be a fixed number, not a variable.\\
int n=5, \\
int a[n]; \\
\textbf{wrong!}\\
 \vskip .5\baselineskip
\item You can declare multiple arrays along with regular variables.\\
\texttt{double student[200], president, professor[10];}\\



\end{itemize}
\vspace{2in}

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


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

\begin{itemize}

\item We can initialize a simple variable when we declare it:\\
{\tt int sum = 0;}
\vskip .5\baselineskip
\item Same with arrays:
\vskip .5\baselineskip
\texttt{\#define SIZE 10}\\
\texttt{\dots}\\
\texttt{int array[SIZE];}\\
\texttt{for(i=0; i < SIZE; i++)}\\
 $\{$ \\
	\texttt{array[i] = 0;}\\
 $\}$ \\

\end{itemize}
\vspace{2in}

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

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

\begin{itemize}
\item We can also initialize an array in its declaration.
%\begin{itemize}
%\item We can omit the size of an array that is being fully initialized since the size can be deduced from the
%initialization list.
 \vskip .5\baselineskip
int prime100[] = \texttt{$\{$}\\
  2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, \\
  41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, \\
89, 97 \texttt{$\}$}\\


\end{itemize}
\vspace{2in}

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



\begin{frame} %-----------------------------------------------------------|
  \frametitle{Array Subscripts}
  \framesubtitle{~}

\begin{itemize}
\item Subscripts denote the \emph{number of places away from the start}.
\item Any expression of type {\tt int} can be used as an array subscript.
\begin{itemize}
\item For Example:  {\tt array$[$2*i+1$]$}
\item To create a valid reference, the value of this subscript must lie between 0 and one less than the
declared size of the array.
\end{itemize}
\end{itemize}
\vspace{2in}

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


\begin{frame} %-----------------------------------------------------------|
  \frametitle{Array Subscripts}
  \framesubtitle{~}


\begin{itemize}
\item To use \texttt{scanf} or \texttt{printf}.\\
\quad \texttt{scanf("\%d", \&x[i]);}  \\
\quad \texttt{printf("\%d$\setminus$n", x[i]);}  \\
\end{itemize}

\vspace{2in}

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



\begin{frame} %-----------------------------------------------------------|
  \frametitle{Using for Loops for Sequential Access}
  \framesubtitle{~}

 \begin{itemize}
\item Usually, we iterate through element in order from 0 ... SIZE$-$1.
\item Use a \texttt{for} loop with index variable, starting at 0 and bounded by \texttt{SIZE}.

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

\item Using the loop counter as an array index (subscript) gives access to each array element in turn.
\item Can not output the whole array by one printf.
printf("\%d",array);\\
\textbf{Wrong!}\\
\end{itemize}
\vspace{2in}

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

\end{document} 