First Upload
This commit is contained in:
1773
15 - Advance Matrixs/PointersLibs.h
Normal file
1773
15 - Advance Matrixs/PointersLibs.h
Normal file
File diff suppressed because it is too large
Load Diff
35
15 - Advance Matrixs/ex1.c
Normal file
35
15 - Advance Matrixs/ex1.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "PointersLibs.h"
|
||||
|
||||
|
||||
int SlantSum(int *ptr_mat, unsigned short rows, unsigned short cols)
|
||||
{
|
||||
int sum = ZERO;
|
||||
cols++;
|
||||
for ( ;rows; rows--)
|
||||
{
|
||||
sum += *ptr_mat;
|
||||
ptr_mat += cols;
|
||||
}
|
||||
|
||||
return (sum);
|
||||
}
|
||||
|
||||
int SlantWithMaxSum(int *ptr_mat, unsigned short rows, unsigned short cols)
|
||||
{
|
||||
int max = MainSlantSum(ptr_mat, rows, cols);
|
||||
int temp;
|
||||
unsigned short slats;
|
||||
ptr_mat++;
|
||||
for (slats = ONE; slats < cols; slats++)
|
||||
{
|
||||
temp = SlantSum(ptr_mat++, rows, cols);
|
||||
max = (temp > max) ? temp : max;
|
||||
}
|
||||
|
||||
return (max);
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
|
||||
}
|
||||
BIN
15 - Advance Matrixs/ex2
Normal file
BIN
15 - Advance Matrixs/ex2
Normal file
Binary file not shown.
56
15 - Advance Matrixs/ex2.c
Normal file
56
15 - Advance Matrixs/ex2.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "PointersLibs.h"
|
||||
|
||||
int SumInSlantUntilNegative(int *ptr_mat, unsigned short rows, unsigned short cols)
|
||||
{
|
||||
int max = *ptr_mat;
|
||||
int steps = cols + ONE;
|
||||
|
||||
while (rows-- && (*ptr_mat < ZERO))
|
||||
{
|
||||
max += *ptr_mat;
|
||||
ptr_mat += steps;
|
||||
}
|
||||
|
||||
return (max - *ptr_mat);
|
||||
}
|
||||
|
||||
int MaxSumSlant(int *ptr_mat, unsigned short rows, unsigned short cols)
|
||||
{
|
||||
int *ptr_max_mat = ptr_mat; // Save the pointer of start max
|
||||
int max = *ptr_max_mat;
|
||||
|
||||
unsigned short steps = cols + ONE;
|
||||
|
||||
int *ptr_temp_mat = ptr_mat + steps; // For checking
|
||||
int temp_max = max,
|
||||
temp;
|
||||
|
||||
rows--;
|
||||
for (; rows; rows--)
|
||||
{
|
||||
temp = temp_max + *ptr_temp_mat;
|
||||
if (temp < temp_max || temp_max > temp)
|
||||
{
|
||||
if (temp_max > max)
|
||||
{
|
||||
max = temp_max;
|
||||
ptr_max_mat = ptr_temp_mat;
|
||||
}
|
||||
temp_max = ZERO;
|
||||
}
|
||||
temp_max += *ptr_temp_mat;
|
||||
ptr_temp_mat += steps;
|
||||
}
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
int mat[4][5] = {
|
||||
{1,2,3,4,5},
|
||||
{2,3,4,5,6},
|
||||
{3,4,-1,6,7},
|
||||
{4,5,6,7,8}
|
||||
};
|
||||
|
||||
printf("%d\n", MaxSumInSlant(mat, 4, 5));
|
||||
}
|
||||
Reference in New Issue
Block a user