Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Develop a small part of spreadsheet software

C Basic Declarations and Expressions: Exercise-148 with Solution

Your task is to develop a small part of spreadsheet software.

Write a C program, which adds up columns and rows of given table as shown in the following figure.

C Programming: Develop a small part of spreadsheet software.

Input:
n (the size of row and column of the given table)
1st row of the table
2nd row of the table
:
:
n th row of the table
The input ends with a line consisting of a single 0.
Output:
For each dataset, print the table with sum of rows and columns.

Sample Solution:

C Code:

#include <stdio.h>
int main()
{
  int cell_data[11][11];
  int i, j, n, sum_val;
    printf("Input number of rows/columns:\n"); 
    scanf("%d", &n);
    printf("Input the cell value\n"); 
    if(n>0)
    {	
    for(i=0;i<n;i++){
    	printf("\nRow %d input cell values\n",i);
      for(j=0;j<n;j++){
	scanf("%d", &cell_data[i][j]);
      }
    }
    printf("\nResult:\n");
    for(i=0;i<n;i++){
      sum_val=0;
      for(j=0;j<n;j++){
	sum_val+=cell_data[j][i];
      }
      cell_data[n][i]=sum_val;
    }
    

    for(i=0;i<n;i++){
      sum_val=0;
      for(j=0;j<n;j++){
	sum_val+=cell_data[i][j];
      }
      cell_data[i][n]=sum_val;
    }

    sum_val=0;
    for(i=0;i<n;i++){
      sum_val+=cell_data[n][i];
    }
    cell_data[n][n]=sum_val;

    for(i=0;i<n+1;i++){
      for(j=0;j<n+1;j++){
       	printf("%5d", cell_data[i][j]);
      }
      printf("\n");
    }
}
  return 0;
}

Sample Output:

Input number of rows/columns:
4
Input the cell value

Row 0 input cell values
25
69
51
26

Row 1 input cell values
68
35
29
54

Row 2 input cell values
54
57
45
63

Row 3 input cell values
61
68
47
59

Result:
   25   69   51   26  171
   68   35   29   54  186
   54   57   45   63  219
   61   68   47   59  235
  208  229  172  202  811

Flowchart:

C Programming Flowchart: Develop a small part of spreadsheet software.

C programming Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C program to find the number of combinations that satisfy p + q + r + s = n where n is a given number <= 4000 and p, q, r, s in the range of 0 to 1000.
Next: Write a C program, which reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP