Question

Question 5 (5 marks] Consider the syntactically correct C code below, which is missing a function print_diagonal include <std

Please explain answer :D

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The following code of the program is explained with comments. Please let me know if you have any doubts regarding the code.

Code:

#include <stdio.h>
typedef int Table[100][100];
/* print_diagonal(T,n)
 * Given a Table T, which will have n rows and n columns, print all of the
 * entries on the main diagonal of T ( that is, entries whose row number and
 * column number are equal). Remember to print a newline at the end. */
void print_diagonal(Table T,int n)
{
    // This variable is used to keep track of index of T
    int i=0;
    // Iterating over the Table T
    for(i=0;i<n;i++)
    {
        // printing the element on the main diagonal which is element at index T[i][i]
        printf("%d ",T[i][i]);
    }
    // Printing the new line
    printf("\n");
}

// This is the main function
int main()
{
    // These three Tables are the inputs that are used to test the above function
    Table T1 = { {10, 10, 2019},
                 {11, 14, 2019},
                 {111, 116, 225} };

    Table T2 = { {1, 2, 3, 4},
                 {5, 6, 7, 8},
                 {9, 10, 11, 12},
                 {13, 14, 15, 16} };

    Table T3 = { {1, 2, 3, 4, 5, 6, 7, 8, 9},
                 {10, 11, 12, 13, 14, 15, 16, 17, 18},
                 {19, 20, 21, 22, 23, 24, 25, 26, 27},
                 {28, 29, 30, 31, 32, 33, 34, 35, 36},
                 {37, 38, 39, 40, 41, 42, 43, 44, 45},
                 {46, 47, 48, 49, 50, 51, 52, 53, 54},
                 {55, 56, 57, 58, 59, 60, 61, 62, 63},
                 {64, 65, 66, 67, 68, 69, 70, 71, 72},
                 {73, 74, 75, 76, 77, 78, 79, 80, 81}};

    // Calling the function print_diagonal with above inputs
    print_diagonal(T1, 3);
    print_diagonal(T2, 4);
    print_diagonal(T3, 9);

    return 0;
}

Sample Output:

10 14 225 1 6 11 16 1 11 21 31 41 51 61 71 81 Process finished with exit code o

Add a comment
Know the answer?
Add Answer to:
Please explain answer :D Question 5 (5 marks] Consider the syntactically correct C code below, which...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the syntactically correct C code below, which is missing a function copy_positive. #include <stdio.h> /*...

    Consider the syntactically correct C code below, which is missing a function copy_positive. #include <stdio.h> /* copy_positive(A, Aout, size) * Given an array A, which will have the provided size, copy all positive * (non-negative/non-zero) elements of A into the provided output array Aout. * Return the size of the resulting array. */ /* (your code from below would be placed here) */ void print_array(int arr[], int n){ for( int i = 0; i < n; i++ ) printf("%d ",...

  • Transfer C code of selection sort to MIPS code and print the sorted array/results

    Transfer C code of selection sort to MIPS code and print the sorted array/results data Array: word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 string: asciz"In" # Trantec the C code of selection sort to MIPS code. Do not modify the existing code and structure! text main la ŞtO, Array li $t1, 0 li $t7,11 mul $17, $17, 4 subi $t8,$t7, 4 # array length n-11 # 4*n #4*(n-1) # lis in $t1 and j is...

  • Canvas → XC D Question 13 Given the code below: typedef struct p { char topic...

    Canvas → XC D Question 13 Given the code below: typedef struct p { char topic [20]; int nunWords; > page; typedef struct mag char name[20]; page pages [250); int numPages; } magazine; magazine m; How would you declare an array of 20 magazines? struct magazine[20] magazine[20] magazine myStack(20) None of the above struct m[191 Question 14 Given the code below: typedef struct char topic[20] Canvas & XC struct m[19] Question 14 Given the code below: typedef struct char topic[20];...

  • Transfer C code of selection sort to MIPS code and print the sorted array/results data Array:...

    Transfer C code of selection sort to MIPS code and print the sorted array/results data Array: word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 string: asciz"In" # Trantec the C code of selection sort to MIPS code. Do not modify the existing code and structure! text main la ŞtO, Array li $t1, 0 li $t7,11 mul $17, $17, 4 subi $t8,$t7, 4 # array length n-11 # 4*n #4*(n-1) # lis in $t1 and j is...

  • Study the c++ code below and answer the question on templates after // A polymorphic swap...

    Study the c++ code below and answer the question on templates after // A polymorphic swap function for any type of object (an example) template<typename T> void MySwap( T& x, T& y) { T temp; temp = x; x = y; y = temp; } // A polymorphic class holding an [x,y] position of any type template<class T> class Position { public: Position(T x=0, T y=0) : x_(x), y_(y) {} friend std::ostream& operator<<(std::ostream& os, const Position& p) { return os...

  • In C please Write a function so that the main() code below can be replaced by...

    In C please Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). Original maino: int main(void) { double milesPerHour, double minutes Traveled; double hours Traveled; double miles Traveled; scanf("%f", &milesPerHour); scanf("%lf", &minutes Traveled); hours Traveled = minutes Traveled / 60.0; miles Traveled = hours Traveled * milesPerHour; printf("Miles: %1f\n", miles Traveled); return 0; 1 #include <stdio.h> 3/* Your solution goes here */ 1 test passed 4 All tests passed...

  • CAN SOMEONE COMPLETE THIS CODE PLEASE THNK YOU!! Question - Write a program in c++ that...

    CAN SOMEONE COMPLETE THIS CODE PLEASE THNK YOU!! Question - Write a program in c++ that keeps an appointment book. Make a class Appointment that stores a description of the appointment, the appointment day, the starting time, and the ending time. Your program should keep the appointments in a sorted vector. Users can add appointments and print out all appointments for a given day. When a new appointment is added, use binary search to find where it should be inserted...

  • Please in C Language Thank you! The following program source code is incomplete 1 #include <stdio.h> 2 3 // TODO:...

    Please in C Language Thank you! The following program source code is incomplete 1 #include <stdio.h> 2 3 // TODO: 1) Add the typedef here: 5// TODO: 2) Modify the parameter of repeat to add irn the function pointer for the function to be called repeatedly: 8 void repeat (int times) for (int k 0; k < times; ++k) 12 // TODO: 3) Add the call to the function pointer here: 14 15 17 void test (void) 18 printf("Test!\n"); 19...

  • A)Correct this code and explain what was corrected // C code - For Loop / Array...

    A)Correct this code and explain what was corrected // C code - For Loop / Array Population // This program will populate integers into an array, and then display those integers. // Developer: Johnson, William CMIS102 // Date: Mar 4, 2020 // Global constants #define INTLIMIT 10 #include <stdio.h> // This function will handle printing my name, class/section number, and the date void printStudentData() { char fullName[19] = "William J. Johnson"; char classNumber[9] = "CMIS 102"; char sectionNumber[5] = "4020";...

  • Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std;...

    Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std; void *PrintHello(void *arg) { int actual_arg = *((int*) arg); cout << "Hello World from thread with arg: " << actual_arg << "!\n"; return 0; } int main() { pthread_t id; int rc; cout << "In main: creating thread \n"; int t = 23; rc = pthread_create(&id, NULL, PrintHello, (void*) &t); if (rc){ cout << "ERROR; return code from pthread_create() is " << rc <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT