SRL Archive - General

PseudoCode Help


[XoL]

2013-09-16 01:07:24

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)

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;

}

Brandon

2013-09-16 09:22:42

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..


#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;
}
}

masterBB

2013-09-16 09:46:21

I think the first one can be done better:

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);
}

Brandon

2013-09-16 10:21:05

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


#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<<" ";
}

[XoL]

2013-09-16 10:25:04

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!

masterBB

2013-09-16 11:15:59

Again taking Brandon his algorithm:

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:
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;

[XoL]

2013-09-16 11:38:32

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!