[Math] How to make a table of arithmetic exercises ? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
SpeedMaster
Posts: 394
Joined: 12 Nov 2016, 16:09

[Math] How to make a table of arithmetic exercises ?

12 Jun 2019, 17:29

Hello,
I'm trying to make a simple arithmetic exercise table (addition only, substration only or both) :think:

my problems:
  • some generated exercises are not well mixed (they only contain additions or too few substrations)
  • generating a table that only contains subtraction exercises is also difficult.

I think I'm doing it the wrong way. :?
Maybe someone else can do a better job. :roll:
Thank you for your help.

Here is my attempt: :problem:

Code: Select all

;============================================ settings ===============================================

nlines:=4           ; number of lines you want
nlength:=5          ; length of a line (amount of numbers per line) note the last entry (maxindex) represents the sum of the previous entries
mindigits:=2        ; min length for a number
maxdigits:=3        ; max length for a number
type:=0             ; -1=only substraction, 1=only addition, 0=mixed between positive and negative numbers

;=====================================================================================================


gui, font, s12
gui, add, text, , How to make a table of arithmetic exercises ?`n`nnote: the last entry (maxindex) represents `nthe sum of the previous entries`n
gui, add, button, gok, generate
gui, font, s10
gui, add, edit, w500 h750 vtext,
gui, show
return

ok:
my_table:=arithmeticTable(nlines, nlength, mindigits, maxdigits, type)

guicontrol,,text, % st_printarr(my_table)
return

guiclose:
exitapp
return


;========================= aritmetic exercise generator function =====================================

ArithmeticTable(nlines:=4,nlength:=3,mindigits:=2,maxdigits:=3, type:=0) {

oTable:=[], tot:=0

loop, % nlines {
    loop, % nlength {
        loop, % rand(mindigits,maxdigits)
            (a_index=1) ? (out .= rand(9)) : (out .= rand(0,9))
     out .= "`n"
    }
 map:=strsplit(out, "`n")
 oTable.push(map)
 out:=""
}

for i, r in oTable {
    for j, c in r {
           ((type=0) && rand(2)=1 && (j>1) && sum>oTable[i][j] && j<oTable[i].maxindex()) ?  oTable[i,j] *= -1 : ((type<0) && (j>1) ?  oTable[i,j] *= -1)
           sum += oTable[i,j]
     }
     sum:=0
}


for i, r in oTable {
    for j, c in r
        (j<r.maxindex()) ? (tot += c) :  (oTable[i,j]:=tot)
    tot:=0
}


return oTable

}


;==================================== random number generator ===========================================


; FROM: http://www.autohotkey.com/forum/viewtopic.php?t=54713&postdays=0&postorder=asc&start=0
; EXAMPLES
/*
Rand() ; - A random float between 0.0 and 1.0 (many uses)
Rand(6) ; - A random integer between 1 and 6 (die roll)
Rand("") ; - New random seed (selected randomly)
Rand("", 12345) ; - New random seed (set explicitly)
Rand(50, 100) ; - Random integer between 50 and 100 (typical use)
*/
; RANDOM FUNCTION
Rand( a=0.0, b=1 ) {
   IfEqual,a,,Random,,% r := b = 1 ? Rand(0,0xFFFFFFFF) : b
   Else Random,r,a,b
   Return r
}


;============================================ debugging function =============================================

;String Things by tidbit
;https://autohotkey.com/boards/viewtopic.php?f=6&t=53&sid=5c401643235e7a2e73d769f1e5deac0f
st_printArr(array, depth=5, indentLevel="")
{
   for k,v in Array
   {
      list.= indentLevel "[" k "]"
      if (IsObject(v) && depth>1)
         list.="`n" st_printArr(v, depth-1, indentLevel . "    ")
      Else
         list.=" => " v
      list.="`n"
   }
   return rtrim(list)
}
Last edited by SpeedMaster on 16 Jun 2019, 07:28, edited 2 times in total.
User avatar
YoucefHam
Posts: 344
Joined: 24 Aug 2015, 12:56
Facebook: https://fb.me/YoucefHamDz
Google: [email protected]
GitHub: YoucefHam
Location: Algeria
Contact:

Re: [Math] How to make a table of arithmetic exercises ?

12 Jun 2019, 18:59

try this, but its in String format

Code: Select all

;============================================ settings ===============================================

nlines:=4           ; number of lines you want
nlenght:=8          ; lenght of a line (amount of numbers per line) note the last entry (maxindex) represents the sum of the previous entries
mindigits:=2        ; min lenght for a number
maxdigits:=3        ; max lenght for a number
mixed:=true         ; mixed between positive and negative numbers

;=====================================================================================================
Loop, % nlines
{
	i := A_Index
	Sum := 0
    loop, % nlenght
	{
		j := A_Index
		;~ oTable[i,j] := (mixed ? (Rand() ? "+" : "-")) RandWithLength(mindigits,maxdigits)
		;~ oTableString .= ((j = nlenght) ? oTable[i,j] : oTable[i,j] "	")
		if (j < nlenght)
		{
			RndNum := RandWithLength(mindigits,maxdigits)
			SignNum := (mixed ? (RandNum() ? "-" RndNum : RndNum) : RndNum)
			Sum += SignNum
		}
		oTableString .= ((j = nlenght) ? Sum : SignNum "	")
	}
	oTableString .= "`n"
}

MsgBox, % oTableString
return

RandNum(min:=0,max:=1)
{
	if (min > max)
		swap(min,max)
	Random, var, % min, % max
	return var
}

RandWithLength(minLength,maxLength)
{
	if (minLength > maxLength)
		swap(minLength,maxLength)
	minV := 1
	maxV := 9
	Loop, % minLength - 1
		minV .= 0
	Loop, % maxLength - 1
		maxV .= 9
	Random, var, % minV, % maxV
	return var
}

swap(ByRef var1, ByRef var2)
{
	var3 := var1
	var1 := var2
	var2 := var3
}
:wave: There is always more than one way to solve a problem. ;)
User avatar
SpeedMaster
Posts: 394
Joined: 12 Nov 2016, 16:09

Re: [Math] How to make a table of arithmetic exercises ?

12 Jun 2019, 20:23

YoucefHam wrote:
12 Jun 2019, 18:59
try this, but its in String format
Thank you for your answer but you are facing the same difficulties as me. :cry:

I need to clarify some rules:
  • An exercise should always start with a positive number. (e. g. +55-12+30=)
  • Numbers starting with one or more zeros are not allowed. (e. g. 0027 0577 001 etc.)
  • The final result and intermediate calculations that fall below zero are not allowed (e. g. 55-123= or 12-266+588= or 4-8+100=)
  • If possible, the final answer should appear somewhere in the table even if not displayed
Cheers,
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: [Math] How to make a table of arithmetic exercises ?

12 Jun 2019, 23:52

I took your code as is, and introduced TestSum.
If mixed, the new algorithm tries to subtract if possible.

Code: Select all

;============================================ settings ===============================================

nlines:=4           ; number of lines you want
nlenght:=8          ; lenght of a line (amount of numbers per line) note the last entry (maxindex) represents the sum of the previous entries
mindigits:=2        ; min lenght for a number
maxdigits:=3        ; max lenght for a number
mixed:=true         ; mixed between positive and negative numbers

;=====================================================================================================


my_table:=arithmeticTable(nlines, nlenght, mindigits, maxdigits, mixed)




msgbox, % "How to make a table of arithmetic exercises ?`n`nnote: the last entry (maxindex) represents `nthe sum of the previous entries`n`n" . st_printarr(my_table)




;========================= aritmetic exercise generator function =====================================

ArithmeticTable(nlines:=4,nlenght:=3,mindigits:=2,maxdigits:=3, mixed:=true) {

oTable:=[], tot:=0

loop, % nlines {
    loop, % nlenght {
        loop, % rand(mindigits,maxdigits)
            (a_index=1) ? (out .= rand(9)) : (out .= rand(0,9))
     out .= "`n"
    }
 map:=strsplit(out, "`n")
 oTable.push(map)
 out:=""
}

if (mixed) ; check first if subtraction is allowed, if Yes, use subtraction
    for i, r in oTable {
         sum:=0, TestSum := 0
        for j, c in r {
            if (TestSum -= c) > 0
                oTable[i,j] *= -1
            TestSum := sum += oTable[i,j]
         }
    }

for i, r in oTable {
    for j, c in r
        (j<r.maxindex()) ? (tot += c) :  (oTable[i,j]:=tot)
    tot:=0
}


return oTable

}


;==================================== random number generator ===========================================


; FROM: http://www.autohotkey.com/forum/viewtopic.php?t=54713&postdays=0&postorder=asc&start=0
; EXAMPLES
/*
Rand() ; - A random float between 0.0 and 1.0 (many uses)
Rand(6) ; - A random integer between 1 and 6 (die roll)
Rand("") ; - New random seed (selected randomly)
Rand("", 12345) ; - New random seed (set explicitly)
Rand(50, 100) ; - Random integer between 50 and 100 (typical use)
*/
; RANDOM FUNCTION
Rand( a=0.0, b=1 ) {
   IfEqual,a,,Random,,% r := b = 1 ? Rand(0,0xFFFFFFFF) : b
   Else Random,r,a,b
   Return r
}


;============================================ debugging function =============================================

;String Things by tidbit
;https://autohotkey.com/boards/viewtopic.php?f=6&t=53&sid=5c401643235e7a2e73d769f1e5deac0f
st_printArr(array, depth=5, indentLevel="")
{
   for k,v in Array
   {
      list.= indentLevel "[" k "]"
      if (IsObject(v) && depth>1)
         list.="`n" st_printArr(v, depth-1, indentLevel . "    ")
      Else
         list.=" => " v
      list.="`n"
   }
   return rtrim(list)
}


I'm not sure if that is better for you?
User avatar
SpeedMaster
Posts: 394
Joined: 12 Nov 2016, 16:09

Re: [Math] How to make a table of arithmetic exercises ?

13 Jun 2019, 08:20

wolf_II wrote:
12 Jun 2019, 23:52
I took your code as is, and introduced TestSum.
If mixed, the new algorithm tries to subtract if possible.
I'm not sure if that is better for you?
"TestSum" creates the same kind of problem (but just the opposite too many subtractions and not enough additions) :cry:

here is the code with "TestSum"

Code: Select all

;============================================ settings ===============================================

nlines:=4            ; number of lines you want
nlenght:=15          ; lenght of a line (amount of numbers per line) note the last entry (maxindex) represents the sum of the previous entries
mindigits:=2         ; min lenght for a number
maxdigits:=4         ; max lenght for a number
type:=0              ; -1=only substraction, 1=only addition, 0=mixed between positive and negative numbers

;=====================================================================================================
gui, font, s12
gui, add, text, , How to make a table of arithmetic exercises ?`n`nnote: the last entry (maxindex) represents `nthe sum of the previous entries`n
gui, add, button, gok, generate
gui, font, s10
gui, add, edit, w500 h750 vtext,
gui, show
return

ok:
my_table:=arithmeticTable(nlines, nlenght, mindigits, maxdigits, type)

guicontrol,,text, % st_printarr(my_table)


;msgbox, % "How to make a table of arithmetic exercises ?`n`nnote: the last entry (maxindex) represents `nthe sum of the previous entries`n`n" . st_printarr(my_table)

return


guiclose:
exitapp
return


;========================= aritmetic exercise generator function =====================================

ArithmeticTable(nlines:=4,nlenght:=3,mindigits:=2,maxdigits:=3, type:=0) {

oTable:=[], tot:=0

loop, % nlines {
    loop, % nlenght {
        loop, % rand(mindigits,maxdigits)
            (a_index=1) ? (out .= rand(9)) : (out .= rand(0,9))
     out .= "`n"
    }
 map:=strsplit(out, "`n")
 oTable.push(map)
 out:=""
}

if (type=0) ; check first if subtraction is allowed, if Yes, use subtraction
    for i, r in oTable {
         sum:=0, TestSum := 0
        for j, c in r {
            if (TestSum -= c) > 0
                oTable[i,j] *= -1
            TestSum := sum += oTable[i,j]
         }
    }

for i, r in oTable {
    for j, c in r
        (j<r.maxindex()) ? (tot += c) :  (oTable[i,j]:=tot)
    tot:=0
}


return oTable

}


;==================================== random number generator ===========================================


; FROM: http://www.autohotkey.com/forum/viewtopic.php?t=54713&postdays=0&postorder=asc&start=0
; EXAMPLES
/*
Rand() ; - A random float between 0.0 and 1.0 (many uses)
Rand(6) ; - A random integer between 1 and 6 (die roll)
Rand("") ; - New random seed (selected randomly)
Rand("", 12345) ; - New random seed (set explicitly)
Rand(50, 100) ; - Random integer between 50 and 100 (typical use)
*/
; RANDOM FUNCTION
Rand( a=0.0, b=1 ) {
   IfEqual,a,,Random,,% r := b = 1 ? Rand(0,0xFFFFFFFF) : b
   Else Random,r,a,b
   Return r
}


;============================================ debugging function =============================================

;String Things by tidbit
;https://autohotkey.com/boards/viewtopic.php?f=6&t=53&sid=5c401643235e7a2e73d769f1e5deac0f
st_printArr(array, depth=5, indentLevel="")
{
   for k,v in Array
   {
      list.= indentLevel "[" k "]"
      if (IsObject(v) && depth>1)
         list.="`n" st_printArr(v, depth-1, indentLevel . "    ")
      Else
         list.=" => " v
      list.="`n"
   }
   return rtrim(list)
}

Thanks for cleaning up the code oTable[i,j] *= -1 :thumbup:

To clarify the problem I updated my previous code
I replaced the variable "mixed" with "type" which accepts three values (-1 = subtractions only, 1 addition only, 0 = mixed)
I still have problem for generating well balanced mixed exercises and substraction only exercises. :think:

Regards
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: [Math] How to make a table of arithmetic exercises ?

13 Jun 2019, 22:53

Maybe this:
subtraction still has a flaw: First entry can get out of bounds.

Code: Select all

;============================================ settings ===============================================

nlines:=4            ; number of lines you want
nlenght:=15          ; lenght of a line (amount of numbers per line) note the last entry (maxindex) represents the sum of the previous entries
mindigits:=2         ; min lenght for a number
maxdigits:=4         ; max lenght for a number
                     ; type: -1=only substraction, 1=only addition, 0=mixed between positive and negative numbers

;=====================================================================================================
gui, font, s12
gui, add, text, , How to make a table of arithmetic exercises ?`n`nnote: the last entry (maxindex) represents `nthe sum of the previous entries`n
gui, add, DropDownList, vTypeTxt, substraction|addition||mixed
gui, add, button, gok, generate
gui, font, s10
gui, add, edit, xm w500 h750 vtext,
gui, show
return

ok:
GuiControlGet, TypeTxt

if (TypeTxt = "substraction")
    type := -1
if (TypeTxt = "addition")
    type := 1
if (TypeTxt = "mixed")
    type := 0

my_table:=arithmeticTable(nlines, nlenght, mindigits, maxdigits,type)
guicontrol,,text, % st_printarr(my_table)
return

guiclose:
exitapp


;========================= aritmetic exercise generator function =====================================

ArithmeticTable(nlines:=4,nlenght:=3,mindigits:=2,maxdigits:=3, type:=0) {
oTable:=[], tot:=0

loop, % nlines {
    loop, % nlenght {
        loop, % rand(mindigits,maxdigits)
            (a_index=1) ? (out .= rand(9)) : (out .= rand(0,9))
     out .= "`n"
    }
 map:=strsplit(out, "`n")
 oTable.push(map)
 out:=""
}

if (type=0) ; check first if subtraction is allowed, if Yes, use subtraction
    for i, r in oTable {
        sum:=0, TestSum := 0, Counter := 0
        for j, c in r {
            if (TestSum -= c) > 0 {
                oTable[i,j] *= -1
                if (++Counter > nlenght/2)
                    break
            }
            TestSum := sum += oTable[i,j]
        }
    }

else if (type=-1) {
    for i, r in oTable {
        sum:=0
        for j, c in r
            if (j != 1) {
                sum += oTable[i,j]
                oTable[i,j] *= -1
            }
        oTable[i,1] := sum + 1
    }
}

for i, r in oTable {
    for j, c in r
        (j<r.maxindex()) ? (tot += c) :  (oTable[i,j]:=tot)
    tot:=0
}


return oTable

}


;==================================== random number generator ===========================================


; FROM: http://www.autohotkey.com/forum/viewtopic.php?t=54713&postdays=0&postorder=asc&start=0
; EXAMPLES
/*
Rand() ; - A random float between 0.0 and 1.0 (many uses)
Rand(6) ; - A random integer between 1 and 6 (die roll)
Rand("") ; - New random seed (selected randomly)
Rand("", 12345) ; - New random seed (set explicitly)
Rand(50, 100) ; - Random integer between 50 and 100 (typical use)
*/
; RANDOM FUNCTION
Rand( a=0.0, b=1 ) {
   IfEqual,a,,Random,,% r := b = 1 ? Rand(0,0xFFFFFFFF) : b
   Else Random,r,a,b
   Return r
}


;============================================ debugging function =============================================

;String Things by tidbit
;https://autohotkey.com/boards/viewtopic.php?f=6&t=53&sid=5c401643235e7a2e73d769f1e5deac0f
st_printArr(array, depth=5, indentLevel="")
{
   for k,v in Array
   {
      list.= indentLevel "[" k "]"
      if (IsObject(v) && depth>1)
         list.="`n" st_printArr(v, depth-1, indentLevel . "    ")
      Else
         list.=" => " v
      list.="`n"
   }
   return rtrim(list)
}

User avatar
SpeedMaster
Posts: 394
Joined: 12 Nov 2016, 16:09

Re: [Math] How to make a table of arithmetic exercises ?  Topic is solved

14 Jun 2019, 13:48

wolf_II wrote:
13 Jun 2019, 22:53
Maybe this:
subtraction still has a flaw: First entry can get out of bounds.
:arrow: not really a problem for me since even the final result of an addition can exceed the limit. :think:

:!: you made the final result of the subtractions too obvious =1 (fortunately this one was easy to fix. :D)

here is the code

Code: Select all

;============================================ settings ===============================================

;nlines:=10           ; number of lines you want
;nlength:=15          ; length of a line (amount of numbers per line) note the last entry (maxindex) represents the sum of the previous entries
;mindigits:=2         ; min length for a number
;maxdigits:=4         ; max length for a number
                      ; type: -1=only substraction, 1=only addition, 0=mixed between positive and negative numbers

;=====================================================================================================
gui, font, s11
gui, -dpiscale
gui, add, text, , How to make a table of arithmetic exercises ?`n`nnote: the last entry (maxindex) represents `nthe sum of the previous entries`n
gui, add, DropDownList, vTypeTxt, substraction|addition|mixed||
gui, add, text, w55 section -border, Exercises
gui, add, text, xp+70 w55 -border, length
gui, add, text, xp+70 w75 -border, Min Digits
gui, add, text, xp+70 w75 -border, Max Digits

gui, add, DropDownList, y+2 xs w60 vnlines, 1|2|3|4|5|6|7|8|9|10||11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50
gui, add, DropDownList, xp+70 w60  vnlength, 1|2|3|4|5|6|7||8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30
gui, add, DropDownList, xp+70 w60 vmindigits, 1|2||3|4|5|6|7|8|9
gui, add, DropDownList, xp+70 w60 vmaxdigits, 1|2|3||4|5|6|7|8|9
gui, add, button, xs gok, Generate table
gui, font, s10
gui, add, edit, xm w500 h750 vtext,
gui, show
return

ok:
GuiControlGet, TypeTxt

gui, submit, nohide

if (TypeTxt = "substraction")
    type := -1
if (TypeTxt = "addition")
    type := 1
if (TypeTxt = "mixed")
    type := 0

my_table:=arithmeticTable(nlines, nlength, mindigits, maxdigits,type)
guicontrol,,text, % st_printarr(my_table)
return

guiclose:
exitapp


;========================= aritmetic exercise generator function =====================================

ArithmeticTable(nlines:=4,nlength:=3,mindigits:=2,maxdigits:=3, type:=0) {
oTable:=[], tot:=0

loop, % nlines {
    loop, % nlength {
        loop, % rand(mindigits,maxdigits)
            (a_index=1) ? (out .= rand(9)) : (out .= rand(0,9))
     out .= "`n"
    }
 map:=strsplit(out, "`n")
 oTable.push(map)
 out:=""
}

if (type=0) ; check first if subtraction is allowed, if Yes, use subtraction
    for i, r in oTable {
        sum:=0, TestSum := 0, Counter := 0
        for j, c in r {
            if (TestSum -= c) > 0 {
                oTable[i,j] *= -1
                if (++Counter > (nlength*40/100))
                    break
            }
            TestSum := sum += oTable[i,j]
        }
    }

else if (type=-1) {
    for i, r in oTable {
        sum:=0
        for j, c in r
            if (j != 1) {
                sum += oTable[i,j]
                oTable[i,j] *= -1
            }
        oTable[i,1] := sum + 1
        loop, % rand(mindigits,maxdigits)
            (a_index=1) ? (out .= rand(9)) : (out .= rand(0,9))
            
        oTable[i,1] += out
        out:=""
    }
    
}

for i, r in oTable {
    for j, c in r
        (j<r.maxindex()) ? (tot += c) :  (oTable[i,j]:=tot)
    tot:=0
}


return oTable

}


;==================================== random number generator ===========================================


; FROM: http://www.autohotkey.com/forum/viewtopic.php?t=54713&postdays=0&postorder=asc&start=0
; EXAMPLES
/*
Rand() ; - A random float between 0.0 and 1.0 (many uses)
Rand(6) ; - A random integer between 1 and 6 (die roll)
Rand("") ; - New random seed (selected randomly)
Rand("", 12345) ; - New random seed (set explicitly)
Rand(50, 100) ; - Random integer between 50 and 100 (typical use)
*/
; RANDOM FUNCTION
Rand( a=0.0, b=1 ) {
   IfEqual,a,,Random,,% r := b = 1 ? Rand(0,0xFFFFFFFF) : b
   Else Random,r,a,b
   Return r
}


;============================================ debugging function =============================================

;String Things by tidbit
;https://autohotkey.com/boards/viewtopic.php?f=6&t=53&sid=5c401643235e7a2e73d769f1e5deac0f
st_printArr(array, depth=5, indentLevel="")
{
   for k,v in Array
   {
      list.= indentLevel "[" k "]"
      if (IsObject(v) && depth>1)
         list.="`n" st_printArr(v, depth-1, indentLevel . "    ")
      Else
         list.=" => " v
      list.="`n"
   }
   return rtrim(list)
}

Thanks again for your help :bravo:

Regeards

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKNoobie, AHKStudent, Bing [Bot], boiler, fliptoback, geloSemen, Google [Bot], mikeyww, PuzzledGreatly, songdg and 47 guests