none
adding, average and how many numbers in an array RRS feed

  • Question

  • i have to add, get the average and find out how many numbers are in an array and im not sure how to do that. any ideas on what i can do? i dont know how to make them into subs that i could use. any help?
    Sunday, November 1, 2015 12:51 AM

Answers

  • If you have all the numbers in an array called  value[], you do number=Array.getItemCount(value)

    for i=1 to number    sum=sum+value[i]   endfor

    and average=sum/number


    Jan [ WhTurner ] The Netherlands

    Sunday, November 1, 2015 12:43 PM
    Answerer
  • The flow of your program should be:

    1. enter or read a number of values (my last eexample.)

    2. use getitemcount to know the number of values

    3. calculate the sum for these values

    4. calculate the average.

    I  hope you can do it with these examples , because I am going offline now. Tomorrow I will be back.


    Jan [ WhTurner ] The Netherlands

    Sunday, November 1, 2015 6:58 PM
    Answerer

All replies

  • If you have all the numbers in an array called  value[], you do number=Array.getItemCount(value)

    for i=1 to number    sum=sum+value[i]   endfor

    and average=sum/number


    Jan [ WhTurner ] The Netherlands

    Sunday, November 1, 2015 12:43 PM
    Answerer
  • If you have all the numbers in an array called  value[], you do number=Array.getItemCount(value)

    for i=1 to number    sum=sum+value[i]   endfor

    and average=sum/number


    Jan [ WhTurner ] The Netherlands

    could you show me what it would look like in a sub? what all would go in there? would it be a sub name then the formula and then end the sub? and those formulas work when you don't have specific numbers to use?

    Sunday, November 1, 2015 5:29 PM
  • If you have all the numbers in an array called  value[], you do number=Array.getItemCount(value)

    for i=1 to number    sum=sum+value[i]   endfor

    and average=sum/number


    Jan [ WhTurner ] The Netherlands

    could you show me what it would look like in a sub? what all would go in there? would it be a sub name then the formula and then end the sub? and those formulas work when you don't have specific numbers to use?


    and they all have to be a separate sub for my program so i need to know how to do that in 3 subs.
    Sunday, November 1, 2015 5:31 PM
  • You can put each of my lines into a sub, e.g.

    sub calc_sum

      for i= 1 to number

         sum=sum+value[i]

      endfor

    endsub

    and the other two are even easier.


    Jan [ WhTurner ] The Netherlands

    Sunday, November 1, 2015 5:46 PM
    Answerer
  • You can put each of my lines into a sub, e.g.

    sub calc_sum

      for i= 1 to number

         sum=sum+value[i]

      endfor

    endsub

    and the other two are even easier.


    Jan [ WhTurner ] The Netherlands


    so i do that but change the formula for each one? just do it like you did and change the name and how you did it would be ok?
    Sunday, November 1, 2015 5:56 PM
  • You can put each of my lines into a sub, e.g.

    sub calc_sum

      for i= 1 to number

         sum=sum+value[i]

      endfor

    endsub

    and the other two are even easier.


    Jan [ WhTurner ] The Netherlands


    so i do that but change the formula for each one? just do it like you did and change the name and how you did it would be ok?
    for the how many numbers sub is there a for statement or just the formula?
    Sunday, November 1, 2015 5:58 PM
  • The number of items in an array is reported by  Array.getItemCount(name_of_array) . So I don't see any advantage to use a subroutine 

    If you want to see how to use a statement : type in the SmallBasic window: Array  and there pops up how to use the statement (on the right) This works for all SB-statements. Didn't you use these very helpfull explanations?


    Jan [ WhTurner ] The Netherlands

    Sunday, November 1, 2015 6:14 PM
    Answerer
  • The number of items in an array is reported by  Array.getItemCount(name_of_array) . So I don't see any advantage to use a subroutine 

    If you want to see how to use a statement : type in the SmallBasic window: Array  and there pops up how to use the statement (on the right) This works for all SB-statements. Didn't you use these very helpfull explanations?


    Jan [ WhTurner ] The Netherlands


    i get how to use them i was just wondering. and i would need a sub because it is one of my menu items or would i just put that in my menu directly?
    Sunday, November 1, 2015 6:28 PM
  • Both are possible. Use what you like (or is required by teacher ;-) )

    Jan [ WhTurner ] The Netherlands

    Sunday, November 1, 2015 6:30 PM
    Answerer
  • The number of items in an array is reported by  Array.getItemCount(name_of_array) . So I don't see any advantage to use a subroutine 

    If you want to see how to use a statement : type in the SmallBasic window: Array  and there pops up how to use the statement (on the right) This works for all SB-statements. Didn't you use these very helpfull explanations?


    Jan [ WhTurner ] The Netherlands


    i get how to use them i was just wondering. and i would need a sub because it is one of my menu items or would i just put that in my menu directly?
    I dont yet have an array for get item count so what do i do? do i need to make one?
    Sunday, November 1, 2015 6:31 PM
  • The number of items in an array is reported by  Array.getItemCount(name_of_array) . So I don't see any advantage to use a subroutine 

    If you want to see how to use a statement : type in the SmallBasic window: Array  and there pops up how to use the statement (on the right) This works for all SB-statements. Didn't you use these very helpfull explanations?


    Jan [ WhTurner ] The Netherlands


    i get how to use them i was just wondering. and i would need a sub because it is one of my menu items or would i just put that in my menu directly?

    I dont yet have an array for get item count so what do i do? do i need to make one?
    i am making comments for my program and i need to put inputs and outputs but what would those be? Im confused as to what they really do so i know what to put for those. thanks
    Sunday, November 1, 2015 6:42 PM
  • Yes, dependent how you have to enter your values (read from file or enter from keyboard).

    For the latter case:

    val=1  'any value not equal zero

    while val<>0

      val = Textwindow.Read()

      i=i+1

      value[i]=val

    endwhile

    value[i] = ""   ' to remove the last value 0

    Then you can enter many numbers, and when you enter a 0 the program continues after the endwhile.


    Jan [ WhTurner ] The Netherlands

    Sunday, November 1, 2015 6:42 PM
    Answerer
  • Yes, dependent how you have to enter your values (read from file or enter from keyboard).

    For the latter case:

    val=1  'any value not equal zero

    while val<>0

      val = Textwindow.Read()

      i=i+1

      value[i]=val

    endwhile

    value[i] = ""   ' to remove the last value 0

    Then you can enter many numbers, and when you enter a 0 the program continues after the endwhile.


    Jan [ WhTurner ] The Netherlands


    where do i put this if i put the count of numbers in to the menu directly? would this be a sub? I had something like it in my example but not sure how i make it work for my new one. is this an array? and if it is what do i do for the array name?
    Sunday, November 1, 2015 6:53 PM
  • The flow of your program should be:

    1. enter or read a number of values (my last eexample.)

    2. use getitemcount to know the number of values

    3. calculate the sum for these values

    4. calculate the average.

    I  hope you can do it with these examples , because I am going offline now. Tomorrow I will be back.


    Jan [ WhTurner ] The Netherlands

    Sunday, November 1, 2015 6:58 PM
    Answerer