I have a project that requires the following things.
Write a program in C and in MIPS assembly language program that:
Initializes an integer array with 3 rows and 5 columns:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Inputs a row and column number from the user
main calls a child function that calculates the memory address of the chosen row & column like this:
int arrayAddress( int row, int col, int ncolumns, array); //returns address of array[row][col]
use shift instead of multiply where appropriate
print the address and the value of the chosen array element.
The problem is I don't know how to do the following -
Get int ncolumns since typing int ncolumns = my_array[][5] produces errors
Remove the following errors shown in the second image
warning: assignment to ‘int ’ from incompatible pointer type ‘int ()[5]’ [-Wincompatible-pointer-types]
on
arrayAddress_A = my_array;
Warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat=]
on
printf("Memory Address : %x\n", arrayAddress_A);
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("Value : %d", arrayAddress_A);
on
printf("Value : %d", arrayAddress_A);
And there might be other errors I am not aware of.
The code I have:
#include <stdio.h>
int main()
{
// array declaration and initialization
int my_array[3][5] = {
{1 ,2 ,3, 4, 5}, //row 1
{6 ,7 ,8, 9, 10}, //row 2
{11, 12, 13, 14, 15}, //row 3
};
{
int i = 0;
int j = 0;
int ncolumns = 5;
my_array[i][j];
printf("Enter row : \n");
scanf("%d",&i);
printf("Enter column : \n");
scanf("%d",&j);
int arrayAddress(int my_array, int i, int j, int ncolumns);
{
int* arrayAddress_A;
arrayAddress_A = my_array;
arrayAddress_A += i * ncolumns + j;
printf("Memory Address : %x\n",arrayAddress_A);
printf("Value : %d", arrayAddress_A);
}
}
}