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

50.1K

Verified Solution

Question

Programming

insert code in all TODO part, it's about pointer. atmel studio7.0. this should be able to run without utilities.h

// includes
#include

// function prototypes
// these are necessary if you are going to call a function beforeit is declared
// without these, we would have to put all of our functions abovemain
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 DeterminantCalculator\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 andinitialize 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 torun
   int method = 0;
  
   // create a variable for the determinant that youcalculate
   int det = 0;
  
   while (1)
   {
       // typical edge detection forbutton
       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 intoget_matrix\r\n\");
              // use get_matrix to get a 3x3 matrix from theuser
              // pass in matrix_array so that it can beupdated 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 withuser input\r\n\");
              // use get_matrix_global to get a 3x3 matrixfrom the user
              // no arguments are passed in, as this functionjust 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 arrayusing malloc\r\n\");
              // use malloc to dynamically allocate an arraythat can hold 9 integer values
              // this allocates the array on the heap, and itmust be freed once we are done
              // with it, or else we will have a memoryleak
              /***TODO - 1 line***/
                 
              // pass in matrix_dynamic to get_matrix so thatit can be updated with user data
              /***TODO - 1 line ***/

              // point matrix to matrix_dynamic
              /***TODO - 1 line ***/
           }
          
           // calculate thedeterminant of the matrix
           /***TODO - 1line***/
          
           // printcalculated determinant value
           printf(\"\r\n ***Determinant = %d\r\n\r\n\", det);
          
           // freematrix_dynamic if necessary
           /***TODO -multiple lines ***/

          
           // incrementmethod 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 3x3matrix\r\n\");
   // press any key to start
   // usart_serial_getchar waits for one character fromPutty
   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 fromPutty to an integer
       matrix[i] = (int) n - '0';
       // reflect the value back toPutty
       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 3x3matrix\r\n\");
   // press any key to start
   // usart_serial_getchar waits for one character fromPutty
   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 fromPutty to an integer
       matrix_global[i] = (int) n -'0';
       // reflect the value back toPutty
       printf(\"%d\r\n\",matrix_global[i]);
   }
   printf(\"Matrix entry complete\r\n\");
}

// calculate the determinant of a 3x3 matrix and return thevalue
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;
}

Answer & Explanation Solved by verified expert
4.4 Ratings (662 Votes)
includes include 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 configureconsolevoid void getmatrixint matrix int calcdetint matrix void getmatrixglobalvoid declare global variables int matrixglobal9 int main void configure system boardinit sysclkinit configureconsole printfnStarting Determinant Calculatorrnn declare constants const int NUMMETHODS 3 declare button variables bool prevbuttonstate BUTTON0INACTIVE bool currentbuttonstate BUTTON0INACTIVE create a fixedsized int array with 9 elements and initialize elements to 0 name matrixarray    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students