In: Computer Science
insert code in all TODO part, it's about pointer. atmel studio 7.0. this should be able to run without utilities.h
// includes
#include <asf.h>
// function prototypes
// these are necessary if you are going to call a function before
it is declared
// without these, we would have to put all of our functions above
main
static void configure_console(void);
void get_matrix(int *matrix);
int calc_det(int *matrix);
void get_matrix_global(void);
// declare global variables
int matrix_global[9];
int main (void)
{
// configure system
board_init();
sysclk_init();
configure_console();
printf("\nStarting Determinant Calculator\r\n\n");
// declare constants
const int NUM_METHODS = 3;
// declare button variables
bool prev_button_state = BUTTON_0_INACTIVE;
bool current_button_state = BUTTON_0_INACTIVE;
// create a fixed-sized int array with 9 elements and
initialize elements to 0
// name = matrix_array
/***TODO - 1 line***/
// create an int pointer and point it to NULL
// name = matrix_dynamic
/***TODO - 1 line***/
// create an int pointer and point it to NULL
// name = matrix
/***TODO - 1 line***/
// create a variable to keep up with what method to
run
int method = 0;
// create a variable for the determinant that you
calculate
int det = 0;
while (1)
{
// typical edge detection for
button
current_button_state =
ioport_get_pin_level(BUTTON_0_PIN);
if ( (current_button_state ==
BUTTON_0_ACTIVE) && (prev_button_state ==
BUTTON_0_INACTIVE))
{
// Method 0:
Pass matrix_array into get_matrix
if((method %
NUM_METHODS) == 0)
{
printf("Method 0: Pass matrix_array into
get_matrix\r\n");
// use get_matrix to get a 3x3 matrix from the
user
// pass in matrix_array so that it can be
updated with user data
/***TODO - 1 line***/
// point matrix to matrix_array
/***TODO - 1 line***/
}
// Method 1:
Update a global array with user input
else if((method
% NUM_METHODS) == 1)
{
printf("Method 1: Update a global array with
user input\r\n");
// use get_matrix_global to get a 3x3 matrix
from the user
// no arguments are passed in, as this function
just updates
// the global matrix, matrix_global
/***TODO - 1 line***/
// point matrix to matrix_global
/***TODO - 1 line***/
}
// Method 2:
Dynamically allocate an array using malloc
else if((method
% NUM_METHODS) == 2)
{
printf("Method 2: Dynamically allocate an array
using malloc\r\n");
// use malloc to dynamically allocate an array
that can hold 9 integer values
// this allocates the array on the heap, and it
must be freed once we are done
// with it, or else we will have a memory
leak
/***TODO - 1 line***/
// pass in matrix_dynamic to get_matrix so that
it can be updated with user data
/***TODO - 1 line ***/
// point matrix to matrix_dynamic
/***TODO - 1 line ***/
}
// calculate the
determinant of the matrix
/***TODO - 1
line***/
// print
calculated determinant value
printf("\r\n ***
Determinant = %d\r\n\r\n", det);
// free
matrix_dynamic if necessary
/***TODO -
multiple lines ***/
// increment
method so that it uses the next method
method++;
}
}
}
// typical configure_console
static void configure_console(void)
{
const usart_serial_options_t uart_serial_options
=
{
.baudrate =
CONF_UART_BAUDRATE,
.charlength =
CONF_UART_CHAR_LENGTH,
.paritytype =
CONF_UART_PARITY,
.stopbits =
CONF_UART_STOP_BITS,
};
/* Configure console. */
stdio_serial_init(CONF_UART,
&uart_serial_options);
}
// get a 3x3 matrix from user input and store it in the
// matrix that was passed into the function
// note that I could've made the parameter be
// int matrix[9] and it would do the same thing
void get_matrix(int *matrix)
{
unsigned char n;
printf("Please press any key, then enter a 3x3
matrix\r\n");
// press any key to start
// usart_serial_getchar waits for one character from
Putty
usart_serial_getchar((Usart *)CONF_UART,
&n);
// now get the 9 elements of the matrix
for(int i=0; i<9; ++i)
{
printf("Element %d: ", i);
usart_serial_getchar((Usart
*)CONF_UART, &n);
// convert the ascii character from
Putty to an integer
matrix[i] = (int) n - '0';
// reflect the value back to
Putty
printf("%d\r\n", matrix[i]);
}
printf("Matrix entry complete\r\n");
}
// get a 3x3 matrix from user input and store it in the
// global matrix that was declare in the global namespace
void get_matrix_global(void)
{
unsigned char n;
printf("Please press any key, then enter a 3x3
matrix\r\n");
// press any key to start
// usart_serial_getchar waits for one character from
Putty
usart_serial_getchar((Usart *)CONF_UART,
&n);
// now get the 9 elements of the matrix
for(int i=0; i<9; ++i)
{
printf("Element %d: ", i);
usart_serial_getchar((Usart
*)CONF_UART, &n);
// convert the ascii character from
Putty to an integer
matrix_global[i] = (int) n -
'0';
// reflect the value back to
Putty
printf("%d\r\n",
matrix_global[i]);
}
printf("Matrix entry complete\r\n");
}
// calculate the determinant of a 3x3 matrix and return the
value
int calc_det(int matrix[9])
{
int cof1 =
matrix[0]*(matrix[4]*matrix[8]-matrix[5]*matrix[7]);
int cof2 =
matrix[1]*(matrix[3]*matrix[8]-matrix[5]*matrix[6]);
int cof3 =
matrix[2]*(matrix[3]*matrix[7]-matrix[4]*matrix[6]);
int det = cof1 - cof2 + cof3;
return det;
}
// includes
#include <asf.h>
// function prototypes
// these are necessary if you are going to call a function before it is declared
// without these, we would have to put all of our functions above main
static void configure_console(void);
void get_matrix(int *matrix);
int calc_det(int *matrix);
void get_matrix_global(void);
// declare global variables
int matrix_global[9];
int main (void)
{
// configure system
board_init();
sysclk_init();
configure_console();
printf("\nStarting Determinant Calculator\r\n\n");
// declare constants
const int NUM_METHODS = 3;
// declare button variables
bool prev_button_state = BUTTON_0_INACTIVE;
bool current_button_state = BUTTON_0_INACTIVE;
// create a fixed-sized int array with 9 elements and initialize elements to 0
// name = matrix_array
/***TODO - 1 line***/
int[] matrix_array = new int[9];
// create an int pointer and point it to NULL
// name = matrix_dynamic
/***TODO - 1 line***/
int *matrix_dynamic = NULL;
// create an int pointer and point it to NULL
// name = matrix
/***TODO - 1 line***/
int *matrix = NULL;
// create a variable to keep up with what method to run
int method = 0;
// create a variable for the determinant that you calculate
int det = 0;
while (1)
{
// typical edge detection for button
current_button_state = ioport_get_pin_level(BUTTON_0_PIN);
if ( (current_button_state == BUTTON_0_ACTIVE) && (prev_button_state == BUTTON_0_INACTIVE))
{
// Method 0: Pass matrix_array into get_matrix
if((method % NUM_METHODS) == 0)
{
printf("Method 0: Pass matrix_array into get_matrix\r\n");
// use get_matrix to get a 3x3 matrix from the user
// pass in matrix_array so that it can be updated with user data
/***TODO - 1 line***/
get_matrix(matrix_array);
// point matrix to matrix_array
/***TODO - 1 line***/
matrix = matrix_array;
}
// Method 1: Update a global array with user input
else if((method % NUM_METHODS) == 1)
{
printf("Method 1: Update a global array with user input\r\n");
// use get_matrix_global to get a 3x3 matrix from the user
// no arguments are passed in, as this function just updates
// the global matrix, matrix_global
/***TODO - 1 line***/
get_matrix_global();
// point matrix to matrix_global
/***TODO - 1 line***/
matrix = matrix_global;
}
// Method 2: Dynamically allocate an array using malloc
else if((method % NUM_METHODS) == 2)
{
printf("Method 2: Dynamically allocate an array using malloc\r\n");
// use malloc to dynamically allocate an array that can hold 9 integer values
// this allocates the array on the heap, and it must be freed once we are done
// with it, or else we will have a memory leak
/***TODO - 1 line***/
matrix_dynamic = (int*)malloc(9 * sizeof(int));
// pass in matrix_dynamic to get_matrix so that it can be updated with user data
/***TODO - 1 line ***/
get_matrix(matrix_dynamic);
// point matrix to matrix_dynamic
/***TODO - 1 line ***/
matrix = matrix_dynamic;
}
// calculate the determinant of the matrix
/***TODO - 1 line***/
det = calc_det(matrix)
// print calculated determinant value
printf("\r\n *** Determinant = %d\r\n\r\n", det);
// free matrix_dynamic if necessary
/***TODO - multiple lines ***/
if(matrix_dynamic != NULL)
free(matrix_dynamic);
// increment method so that it uses the next method
method++;
}
}
}
// typical configure_console
static void configure_console(void)
{
const usart_serial_options_t uart_serial_options =
{
.baudrate = CONF_UART_BAUDRATE,
.charlength = CONF_UART_CHAR_LENGTH,
.paritytype = CONF_UART_PARITY,
.stopbits = CONF_UART_STOP_BITS,
};
/* Configure console. */
stdio_serial_init(CONF_UART, &uart_serial_options);
}
// get a 3x3 matrix from user input and store it in the
// matrix that was passed into the function
// note that I could've made the parameter be
// int matrix[9] and it would do the same thing
void get_matrix(int *matrix)
{
unsigned char n;
printf("Please press any key, then enter a 3x3 matrix\r\n");
// press any key to start
// usart_serial_getchar waits for one character from Putty
usart_serial_getchar((Usart *)CONF_UART, &n);
// now get the 9 elements of the matrix
for(int i=0; i<9; ++i)
{
printf("Element %d: ", i);
usart_serial_getchar((Usart *)CONF_UART, &n);
// convert the ascii character from Putty to an integer
matrix[i] = (int) n - '0';
// reflect the value back to Putty
printf("%d\r\n", matrix[i]);
}
printf("Matrix entry complete\r\n");
}
// get a 3x3 matrix from user input and store it in the
// global matrix that was declare in the global namespace
void get_matrix_global(void)
{
unsigned char n;
printf("Please press any key, then enter a 3x3 matrix\r\n");
// press any key to start
// usart_serial_getchar waits for one character from Putty
usart_serial_getchar((Usart *)CONF_UART, &n);
// now get the 9 elements of the matrix
for(int i=0; i<9; ++i)
{
printf("Element %d: ", i);
usart_serial_getchar((Usart *)CONF_UART, &n);
// convert the ascii character from Putty to an integer
matrix_global[i] = (int) n - '0';
// reflect the value back to Putty
printf("%d\r\n", matrix_global[i]);
}
printf("Matrix entry complete\r\n");
}
// calculate the determinant of a 3x3 matrix and return the value
int calc_det(int matrix[9])
{
int cof1 = matrix[0]*(matrix[4]*matrix[8]-matrix[5]*matrix[7]);
int cof2 = matrix[1]*(matrix[3]*matrix[8]-matrix[5]*matrix[6]);
int cof3 = matrix[2]*(matrix[3]*matrix[7]-matrix[4]*matrix[6]);
int det = cof1 - cof2 + cof3;
return det;
}
I've added the code in all the required TODO but, could not test run the solution as I don't have asf.h file. but it'll be able to run without any issues. Please let me know in case if you face any difficulty. Hope this helps!