electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Articles  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links  |  Store
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
Live streaming at radio.electro-music.com

  host / artist show at your time
  Paulo Santos Synth-ing NºX
Please visit the chat
 Forum index » DIY Hardware and Software » ChucK programming language
Help with sort functions - Arrays and logic
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [4 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
jutar



Joined: Apr 25, 2016
Posts: 2
Location: Connecticut

PostPosted: Mon Apr 25, 2016 7:18 pm    Post subject: Help with sort functions - Arrays and logic
Subject description: Trying to write using randomly generated sets, stuck on the logic
Reply with quote  Mark this post and the followings unread

I'm working on a project using ChucK to generate random sets. The end goal is to use those sets to play a fugue-style piece, with each successive variation and response as a manipulation of the set. I got ChucK to shuffle my tones, but I'm stuck sorting them into Normal form. Once I get this down, inversions will be easy, though Prime form will be its own headache. Here's what I have so far. I'm hitting two blocks. 1- the swap function overwrites items that shouldn't be in the swap. 2- I get an array out of bounds error when printing the results and I can't find the source. Here's my code so far:

Code:

int i,j, dummy, iMin, hold;
[5, 3, 6] @=> int a[];

/* advance the position through the entire array */
/*   (could do j < n-1 because single element is also min element) */
for ( 0 => j; j <= a.cap()-1; j++) {
   
    /* assume the min is the first element */
     j => iMin;
     
    /* test against elements after j to find the smallest */
    for ( j+1 => i; i < a.cap()-1 ; i++) {

        if(a[i] < a[iMin]){
            i => iMin;
        } 
    }
    <<< a[i], i, a[iMin], iMin, a[j], j, "pre">>>;
    if (iMin != j) {
        a[j] => dummy;
       
        a[iMin] => a[j];
        <<<a[i], i, a[iMin], iMin, a[j], j, "mid">>>;
        dummy => a[iMin]; 
    }
  <<< a[i], i, a[iMin], iMin, a[j], j, "post">>>;
}
for(int k; k < a.cap(); k++) <<< "Set1[", k,"] = ", a[k] >>>;
[/code]
Back to top
View user's profile Send private message
randbits



Joined: Jun 28, 2016
Posts: 6
Location: Utica, NY, USA

PostPosted: Tue Jun 28, 2016 6:22 pm    Post subject:  Fixed
Subject description: now the selection sort algorithm
Reply with quote  Mark this post and the followings unread

The right-to-left assignment of values can make things a bit confusing.
The code now sorts in ascending order correctly.

Code:

int i,j, dummy, iMin;
[5, 10, 3, 6, 1, 9, 2, 7, 8] @=> int a[];

/* advance the position through the entire array */
for ( 0 => j; j < a.cap(); j++) {
    /* assume the min is the first item */
    j => iMin;
    /* test against elements after j to find the smallest */
    for ( j+1 => i; i < a.cap() ; i++) {
        if(a[i] < a[iMin]) {
           i => iMin;
        }
    }
    a[j] => dummy;
    a[iMin] => a[j];
    dummy => a[iMin];
}
for(int k; k < a.cap(); k++) <<< "Set1[", k,"] = ", a[k] >>>;


This algorithm is very slow. If you feel like taking on a challenge, implement mergeSort instead (https://en.wikipedia.org/wiki/Merge_sort) Wicked fast!

Cheers
Back to top
View user's profile Send private message
MusicMan11712



Joined: Aug 08, 2009
Posts: 977
Location: Out scouting . . .

PostPosted: Wed Jun 29, 2016 12:22 pm    Post subject: Reply with quote  Mark this post and the followings unread

Why would someone want to sort values like this as part of making a music generating program? If randomly generated numbers are assigned to an array in the order they are generated, it seems to me having an algorithm to sort the numbers into some other non-random order defeats the purpose of having randomly generated numbers in the first place. Just wondering.

Steve
Back to top
View user's profile Send private message
Blue Hell
Site Admin


Joined: Apr 03, 2004
Posts: 22062
Location: The Netherlands, Enschede
Audio files: 206
G2 patch files: 319

PostPosted: Thu Jun 30, 2016 2:45 pm    Post subject: Reply with quote  Mark this post and the followings unread

The idea to me seems to be to make sets of numbers. To do operations on sets more efficiently it makes sense to sort the elements (although the order of the elements in a set is, by definition, irrelevant).

Anyway, the numbers would still be random, and it would be very easy to extract them again in a random order by randomly indexing the elements.

Sorry for not being able to answer the ChucK question.

_________________
Jan
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [4 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » ChucK programming language
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
e-m mkii

Please support our site. If you click through and buy from
our affiliate partners, we earn a small commission.


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use