Saturday, September 25, 2010

Is this thing sorted?

C++

int array_size = n  \\ n is the conceptual limit of the array
double array[array_size];
void IsSorted(array[])
{
      int sort_check = 0;
      int count_n = 0;
      while(sort_check ==0 || count_n == array_size)
      {
            if(array[count_n] > array[count_n + 1])
                  sort_check++;
            count_n++;
      }
      if(sort_check != 0)
            cout << "NO" << endl;
      else
            cout << "YES" << endl;
}


The algorithm will work for any value of n, as it will loop through the whole array or when it finds an unsorted pairing.  The algorithm will work for any set of numbers within the array and any permutation of the set as the array is to hold double values.  The solution finishes either when it discovers an unsorted pair or when it goes through the entire array, both being finite amounts of time.

Bam.

Sunday, September 19, 2010

Searching the list

1.  Prompt user for search value 'k'
2.  Receive 'k'
3.  Initialize a loop to search the array
4.  Compare 'k' to each of the values in the array
5.  If 'k' is equal to any of the values in the array exit the array and display "Yes", if not display "No"

int array[40];
int k, count_k, count_array;
cout << "Enter the value you are searching for: ";
cin >> k;
count_k = 0;
count_array = 0;
while(count_k != 0 || count_array == 39)
{
    if(count_array == k)
          count_k++;
    else
          count_array++;
}
if(count_k != 0)
     cout << "Yes.";
else
     cout << "No.";


The program will end in finite time with an upper bound of O(n): the time it takes to go through the loop once.  The program will always have an output, as either it finds the value in the array and displays "Yes", or it doesn't find the value and displays "No."

Saturday, September 11, 2010

Finding that max

Initialize an integer value of "largest" to the first value of the array.  Begin a loop to search through the entire array.  Within the loop the value of "largest" will be compared to each value within the array sequentially.  If, at any time, the array value is larger than "largest", then "largest" will have its value set to the larger array value.

An array with 40 values:

int array[40];
int largest = int array[0];
for(int a=0; a<40; a++)
{
   if(array[a] > large)
       largest = array[a];
}

The algorithm will work as it will always go through the entire array and will set the variable "largest" to the largest value in the array it comes across.  It will also finish in finite time (n-1 time), as it needs only to loop through and compare all the values to "largest" once to find the largest value in the array.

Monday, September 6, 2010

Round Two

I was actually excited for a moment when I thought we had to post a sorting code for ASU101, as I've done it before and have some code saved from back then.  But no, I don't get to use some copy + paste for this post, and for that I am a slight sad.  Oh well.  So I think this post is supposed to be about whatever we want, right?  Just about stuff that's going on?  Alright, I can swing that.  So last night was pretty sweet; I went to a rush event for a frat with a friend, and there was a flowrider there.  At first I could not get it to work: I continuously fell hard on my ass (so many bruises), but after a while I began to understand, and toward the end I was standing up and flowridin' like a champion.  A few more runs on it and I could have gotten some tricks down, but time ran out so that didn't happen.  And now I have flowrider withdrawal.  Damn it.