Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 22nd, 2007, 10:07 AM   #1
sepalm
Newbie
 
Join Date: Nov 2007
Posts: 5
Rep Power: 0 sepalm is on a distinguished road
Degree project help! Sorting an array

Right, We were set the task of designing a program for a fictional company "Loud music". I've come to a point in the code which i've never crossed before, i have to sort my array into ascending order. I've no idea how to sort an array, i've looked at numerous tutorials on numerous websites, and come to the conclusion bubble sort would be best? but can't apply it. This is the code:

Dim totalbr0(1 To 8) As Single

Private Sub cmdsearch_Click()
totalbr0(1) = 0
totalbr0(2) = 0
totalbr0(3) = 0
totalbr0(4) = 0
totalbr0(5) = 0
totalbr0(6) = 0
totalbr0(7) = 0
totalbr0(8) = 0
Dim startweek As Integer
Dim endweek As Integer
If txtweek1.Text = "" Then
    txtweek1.Text = txtweek.Text
End If
startweek = Val(txtweek.Text)
endweek = Val(txtweek1.Text)
Dim filename As String
filename = App.Path & "\productdata.csv"
Open filename For Input As #1
 Do
    Input #1, branch, week, prod, Item, gen, sales
    Dim fileweek As Integer
    fileweek = Val(week)
    If fileweek >= startweek And fileweek <= endweek Then
        If branch = "br01" Then
          totalbr0(1) = totalbr0(1) + sales
        End If
        If branch = "br02" Then
            totalbr0(2) = totalbr0(2) + sales
         End If
         If branch = "br03" Then
            totalbr0(3) = totalbr0(3) + sales
         End If
         If branch = "br04" Then
            totalbr0(4) = totalbr0(4) + sales
        End If
        If branch = "br05" Then
          totalbr0(5) = totalbr0(5) + sales
        End If
        If branch = "br06" Then
          totalbr0(6) = totalbr0(6) + sales
        End If
        If branch = "br07" Then
          totalbr0(7) = totalbr0(7) + sales
        End If
        If branch = "br08" Then
          totalbr0(8) = totalbr0(8) + sales
        End If
    End If
Loop Until EOF(1)
Close #1
End Sub

I'm not asking anyone to do my "schoolwork" for me, i'm hoping someone will be able to lay out what i'd have to do in lamens terms because im a novice programmer, and up to now the program was going quite well. Thankyou.
sepalm is offline   Reply With Quote
Old Nov 22nd, 2007, 10:39 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Degree project help! Sorting an array

Twenty-five ways.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 22nd, 2007, 10:58 AM   #3
sepalm
Newbie
 
Join Date: Nov 2007
Posts: 5
Rep Power: 0 sepalm is on a distinguished road
Re: Degree project help! Sorting an array

Quote:
Originally Posted by DaWei View Post
I appreciate that, and im sorry but i didn't understand it. if someone could just write a standardised bubblesort in pseudo code i might be able to work it out. Thanks.
sepalm is offline   Reply With Quote
Old Nov 22nd, 2007, 10:59 AM   #4
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3 kruptof is on a distinguished road
Re: Degree project help! Sorting an array

Quote:
Originally Posted by sepalm View Post
conclusion bubble sort would be best?
How did you arrive at that?
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Nov 22nd, 2007, 11:07 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Degree project help! Sorting an array

So you only want us to do HALF your work for you? How about an explanation, instead. No frills for optimization.

Compare the first element to every other element in the array. If the compared element is smaller, swap 'em and continue. This will bubble the smallest item to the top.

Move to the second element. Come it to every remaining element in the array. Swap if necessary.

Rinse and repeat until done.

For a descending sort, reverse the nature of the comparison.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 22nd, 2007, 11:17 AM   #6
sepalm
Newbie
 
Join Date: Nov 2007
Posts: 5
Rep Power: 0 sepalm is on a distinguished road
Re: Degree project help! Sorting an array

how is explaining the concept of a bubble sort and how it's done DOING half the work for me?

Thankyou for the explanation, it made more sense than most of the tutorials that i've looked at on the internet. I'll attempt to apply it.
sepalm is offline   Reply With Quote
Old Nov 22nd, 2007, 11:28 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Degree project help! Sorting an array

Writing pseudocode, as you asked, would be doing half the work for you. You'd only need a translator, not a thought process.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 22nd, 2007, 11:32 AM   #8
sepalm
Newbie
 
Join Date: Nov 2007
Posts: 5
Rep Power: 0 sepalm is on a distinguished road
Re: Degree project help! Sorting an array

by writing pseudo code i literally just meant, could you write the standard form of a bubble sort

for example an if statement as
if (condition, statement)

i don't want someone to literally just write the code so i can copy it, that's of no use to me. I want to learn it, because without learning it i could be questioned on it and not know what i'm talking about.
sepalm is offline   Reply With Quote
Old Nov 22nd, 2007, 11:36 AM   #9
null_ptr0
12 years old
 
Join Date: Nov 2007
Posts: 80
Rep Power: 1 null_ptr0 is on a distinguished road
Re: Degree project help! Sorting an array

Quote:
Originally Posted by wikipedia
pseudocode Syntax (Toggle Plain Text)
  1. procedure bubbleSort( A : list of sortable items ) defined as:
  2. for each i in 1 to length(A) do:
  3. for each j in length(A) downto i + 1 do:
  4. if A[ j -1 ] > A[ j ] then
  5. swap( A[ j - 1], A[ j ] )
  6. end if
  7. end for
  8. end for
  9. end procedure
Quote:
Originally Posted by paked.net
c++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2.  
  3. int BubbleSort(int [], int);
  4.  
  5. int main()
  6. {
  7. const int NUMEL = 10;
  8. int nums[NUMEL] = {22,5,67,98,45,32,101,99,73,10};
  9. int i, moves;
  10.  
  11. moves = BubbleSort(nums, NUMEL);
  12.  
  13. cout << "The sorted list, in ascending order, is:\n";
  14. for (i = 0; i < NUMEL; ++i)
  15. cout << " " <<nums[i];
  16.  
  17. cout << '\n' << moves << " were made to sort this list\n";
  18.  
  19. return 0;
  20. }
  21.  
  22. int BubbleSort(int num[], int numel)
  23. {
  24. int i, j, grade, moves = 0;
  25.  
  26. for ( i = 0; i < (numel - 1); i++)
  27. {
  28. for(j = 1; j < numel; j++)
  29. {
  30.  
  31. if (num[j] < num[j-1])
  32. {
  33. grade = num[j];
  34. num[j] = num[j-1];
  35. num[j-1] = grade;
  36. moves++;
  37. }
  38. }
  39. }
  40.  
  41. return moves;
  42. }
Really, just search these things up.
null_ptr0 is offline   Reply With Quote
Old Nov 22nd, 2007, 5:33 PM   #10
sepalm
Newbie
 
Join Date: Nov 2007
Posts: 5
Rep Power: 0 sepalm is on a distinguished road
Re: Degree project help! Sorting an array

Managed to get it working, and now i've done it i actually understand a bubble sort.

Thanks dawei and null_ptr0
sepalm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
tasm sorting 5 integers with array akioshin Assembly 15 Oct 30th, 2007 1:45 AM
sorting an array by field cwl157 C 4 Apr 4th, 2007 2:48 PM
Sorting an array of objects oNe8 Java 2 Feb 22nd, 2006 10:59 PM
Sorting a Numeric Array little_valaree Java 2 Nov 21st, 2005 11:00 AM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 2:36 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC