Question

In: Computer Science

insert code in all TODO part, it's about pointer. atmel studio 7.0. this should be able...

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;
}

Solutions

Expert Solution

// 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!


Related Solutions

C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c...
C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c program, you should end up with a simple English-French and French-English dictionary with a couple of about 350 words(I've provided 5 of each don't worry about this part). I just need a way to look up a word in a sorted array. You simply need to find a way to identify at which index i a certain word appears in an array of words...
USING MATLAB Part 2: Insert coins For this part, you are going implement the code that...
USING MATLAB Part 2: Insert coins For this part, you are going implement the code that asks the user to enter coins until they have entered enough for the NAU power juice. Open the insert_coins.m file. Initialize total to 0. We do this because initially, no coins have been entered. Using a loop, ask the user to enter a coin until the total matches or exceeds 115 cents. The input should be a char or string, so make sure that...
It's Java; the code should be the same as the output sample below; please, thank you....
It's Java; the code should be the same as the output sample below; please, thank you. Note: create a single-file solution that has multiple class definitions. Also, note that the static main() method should be a member of the Vehicle class. Create a class called Vehicle that has the manufacturers name (type String), number of cylinders in the engine (type int), and owner (type Person given next). Then, create a class called Truck that is derived from Vehicle and has...
Write a MATLAB code for importing an image and then being able to find all the...
Write a MATLAB code for importing an image and then being able to find all the coordinates that would draw that image on a plot column by column and row by row with a continuous line.
find all the errors c++ should be able to be compiled and run to perform the...
find all the errors c++ should be able to be compiled and run to perform the task #include iostream #include <string> Using namespace std; class Customer { // Constructor void Customer(string name, string address) : cust_name(name), cust_address(address) { acct_number = this.getNextAcctNumber(); } // Accessor to get the account number const int getAcctNumber() { return acct_number; } // Accessor to get the customer name string getCustName(} const { return cust_name; } // Accessor to get the customer address string getCustAddress() const...
R studio questions Write up your answers and paste the R code Copy and paste all...
R studio questions Write up your answers and paste the R code Copy and paste all plots generated. First create a sample drawn from a normal random variable. R has many distributions for which you can get probabilities and draw random numbers. We are going to use the normal. Go to help in R and type in rnorm. You will see a write up for functions associated with the normal distribution. dnorm is the density; pnorm is the probability distribution...
​​​​​​C++ It may seem hard but the instructions are listed, it's all about loops For this...
​​​​​​C++ It may seem hard but the instructions are listed, it's all about loops For this part, the program gives the user 4 choices for encrypting (or decrypting) the first character of a file. Non-lowercase characters are simply echoed. The encryption is only performed on lowercase characters. If c is char variable, then islower(c) will return true if c contains an lowercase character, false otherwise To read a single character from a file (let inFile be the name of your...
Be able to list and explain why alternate standards of care should be part of ethics-based...
Be able to list and explain why alternate standards of care should be part of ethics-based planning for disasters
**NO PAPER ANSWERS. ALL ANSWER SUBMISSIONS SHOULD BE ABLE TO RUN WITH NO ERRORS** **TO CREATE...
**NO PAPER ANSWERS. ALL ANSWER SUBMISSIONS SHOULD BE ABLE TO RUN WITH NO ERRORS** **TO CREATE THIS JAVA PROGRAM USE ECLIPSE NEON** The assignment is as follows: Connect four is a two-player board game in which the players alternately drop colored disks into a seven-column, six-row vertically-suspended grid. The objective of the game is to connect four same-colored disks in a row, a column, or a diagonal before your opponent can do likewise. The program prompts a player to drop...
For all code assignments there should be exhaustive test code which takes it input from stdin....
For all code assignments there should be exhaustive test code which takes it input from stdin. Your code should be reasonably efficient - it should not have big-Oh larger than what would be expected from good implementation Q1. Implement a double linked list with a sentinel element. The API should have four methods (no more) to: i) A method to create a new list instantiated with some type of elements (data stored in the list) defined at the time the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT