From f9977f4c35897d490d2a3b3cc0be483c4ad1668d Mon Sep 17 00:00:00 2001 From: Ramya Date: Mon, 24 Nov 2025 02:23:08 -0500 Subject: [PATCH] Update cachematrix.R assignment 2 lexical scoping --- cachematrix.R | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..546aaaf3828 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,12 +4,30 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - +k<-NULL + set<-function(y){ + x<<-y + k<<-NULL + } + get<-function()x + setInverse<-function(inverse) + k<<-inverse + getInverse<-function()k + list(set=set,get=get,setInverse<-setInverse,getInverse<-getInverse) } ## Write a short comment describing this function cacheSolve <- function(x, ...) { + k<-x$getInverse() + if(!is.null(k)){ + message("getting cached data") + return(k) + } + mat<-x$get() + k<-solve(mat,...) + x$setInverse(k) + k ## Return a matrix that is the inverse of 'x' }