Help .. on this program
Author |
Message |
armiento
|
Posted: Fri Jun 13, 2008 8:26 pm Post subject: Help .. on this program |
|
|
For this program teacher wants to get the names of the students, and their marks, and their 4 subjects along with their grade avg, and also he want it to all be sorted in alphabetical order for the students names. For the the marks he wants to get them greatest to least , and same goes with the avg. i'm having trouble on this.
This is what I got so far.
var marks : string
var students : int
put "Select a number of students, to view specified statistics"
get students
var names : array 1 .. students of string
var sub1 : array 1 .. students of string
var sub2 : array 1 .. students of string
var sub3 : array 1 .. students of string
var sub4 : array 1 .. students of string
var mark1 : array 1 .. students of string
var mark2 : array 1 .. students of string
var mark3 : array 1 .. students of string
var mark4 : array 1 .. students of string
for i : 1 .. students
put "Enter the name of a student"
get names (i)
put "Enter the four subjects and the marks for each subject."
put "Subject 1:"
get sub1 (i)
put "What is the mark:"
get mark1 (i)
put "Subject 2:"
get sub2 (i)
put "What is the mark:"
get mark2 (i)
put "Subject 3:"
get sub3 (i)
put "What is the mark:"
get mark3 (i)
put "Subject 4:"
get sub4 (i)
put "What is the mark:"
get mark4 (i)
end for
var avg : array 1 .. students of string
for i : 1 .. students
avg (i) := (mark1 (i) + mark2 (i) + mark3 (i) + mark4 (i)) / 4
end for |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
riveryu
![](http://compsci.ca/v3/uploads/user_avatars/196991102647ae2debe0bf0.jpg)
|
Posted: Fri Jun 13, 2008 10:20 pm Post subject: Re: Help .. on this program |
|
|
- Use syntax tags when posting, press more tags and select syntax then in the quotations of [ syntax =" "] type turing.
- You did not say what exactly is your problem....
-To make your names alphabetical you need to know ASCII representation of chars
-To make your marks from least to high use bubble sort? (I dont know actually, just search it up) |
|
|
|
|
![](images/spacer.gif) |
Euphoracle
![](http://compsci.ca/v3/uploads/user_avatars/11170373664bf5f25f636f1.png)
|
Posted: Sat Jun 14, 2008 12:26 am Post subject: Re: Help .. on this program |
|
|
riveryu @ Fri Jun 13, 2008 10:20 pm wrote: - Use syntax tags when posting, press more tags and select syntax then in the quotations of [ syntax =" "] type turing.
- You did not say what exactly is your problem....
-To make your names alphabetical you need to know ASCII representation of chars
-To make your marks from least to high use bubble sort? (I dont know actually, just search it up)
Actually, you can cheat on this one with Turing. Turing will evaluate ("Eggs")>("Waffle") as far as I know. |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Sat Jun 14, 2008 12:36 am Post subject: RE:Help .. on this program |
|
|
Not just Turing, most languages will do that. Just keep in mind the underlying structure of the comparison -- strings are arrays of characters. Also case matters. "B" is alphabetically earlier than "a". |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
Euphoracle
![](http://compsci.ca/v3/uploads/user_avatars/11170373664bf5f25f636f1.png)
|
Posted: Sat Jun 14, 2008 12:52 am Post subject: Re: RE:Help .. on this program |
|
|
Tony @ Sat Jun 14, 2008 12:36 am wrote: Not just Turing, most languages will do that. Just keep in mind the underlying structure of the comparison -- strings are arrays of characters. Also case matters. "B" is alphabetically earlier than "a".
Some other languages (such as the second most commonly used language here, Java) use methods to do it. Turing is cute that way; the greater than operator is just sooo handy
Good point about the underlying concepts though. To determine the ascii characters easily, one can use the ord function, which will return the ascii value of the given character (or single-length string). Similarly, the chr function does the inverse of this operation by providing the character (or single-length string) for the given ascii value.
The functionality follows:
Turing: |
put ord("a")
--> 97
put ord("A") %To prove Tony's point
--> 65
put ord("z")
--> 122
put ord("Z")
--> 90 %(Notice the gap between the uppercase and lowercase ascii ranges and the order in which they go in!)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
put chr(65)
--> A
put chr(97)
--> a
put chr(90)
--> Z
put chr(122)
--> z %(and so forth)
|
|
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Sun Jun 15, 2008 1:01 pm Post subject: RE:Help .. on this program |
|
|
Bubble sorting is a big pain in the butt. I spent somewhere around 10 hours tryinng to figure out how it works, but once you understand this, it's easy to modify to your needs. You can use bubble sorting for the names, averages, and well, everything sorting (unless I'm wrong, then I look stupid). I've been told it is very inefficient though, and larger sorting problems should use some other kind. |
|
|
|
|
![](images/spacer.gif) |
gitoxa
![](http://compsci.ca/v3/uploads/user_avatars/125344263047f801d546bcb.jpg)
|
Posted: Sun Jun 15, 2008 2:31 pm Post subject: RE:Help .. on this program |
|
|
You spent 10 hours on bubble sorting? Good luck with the more complicated stuff then. ![Confused Confused](http://compsci.ca/v3/images/smiles/icon_confused.gif) |
|
|
|
|
![](images/spacer.gif) |
riveryu
![](http://compsci.ca/v3/uploads/user_avatars/196991102647ae2debe0bf0.jpg)
|
Posted: Sun Jun 15, 2008 9:03 pm Post subject: RE:Help .. on this program |
|
|
about the ASCII thing i was just trying to emphasize that he should understand it before he use it. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Mon Jun 16, 2008 7:30 am Post subject: RE:Help .. on this program |
|
|
I guess I should mention my heart really wasn't in it; I played games for a good chunk of the time.
Damn, I feel stupid... |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Mon Jun 16, 2008 8:51 am Post subject: RE:Help .. on this program |
|
|
About Bubblesort (and sorts in general):
Most sorts ("general" sorts) can sort any kind of data, as long as you have a means of comparing two values to determine which is greater than the other. This obviously applies for ints, reals, strings and characters, but can also apply for objects, depending on circumstance. You could also have objects that are sorted by one of their fields - perhaps you want to sort players by their score, or racecars by their horsepower rating. Either way, the "general" sorts will let you sort your information; bubblesort is probably the simplest of all sorts, and also one of the most inefficient.
There are only really three conditions to evaluate sorting algorithms by: their execution time, additional space required, and whether they're "stable". Stability means that objects which are equal will stay in their original order; this is useful when you want to sort emails by date, then by sender, so that all the emails from each sender are in chronological order...for example).
Execution time varies wildly and can be hard to measure accurately. The simplest metric is simply to count the number of "checks" and "swaps" - checks occur when the algorithm compares two values, while swaps occur when the algorithm elects to change their positions.
Bubblesort is an O(n^2) sort algorithm, which means that if you have n things to sort, it will take time proportional to n^2. For n=10, that's about zero, but for n=1000, it starts becoming really noticeable. Trying a bubblesort with n=1000000 is an exercise in learning why we should use different algorithms. It does have the distinct advantage of not requiring additional storage.
There are other sorts which are also O(n^2), but have a better execution time. Linear insertion sort is one example.
The better sorts tend to have differing best- and worst-cases. "Best case" refers to when it finishes the fastest and "worst case" to when it finishes the slowest. Believe it or not, they can actually be dramatically different.
Merge Sort is O(n * log(n)) in both best and worst case, and can be written to use no additional memory - though the simple implementations generally use as much additional space as the original array ( O(n) extra space).
Quicksort is a recursive sort that has a best case of O(n * log(n)) and a worst case of O(n^2). It uses additional space due to the recursion (O(log n)), but that amount of space tends to be trivial in comparison to the data. Quicksort is very popular for large datasets, and has the advantage that it tends to be faster than any other sort. Its worst case can be nearly eliminated by the use of clever partitioning.
There are also many sorts that are a combination of other sorts. Quicksort tends to be inefficient below a certain value, even when compared to a linear insertion sort, so you can have hybrid algorithms that will use one algorithm for large datasets, and then swap to a different one for smaller sets. Introsort is one example.
Most languages implement some sort of standard sort so that programmers can just say sort( myArray ) instead of having to write their own array. In Java, Array.sort() and Collection.sort() are already there for the programmer - they use a tuned Quicksort. The C++ STL uses Introsort. Turing...probably doesn't support anything of the kind.
For more detailed information, see: http://en.wikipedia.org/wiki/Sorting_algorithm |
|
|
|
|
![](images/spacer.gif) |
|
|