0

I am trying to write down my algorithm and faked I wrote Foreach loop. Otherwise there is no command \Foreach. The followingmy program could not be executed. Also I use package \usepackage{algpseudocode} and \usepackage{algorithm}.

\begin{algorithm}
  \caption{Algorithm for generating atn.json file}
  \begin{algorithmic}
    \Statex \Comment { \textbf{Input:} ListofOrderedSelectedJobs, testDuration, beginDate, endDate, MapDeviceTranmissionTime, \textbf{Output:} atn.json
   \For{\textbf{each} selectJob \textit{in} ListofOrderedSelectedJobs \textit{do}} 
        \If {selectJob \textit{contains} Adevice \textit{then} \textit{addAll} Adevices}
    \Statex\Comment{ \%comment: Do the same process for group B, C and D devices\%}
        \EndIf 
        \If {$selectJob == 'U'$ \textit{then}}
        \For{\textbf{each} device \in choosenDevice \textit{do}}
    \State  MapDeviceTranmissionTime.put(device,beginDate)  
    \Statex\Comment{ \%comment: Put begin test date for each device and store that value in the map\%} 
    \If {$selectJobType == 'Z'$ \textit{then}}
        \If { randomTransmissionDate \textit{exists} \textit{then} readRandomExecutionDate() }
        \Else { addRandomExecutionDate()}
        \EndIf
        \EndIf
    \EndFor 
        \If { $selectJobInterval \leq testDuration$ \textit{then}}
        \State $times = testDuration / selectJobInterval$
        \Else
        \State $times = (testDuration / selectJobInterval) + 1$
        \EndIf
    \For{$i = 1$ to times \textit{do}}
        \If {$selectJob == 'U'$ \textit{then}}
        \For{\textbf{each} device \in choosenDevice \textit{do}}
        \State timesDate = timesDate + Interval 
        \State MapDeviceTranmissionTime.put(device,timesDate) 
        \Statex\Comment{ \%comment: Create URL string and encode it by using timesDate date\%}
    \If {$selectJobType == 'Z'$ \textit{then}}
        \If { randomTransmissionDate \textit{exists} \textit{then} readRandomExecutionDate() }
        \Else { addRandomExecutionDate()}
        \EndIf
    \EndFor
        \EndIf
        \EndFor
        \EndFor
  \end{algorithmic}
\end{algorithm} 

Could someone please help me what I made wrong.

4
  • Any comments, answers? please
    – limonik
    Apr 9, 2016 at 17:35
  • any help or idea?
    – limonik
    Apr 9, 2016 at 22:50
  • It is not clear what are you asking for. At least not to me.
    – wilx
    Apr 9, 2016 at 22:51
  • @wilx when I put this in my tex file it gave error messages and when I click error messages it leads me another file. Since this is my thesis I used different .tex file for each chapter. Therefore I could not understand the root of the error. Could you pleas check my code
    – limonik
    Apr 9, 2016 at 22:55

1 Answer 1

2

Your problem is that you are missing some closing \EndIf and \EndFor. I have made some changes to your code and indented it properly and I am able to compile it. Also, you are not using the package provided macros correctly.

\documentclass{article}

\usepackage{algpseudocode}
\usepackage{algorithm}

\begin{document}
\begin{algorithm}
  \caption{Algorithm for generating atn.json file}
  \begin{algorithmic}
    \Statex{\Comment{\textbf{Input:} ListofOrderedSelectedJobs, testDuration,
      beginDate, endDate, MapDeviceTranmissionTime; \textbf{Output:}
      atn.json}}
    \Statex
    \ForAll{selectJob $\in$ ListofOrderedSelectedJobs}
      \If{selectJob \textit{contains} Adevice}
        \textit{addAll} Adevices
        \Statex\Comment{\%comment: Do the same process for group B, C and D devices\%}
      \EndIf
      \If{$selectJob == 'U'$}
        \ForAll{device $\in$ choosenDevice}
          \State MapDeviceTranmissionTime.put(device,beginDate)
          \Statex\Comment{\%comment: Put begin test date for each device and store that value in the map\%}
          \If{$selectJobType == 'Z'$}
            \If{$\exists$ randomTransmissionDate}
              \State readRandomExecutionDate()
            \Else
              \State addRandomExecutionDate()
            \EndIf
          \EndIf
        \EndFor
      \EndIf
      \If{ $selectJobInterval \leq testDuration$}
        \State $times = testDuration / selectJobInterval$
      \Else
        \State $times = (testDuration / selectJobInterval) + 1$
      \EndIf
      \For{$i = 1 \to times$}
        \If {$selectJob == 'U'$}
          \ForAll{device $\in$ choosenDevice}
            \State timesDate = timesDate + Interval
            \State MapDeviceTranmissionTime.put(device,timesDate)
            \Statex\Comment{ \%comment: Create URL string and encode it by using timesDate date\%}
            \If{$selectJobType == 'Z'$}
              \If{$\exists$ randomTransmissionDate}
                \State readRandomExecutionDate()
              \Else
                \State addRandomExecutionDate()
              \EndIf
            \EndIf
          \EndFor
        \EndIf
      \EndFor
    \EndFor
  \end{algorithmic}
\end{algorithm}
\end{document}

Demo at Overleaf.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .