Question

Write a C program (not C++) that calls a function to multiply a matrix (two dimensional...

Write a C program (not C++) that calls a function to multiply a matrix (two dimensional array).

The main program should ask the user to enter an integer that will be used as the matrix “adder” and then call the function. The function should perform a matrix increment (every element of the array is incremented by the same value) and assign the result to another array. The function should be flexible enough to work with any array size and “adder”, communicated to the function from the main function. After the function call, the main program should print the array with the initial values followed by the multiplied values as shown below. Use as many variables and arrays as needed. When initializing the array, use the initial values shown in the output.

Provide restriction so that the original array passed to it cannot be modified by the function.

Expected input / output.

Enter adder: 2

Initial values

5    2    7  

1    6    3

After adding by 2

7   4   9  

3   8   5

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

Here is the code for you:

#include <stdio.h>
#include <stdlib.h>
int** incrementBySpecifiedValue(int array[][3], int rows, int cols, int value)
{
    int **newArray = malloc(sizeof(int*) * rows);
    for(int i = 0; i < rows; i++)
    {
       *(newArray + i) = malloc(sizeof(int) * cols);
       for(int j = 0; j < cols; j++)
           *(*(newArray + i) + j) = array[i][j] + value;  
    }
    return newArray;
}
int main()
{
    int array[][3] = {{5, 2, 7}, {1, 6, 3}};
    //ask the user to enter an integer that will be used as the matrix “adder”
    int addValue;
    printf("Enter adder: ");
    scanf("%d", &addValue);
    //Call the function.
    int** newValue = incrementBySpecifiedValue(array, 2, 3, addValue);
    //Print the array with the initial values followed by the multiplied values as shown below.
    printf("Initial values\n");
    for(int i = 0; i < 2; i++)
    {
        for(int j = 0; j < 3; j++)
            printf("%d\t", array[i][j]);
        printf("\n");  
    }
      
    printf("Initial values\n");
    for(int i = 0; i < 2; i++)
    {
        for(int j = 0; j < 3; j++)
            printf("%d\t", newValue[i][j]);
        printf("\n");  
    }
}

And the output screenshot is:

Add a comment
Know the answer?
Add Answer to:
Write a C program (not C++) that calls a function to multiply a matrix (two dimensional...
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
  • Q 3-) (30 points) Write a C program (MAIN function) and a FUNCTION to create and...

    Q 3-) (30 points) Write a C program (MAIN function) and a FUNCTION to create and print the transpose of a two- dimensional array. Within C program (the MAIN function); Declare a two- dimensional 3X2 integer array A and initialise with the values (1,2), (3,4), (5,6), declare also a two-dimensional 2X3 integer array B and initialise it with zero. Call the FUNCTION and pass arrays A and B to the FUNCTION as arguments. Print the array A and its transpose....

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

    C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result. a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array. b) Function addMatrices: This function calculatesthe sum result of two matrices. c) Function printMatrix: This function prints the matrix to screen....

  • In pseudocode only Design (pseudocode) a program (name it Occurrences) to determine whether two two-dimensional arrays...

    In pseudocode only Design (pseudocode) a program (name it Occurrences) to determine whether two two-dimensional arrays are equivalent or not. Two arrays are equivalent if they contain the same values in any order. The program main method defines two two-dimensional array of size 3-by-3 of type integer, and prompts the user to enter integer values to initialize the arrays. The main method calls method isEquivalent()that takes two two-dimensional arrays of integer and returns true (boolean value) if the arrays contain...

  • Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters:...

    Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters: an unsigned doubleword array and the length of the array. The function must return the value of the largest array member in eax. Preserve all registers (except eax) that are modified by the function. Write a test program in main that calls findLargest three times, each call using a different array with different lengths. Function called countHits Write a function called named countHits that...

  • Write a C function named add with the following parameters: three float arrays a, b and c, each of the three arrays is...

    Write a C function named add with the following parameters: three float arrays a, b and c, each of the three arrays is a two dimensional array. The function should add the first two arrays a and b (entry by entry), storing the results in the third array c. In the main function, call the add function with appropriate values. The main function should print the sum of the two arrays stored in the third array. Sample run: float al...

  • I should use the array and loop to create a java program according to the instruction,...

    I should use the array and loop to create a java program according to the instruction, but I have no idea how to do it. Introduction This lab assignment continues to give you practice using loops, particularly loops with variable termination conditions, and it also provides you an opportunity to use one-dimensional arrays. Recall that an array is used to store a collection of data. The data can be values of Java primitive data types or else objects (for instance,...

  • Write a C program as follows: Single source code file Calls a function with an arbitrary...

    Write a C program as follows: Single source code file Calls a function with an arbitrary name (i.e. you name it) that accepts two arrays of the same size The function should add each element in the arrays together and place the values in a third array Each array element, each array address, and the sum are printed to the screen in tabulated format with headersI also would like to request that LOTS of comments be included, as I need...

  • Write an object-oriented C++ program (i.e. a class and a main function to use it), using...

    Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers. This program has six required outputs. Start by initializing an array with the following integers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Your array input may be hardcoded...

  • Write a C function named add with the following parameters: three float arrays a, b and...

    Write a C function named add with the following parameters: three float arrays a, b and c, each of the three arrays is a two dimensional array. The function should add the first two arrays a and b (entry by entry), storing the results in the third array c. In the main function, call the add function with appropriate values. The main function should print the sum of the two arrays stored in the third array. Sample run: float al...

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