Actionscript questions

help me with some code

Page 1 of 1

6 Replies - 1007 Views - Last Post: 06 May 2009 - 04:55 PM

#1 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

Actionscript questions

Posted 01 May 2009 - 01:14 PM

Im having trouble with finding the biggest number,
i can get it for the first 2 #s but i cant get it to keep going.

i put >>>>>> where the code starts and ends that im having trouble with.

Heres the code

//*********
//Connor Weible
//DC225
//Week 2 HW
// ***Create an Array with 100 numbers in it. Each number should be randomly generated, and somewhere between 1 and 100. 
// ***Tell me the total of all the numbers in the array. 
// ***Tell me the average of all the numbers in the array. 
// Tell me the value of the biggest number in the array. 
// Tell me the value of the smallest number in the array. 
// Sort the array (by hand, DO NOT USE the built in sort() function)
// Tell me the median value of the numbers in the array. 
// ***Tell me the mode of the array. 
// Put all the steps in its own little function
//*****************

package {

	import flash.display.*;

	public class Arrayweible extends MovieClip {

		public function Arrayweible() {

			//declare
			var stuff:Array;
			//initialize
			stuff=new Array(100);
			//fill it with stuff
			for (var i:int=0; i<stuff.length; i++) {
				stuff[i]=Math.floor(Math.random()*100)+1;
			}

			//trace all the random numbers
			for (i=0; i<stuff.length; i++) {
				trace("stuff["+i+"] = "+stuff[i]);
			}

			//random gen code over.


			//Sorting the array
			//how in the HELL do i do this?!
			trace("");
			trace("The array is now sorted");
			//Sorting code end

			//Total of all the numbers in the array
			var total:int=0;
			for (i=0; i<=stuff.length-1; i++) {
				total+=stuff[i];
			}
			trace("");
			trace("Total: "+total);
			//total code end

			//Average of all numbers
			var average:int=0;
			for (i=0; i<=stuff.length-1; i++) {
				total+=stuff[i];
				average=total/100;
			}
			trace("");
			trace("Average: "+average);
			//average code end
			

	>>>>>>	//Value of the largest number
			var largest:int=0;
			var champ:int;
			var competer:int=0;
			for (var i:int=0; i<stuff.length-1; i++) {
				for (var j:int = i + 1; j < stuff.length; j++) {
					if (stuff[i]>stuff[j]) {
						competer=stuff[i];
						i++
					} else {
						competer=stuff[j];
						i++
					}
					largest=champ;
				}
			}
			trace("");
			trace("The largest number is: "+largest);
	>>>>>	//largest code end

			//Value of the smallest number
			var smallest:int=0;
			var loser:int=0;
			var compete:int=0;
			for (i=0; i<stuff.length-1; i++) {
				
			}


			trace("");
			trace("The smallest number is: "+smallest);
			//smallest code end

			//Median of the array
			trace("");
			trace("The median is: "+stuff[49]);//traces middle!!!
			//median code end

			//Mode of the array
			//vars
			var count:int=0;
			var prevCount:int=0;
			var totalCount:int=0;
			//countings, also may need to declare i again once put into functions.
			for (var i:int=0; i <stuff.length; i++) {
				for (var j:int = i + 1; j < stuff.length; j++) {
					if (stuff[i]==stuff[j]) {
						count++;
						i++;
					}
				}
				//counting vars
				if (count>=prevCount) {
					totalCount++;
				}

				prevCount=count;

				count=0;
			}
			trace("");
			trace("The mode is: "+totalCount);
			//Mode code end



		}

	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Actionscript questions

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,207
  • Joined: 07-September 06

Re: Actionscript questions

Posted 01 May 2009 - 09:18 PM

I would change this:

var largest:int=0;
			var champ:int;
			var competer:int=0;
			for (var i:int=0; i<stuff.length-1; i++) {
				for (var j:int = i + 1; j < stuff.length; j++) {
					if (stuff[i]>stuff[j]) {
						competer=stuff[i];
						i++
					} else {
						competer=stuff[j];
						i++
					}
					largest=champ;
				}
			}
			trace("");
			trace("The largest number is: "+largest);


Out for something more like so:

var largestIndex:Number = 0;
for(var i:Number = 0; i<stuff.length; i++){
  if(stuff[i] > stuff[largestIndex]){
	largestIndex = i;
  }
}
trace("\nLargest number: "+stuff[largestIndex]);


Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 SoLi  Icon User is offline

  • andydust.com

Reputation: 41
  • View blog
  • Posts: 1,438
  • Joined: 27-January 02

Re: Actionscript questions

Posted 02 May 2009 - 04:56 AM

or just...
stuff.sort(Array.NUMERIC)[stuff.length - 1];


This post has been edited by SoLi: 02 May 2009 - 04:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

Re: Actionscript questions

Posted 05 May 2009 - 03:34 PM

Alright i got everything all sorted out.

Once last question, how would i find the mode of my array?

i put the mode code under that >>>>>>>>>>>>>>>>>>>>>>>>


package {

	import flash.display.*;

	public class Arrayweible extends MovieClip {
		var stuff:Array;
		var i:int;
		var j:int;
		var total:int=0;
		var count:int=0;
		var prevCount:int=0;
		var totalCount:int=0;
		public function Arrayweible() {

			//declare
			stuff=new Array(100);
			//fill it with stuff
			for (i=0; i<stuff.length; i++) {
				stuff[i]=Math.floor(Math.random()*100)+1;

			}
			sortfun();
			//trace all the random numbers
			for (i=0; i<stuff.length; i++) {

				trace("stuff["+i+"] = "+stuff[i]);
			}
			//random gen code over.
			totalfun();
			averagefun();
			largestfun();
			smallestfun();
			medianfun();
			modefun();
		}

		//Sorting the array-------------FIX THIS
		public function sortfun():void {
			var tmp:int;
			for (i=0; i < stuff.length - 1; i++) {
				for (var j:int= i + 1; j < stuff.length; j++) {
					if (stuff[i]>stuff[j]) {
						tmp=stuff[i];
						stuff[i]=stuff[j];
						stuff[j]=tmp;
					}

				}
			}
		}
		//Sorting code end*/

		public function totalfun():void {
			//Total of all the numbers in the array
			for (var i:int=0; i<=stuff.length-1; i++) {
				total+=stuff[i];
			}
			trace("");
			trace("Total: "+total);
		}
		//total code end

		function averagefun():void {
			//Average of all numbers
			var average:int=0;
			for (var i:int=0; i<=stuff.length-1; i++) {
				total+=stuff[i];
				average=total/100;
			}
			trace("");
			trace("Average: "+average);
		}
		//average code end

		function largestfun():void {
			//Value of the largest number
			var largest:int=0;
			var champ:int;
			var competer:int=0;
			var competer2:int;
			for (var i:int=0; i<stuff.length-1; i++) {
				for (var j:int= i + 1; j < stuff.length; j++) {
					if (stuff[i]>stuff[j]) {
						competer=stuff[i];
					} else {
						competer=stuff[j];
					}
					competer2=competer;
					if (competer>champ) {
						champ=competer2;
					}
					largest=champ;
				}
			}
			trace("");
			trace("The largest number is: "+largest);
		}
		//largest code end

		function smallestfun():void {
			//Value of the smallest number
			var smallest:int=0;
			var loser:int=1000;
			var competer3:int=0;
			var competer4:int;
			for (var i:int=0; i<stuff.length-1; i++) {
				for (var j:int= i + 1; j < stuff.length; j++) {
					if (stuff[i]>stuff[j]) {
						competer3=stuff[j];

					} else {
						competer3=stuff[i];
					}
					if (competer3<loser) {
						loser=competer3;
					}
					smallest=loser;
				}
			}
			trace("");
			trace("The smallest number is: "+smallest);
		}
		//smallest code end

		//Median of the array
		function medianfun():void {

			var median:int=stuff[49]+stuff[50];
			trace("");
			trace("The median is: " +median/2);
		}
		//median code end

	>>>>>>>>>>>>>>>>>>>>>>>	//--------------Mode of the array--------------FIX THIS

		function modefun():void {
			//countings
			var stuffmode:int=0;
			for (i=0; i <stuff.length; i++) {
				for (j= i + 1; j < stuff.length; j++) {
					if (stuff[i]==stuff[j]) {
						stuffmode=stuff[i];
						count++;
						i++;
					}else{
						stuffmode=stuff[j];
					}
				}
				//counting vars
				if (count>=prevCount) {
					totalCount++;
				}

				prevCount=count;

				count=0;
			}
			trace("");
			trace("The mode is: "+stuffmode+" (not working)");
		}

		//Mode code end


	}

}	


This post has been edited by MrKarazy: 05 May 2009 - 06:56 PM

Was This Post Helpful? 0
  • +
  • -

#5 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,207
  • Joined: 07-September 06

Re: Actionscript questions

Posted 05 May 2009 - 09:01 PM

Something like so may work:

WARNING - Untested code:

function mode(arr:Array):Number{
  arr.sort();
  var smallest:Number = arr[0];
  var countArray:Array = new Array();
  // this loop is responsible for getting the the number of occurances of each element correct.
  for(var i:Number = 0; i<arr.length; i++){
	if(countArray[arr[i]-smallest] == null){
	  countArray[arr[i]-smallest] = 1;
	}
	else{
	  countArray[arr[i]-smallest]++;
	}
  }
  // this loop is used to find the largest value in the array without changing around the indexes
  var largestIndex:Number = 0;
  for(var i:Number = 0; i<stuff.length; i++){
	if(countArray[i] == null){
	  countArray[i] = -1; // obviously out of bounds value
	}
	if(countArray[i] > countArray[largestIndex]){
	  largestIndex = i;
	}
  }
  // should return the most common number on arr
  return arr[largestIndex+smallest];
}


Not sure that works, but I think it may (or at least be close).

Hope that helps.

<EDIT>

Okay, as thought, the above code didn't work. Here is some that seems to:

var test:Array = new Array(1,1,1,2,3,4,5,6,7,7,7,7,7,7,7,8,9,8,9,0,0,0, 555, 555, 555, 555, 555, 555, 555, 555, 555, 555, 55, 555, 55, 5555, 555, 555, 555, 555, 555, 555);
function mode(arr:Array):Number{
  arr.sort();
  for(var j:Number=0; j<arr.length; j++){
	  trace(arr[j]);
  }
  var smallest:Number = arr[0];
  var countArray:Array = new Array();
  for(var i:Number = 0; i<arr.length; i++){
	if(countArray[arr[i]-smallest] == null){
	  countArray[arr[i]-smallest] = 1;
	}
	else{
	  countArray[arr[i]-smallest]++;
	}
  }
  var largestIndex:Number = 0;
  for(i = 0; i<countArray.length; i++){
	if(countArray[i] == null){
	  countArray[i] = -1;
	}
	if(countArray[i] > countArray[largestIndex]){
	  largestIndex = i;
	}
  }
  return arr[arr.indexOf(largestIndex+smallest)];
}
trace("Mode of "+test.toString()+" is "+mode(test));


(Comes with test case)

As far as I can tell the only real problem with my last code was I wasn't returning the correct arr index, so it sometimes had issues, but I fixed that.

NOTE - This is by no means the most efficient way of doing things, just the first one I came up with. (It uses a lot of extra memory with the countArray).

Hope that helps.

</EDIT>
Was This Post Helpful? 0
  • +
  • -

#6 MrKarazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-May 09

Re: Actionscript questions

Posted 06 May 2009 - 07:40 AM

Ill try that and see if i can get it working,
but im still not sure how you were able to find the mode,If you did.

Thanks for all your help
Was This Post Helpful? 0
  • +
  • -

#7 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,207
  • Joined: 07-September 06

Re: Actionscript questions

Posted 06 May 2009 - 04:55 PM

The edit has working code.

It goes through the array and counts the instances of each number (sorts the array first), then goes through, and finds the largest index in the array (can't sort as it will change the index, and you won't be able to get back to the original array index, though, I think it may have an easier way to go...) Then it returns the number that appears the most in the array.

HTMS
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1