Skip to main content
Engineering LibreTexts

10.7: Exercises

  • Page ID
    83036
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    1. Change the PrintIntArray subprogram so that it prints the array from the last element to the first element.

    2. The following pseudo code converts an input value of a single decimal number from 1 ≤ n ≥ 15 into a single hexadecimal digit. Translate this pseudo code into MIPS assembly.

    main 
    { 
        String a[16]
        a[0] = "0x0" 
        a[1] = "0x1" 
        a[2] = "0x2" 
        a[3] = "0x3" 
        a[4] = "0x4" 
        a[5] = "0x5" 
        a[6] = "0x6" 
        a[7] = "0x7" 
        a[8] = "0x8" 
        a[9] = "0x9" 
        a[10] = "0xa" 
        a[11] = "0xb" 
        a[12] = "0xc" 
        a[13] = "0xd" 
        a[14] = "0xe" 
        a[15] = "0xf" 
        
        int i = prompt("Enter a number from 0 to 15 ") 
        print("your number is " + a[i]
    }
    

    3. The following method returns a random number from 1 to n, where n is stored in r1.

    # Calculate a random number 
    # arguments: r0 - seed (if seed is 0, get next random value) 
    #            r1 - range (from 1 to r1). If r1 is 0 or negative, 
    #            range is all ints) 
    # 
    Random: 
        SUB sp, sp, #8 
        # Save return to os on stack 
        STR lr, [sp, #0] @ Prompt For An Input 
        STR r4, [sp, #4] 
    # 
        MOV r3, #0 
        CMP r0, r3 
        BNE Reset 
            LDR r0, =seed @ get the seed 
            LDR r0, [r0, #0] 
        Reset: 
        
        ADD r0, r0, #137 @ get the next seed 
        EOR r0, r0, r0, ror #13 
        LSR r0, r0, #1   @ make sure it is positive 
        MOV r4, r0       @ save the value to r4 
        
    # Get the remainder 
        MOV r3, #0 
        CMP r1, r3 
        BLE NoRange 
            BL __aeabi_idiv 
            MUL r1, r0, r1
            SUB r4, r4, r1 
        NoRange:
    
    # Save the seed to memory 
        LDR r0,=seed 
        STR r4,[r0, #0] 
        
    # Return to the OS 
        MOV r0, r4 
        LDR lr, [sp, #0] 
        LDR r4, [sp, #4] 
        ADD sp, sp, #8 
        MOV pc, lr 
        
    .data 
        seed: .word 25 
    #end Random
    

    a) Create an array of 100 values, and populate it with 100 random numbers.

    b) Using the array in part a, find the minimum and maximum value in the array.

    c) Calculate the sum and average of all values in the array

    d) Implement a Bubble Sort of the array.

    e) Find the median value in the array.

    4. Create strings of numbers (0+1, Combinations, permutations; using String functions)

    5. Reverse a string using procedural programming and recursion.

    6. The following pseudo code programs calculates the Fibonacci numbers from 1..n, and stores them in an array. Translate this pseudo code into MIPS assembly, and use the PrintIntArray subprogram to print the results.

    main 
    { 
        int size = PromptInt(“Enter a max Fibonacci number to calc: “) 
        int Fibonacci[size] 
        Fibonacci[0] = 0 
        Fibonacci[1] = 1 
        for (int i = 2; i < size; i++) 
        {
            Fibonacci[i] = Fibonacci[i-1] + Fibonacci[i-2] 
        } 
        PrintIntArray(Fibonacci, size)
    

    10.7: Exercises is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?