I am trying to create and store values inside a matrix, represented as a 1 dimensional array.
When I try to set the values, my output comes out wrong.
Matrix Struct
struct matrix{
char matrixName[50];
int rows;
int columns;
float* data;
};
typedef struct matrix matrix_t;
matrix_t our_matrix[MAX_MATRICES];
Creating the matrix:
int create_matrix(matrix_t* matrixP, int matrixLocation){
char tempName[50];
int rows, cols;
printf("Enter a name for your matrix>\n");
scanf("%s", tempName);
printf("Enter the number of rows>\n");
scanf("%d", &rows);
printf("Enter the number of cols>\n");
scanf("%d", &cols);
float * data = (float *) malloc(rows * cols * sizeof(float));
int row = 0, col = 0;
for (row = 1; row <= rows; row++) {
for (col = 1; col <= cols; col++) {
data[(row-1) + (col-1) * cols] = 0;
}
}
strcpy(matrixP[matrixLocation].matrixName, tempName);
matrixP[matrixLocation].rows = rows;
matrixP[matrixLocation].columns = cols;
matrixP[matrixLocation].data = data;
return 0;
}
Setting Values:
int setValues(matrix_t* our_matrix, int number_of_matrices) {
int i, matrix_index;
for(i = 0; i < number_of_matrices; i++){
printf("Matrix %i\t %i Rows\t %i Columns - Name: %s\n", i+1, our_matrix[i].rows, our_matrix[i].columns, our_matrix[i].matrixName);
}
printf("\nWhich matrix would you like to set the values for?\n");
printf("Select a matrix number from the list above>\n");
scanf("%d", &matrix_index);
while(matrix_index < 1 || matrix_index > number_of_matrices){
printf("Enter a number from the list of available matrices - number must be greater than zero and less than or equal to the number of available matrices:\n");
scanf("%d", &matrix_index);
}
int max;
matrix_index -= 1;
max = our_matrix[matrix_index].columns;
float *data = our_matrix[matrix_index].data;
int row = 0, col = 0;
for (row = 0; row < our_matrix[matrix_index].rows; row++) {
for (col = 0; col < our_matrix[matrix_index].columns; col++) {
printf("Enter the value for column number %d of row number %d>\n", col+1, row+2);
scanf("%f", &data[(row) + (col) * (max)]);
}
/* separate rows by newlines */
}
our_matrix[matrix_index].data = data;
return 0;
}
When I try call setValues on a 5 x 2 matrix and I give it the values 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 these are the values that I get:
I honestly can't fathom how it stores those values and not the 1:10 that i'm passing it.
You have one little confusion in address computation: You inverted row ans col in multiplication.
You write (I replaced max by its value) in setValues
scanf("%f", &data[(row) + (col) * (our_matrix[matrix_index].columns)]);
the same in create_matrix function:
data[(row-1) + (col-1) * cols] = 0;
You should have written:
scanf("%f", &data[(col) + (row) * (our_matrix[matrix_index].columns)]);
and
data[(col-1) + (row-1) * cols] = 0;
You can do the math to be convinced:
If rows is 2 and cols is 10, with your method, the max index is 2 + 9 * 10 = 90, it's too big for a 2x10 matrix.
One more thing:
The function create_matrix can be simplified:
int create_matrix(matrix_t* matrixP, int matrixLocation){
int rows, cols;
printf("Enter a name for your matrix>\n");
/* Warning, you should test what scanf returned! */
scanf("%s", matrixP[matrixLocation].matrixName);
printf("Enter the number of rows>\n");
/* Warning, you should test what scanf returned! */
scanf("%d", &rows);
printf("Enter the number of cols>\n");
/* Warning, you should test what scanf returned! */
scanf("%d", &cols);
matrixP[matrixLocation].rows = rows;
matrixP[matrixLocation].cols = cols;
/* Warning, you should test what calloc returned! */
matrixP[matrixLocation].data = calloc(rows * cols, sizeof(float));
return 0;
}
Related
int a[3][4], i, j;
int row_total;
printf("Enter matrix values : \n");
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 4; j++)
scanf("%d", &a[i][j]);
}
row_total *= a[i][j];
if (row_total > 0) {
printf("%d\n", row_total);
}
The matrix in the example has 3 rows and 4 columns. The values of this matrix are determined by the user. There are examples everywhere in the form of the product of 2 matrices. But I couldn't find how to process the values in rows of a single matrix. I need to multiply the values that the user enters for the row. How can I find the product of the values written in the rows of the matrix?
In the last part, I just tried to print the values to see if I could read the matrix multiplication of the code. But I could not read the values and get any multiplication result.
initialization
Always initialize your variables
because when you don't initialize it, it will have a "garbage" value.
You had problems reading the values because of the garbage value in row_total.
https://stackoverflow.com/a/1597426/17652416
Arrays
Pay attention that arrays start from 0 not 1 so if the amount of rows is 4 for
example, the rows will be - 0, 1, 2, 3 and not 1, 2, 3, 4.
So the iterations should start from i = 0; i < 4 and not i = 1; i <= 4.
More about arrays in C
Fixed initialization
Defines
#define ROWS 3
#define COLS 4
int a[ROWS][COLS] = { 0 };
int row = 0, col = 0;
int row_total = 1;
printf("Enter matrix values : \n");
//Get the values for the matrix from the user
for (row = 0; row < ROWS; row++)
{
for (col = 0; col < COLS; col++)
{
scanf("%d", &a[row][col]);
}
}
Iterating on the rows - the answer
//Print the product of every row
for (row = 0; row < ROWS; row++)
{
row_total = 1;
for (col = 0; col < COLS; col++)
{
row_total *= a[row][col];
}
printf("The product of row %d is: %d\n", row, row_total);
}
I have the following 2D array:
int censusData[4][3] = {{87290, 77787, 55632},
{83020, 78373, 62314},
{95588, 87934, 705421},
{112456, 97657, 809767}};
I want to print values column-wise in that, after 3 passes of a loop, it would print:
87290 83020 95588 112456
77787 78373 87934 97657
55632 62314 705421 809767
Notice how it's the first element from each sub-array, then the 2nd, then the third..etc
I can easily access each subarray when going row-wise using this:
int* averageCityIncome(int size, int size_2, int arr[][size_2]){
int* t_arr = malloc(4 * sizeof(int));
for (int i = 0; i < size; i++){
int avg = 0;
for (int j = 0; j < size_2; j++){
avg += arr[i][j];
}
avg /= size_2;
t_arr[i] = avg;
}
return t_arr;
}
But now I'm trying to read column-wise as stated above and I so far have this:
int* averageIncome(int size, int size_2, int arr[][size_2]){
int* t_arr = malloc(4 * sizeof(int));
for (int i = 0; i < size_2; i++){
for (int j = 0; j < size; j++){
printf("%d\n", arr[i][j]);
}
printf("-----\n");
}
return t_arr;
}
But it doesn't seem to be working. I'm still pretty new to C, and it's difficult to wrap my mind around 2D arrays still. Any help would be greatly appreciated.
You simply need to swap i and j for the dimensions when addressing a certain element of the array in the caller.
int* averageIncome(int size, int size_2, int arr[][size_2]) {
int* t_arr = calloc(4, sizeof(int)); // calloc to initialize the array elements to 0.
if ( t_arr == NULL )
{
fputs("Error at memory allocation for t_arr!", stderr);
exit(1);
}
int avg = 0; // definition of avg placed outside of the loop.
for (int i = 0; i < size_2; i++) {
for (int j = 0; j < size; j++) {
printf("%d\n", arr[j][i]); // j swapped with i.
avg += arr[j][i]; // same here too.
}
printf("-----\n");
t_arr[i] = avg / size;
avg = 0;
}
return t_arr;
}
Example (Online):
#include <stdio.h>
#include <stdlib.h>
#define ROWS 4
#define COLS 3
int* averageIncome(int size, int size_2, int arr[][size_2]) {
int* t_arr = calloc(4, sizeof(int)); // calloc to initialize the array elements to 0.
int avg = 0; // definition of avg placed outside of the loop.
for (int i = 0; i < size_2; i++) {
for (int j = 0; j < size; j++) {
printf("%d\n", arr[j][i]); // j swapped with i.
avg += arr[j][i]; // same here too.
}
printf("-----\n");
t_arr[i] = avg / size;
avg = 0;
}
return t_arr;
}
int main (void)
{
int censusData[ROWS][COLS] = {
{87290, 77787, 55632},
{83020, 78373, 62314},
{95588, 87934, 705421},
{112456, 97657, 809767}
};
int* p = averageIncome(ROWS, COLS, censusData);
for ( int i = 0; i < COLS; i++ )
{
printf("Average Income of %d. column is: %d\n", i + 1, p[i]);
}
return 0;
}
Output:
87290
83020
95588
112456
-----
77787
78373
87934
97657
-----
55632
62314
705421
809767
-----
Average Income of 1. column is: 94588
Average Income of 2. column is: 85437
Average Income of 3. column is: 408283
Side notes:
Always check the returned pointer from a memory-management for a null pointer if the allocation failed.
I used calloc() instead of malloc() to initialize all elements of the dynamically allocated array to 0.
The definition of avg should be placed before the nested loops, not within. Reset avg to 0 at the end of the outer loop.
avg /= size_2; t_arr[i] = avg; should be avg /= size; t_arr[i] = avg;. Note the replacement of size_2 with size.
avg /= size; t_arr[i] = avg; can be simplified by t_arr[i] = avg / size;.
#include <stdio.h>
int censusData[4][3] = {{87290, 77787, 55632},
{83020, 78373, 62314},
{95588, 87934, 705421},
{112456, 97657, 809767}};
int main() {
int* t_arr = malloc(3 * sizeof(int));
for(int i=0;i<3;i++){
int avg = 0;
for(int j=0;j<4;j++){
//Keep the I fixed here now J is varying and its position of column
//So you are reading all column values for ith row.
avg+=censusData[j][i];
}
avg/=4;
t_arr[i] = avg;
}
for(int i=0;i<3;i++){
printf("%d,",t_arr[i]);
}
return 0;
}
You can loop over the columns first and then print the corresponding element of each row:
for (int col = 0; col < 3; ++col)
{
for (int row = 0; row < 4; ++row)
{
printf("%d\n", censusData[row][col]);
// Do other stuff here
}
printf("-----\n");
}
For each row, the colth element is printed. So for col = 0, censusData[0][0], censusData[1][0], censusData[2][0], censusData[3][0] will be printed.
In your code, you can just swap the positions of the two for loops to iterate over the columns first.
Im trying to make a function that sets the values of a 2D array, and then another function that prints those values.
For some reason, with my current implementation, if I create a 3x3 matrix and I set every value to the number 5, it prints the values, but it prints them counting up from 5, so it prints 567, 678, 789, when instead I want it to print the exact value that I set.
Here are the function definitions - what am I doing wrong?:
Struct Definition:
struct matrix{
char matrixName[50];
int rows;
int columns;
float* data;
};
typedef struct matrix matrix_t;
Matrix Creation:
int create_matrix(matrix_t* matrixP, int matrixLocation){
char tempName[50];
int rows, cols;
printf("Enter a name for your matrix>\n");
scanf("%s", tempName);
printf("Enter the number of rows>\n");
scanf("%d", &rows);
printf("Enter the number of cols>\n");
scanf("%d", &cols);
float * our_matrix = (float *) malloc(rows * cols * sizeof(float));
strcpy(matrixP[matrixLocation].matrixName, tempName);
matrixP[matrixLocation].rows = rows;
matrixP[matrixLocation].columns = cols;
matrixP[matrixLocation].data = our_matrix;
return 0;
}
Set Values:
int setValues(matrix_t* our_matrix, int matrix_index) {
int counter = 0;
int row = 0, col = 0;
for (col = 1; col <= our_matrix[matrix_index].columns; col++) {
for (row = 1; row <= our_matrix[matrix_index].rows; row++) {
counter++;
printf("Enter the value for column number %d of row number %d>\n", col, row);
scanf("%f", our_matrix[matrix_index].data+(col-1)+(row-1));
}
/* separate rows by newlines */
}
return 0;
}
Print:
int printMatrix(matrix_t* our_matrix, int index) {
int row = 0, col = 0;
for (col = 1; col <= our_matrix[index].columns; col++) {
for (row = 1; row <= our_matrix[index].rows; row++) {
printf(" %2.f ", *our_matrix[index].data+(col-1)+(row-1));
}
printf("\n");
}
return 0;
}
If I call the printMatrix() function without first using setValues, it seems to print the relative position of the cell that it's printing like so:
But when I call setValues() and set all values to the number 5, its prints counting up from the number 5 like this:
Your position calculation is incorrect. There's one problem and one 'aconventional notation' issue.
The problem is the calculation of the array subscripts:
scanf("%f", our_matrix[matrix_index].data+(col-1)+(row-1));
printf(" %2.f ", *our_matrix[index].data+(col-1)+(row-1));
You need to multiply the (row - 1) value by our_matrix[matrix_index].cols. You will probably do better with some shorter names for the values:
int max_col = our_matrix[matrix_index].columns;
int max_row = our_matrix[matrix_index].rows;
float *data = our_matrix[matrix_index].data;
for (col = 1; col <= max_col; col++)
{
for (row = 1; row <= max_row; row++)
{
printf("Enter the value for column number %d of row number %d>\n", col, row);
if (scanf("%f", data + (col-1) + (row-1) * max_col) != 1)
{
…report error and do not continue…
}
}
}
for (col = 1; col <= max_col; col++)
{
for (row = 1; row <= max_row; row++)
printf(" %.2f", data[(col-1) + (row-1) * max_col]);
putchar('\n');
}
That deals with the substantive bug. Note the error checking on scanf(). That is important in 'real world' programs (even if you get away without it in classes or in online contests). You could sensibly use the notation &data[(col-1) + (row-1) * max_cols] in place of data + (col-1) + (row-1) * max_cols in the call to scanf() too — that would improve the consistency of the code.
The 'aconventional notation' issue is that in C, array indexing starts at 0, not 1, and you could avoid the multitude of -1 terms by following the C conventions. This snippet also declares the loop variables when they're needed, minimizing their scope. This is a feature of C since C99 — it may not be available to you on some retrograde (but popular and widespread) platforms.
for (int col = 0; col < max_col; col++)
{
for (int row = 0; row < max_row; row++)
printf(" %.2f", data[col + row * max_col]);
putchar('\n');
}
I created a program that does matrix addition,subtraction, and multiplication. I have handled the addition and subtraction portions, but when I reached the multiplication I am having trouble outputting the correct values. I have only placed the code with the multiplication function below.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *elements;
int rows;
int columns;
} matrix;
void main() {
matrix a, b, c;
void read_matrix(matrix *);
void deallocate(matrix *);
void print(matrix);
matrix add(matrix, matrix);
matrix subtract(matrix, matrix);
matrix multiply(matrix, matrix);
read_matrix(&a);
read_matrix(&b);
/*
c = add(a, b);
printf("The answer of Matrix (a + b) is \n\n");
print(a);
printf("\n +\n\n");
print(b);
printf("\n =\n\n");
print(c);
printf("\n");
deallocate(&c);
c = subtract(a, b);
printf("The answer of Matrix (a - b) is \n\n");
print(a);
printf("\n -\n\n");
print(b);
printf("\n =\n\n");
print(c);
printf("\n");
deallocate(&c);
*/
c = multiply(a, b);
printf("The answer of Matrix (a * b) is \n\n");
print(a);
printf("\n *\n\n");
print(b);
printf("\n =\n\n");
print(c);
printf("\n");
}
void read_matrix(matrix *z) {
int d1, d2, allc, i, x, y, j, val;
int res;
printf("\nWhat is the first dimension of the array? ");
res = scanf("%d", &d1);
if (res != 1) {
fprintf(stderr, "Something went wrong with your first dimension!");
return;
}
printf("What is the second dimension of the array? ");
res = scanf("%d", &d2);
if (res != 1) {
fprintf(stderr, "Something went wrong with your second dimension!");
return;
}
printf("Matrix Dimension is %dx%d\n", d1, d2);
allc = d1*d2;
(*z).elements = (int *)calloc(allc, sizeof(int));
(*z).rows = d1;
(*z).columns = d2;
x = 0;
j = 0;
printf("\n");
for (i = 0; i < d1; i++) {
x++;
for (y = 0; y < d2; y++) {
printf("Enter the value for row %d column %d: ", x, y + 1);
res = scanf("%d", &val);
if (res != 1) {
fprintf(stderr, "Something went wrong while reading value %d\n", x);
return;
}
(*z).elements[j++] = val;
}
}
}
void deallocate(matrix *c) {
free((*c).elements);
(*c).elements = NULL;
(*c).rows = 0;
(*c).columns = 0;
}
void print(matrix z) {
int i, j, x;
x = 0;
for (i = 0; i < z.rows; i++) {
printf("[ ");
for (j = 0; j < z.columns; j++) {
printf("%-4d", z.elements[x++]);
}
printf("]\n");
}
}
matrix multiply(matrix a, matrix b) {
matrix c;
int a1, a2, b1, b2, allc, i, j, x, y, z, alc, addval, val;
i = 0;
j = 0;
alc = 0;
val = 0;
addval = 0;
a1 = a.rows;
a2 = a.columns;
b1 = b.rows;
b2 = b.columns;
allc = (a1 * b2);
c.elements = (int *)calloc(allc, sizeof(int));
c.columns = a1;
c.rows = b2;
if (a2 != b1) {
printf("\n\nThe inner dimensions of your matrices do not match! Multiplication cannot be done!\n\n");
exit(1);
}
for (x = 0; x < c.rows; x++) {
for (y = 0; y < c.rows; y++) {
for (z = 0; z < c.rows; z++) {
i = (i * c.rows);
addval = (a.elements[j]) * (b.elements[i]);
val += addval;
j++;
i++;
}
c.elements[alc] = val;
printf("VAL IS: %d\n\n", val);
val = 0;
i = 0;
alc++;
}
}
printf("\n\n");
return c;
}
In the multiplication function, the triple nested for loop is supposed to go through enough times to print out the correct number of items for the dimension of the new array. I am aware how to do matrix multiplication, but I am not sure if I have represented it correctly here.
The output for an example is:
What is the first dimension of the array? 3
What is the second dimension of the array? 3
Matrix Dimension is 3x3
Enter the value for row 1 column 1: 1
Enter the value for row 1 column 2: 2
Enter the value for row 1 column 3: 3
Enter the value for row 2 column 1: 4
Enter the value for row 2 column 2: 5
Enter the value for row 2 column 3: 6
Enter the value for row 3 column 1: 7
Enter the value for row 3 column 2: 8
Enter the value for row 3 column 3: 9
What is the first dimension of the array? 3
What is the second dimension of the array? 3
Matrix Dimension is 3x3
Enter the value for row 1 column 1: 1
Enter the value for row 1 column 2: 2
Enter the value for row 1 column 3: 3
Enter the value for row 2 column 1: 4
Enter the value for row 2 column 2: 5
Enter the value for row 2 column 3: 6
Enter the value for row 3 column 1: 7
Enter the value for row 3 column 2: 8
Enter the value for row 3 column 3: 9
The answer of Matrix (a * b) is
[ 1 2 3 ]
[ 4 5 6 ]
[ 7 8 9 ]
*
[ 1 2 3 ]
[ 4 5 6 ]
[ 7 8 9 ]
=
[ 216847534336951265054271]
[ 1641572693-138635672036124672]
[ 1352368309-50514195286739134]
Press any key to continue . . .
Below is my rework of your code that does matrix multiplication. The problem was with your multiply() function. You can see that it is doomed with it walking the a matrix by just incrementing the variable j - in a working solution every element of matrix a participates in the multiplication multiple times.
I also changed your routines to all pass pointers to matrix structures rather than your mix of passing pointers and passing/returning matrix structures directly by value. I think this is more consistent and easier to follow but it means you'll need to change your other matrix math routines to work the same, if you keep it this way.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *elements;
int rows;
int columns;
} matrix;
void read_matrix(matrix *z) {
int d1, d2, val;
printf("\nWhat is the first dimension of the array? ");
int result = scanf("%d", &d1);
if (result != 1) {
fprintf(stderr, "Something went wrong with your first dimension!\n");
exit(1);
}
printf("What is the second dimension of the array? ");
result = scanf("%d", &d2);
if (result != 1) {
fprintf(stderr, "Something went wrong with your second dimension!\n");
exit(1);
}
printf("Matrix Dimension is %dx%d\n", d1, d2);
z->elements = calloc(d1 * d2, sizeof(int));
z->rows = d1;
z->columns = d2;
int x = 0;
int j = 0;
printf("\n");
for (int i = 0; i < d1; i++) {
x++;
for (int y = 0; y < d2; y++) {
printf("Enter the value for row %d column %d: ", x, y + 1);
result = scanf("%d", &val);
if (result != 1) {
fprintf(stderr, "Something went wrong while reading value (%d, %d)\n", x, y + 1);
exit(1);
}
z->elements[j++] = val;
}
}
}
void deallocate(matrix *c) {
free(c->elements);
c->elements = NULL;
c->rows = 0;
c->columns = 0;
}
void print(matrix *z) {
int x = 0;
for (int i = 0; i < z->rows; i++) {
printf("[ ");
for (int j = 0; j < z->columns; j++) {
printf("%-4d", z->elements[x++]);
}
printf("]\n");
}
}
void multiply(matrix *a, matrix *b, matrix *c) {
if (a->columns != b->rows) {
fprintf(stderr, "The inner dimensions of your matrices do not match! Multiplication cannot be done!\n");
exit(1);
}
c->elements = calloc(a->rows * b->columns, sizeof(int));
c->columns = b->columns;
c->rows = a->rows;
int alc = 0;
for (int x = 0; x < a->rows; x++) {
for (int y = 0; y < b->columns; y++) {
int value = 0;
for (int z = 0; z < b->rows; z++) {
value += a->elements[z + x * a->columns] * b->elements[y + z * b->columns];
}
c->elements[alc++] = value;
}
}
}
int main() {
matrix a, b, c;
read_matrix(&a);
read_matrix(&b);
multiply(&a, &b, &c);
printf("The answer of Matrix (a * b) is \n\n");
print(&a);
printf("\n *\n\n");
print(&b);
printf("\n =\n\n");
print(&c);
printf("\n");
deallocate(&a);
deallocate(&b);
deallocate(&c);
return 0;
}
#include <stdio.h>
int main()
{
//Variables
int x=0,y=0;
int my_data[7][7];
for (x=0;x<9;x++) {
printf("Enter rows:");
scanf("%i",&x);
// printf("%i\n",my_data[x][y]);
}
for(y=0;y<9;y++) {
printf("Enter columns: ");
scanf("%i", &y);
//printf("%i\n",my_data[x][y]);
}
for(y=0;y<9;y++) {
printf("%i\n",my_data[0][y]);
}
return 0; //Return process complete.
}
declares an 8X8 integer array my_data
prompts the user for data to fill the 8X8 array my_data
prints the sum of all the elements for each row
prints the sum of all the elements in the 8X8 array
prints the sum of all the elements in the diagonal of the 8x8 array.
What did I do wrong? When I enter 1,2,3,4,5,6,7,8; 1,2,3,4,5,6,7,8
I get back:
3612392
2686632
1966142592
3604480
0
1966142601
1825884643
4
3612396
Several issues right away:
The array is 7x7, you want it to be int my_data[8][8];
Instead of using the number for the size of the array over and over, define a constant #define FOO 8
The loops are not properly bounded, should be for (x=0;x<8;x++). (again, see my note on using a defined constant for the size)
You are not saving the value to the array, you are saving it to the iterator variable.
With two separate for loops you will not be able to fill the entire table, reconsider this structure because you will likely have to use nested loops.
You can use one loop nested in the other:
#define SIZE 8
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("(%d,%d): ", i+1, j+1);
scanf("%d", &value);
array[i][j] = value;
}
}
for (x=0;x<9;x++) {
printf("Enter rows:");
scanf("%i",&x);
// printf("%i\n",my_data[x][y]);
}
What happens here is that you change the value of x. You do not save anything on the array.
Similarly for y
Also you should iterate from 0 to 6 (inclusive)
Try
int i;
for(i = 0 ; i < 7 ; i++)
{
scanf("%d" , &my_data[0][x]);
}
Or :
int row , col;
for (row=0; row<7; row++)
{
for (col=0; col<7; col++)
{
scanf("%d" , &ticTacToeBoard[row][col]);
}
}
What you see is the values that your array has when it was declared. The reason for that is when you declare a variable in C without initializing it, as in your case, your program just uses enough memory to store your data into the memory but does not set that memory to 0, hence seeing what the contents of the memory are from previous usage.
int my_data[7][7]; is a 7 x 7 array. Use int my_data[8][8]; for 8 x 8.
my_data is an array with space for 49 int, not 64!
In your for loops you are changing the loop control variable itself by using scanf with its address!
I think you want:
#include <stdio.h>
#define ROWS 8
#define COLUMNS 8
int main(void) {
int my_data[ROWS][COLUMNS];
int rows[ROWS];
int cols[COLUMNS];
size_t i, row, col;
/* input rows */
printf("Enter rows: ");
fflush(stdout);
for (i = 0; i < ROWS; i++) scanf("%d", &rows[i]);
/* input columns */
printf("Enter cols: ");
fflush(stdout);
for (i = 0; i < COLUMNS; i++) scanf("%d", &cols[i]);
/* calculate sums */
for (row = 0; row < ROWS; row++) {
for (col = 0; col < COLUMNS; col++) {
my_data[row][col] = rows[row] + cols[col];
}
}
/* print results */
for (row = 0; row < ROWS; row++) {
for (col = 0; col < COLUMNS; col++) {
printf("%d ", my_data[row][col]);
}
puts("");
}
return 0;
}