Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ##########Intro###############
- #So for this program I tried desperately to get it to work with the grading script.
- #Sadly I was unsuccessful, I will rewatch the stdout videos and see where I went wrong.
- #However, I took an approach of using arrays to calculate the matrixes after studying up
- #on how they work in bash. I tested my program mainly using files to ensure the core functionality
- #worked. I outputted my work using printf so that I could use \t as tab instead of echo as it was
- #not working how I wanted it to. However, since I was using printf which defaults to stdout I thought
- #that it would work without too much trouble. Apparently I was wrong, so I spent a lot of my time
- #trying to understand why the grading script wasn't seeing the output correctly. Overall, my program
- #works and is able to go through matrixes quite quickly and give the right outputs. Sadly I will
- #get a zero on this since I wasn't able to correctly output the information.
- ##########Dims####################
- Dim(){
- #Declare an array to store the items of the first line
- #I then use this to find the number of columns by length
- #of the array.
- declare -a array=()
- read -a line < "$inputFile"
- COLS=${#line[@]}
- ROWS=0
- #Iterate through the lines until eof
- while read -a line
- do
- ((ROWS++))
- done < "$inputFile"
- printf "%s %s\n" ${ROWS} ${COLS}
- exit 0
- }
- ##########Transpose##############
- Transpose(){
- #Declare array and set columns
- declare -a array=()
- read -a line < "$inputFile"
- COLS=${#line[@]}
- #iterate through the file and place in 1d array
- #I decided that this would be the easiest way to
- #capture the rows and also be able to work immediately
- #with the array.
- index=0
- while read -a line
- do
- for(( COUNTER=0; COUNTER<${#line[@]}; COUNTER++ ))
- do
- array[$index]=${line[$COUNTER]}
- ((index++))
- done
- done < "$inputFile"
- #Using a double for loop we place each element in the corresponding
- #column before moving to the next row. Conditionals were placed to
- #see if the grading script would accept the formatted output. However,
- #it became bloated and it wasn't helping so I removed them.
- for (( ROW = 0; ROW < COLS; ROW++ ))
- do
- for (( COUNTER = ROW; COUNTER < ${#array[@]}; COUNTER += COLS ))
- do
- printf "%s\t" ${array[$COUNTER]}
- done
- printf "\n"
- done
- exit 0
- }
- ###########Mean#################
- Mean(){
- #Declare our initial variables, arrays, ect...
- declare -a array=()
- read -a line < "$inputFile"
- COLS=${#line[@]}
- ROWS=0
- tempA=0
- tempB=0
- #From here we add to the respective column in the array, so elements
- #in column one are added to array[0] and so forth.
- while read -a line
- do
- for(( COUNTER=0; COUNTER<${#line[@]}; COUNTER++ ))
- do
- tempA=${array[$COUNTER]}
- tempB=${line[$COUNTER]}
- array[$COUNTER]=$((tempA + tempB))
- done
- ((ROWS++))
- done < "$inputFile"
- #Depending on the number of rows we divide the elements,
- #and if the sum is negative round with a negative 1.
- for(( COUNTER=0; COUNTER<COLS; COUNTER++ ))
- do
- tempA=${array[$COUNTER]}
- if [ "$tempA" -gt "0" ]
- then
- tempA=$(( (tempA+(ROWS/2))/ROWS ))
- else
- tempA=$(( (tempA+(ROWS/2)*(0-1))/ROWS ))
- fi
- array[$COUNTER]=$((tempA))
- printf "%s\t" ${array[$COUNTER]}
- done
- printf "\n"
- }
- ###########Add##################
- Add(){
- #Declare the arrays that we need to add to
- declare -a arrayA=()
- declare -a arrayB=()
- declare -a arrayC=()
- arrayC[0]=0
- read -a line < "$inputFile"
- COLSA=${#line[@]}
- ROWSA=0
- index=0
- #We read from the two files into their respective arrays A or B
- while read -a line
- do
- for(( COUNTER=0; COUNTER<${#line[@]}; COUNTER++))
- do
- arrayA[$index]=${line[$COUNTER]}
- ((index++))
- done
- ((ROWSA++))
- done < "$inputFile"
- read -a line < "$modFile"
- COLSB=${#line[@]}
- ROWSB=0
- index=0
- while read -a line
- do
- for(( COUNTER=0; COUNTER<${#line[@]}; COUNTER++))
- do
- arrayB[$index]=${line[$COUNTER]}
- ((index++))
- done
- ((ROWSB++))
- done < "$modFile"
- #End of reading into the arrays.
- #Here we check to see if the arrays are valid, I should do this check initially
- #but since my method required me to read the arrays first. I decided to do it
- #afterwards. Pretty simple, add each element from both arrays into a new array.
- tempA=0
- tempB=0
- if [[ "$ROWSB" == "$ROWSA" && "$COLSA" == "$COLSB" ]]
- then
- for(( COUNTER=0; COUNTER<index; COUNTER++))
- do
- tempA=${arrayA[$COUNTER]}
- tempB=${arrayB[$COUNTER]}
- arrayC[$COUNTER]=$(( tempA+tempB ))
- done
- index=0
- for(( ROW=0; ROW<ROWSA; ROW++ ))
- do
- for(( COL=0; COL<COLSA; COL++ ))
- do
- printf "%s\t" ${arrayC[$index]}
- ((index++))
- done
- printf "\n"
- done
- else
- echo "Incompatible" >&2
- exit 1
- fi
- }
- ###########Multiply###############
- Multiply(){
- #Declare arrays for multiplication
- declare -a arrayA=()
- declare -a arrayB=()
- declare -a arrayC=()
- read -a line < "$inputFile"
- COLSA=${#line[@]}
- ROWSA=0
- index=0
- #ANOTHER READ BLOCK, so exciting
- while read -a line
- do
- for(( COUNTER=0; COUNTER<${#line[@]}; COUNTER++))
- do
- arrayA[$index]=${line[$COUNTER]}
- ((index++))
- done
- ((ROWSA++))
- done < "$inputFile"
- read -a line < "$modFile"
- COLSB=${#line[@]}
- ROWSB=0
- index=0
- while read -a line
- do
- for(( COUNTER=0; COUNTER<${#line[@]}; COUNTER++))
- do
- arrayB[$index]=${line[$COUNTER]}
- ((index++))
- done
- ((ROWSB++))
- done < "$modFile"
- #END OF READ BLOCK.... Yay! Have cols and rows of both arrays
- #Tests to see if the NxM MxP if the M are equal to each other.
- #If they are, multiply the correct elements to each other.
- #Again the check is done after loading the arrays, due to my
- #method of iterating through the files.
- tempA=0
- tempB=0
- index=0
- if [ "$COLSA" == "$ROWSB" ]
- then
- for(( COUNTERA=0; COUNTERA<ROWSA; COUNTERA++ ))
- do
- for(( COUNTERB=0; COUNTERB<COLSB; COUNTERB++ ))
- do
- temp=0
- for(( COUNTERC=0; COUNTERC<ROWSB; COUNTERC++ ))
- do
- tempA=${arrayA[$COUNTERA*$COLSA+$COUNTERC]}
- tempB=${arrayB[$COUNTERC*$COLSB+$COUNTERB]}
- temp=$(( temp+tempA*tempB ))
- done
- arrayC[$index]=$temp
- ((index++))
- done
- done
- index=0
- for(( ROW=0; ROW<ROWSA; ROW++ ))
- do
- for(( COL=0; COL<COLSB; COL++ ))
- do
- printf "%s\t" ${arrayC[$index]}
- ((index++))
- done
- printf "\n"
- done
- else
- echo "Incompatible" >&2
- exit 1
- fi
- }
- ###########Main Code##############
- #Initial check to make sure no arguements exceed the max of 3
- if [ "$#" -gt "3" ]
- then
- echo "Invalid number of parameters" >&2
- exit 1
- fi
- inputFile="$2"
- modFile="$3"
- #Tests to see if inputFile is valid, should put another for modfile
- #but it won't matter to the gradingscript... Oh well make it work
- #and not break it. Also made a check to see if args were 1
- #That way I could pipe stdin to a tmp file and then inputfile
- #would receive that file. However, I did not give myself enough
- #time to finish this.
- if test -e "$inputFile" -a -r "$inputFile" #-o "$#" -eq 1
- then
- case "$1" in
- add)
- #echo "add mode active"
- if test -e "$modFile" -a -r "$modFile" -a "$#" -ne "3"
- then
- echo "Invalid number of parameters" >&2
- exit 1
- fi
- Add
- ;;
- dims)
- #echo "attempting dims mode"
- if [ "$#" -gt "2" ]
- then
- echo "Invalid number of parameters" >&2
- exit 1
- fi
- Dim
- ;;
- mean)
- #echo "mean mode active"
- if [ "$#" -ne "3" ]
- then
- echo "Invalid number of parameters" >&2
- exit 1
- fi
- Mean
- ;;
- multiply)
- #echo "multiply mode active"
- if test -e "$modFile" -a -r "$modFile" -a "$#" -ne "3"
- then
- echo "Invalid number of parameters" >&2
- exit 1
- fi
- Multiply
- ;;
- transpose)
- #echo "transpose mode active"
- if [ "$#" -gt "2" ]
- then
- echo "Invalid number of parameters" >&2
- exit 1
- fi
- Transpose
- ;;
- *)
- echo $"Usage: $0 {add|dims|mean|multiply|transpose}">&2
- exit 1
- esac
- else
- echo "Invalid File" >&2
- exit 1
- fi
- exit 0
- #IamterribleatBASH
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement