In C language How to write a program that allows main to get

(In C language) How to write a program that allows main to get the size of the array (Rows & columns), then create the array, then call fillArray,  printArray, and identifySmallestElement. Have the user to enter the # of rows and columns for the array in the form of n x m on a single line, where n is the number of rows and m is the number of columns; main reads the nxm with a single scanf statement.

Put the main function first, at the beginning of the file; then define all the other functions after main

Don’t use global variables.

Solution

#include <stdio.h>
#include<stdlib.h>

void fillArray(int *arr,int row,int col)
{
printf(\"Enter array data : \");
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf(\"%d\",&arr[i*row+j]);
}
}
}

void printArray(int *arr,int row,int col)
{
printf(\"Array data is : \ \");
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(\"%d \",arr[i*row+j]);
}
printf(\"\ \");
}
}

int smallestElement(int *arr,int row,int col)
{
int i,j,smallest;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==0 && j==0)
smallest = arr[i*row+j];
else
{
if(arr[i*row+j]<smallest)
smallest = arr[i*row+j];
}
}
}
  
return smallest;
}

int main(void) {
   // your code goes here
int row,col;
  
printf(\"Enter the number of rows : \");
scanf(\"%d\",&row);
  
printf(\"Enter the number of columns : \");
scanf(\"%d\",&col);
  
int *arr = (int*)malloc(sizeof(int)*row*col);
fillArray(arr,row,col);
printArray(arr,row,col);
printf(\"%d is the smallest element.\",smallestElement(arr,row,col));
   return 0;
}

OUTPUT:

Enter the number of rows : 2

Enter the number of columns : 2

Enter array data : 1 2 3 4

Array data is :
1 2
3 4
1 is the smallest element.

(In C language) How to write a program that allows main to get the size of the array (Rows & columns), then create the array, then call fillArray, printArra
(In C language) How to write a program that allows main to get the size of the array (Rows & columns), then create the array, then call fillArray, printArra

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site