Answer to Question #197553 in C for Hassam

Question #197553

Your task is to declare a 2D array whose dimensions should be entered by the user of the program.

• Then you should initialize the array with ones (using nested loops).

• Next you have to write a function array_multiply() which takes in the array or its pointer as argument and multiplies all its entries with a user input number. (Hint: you will also need to pass in the dimensions of the matrix to this function).

• Similarly write a function array_add() that adds a constant number to all the entries in a 2D array.

• Print the results of calling these functions. 


1
Expert's answer
2021-05-27T23:05:37-0400
#include<stdio.h>
#include<stdlib.h>
void array_multiply(int rows, int cols,int arr1[rows][cols]);
void array_add(int rows, int cols,int arr2[rows][cols]);
int main()
{
    int i,j,rows,cols,n;
    printf("Enter the number of rows : ");
    scanf("%d",&rows);
    printf("Enter the number of columns : ");
    scanf("%d",&cols);
    int arr[rows][cols];
    for(i=0;i<rows;i++)
    {
        for(j=0;j<cols;j++)
        {
            arr[i][j]=1;
        }
    }
    array_multiply(rows,cols,arr);
    array_add(rows,cols,arr);
}
void array_multiply(int rows, int cols, int arr1[rows][cols])
{
    int num=0,i,j;
    printf("Enter a number : ");
    scanf("%d",&num);
    for(i=0;i<rows;i++)
    {
        for(j=0;j<cols;j++)
        {
            arr1[i][j]=arr1[i][j]*num;
        }
    }
    for(i=0;i<rows;i++)
    {
        for(j=0;j<cols;j++)
        {
            printf("%d ",arr1[i][j]);
        }
        printf("\n");
    }
}


void array_add(int rows, int cols, int arr2[rows][cols])
{
    int num1=0,i,j;
    printf("Enter a number : ");
    scanf("%d",&num1);
    for(i=0;i<rows;i++)
    {
        for(j=0;j<cols;j++)
        {
            arr2[i][j]=arr2[i][j]+num1;
        }
    }
    for(i=0;i<rows;i++)
    {
        for(j=0;j<cols;j++)
        {
            printf("%d ",arr2[i][j]);
        }
        printf("\n");
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Hassam
27.05.21, 08:55

hi point 3 and 4 is not working... could you please look it again and correct it.

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS