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.
#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");
}
}
Comments
Leave a comment