So the exercise goes like this:
We are given a matrix and we are suppose to add the elements of the matrix into a vector array in a concentric way. What I mean is that we have for example a 5x5 matrix with certain values and in another vector sum[3] (I assume 3 because with a 5x5 matrix we have 3 concentric "circles" of numbers) and we add them there.
I hope I was clear enough.
So anyway i remember doing something similar some weeks ago where we had to do a concentric array of numbers meaning the given the number of rows and columns the matrix should have looked like this: (I did a 3x3 here just for demonstration)
1 2 3
8 1 4
7 6 5
So basically I used the same idea here but to no avail
I don't get the values I'm supposed to get . For those who are wondering what are the values of the matrix since I read it from a file:
1 2 3 4 5
6 7 8 9 0
1 2 3 4 5
6 7 8 9 0
1 1 1 1 1
this is what I have done so far :
#include <stdio.h>
#include <stdlib.h>
#define N 5
int main()
{
int v[N][N];
int i, j;
int k = 0;
FILE*file;
file = fopen("file", "r");
int sum[3] = {0,0,0};
for (i = 0; i < N; i )
{
for (j = 0; j < N; j )
{
fscanf(file, "%d", &v[i][j]);
}
}
printf("The matrix: \n");
for (i = 0; i < N; i )
{
for (j = 0; j < N; j )
{
printf("%d ", v[i][j]);
}
printf("\n");
}
while (k < 3)
{
for (i = 0; i < N; i )
{
for (j = i; j < N - 1; j )
sum[k] = sum[k] v[i][j];
for (j = i - 1; j < N - 1 - i; j )
sum[k] = sum[k] v[j][N - 1 - i];
for (j = N - 2 - i; j >= i; j--)
{
sum[k] = sum[k] v[N - 1 - i][j];
}
for (j = N - 2 - i; j > i; j--)
{
sum[k] = sum[k] v[j][i];
}
k ;
}
}
for (i = 0; i < 3; i )
{
printf("%d ", sum[i]);
}
return 0;
}
CodePudding user response:
I dont know if I understand your question correctly....
is this what you looking for?
int main(void) {
int MatrixSize = 5;
int Matrix[5][5] = {
{1,1,1,1,1},
{1,3,2,2,1},
{1,2,3,2,1},
{1,2,2,4,1},
{1,1,1,1,1}
};
int Sum[3] = {0,0,0};
int ArraySize = 3;
for (int k = 0; k < ArraySize; k) {
for (int j = k; j < MatrixSize - k; j) {
Sum[k] = Matrix[k][j];
if (MatrixSize - 1 - k > k) {
Sum[k] = Matrix[MatrixSize - 1 - k][j];
}
}
for (int i = k 1; i < MatrixSize-k-1; i) {
Sum[k] = Matrix[i][k];
Sum[k] = Matrix[i][MatrixSize - 1 - k];
}
}
for (int i = 0; i < ArraySize; i) {
printf("%d ", Sum[i]);
}
return 0;
}
