1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int m, n;
printf("Please enter array's row and col: ");
scanf("%d %d", &m, &n);
int arr[m][n];
for (size_t i = 0; i < m; i++)
for (size_t j = 0; j < n; j++)
scanf("%d", &arr[i][j]);
for (size_t i = 0; i < m; i++){
int row_sum = 0;
for (size_t j = 0; j < n; j++){
row_sum += arr[i][j];
}
printf("row %lu sum: %d\n", i, row_sum);
}
for (size_t i = 0; i < n; i++){
int col_sum = 0;
for (size_t j = 0; j < m; j++){
col_sum += arr[j][i];
}
printf("col %lu sum: %d\n", i, col_sum);
}
return 0;
}
Please enter array's row and col: 2 3
1 2 3
4 5 6
row 0 sum: 6
row 1 sum: 15
col 0 sum: 5
col 1 sum: 7
col 2 sum: 9
2.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
int n;
printf("Please enter array size: ");
scanf("%d", &n);
int arr[n][n];
for(size_t i = 0; i < n; i++)
for(size_t j = 0; j < n; j++)
scanf("%d", &arr[i][j]);
int four_edge_sum = 0;
int two_diag_sum = 0;
for(size_t i = 0; i < n; i++)
for(size_t j = 0; j < n; j++){
if(i == 0 || i == n-1 || j == 0 || j == n-1)
four_edge_sum += arr[i][j];
if(i == j || (i + j) == n-1)
two_diag_sum += arr[i][j];
}
printf("four_edge_sum = %d, two_diag_sum = %d, "
"result = %d\n", four_edge_sum, two_diag_sum, four_edge_sum - two_diag_sum);
return 0;
}
Please enter array size: 3
1 2 3 4 5 6 7 8 9
four_edge_sum = 40, two_diag_sum = 25, result = 15
3.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int main(int argc, const char *argv[])
{
int m, n;
printf("Please enter array's row and col: ");
scanf("%d %d", &m, &n);
int arr[m][n];
for (size_t i = 0; i < m; i++)
for (size_t j = 0; j < n; j++)
scanf("%d", &arr[i][j]);
int max = arr[0][0];
for (size_t i = 0; i < m; i++)
for (size_t j = 0; j < n; j++)
if(max <= arr[i][j])
max = arr[i][j];
int second_max = INT_MIN;
for (size_t i = 0; i < m; i++)
for (size_t j = 0; j < n; j++)
if(arr[i][j] != max && second_max <= arr[i][j])
second_max = arr[i][j];
printf("max = %d, second_max = %d\n", max, second_max);
return 0;
}
Please enter array's row and col: 2 3
1 4 0
2 3 5
max = 5, second_max = 4
4.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
int row, col;
printf("Please enter array size (row, col): ");
scanf("%d %d", &row, &col);
int arr[row][col];
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
scanf("%d", &arr[i][j]);
int arr_tran[col][row];
for(int i = 0; i < col; i++)
for(int j = 0; j < row; j++)
arr_tran[i][j] = arr[j][i];
for(int i = 0; i < col; i++){
for(int j = 0; j < row; j++)
printf("%d ", arr_tran[i][j]);
putchar(10);
}
return 0;
}
Please enter array size (row, col): 2 3
1 2 3
4 5 6
1 4
2 5
3 6
5.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
char str[32];
gets(str);
int i = 0;
while(str[i] != '\0'){
while(str[i] == ' ')
for(int j = i; str[j] != '\0'; j++)
str[j] = str[j+1];
i++;
}
puts(str);
return 0;
}
h e ll o wo r l d
helloworld
6.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void reverse(char *, int, int);
int main(int argc, const char *argv[])
{
char str[128];
gets(str);
reverse(str, 0, strlen(str) - 1);
puts(str);
int beg = 0, end;
while (str[beg] != '\0') {
while (str[beg] == ' ') {
beg++;
if (str[beg] == '\0')
break;
}
if (str[beg] != '\0')
end = beg + 1;
else
break;
while (str[end] != ' ' && str[end] != '\0')
end++;
reverse(str, beg, end - 1);
if (str[end] == '\0')
break;
beg = end + 1;
}
puts(str);
return 0;
}
void reverse(char *str, int beg, int end){
for(int i = beg, j = end; i <= j; i++, j--){
int temp = str[i];
str[i] = str[j];
str[j] = temp;
}
return;
}
good good study
yduts doog doog
study good good