## Generate exercises with a button?

General discussion about Mathematica features and functionality...
Forum Rules
By using the Wolfram Faculty Program Forum, you agree not to post any abusive, obscene, vulgar, slanderous, hateful, threatening, or sexually oriented material. Wolfram Faculty Program Forum administrators have the right to remove, edit, move or close any topic at any time should we see fit.

Personal Information: Posts in this forum may be viewed by non-members; however, the forum prohibits non-members from viewing your profile. Although your email address is hidden from both non-members and members, your account is initially configured to allow members to contact you via email through the forum. If you wish to hide your profile, or prohibit others from contacting you directly, you may change these settings by updating your profile through the User Control Panel.

Attachments: Attachments are not currently enabled on this forum. To share a file with others on this site, simply upload your file to the online storage service of your choice and include a link to the file within your post. If your school does not offer an online file storage and sharing service, the following sites provide free basic online file storage and sharing: Mozy, FilesAnywhere, Adrive, and KeepandShare.

### Generate exercises with a button?

Hi all:

I've made some code that generates arbitrary factorable quadratics, and displays the solutions when clicked. It's below:
_______________
(* Coefficient Functions *)
factor[x_] := RandomInteger[{1, 5}];
factor2[x_] := RandomInteger[{-5, 5}];

(* Generating the random exercises *)
table1 = Table[{
Row[{n, ". "}],
Expand[(s = (factor[n] x + factor2[n]))*(s1 = (factor[n] x +
factor2[n]))] // TraditionalForm, " = ",
s* s1 // TraditionalForm}, {n, 1, 10}];

FlipView[{

Style[Grid[table1, ItemStyle -> {{-1 -> White}, Automatic},
Dividers -> {{True, False, False, False, True}, All},
Spacings -> {1, 1.5}], FontFamily -> Times],

Style[Grid[table1, ItemStyle -> {{-1 -> Blue}, Automatic},
Dividers -> {{True, False, False, False, True}, All},
Spacings -> {1, 1.5}], FontFamily -> Times]

}]
_________________

Now, what I'd like to do is make a CDF out of this with a button a the top, where the students could regenerate a new list of 10 if they would like to do so. How can I do that?

Thanks!

Rob

Rob_Holman

Posts: 14
Joined: Wed Feb 29, 2012 7:03 pm
Organization: The Walker School
Department: Mathematics and Science

### Re: Generate exercises with a button?

Rob,

Do you want to only display one set of exercises at a time, or do you want clicking the button to generate a new set of exercises but not override the previous set? If it's the latter, this is very simple: just use Button, give it a label, and for the second argument, copy and paste your code inside a set of parentheses, and add //Print to the end of the second argument. There are two example files here:
http://members.wolfram.com/mmorrison/FacultyProgram.html

The first is a notebook with all of the code, the second is a CDF, without code, but which has the button and is still functional.

Hope that helps,
-Michael

Michael_Morrison

Posts: 42
Joined: Fri Sep 11, 2009 9:50 pm
Organization: Wolfram Research, Inc.

### Re: Generate exercises with a button?

What I ended up doing is making a Manipulate panel with the ContinuousAction set to None. Thus, the parameters can be changed, and a new batch of exercises isn't constructed until the update icon (U in the top right) is clicked.

Now... for the icing on the cake, is there a way to embed a more obvious button in the gray upper part of the Manipulate panel that will do the same thing as that little "U" icon? I'd love to have a big "Generate Exercises" button sitting below the input fields.

Code is below -- and thanks for the help!

Rob

_______________

Manipulate[

(*Coefficient Functions*)

factor2[x_] := RandomInteger[{minconst, maxconst}];

(*Generating the random exercises*)

table1 =
Table[{Row[{n, ". "}],
Expand[(s = (factor[n] x + factor2[n]))*(s1 = (factor[n] x +
factor2[n]))] // TraditionalForm, " = ",
s*s1 // TraditionalForm}, {n, 1, exer}];

FlipView[{

Style[Grid[table1, ItemStyle -> {{-1 -> White}, Automatic},
Dividers -> {{True, False, False, False, True}, All},
Spacings -> {1, 1.5}], FontFamily -> Times],

Style[Grid[table1, ItemStyle -> {{-1 -> Blue}, Automatic},
Dividers -> {{True, False, False, False, True}, All},
Spacings -> {1, 1.5}], FontFamily -> Times]}],

{{exer, 10, "Number of Exercises"}, {10, 15, 20}}, {{minlead, 1,
{{minconst, -5, "Minimum Factor Constant Term"}, InputField},
{{maxconst, 5, "Maximum Factor Constant Term"}, InputField},
TrackedSymbols -> All, ContinuousAction -> None, Alignment -> Center
]

Rob_Holman

Posts: 14
Joined: Wed Feb 29, 2012 7:03 pm
Organization: The Walker School
Department: Mathematics and Science

### Re: Generate exercises with a button?

Normally, I insert a dummy variable in the Manipulate expression and bump the value when the button is pressed. The change in value triggers an update if the variable is tracked. In your case, it would be the only tracked variable. Rough idea:

Manipulate[generate; (* dummy statement, for dependency *)
StringForm["Exercise set ``", x], {x, 0, 1}, {{generate, 0}, None},
Button["Generate Exercises", generate++],
TrackedSymbols :> {generate}]

However, in your particular code, you can use table1, instead of inserting a new dummy variable. I stuck the exercise set generating code in a new function, GenerateExercises[]. (Note: ContinuousAction -> None should be omitted below.)

Manipulate[(*Coefficient Functions*)(*Generating the random exercises*)
Style[Grid[table1, ItemStyle -> {{-1 -> White}, Automatic},
Dividers -> {{True, False, False, False, True}, All},
Spacings -> {1, 1.5}], FontFamily -> Times],(*Answers Shown*)
Style[Grid[table1, ItemStyle -> {{-1 -> Blue}, Automatic},
Dividers -> {{True, False, False, False, True}, All},
Spacings -> {1, 1.5}], FontFamily -> Times]}], {{exer, 10,
"Number of Exercises"}, {10, 15, 20}},
{{minconst, -5, "Minimum Factor Constant Term"}, InputField},
{{maxconst, 5, "Maximum Factor Constant Term"}, InputField},
(*{{table1,{}},None},*)

Button["Generate Exercises", GenerateExercises[]],
TrackedSymbols :> {table1}, Alignment -> Center,
factor2[x_] := RandomInteger[{minconst, maxconst}];
GenerateExercises[] := (table1 =
Table[{Row[{n, ". "}],
Expand[(s = (factor[n] x + factor2[n]))*(s1 = (factor[n] x +
factor2[n]))] // TraditionalForm, " = ",
s*s1 // TraditionalForm}, {n, 1, exer}]);
GenerateExercises[])]

Cheers,
Michael

Michael_Rogers

Posts: 10
Joined: Mon May 24, 2010 3:28 pm
Organization: Emory University
Department: Oxford College