Results 1 to 7 of 7

Thread: PseudoCode Help

  1. #1
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default PseudoCode Help

    Hey guys writing some psuedocode for a few algorithms and I appear to be stuck this is the assignment sheet



    Here is what I got so far, mind you these may not be correct (Please help correct them if that is the case)
    Code:
    1) 
    theArray[n] ? A[j] + A[i]
    for : ?0 to n-1 do
    	for j ?0 to i do
    		if j =/= i
    			then return theArray[n]
    				return n
    		end if
    	end for
    end for
    
    
    2) .
    For int I ?0 j I<A.length i++
    	For j?0 J<B.length i++
    		For i?0 k<c.length j k++
    			If A[i] + B[i] + C[i] ?0
    				Return true
    			Else if A[i] + B[i] + C[j] ? 0
    				Return true
    			Else if A[i] + B[i] + C[k] ?0
    				Return true
    			Else if A[i] + B[j] + C[i] ?0
    				Return true
    			Else if A[i] + B[j] + C[j] ?0
    				Return true
    			Else if A[i] + B[j] + C[k] ?0
    				Return true
    			Else if A[i] + B[k] + C[i] ?0
    				Return true
    			Else if A[i] + B[k] + C[j] ?0
    				Return true
    			Else if A[i] + B[k] + C[k] ?0
    				Return true
    			Else if A[i] + B[i] + C[k] ?0
    				Return true
    			Else if A[i] + B[i] + C[k] ?0
    				Return true
    			Else if A[i] + B[i] + C[k] ?0
    				Return true
    			Etc. (Follows this pattern of checking EVERY possible combination)
    			Else if =/=0 then
    		End for
    	End for
    End for
    
    3)
    
    lowerBound ? 0
    upperBound ? n – 1
    while(true)
    line <- (lowerBound + upperBound)/2
    if A[line] = searchKey then
    		return line
    else if lowerBound > upperBound then
    		return n
    else
    		if A[line] > searchKey then
    			upperBound ? line – 1
    		else
    			lowerBound ? line + 1
    		end if
    end if
    end while
    4)
    The Hailstone Series one
    (No idea what to do here, something along these lines not in pseudo yet)
    static int hailstone(int x){
                   
                    int steps = 1;
                    highest = x ;          
                    while (x!=1){
                           
                            if (x>highest) highest = x;
                            if (x%2==0) x/=2;
                            else x=x*3+1;
                           
                            steps++;                       
                    }
                   
                    return steps;
                   
            }




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Uhh I don't have time to do much since I'm in class atm and I'm teaching but here goes nothing.. I can do the rest when I get home or get some free time in class if you like..

    C++ Code:
    #include <iostream>
    #include <array>

    template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
    T Max(T* theArray, int Size)
    {
        T MaxVal = 0;

        for(int I = 0; I < Size; ++I)
        {
            for (int J = 0; J < Size; ++J)
            {
                if (I != J)
                {
                    if (theArray[I] + theArray[J] > MaxVal)
                        MaxVal = theArray[I] + theArray[J];
                }
            }
        }
        return MaxVal;
    }

    template<std::size_t N>
    bool FindTriple(std::array<int, N> ArrayA, std::array<int, N> ArrayB, std::array<int, N> ArrayC)
    {
        for (std::size_t I = 0; I < N; ++I)
        {
            for (std::size_t J = 0; J < N; ++J)
            {
                for (std::size_t K = 0; K < N; ++K)
                {
                    if (ArrayA[I] + ArrayB[J] + ArrayC[K] == 0)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    void Sort(int* theArray, int Size)
    {
        auto FindMinIndex = [&](int Start){
            int Index = Start;
            int Minimum = theArray[Start];

            for (int I = Start; I < Size; ++I)
            {
                if (theArray[I] < Minimum)
                {
                    Minimum = theArray[I];
                    Index = I;
                }
            }
            return Index;
        };


        for (int I = 0; I < Size; ++I)
        {
            int MinIndex = FindMinIndex(I);
            int T = theArray[I];
            theArray[I] = theArray[MinIndex];
            theArray[MinIndex] = T;
        }
    }
    Last edited by Brandon; 09-16-2013 at 01:25 PM.
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    I think the first one can be done better:

    c Code:
    template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
    T Max(T* theArray, int Size)
    {
        T MaxVal1 = 0;  //highest value
        T MaxVal2 = 0;  

        for(int I = 0; I < Size; ++I)
        {
            if (theArray[I] > MaxVal1)
            {
                MaxVal1 = theArray[I];
            } else if (theArray[I] > MaxVal2) {
                MaxVal2 = theArray[I];
            }
               
        }
        return (MaxVal1 + MaxVal2);
    }
    Working on: Tithe Farmer

  4. #4
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Here I just added the hailstone really quickly.. Here goes:

    C++ Code:
    #include <iostream>
    #include <array>
    #include <stdexcept>
    #include <vector>

    template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
    T MaxN(T* theArray, int Size)
    {
        T MaxVal = 0;

        for(int I = 0; I < Size; ++I)
        {
            for (int J = 0; J < Size; ++J)
            {
                if (I != J)
                {
                    if (theArray[I] + theArray[J] > MaxVal)
                        MaxVal = theArray[I] + theArray[J];
                }
            }
        }
        return MaxVal;
    }

    template<std::size_t N>
    bool FindTriple(std::array<int, N> ArrayA, std::array<int, N> ArrayB, std::array<int, N> ArrayC)
    {
        for (std::size_t I = 0; I < N; ++I)
        {
            for (std::size_t J = 0; J < N; ++J)
            {
                for (std::size_t K = 0; K < N; ++K)
                {
                    if (ArrayA[I] + ArrayB[J] + ArrayC[K] == 0)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    void Sort(int* theArray, int Size)
    {
        auto FindMinIndex = [&](int Start){
            int Index = Start;
            int Minimum = theArray[Start];

            for (int I = Start; I < Size; ++I)
            {
                if (theArray[I] < Minimum)
                {
                    Minimum = theArray[I];
                    Index = I;
                }
            }
            return Index;
        };

        for (int I = 0; I < Size; ++I)
        {
            int MinIndex = FindMinIndex(I);
            int T = theArray[I];
            theArray[I] = theArray[MinIndex];
            theArray[MinIndex] = T;
        }
    }

    std::vector<int> HailStone(int PickedNumber)
    {
        if (PickedNumber <= 0)
            throw std::invalid_argument("Value Entered Must be greater than 0.");

        int Count = 0;
        std::vector<int> Result(1, PickedNumber);
        auto IsOdd = [&](int Value) {return Value & 1;};
        int Orig = (PickedNumber % 4) == 0 ? PickedNumber - 1 : PickedNumber + 1;

        while(true)
        {
            if (IsOdd(PickedNumber))
            {
                PickedNumber *= 3;
                PickedNumber += 1;
                Result.push_back(PickedNumber);
            }
            else
            {
                PickedNumber /= 2;
                Result.push_back(PickedNumber);
            }

            if (Count > 0)
            {
                ++Count;
            }

            if (Count >= Orig)
                break;

            if (Result.back() == 4)
            {
                ++Count;
                continue;
            }
        }
        return std::move(Result);
    }

    int main()
    {
        auto Result = HailStone(4);
        for (auto it = Result.begin(); it != Result.end(); ++it)
            std::cout<<*it<<" ";
    }
    Last edited by Brandon; 09-16-2013 at 02:43 PM.
    I am Ggzz..
    Hackintosher

  5. #5
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

    Wow thanks guys, I am unsure why I did not bother to think of a different method to find every possible combination and my instant was to actually write out every possible combination lol. Thanks again!




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  6. #6
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Again taking Brandon his algorithm:

    c++ Code:
    void Sort(int* theArray, int Size)
    {
        int lowestIndex;  //the index of the lowest element
        int lowestValue;  //the value of that element

        for (int I = 0; I < (Size - 1); ++I) //last element will already be sorted
        {
            lowestIndex  = 0;  
            lowestValue = 0;
           
            for( int J = I; J < Size; ++J){
                if (theArray[J] < lowestValue) {
                    lowestValue = theArray[J];
                    lowestIndex = J;
                }
            }
           
            int T = theArray[I];
            theArray[I] = theArray[lowestIndex];
            theArray[lowestIndex] = T;
        }
    }

    Exact same code. Just removed the internal function to make it easier to read imo.

    I wrote the last assignment in pascal:
    Simba Code:
    procedure HailStone(input:Integer);
    var
      HighestNumber, Loops: Integer;
    begin
      if input = 0 then
        Exit;

      HighestNumber := input
      Loops := 1;

      repeat
        if(input and 1)then  //check last bit (5 -> 101, 9 -> 1001 but even numbers are 4 -> 100, 12 -> 1100)
          input := input * 3 + 1
        else
          input := input div 2;

        Inc(Loops);

        if(input > HighestNumber) then
          HighestNumber := input;

        writeln('amount of loops: ' + IntToStr(Loops));
        writeln('highest number: ' + IntToStr(HighestNumber));
      until(input = 4);
    end;
    Working on: Tithe Farmer

  7. #7
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

    Thanks masterBB, lol I was a little bit confused on the last one but the pascalscript made it easy to understand.

    Thanks again guys really appreciate it!




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •