Question

Design and implement an algorithm that is given as input an integer value k0k \geq 0 and a list of k numbers N1,N2,,Nk\mathrm{N}_{1}, \mathrm{N}_{2}, \ldots, \mathrm{N}_{\mathrm{k}} Your algorithm should reverse the order of the numbers in the list. That is, if the original list contained: N1=5,N2=13,N3=8,N4=27,N5=10(k=5)\mathrm{N}_{1}=5, \mathrm{N}_{2}=13, \mathrm{N}_{3}=8, \mathrm{N}_{4}=27, \mathrm{N}_{5}=10(\mathrm{k}=5) then when your algorithm has completed, the values stored in the list will be: N1=10,N2=27,N3=8,N4=13,N5=5\mathrm{N}_{1}=10, \mathrm{N}_{2}=27, \mathrm{N}_{3}=8, \mathrm{N}_{4}=13, \mathrm{N}_{5}=5

Solution

Verified
Answered 4 months ago
Answered 4 months ago

Let's store the elements in reversed order in the sequence: R1,R2,...RkR_1, R_2, ... R_k

After that, let's copy the already reversed list back into N1,N2,...NkN_1, N_2, ... N_k

  • Get the value of kk
  • Get the elements of the list N1,N2,...NkN_1, N_2, ... N_k
  • Set the value of ii to 1
  • While (ik)\left(i\leq k \right) do
  •      Set value of RiR_i to Nk+1iN_{k+1-i}
  •      Increment ii by 1
  • End of loop
  • Set value of ii to 1
  • While (ik)\left(i\leq k \right) do
  •      Set value of NiN_i to RiR_i
  •      Increment ii by 1
  • End of loop
  • Print the result: N1,N2,...NkN_1, N_2, ... N_k

Create an account to view solutions

By signing up, you accept Quizlet's Terms of Service and Privacy Policy
Continue with GoogleContinue with Facebook

Create an account to view solutions

By signing up, you accept Quizlet's Terms of Service and Privacy Policy
Continue with GoogleContinue with Facebook

Recommended textbook solutions

Computer Organization and Design MIPS Edition: The Hardware/Software Interface 5th Edition by David A. Patterson, John L. Hennessy

Computer Organization and Design MIPS Edition: The Hardware/Software Interface

5th EditionISBN: 9780124077263 (5 more)David A. Patterson, John L. Hennessy
220 solutions
Fundamentals of Database Systems 7th Edition by Ramez Elmasri, Shamkant B. Navathe

Fundamentals of Database Systems

7th EditionISBN: 9780133970777 (1 more)Ramez Elmasri, Shamkant B. Navathe
687 solutions
Introduction to Algorithms 3rd Edition by Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen

Introduction to Algorithms

3rd EditionISBN: 9780262033848 (2 more)Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
726 solutions
Invitation to Computer Science 7th Edition by G Michael Schneider, Judith L. Gersting

Invitation to Computer Science

7th EditionISBN: 9781305075771 (1 more)G Michael Schneider, Judith L. Gersting
112 solutions

Related questions