1
$\begingroup$

I'm trying to figure out how to create an array with $n$ elements in it and then fill it with numbers than I input from the keyboard.

I understand how to do this logically from some experience I have in programming, but I don't know what command to use.

I know that I'd need to create an array with a length of $n$, and then most likely populate it using a For loop.

However, I have no clue as to what commands I need to use in the generation of the array, nor do I understand how to access an element given its index in the array.

EDIT

Essentially, I'm supposed to begin by taking an input n, which is an integer. Then I need to input n more numbers, by keyboard(which I assume is the same as input during execution, so I'll take a look at the Input command again), however I'm then supposed to calculate how many numbers the inputted number is divisible by. This made me think that I'm taking in an array of numbers, then going from 0 to n, calculating how many numbers the element is divisible by, and adding them into a variable. I'm sorry if this doesn't make much sense, I'm struggling to translate some of these terms into English

$\endgroup$
  • 2
    $\begingroup$ Could you explain the context in which you would like to do this? If you are OK with typing in the values, then just type them in at the assignments stage, i.e. yourarray = {{first, row, values}, {second, row, values}, ...}. If you mean that you have to take user input during execution, you could look at Input. A very brute force approach could be Table[Input[], numRows, numColumns] where you substitute the actual number of rows and columns you want in the array. $\endgroup$ – MarcoB Jun 18 '18 at 15:33
  • $\begingroup$ Essentially, I'm supposed to begin by taking an input n, which is an integer. Then I need to input n more numbers, by keyboard(which I assume is the same as input during execution, so I'll take a look at the Input command again), however I'm then supposed to calculate how many numbers the inputted number is divisible by. This made me think that I'm taking in an array of numbers, then going from 0 to n, calculating how many numbers the element is divisible by, and adding them into a variable. I'm sorry if this doesn't make much sense, I'm struggling to translate some of these terms into english $\endgroup$ – Ammonium Jun 18 '18 at 15:41
  • 1
    $\begingroup$ Look at Divisors in the documentation as well. The number of integer divisors would then be given by Length[Divisors[yourNumber]] for each one of the numbers you want. Also, in your description I do not understand what the "other" numbers are for, i.e. 1) you ask the user for a number; 2) you calculate the number of divisors for that number; 3) ....? What is your stopping condition? I.e. when do you stop accepting numbers? $\endgroup$ – MarcoB Jun 18 '18 at 15:54
  • 1
    $\begingroup$ Additionally, you should add the description of your problem you gave in comments to your original question. You can always edit that question (see the edit link at the bottom). You might also want to take the tour. $\endgroup$ – MarcoB Jun 18 '18 at 15:55
  • $\begingroup$ Welcome! To make the most of Mma.SE start by taking the tour now. It will help us to help you if you write an excellent question. Edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$ – rhermans Jun 18 '18 at 17:55
1
$\begingroup$

Try this interactive approach base on Manipulate:

(* These are just two helper functions to simplify the alignments in Grid *)
right = Item[##, Alignment -> Right] &;
left = Item[##, Alignment -> Left] &;

Manipulate[
 With[{divlist = Divisors[number]},
   Grid[{
     {right["list of divisors of " <> ToString[number] <> ":"], left[divlist]},
                            {right["tot. number of divisors:"], left[Length[divlist]]}
   }]
 ],
 {{number, 1024}, InputField[]}
]

Manipulate panel

Type a new number in the input field "number" and hit Return to get the calculations updated.

$\endgroup$
0
$\begingroup$

To write an array into a notebook, just click on the notebook and type the numbers separated by comma and surrounded by curly brackets, like this

{1, 2, 3, 4, 5}

For the kernel to actual interpret what you typed, at the end press Shift-Enter.

You can assign that list to a variable name data using Set, the single = sign. Do not confuse Set (=) with Equal (==).

data = {1, 2, 3, 4, 5}

You can also insert arrays or tables using the Insert > Table/Matrix menu. After entering a number you move to the next fields using Tab.

enter image description here

To see the Divisors of you data you can do

Divisors[{1, 2, 3, 4, 5}]

or

Divisors[data]
(* {{1}, {1, 2}, {1, 3}, {1, 2, 4}, {1, 5}} *)

And to count the divisors you can ask for the Length of the List of divisors

Length /@ Divisors[data]
(* {1, 2, 2, 3, 2} *)

The /@ is a shortcut to Map the function on the left, in this case Length to each element of the list on the right, in this case the list of divisors.

Notice the links to the documentation.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.