Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Search Today's Posts Mark Forums Read
Search Forums:
Click here to use Advanced Search

Closed Thread
 
Thread Tools Display Modes
Old 08-08-2005, 06:37 PM   #1
Kon-Tiki
[BANNED]

 
Join Date: Sep 2004
Location: Dentergem, Belgium
Posts: 1,811
Default

Here are the solutions to the questions in the exams-thread. Mind that these solutions're set up in such a way that they'd solve it for most programming languages, so it'll still require some minor thought in order to make it to work in yours. Thanks go to Reup for helping out with these.

This part is still under construction and only has the answers to the Medium-level questions for now

Medium:
Q: Sort an array of N (N<=50) elements and print the elements from X-th to Y-th.

A: There are a lot of sorting algorithms (<http://linux.wku.edu/~lamonml/algor/sort/sort.html>) that can be used. Some are slow, some are fast. The one described here is called ?Bubble sort? because the largest results will ?bubble? up to the end of the array. It?s not a fast sort (it?s the slowest one around actually), but it?s easy to understand:

The bubble sort works by comparing each item in the list with the item next to it, and swapping them if required. The algorithm repeats this process until it makes a pass all the way through the list without swapping any items (in other words, all items are in the correct order).

Code:
void bubbleSort(int numbers[], int array_size)
{
 *int i, j, temp;

 *for (i = (array_size - 1); i >= 0; i--)
 *{
 * *// start at position 1 (2nd in array) 
 * *for (j = 1; j <= i; j++)
 * *{
	// if the number before me is smaller than I am
	// if not, do nothing, go on the the next j
 * * *if (numbers[j-1] > numbers[j])
 * * *{
 * // swap them, using the temp variable	
 * * * *temp = numbers[j-1];
 * * * *numbers[j-1] = numbers[j];
 * * * *numbers[j] = temp;
 * * *}
 * *}
 *}
}
Tip: if you?ve got trouble understanding the algorithm (or any sorting algorithm), try doing it on paper, by writing out the contents of the array after each iteration!

Q: Write a program that will find the minimum and maximum element in the array that consists of N elements (N<=100).

A: This is a rather simple solution. There is a caveat: if you had set the max and min values to 0 at first, the results would be faulty for an array with all positive numbers. By setting the max and min values to the first element of the array, you can prevent this!

Code:
var max = array[0], min = array[0]
for( i = 1; i <= array.length; i++)
{
	if array[i] > max
 *max = array[i]
	else if array[i] < min
 *min = array[i]
}
print max, min
Q: For the given web addresses you have to print out their domains.
Example:
http://www.abandonia.com/forum/index => com
http://www3.kingdomofloathing.com => com
programiranje.net/forum/index.php => net
www.google.com/index.php => com

A: For this assignment you have to understand how an URL is setup:

<http://www.abandonia.com/forum/index>

http:// this is the protocol part. It always end with the double frontslash
www.abandonia.com The domain name itself. It could consist of two or more stirngs separated by dots. The last one, however, is always the desired top-level domain indicator (com, net, org)
/forum/index This is some path info. It?s always separated from the domain part with a forward slash (although backslashes can be used on win32 platforms!)

// set up the variables
string URL, domain

// remove the first protocol part and including the ?://?
remove substring( 0, ?://?, URL )

// from the remaining URL fetch the part until the first
// front or backslash. Store the result in the domain-string
domain = get substring( 0, ?/? OR ?\?, URL )

// fetch the part after the last ?.? In the domain string
domain = get substring( lastInstanceOf( ?.? ), n, domain)
print domain

Pay attention to the fact that it depends on the implementation of the substring methods, whether or not the :// part get included in the substring or not. Look this up in your language specification or reference!
Kon-Tiki is offline                         Send a private message to Kon-Tiki
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Discussion Of Exams Kon-Tiki Programming 8 30-03-2007 08:27 PM
How Often Do You Cheat On Exams / Tests nace Blah, blah, blah... 40 16-10-2006 08:57 PM
Solution To Speed! Mr.Snuggles Troubleshooting 0 05-10-2005 11:30 PM
Preparation of Exams Unknown Hero Programming 37 03-05-2005 04:18 PM
Exams Unknown Hero Programming 0 12-04-2005 09:40 PM

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 02:17 PM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.