Question

Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of...

  • Write a C function  f such that …
    • function  f accepts, as input, …
      • a two-dimensional array of integers in which each row has three columns
      • the number of rows in the array
    • function  f returns the sum of the odd integers in the given array of integers
      • and returns zero if there are no odd integers in the array

COMPILER STACK TRACE

None

PROGRAM EXECUTION STACK TRACE

None

COMPILER COMMAND LINE ARGUMENTS

-lm

INPUT OF THE TEST CASE

 
1 #define NUM_ROWS 5
2
3 int arr [NUM_ROWS][3]
4 = { { -18, 5, 64 },
5 { -29, 77, -87 },
6 { 53, 14, 20 },
7 { 128, -64, -47 },
8 { 107, 87, -32 } } ;
9
10 int sum = f ( arr, NUM_ROWS ) ;
11
12 munit_assert ( sum == ((5) + (-29) + (77) + (-87) + (53) + (-47) + (107) + (87)) ) ;
13
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
 *  C Program to sum odd integers in a matrix and return
 */

#include <stdio.h>
#include <assert.h>
#define NUM_ROWS 5

int f(int mat[][3], int num_rows)
{
  int i, j;
  int sum = 0;

  for (i = 0; i < num_rows; i++)
  {
    for (j = 0; j < 3; j++)
    {
      if (mat[i][j] % 2 != 0)
      {
        sum += mat[i][j];
      }
    }
  }

  return sum;
}

int main()
{
  int arr [NUM_ROWS][3] = { { -18, 5, 64 },
                            { -29, 77, -87 },
                            { 53, 14, 20 },
                            { 128, -64, -47 },
                            { 107, 87, -32 } } ;

  int sum = f(arr, NUM_ROWS);

  //munit_assert(sum == ((5) + (-29) + (77) + (-87) + (53) + (-47) + (107) + (87)));

  assert(sum == ((5) + (-29) + (77) + (-87) + (53) + (-47) + (107) + (87)));
  
  return 0;
}

/*  Program ends here */
Add a comment
Know the answer?
Add Answer to:
Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of...
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
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