Good evening, I\'m looking for some help on my C programming script. I need it s
ID: 3741167 • Letter: G
Question
Good evening,
I'm looking for some help on my C programming script.
I need it so each of the calculations on the sum is done by using "User defined functions"
I appreciate it in advance.
Using codeblocks.
#include <stdio.h>
int main()
{
int rowsize, i, j, sumofrow = 0, columnsum = 0, sum = 0, diagnalsum = 0;
int a[100][100];
printf("Enter how many rows are in your table: ");
scanf("%d", &rowsize);
printf("Enter how many elements ar: ");
for (i = 0; i < rowsize; i++) {
for (j = 0; j < rowsize; j++) {
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < rowsize; i++) {
sumofrow = 0;
columnsum = 0;
for (j = 0; j < rowsize; j++) {
sumofrow += a[i][j];
columnsum += a[j][i];
}
printf("row%d: %d column%d: %d ", i + 1, sumofrow, i + 1, columnsum);
}
for (i = 0; i < rowsize; i++) {
for (j = 0; j < rowsize; j++) {
if (i == j) {
diagnalsum += a[i][j];
}
sum += a[i][j];
}
}
printf("Diagonal sum = %d ", diagnalsum);
printf("Total sum = %d ", sum);
return 0;
}
Explanation / Answer
#include <stdio.h>
// function 1
void calc(int a[][100], int rowsize)
{
int i,j,diagnalsum = 0, sum = 0;
for (i = 0; i < rowsize; i++) {
for (j = 0; j < rowsize; j++) {
if (i == j) {
diagnalsum += a[i][j];
}
sum += a[i][j];
}
}
printf("Diagonal sum = %d ", diagnalsum);
printf("Total sum = %d ", sum);
}
// function 2
void calculate(int a[][100], int rowsize)
{
int i, j, sumofrow, columnsum;
for (i = 0; i < rowsize; i++) {
sumofrow = 0;
columnsum = 0;
for (j = 0; j < rowsize; j++) {
sumofrow += a[i][j];
columnsum += a[j][i];
}
printf("row%d: %2d column%d: %d ", i + 1, sumofrow, i + 1, columnsum);
}
}
int main()
{
int rowsize, i, j, sumofrow = 0, columnsum = 0, sum = 0, diagnalsum = 0;
int a[100][100];
printf("Enter how many rows are in your table: ");
scanf("%d", &rowsize);
printf("Enter how many elements ar: ");
for (i = 0; i < rowsize; i++) {
for (j = 0; j < rowsize; j++) {
scanf("%d", &a[i][j]);
}
}
calculate(a,rowsize);
calc(a,rowsize);
return 0;
}
/*SAMPLE OUTPUT
Enter how many rows are in your table:
2
Enter how many elements ar:
1 2
3 4
row1: 3 column1: 4
row2: 7 column2: 6
Diagonal sum = 5
Total sum = 10
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.