Queston 3 is done
This commit is contained in:
27
Q3.c
27
Q3.c
@@ -1,11 +1,14 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
// Question 3
|
||||||
|
|
||||||
/* I know that the program would be overall simpler if I printed the sums in the function as they are
|
/* I know that the program would be overall simpler if I printed the sums in the function as they are
|
||||||
* calculated, but I have always been taught to avoid printing within functions, so I went with the
|
* calculated, but I have always been taught to avoid printing within functions, so I went with the
|
||||||
* returning an array of the sums method. It has the benefit of being able to use the sums later in the program
|
* returning an array of the sums method. It has the benefit of being able to use the sums later in the program
|
||||||
* if necessary
|
* if necessary
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Function to calculate sum of each row of matrix. Takes
|
// Function to calculate sum of each row of matrix
|
||||||
|
// Arguments are array width, height, the array, and the array of return values
|
||||||
void row_sums(int width, int height, int matrix[width][height], int return_array[]) {
|
void row_sums(int width, int height, int matrix[width][height], int return_array[]) {
|
||||||
int row_sum;
|
int row_sum;
|
||||||
for(int i = 0; i < height; i++) {
|
for(int i = 0; i < height; i++) {
|
||||||
@@ -16,13 +19,14 @@ void row_sums(int width, int height, int matrix[width][height], int return_array
|
|||||||
return_array[i] = row_sum;
|
return_array[i] = row_sum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Function to calculate sum of each column of matrix. Takes
|
// Function to calculate sum of each column of matrix
|
||||||
|
// Arguments are array width, height, the array, and the array of return values
|
||||||
void column_sums(int width, int height, int matrix[width][height], int return_array[]) {
|
void column_sums(int width, int height, int matrix[width][height], int return_array[]) {
|
||||||
int column_sum;
|
int column_sum;
|
||||||
for(int i = 0; i < width; i++) {
|
for(int i = 0; i < height; i++) {
|
||||||
column_sum = 0;
|
column_sum = 0;
|
||||||
for(int j = 0; j < height; j++) {
|
for(int j = 0; j < width; j++) {
|
||||||
column_sum+=matrix[i][j];
|
column_sum+=matrix[j][i];
|
||||||
}
|
}
|
||||||
return_array[i] = column_sum;
|
return_array[i] = column_sum;
|
||||||
}
|
}
|
||||||
@@ -30,12 +34,13 @@ void column_sums(int width, int height, int matrix[width][height], int return_ar
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int width, height;
|
int width, height;
|
||||||
|
|
||||||
printf("Enter width then enter height:\n");
|
printf("Enter width then enter height:\n");
|
||||||
scanf("%d", &width);
|
scanf("%d", &width);
|
||||||
scanf("%d", &height);
|
scanf("%d", &height);
|
||||||
|
|
||||||
int matrix[width][height];
|
int matrix[width][height]; // The 2D array that holds the matrix
|
||||||
|
// Input values into the matrix
|
||||||
for(int i = 0; i < width; i++) {
|
for(int i = 0; i < width; i++) {
|
||||||
for(int j = 0; j < height; j++) {
|
for(int j = 0; j < height; j++) {
|
||||||
printf("Enter matrix value %d,%d: ", i, j);
|
printf("Enter matrix value %d,%d: ", i, j);
|
||||||
@@ -50,7 +55,8 @@ int main() {
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
int row_sum_values[height];
|
// Find sums for each row and print them
|
||||||
|
int row_sum_values[width]; // Holds the sums for each row
|
||||||
row_sums(width, height, matrix, row_sum_values);
|
row_sums(width, height, matrix, row_sum_values);
|
||||||
printf("Row sum values are: ");
|
printf("Row sum values are: ");
|
||||||
for(int i = 0; i < width; i++) {
|
for(int i = 0; i < width; i++) {
|
||||||
@@ -58,10 +64,11 @@ int main() {
|
|||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
int column_sum_values[height];
|
// Find sums for each column and print them
|
||||||
|
int column_sum_values[height]; // Holds the sums for each column
|
||||||
column_sums(width, height, matrix, column_sum_values);
|
column_sums(width, height, matrix, column_sum_values);
|
||||||
printf("Column sum values are: ");
|
printf("Column sum values are: ");
|
||||||
for(int i = 0; i < width; i++) {
|
for(int i = 0; i < height; i++) {
|
||||||
printf("%d, ", column_sum_values[i]);
|
printf("%d, ", column_sum_values[i]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user