/* Write C program to accept and display 2d array of user specified size.Also write functions to perform the following on the 2d array
I. Function row_sum that takes row number as parameter and returns the sum of the row
II. Function col_sum that takes column number as parameter and returns the sum of the column
III. Function secondary _diagonal_sum that returns the sum of secondary diagonal elements if possible else should return -1
IV. Function primary_diagonal_sum that returns the sum of primary diagonal elements if possible else should return -1
*/
#include<stdio.h>
int main()
{
void read_array(int[][5],int,int);
void display_array(int[][5],int,int);
int row_sum(int [][5],int,int);
int col_sum(int [][5],int,int);
int sec_diagonal_sum(int [][5],int,int);
int pri_diagonal_sum(int [][5],int,int);
int nr,nc,ch,kr,kc,res;
int a[5][5];
printf("input no of rows & columns < =5\n");
scanf("%d %d",&nr,&nc);
read_array(a,nr,nc);
display_array(a,nr,nc);
while(1)
{
printf("1-rowsum\n2-colsum\n3-principal diagonal sum\n4-secondary diagonal sum\n 5-display\nanyother terminate\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("input row number of the row to be summed valid range 1 to %d\n",nr);
scanf("%d",&kr);
if(kr>nr || kr<0)
printf("invalid row no");
else
{
res=row_sum(a,kr-1,nc);
printf("sum of elements of %d row is=%d\n",kr,res);
}
break;
case 2:printf("input column number of the row to be summed valid range 1 to %d\n",nc);
scanf("%d",&kc);
if(kc>nc || kc<0)
printf("invalid row no");
else
{
res=col_sum(a,kc-1,nr);
printf("sum of elements of %d col is=%d\n",kc,res);
}
break;
case 3: res=pri_diagonal_sum(a,nr,nc);
if(res==-1)
printf("not square matrix\n");
else
printf("sum of principal diagonal elements is=%d\n",res);
break;
case 4: res=sec_diagonal_sum(a,nr,nc);
if(res==-1)
printf("not square matrix\n");
else
printf("sum of secondary diagonal elements is=%d\n",res);
break;
case 5:display_array(a,nr,nc);
break;
default:return(0);
}
}
}
/* returns row sum of specified row , if square matrix else returns -1
a1:2d array
kr1:row number to be summed
nr1:number of rows
nc1:number of columns
returns row sum or -1
*/
void read_array(int a1[][5],int nr1,int nc1)
{
for(int row=0;row<nr1;row++)
{
printf("input %d no of elements of %d row\n",nc1,row);
for(int col=0;col<nc1;col++)
scanf("%d",&a1[row][col]);
printf("\n");
}
}
/* returns row sum of specified row , if square matrix else returns -1
a1:2d array
kr1:row number to be summed
nr1:number of rows
nc1:number of columns
returns row sum or -1
*/
void display_array(int a1[][5],int nr1,int nc1)
{
printf("contents are \n");
for(int row=0;row<nr1;row++)
{
for(int col=0;col<nc1;col++)
printf("%d\t",a1[row][col]);
printf("\n");
}
}
/* returns row sum of specified row , if square matrix else returns -1
a1:2d array
kr1:row number to be summed
nc1:number of columns
returns row sum or -1
*/
int row_sum(int a1[][5],int kr1,int nc1)
{
int rsum=0;
for(int col=0;col<nc1;col++)
rsum+=a1[kr1][col];
return rsum;
}
/* returns column sum of specified column, if square matrix else returns -1
a1:2d array
kc1:column number to be summed
nr1:number of rows
returns column sum or -1
*/
int col_sum(int a1[][5],int kc1,int nr1)
{
int csum=0;
for(int row=0;row<nr1;row++)
csum+=a1[row][kc1];
return csum;
}
/* returns secondary diagonal sum,if square matrix else returns -1
a1:2d array
nr1:number of rows
nc1:number of columns
returns secondary diagonal sum or -1
*/
int sec_diagonal_sum(int a1[][5],int nr1,int nc1)
{
int row,col,ssum=0;
if(nr1==nc1)
{
for(row=0,col=nc1-1;(row<nr1 && col>=0);row++,col--)
ssum+=a1[row][col];
return ssum;
}
else
return -1;
}
/* returns principal diagonal sum,if square matrix else returns -1
a1:2d array
nr1:number of rows
nc1:number of columns
returns principal diagonal sum or -1
*/
int pri_diagonal_sum(int a1[][5],int nr1,int nc1)
{
int psum=0;
if(nr1==nc1)
{
for(int row=0;row<nr1;row++)
psum+=a1[row][row];
return psum;
}
else
return -1;
} Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!
OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.
#include <stdio.h>
int main()
{
char name[50];
printf("Enter name:");
scanf("%s", name);
printf("Hello %s \n" , name );
return 0;
}
C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.
When ever you want to perform a set of operations based on a condition if-else is used.
if(conditional-expression) {
// code
} else {
// code
}
You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to if-else-if ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type array-name[size];
data-type array-name[size][size];
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Two types of functions are present in C
Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,
User defined functions are the ones which are written by the programmer based on the requirement.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
//code
}