Display specific row of an array using pointer and addressing techniques - c

Hi I am working on a C program that declares a 2 dimensional integer array with N rows and M columns and loads it with consecutive numbers using conventional array element access methods. N and M are equal to 10. I need display a specific row of the array using this function
void showRow(int *arrayName, int rowNumber, int nColsInRow)
Then I need to use that function to display the entire array.
I have to do the same thing with the showCol function in my code below.
I have both functions in my code, but I am getting confused on how to implement them. I appreciate any help that y'all can give.
int main(void){
int array[10][10];
int i,j;
int *row0, *row1, *row2, *row3, *row4, *row5, *temp;
int add = 0;
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
array[i][j]= add;
add++;
}
}
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
printf("%d ", array[i][j]);
}
printf(" \n");
}
void showRow(int *array, int rowNumber, int nColsInRow)
{
row0 = &(array[0][0]);
row1 = &(array[1][0]);
row2 = &(array[2][0]);
row3 = &(array[3][0]);
row4 = &(array[4][0]);
row5 = &(array[5][0]);
row6 = &(array[6][0]);
row7 = &(array[7][0]);
row8 = &(array[8][0]);
row9 = &(array[9][0]);
row10 = &(array[10][0]);
for(j = 0; j < 10; j++)
{
printf(" %d", *(rowNumber + j);
}
//}
void showCol(int *array, int colNumber, int nRowsInCol)
{
col0 = &(array[0][0]);
col1 = &(array[0][1]);
col2 = &(array[0][2]);
col3 = &(array[0][3]);
col4 = &(array[0][4]);
col5 = &(array[0][5]);
col6 = &(array[0][6]);
col7 = &(array[0][7]);
col8 = &(array[0][8]);
col9 = &(array[0][9]);
col10 = &(array[0][10]);
for(j = 0; j < 10; j++)
{
printf(" %d", *(col + j);
}
}
}

I made a change to the showRow function to accept a void pointer. Then inside of the showRow function I cast it back to 2 dim array. After that, you can go back to treating just like you would any other 2 dim array.
void showRow(void *array, int rowNumber, int nColsInRow)
{
int (*a)[10] = (int (*)[10]) array;
int i;
for (i=0; i < nColsInRow; i++) {
printf("%d\n", a[rowNumber][i]);
}
}
int main()
{
int array[10][10];
int i,j;
int add = 0;
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
array[i][j]= add;
add++;
}
}
showRow (&array, 1, 10);
return 0;
}

Without changing the signature of the functions, try this code:
#include <stdio.h>
#include <stdlib.h>
void showRow(int *array, int rowNumber, int nColsInRow);
void showCol(int *array, int colNumber, int nRowsInCol);
int
main(void)
{
int array[10][10];
int i,j;
int *p;
int add = 0;
p = (int *)array; //Set a pointer point to this array.
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
array[i][j]= add;
add++;
}
}
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
printf("%d ", array[i][j]);
}
printf(" \n");
}
showRow( p, 2, 3);
showCol( p, 2, 3);
}
void showRow(int *array, int rowNumber, int nColsInRow)
{
int *row = &array[rowNumber*10];//row points at the particular row.
int j;
for(j = 0; j < 10; j++)
{
printf(" %d", row[j]);
}
printf("\n%d\n", row[nColsInRow]);
}
void showCol(int *array, int colNumber, int nRowsInCol)
{
int *col = &array[colNumber]; //col points at the particular col of first row.
int j, n;
for(j = 0; j < 100; j = j + 10) //Increment 10 since row size is 10.
{
printf(" %d", col[j]);
}
n = 10 * nRowsInCol;
printf( "\n%d\n", col[n]);
}
Explanation: An integer array is stored in a continuous memory.
In this case, although it is a 2 dimensional array,
it stored in 100 continuous unit of 4 bytes. (assuming
size of int is 4 in your computer.)
With int pointer, it can point to any index.
e.g. int *p = &array[58];
p points at 58th unit of this array.
Using this concept, now you can do math to get
particular row or column.

Related

Problem adding two 4x4 matrices and printing the sum result in a new matrix in C

Could someone help me please?
I need to perform the sum of two matrices that the data will be sent by the user and print the result in a new matrix.
I managed to capture the data from the two arrays, but when I try to add the two, the code does not print the sum, where is the error?
Thanks
#include <stdio.h>
#include <stdlib.h>
void sum(int *mat_A, int *mat_B, int *mat_C);
int main() {
int mat_A[4][4];
int mat_B[4][4];
int mat_C[4][4];
int i, j, value;
printf("\nEnter integer values ​​for the elements of matrix A: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_A[i][j] = value;
}
}
printf("\nEnter integer values ​​for the elements of matrix B: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_B[i][j] = value;
}
}
calc_soma(*mat_A, *mat_B, *mat_C);
printf("\nSum of matrices A with B: \n\n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_C[i][j];
printf("%d", value);
}
printf("\n");
}
return 0;
}
void sum(int *mat_A, int *mat_B, int *mat_C) {
int i, j;
int value;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = *mat_A + *mat_B;
*mat_C = value;
}
}
}
You need to declare the parameters to sum() as 2-dimensional arrays, not int *, which is a pointer to a 1-dimensional array. Then use i and j as array indexes.
You're also calling the function with the wrong name calc_soma.
#include <stdio.h>
#include <stdlib.h>
void sum(int mat_A[4][4], int mat_B[4][4], int mat_C[4][4]);
int main() {
int mat_A[4][4];
int mat_B[4][4];
int mat_C[4][4];
int i, j, value;
printf("\nEnter integer values for the elements of matrix A: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_A[i][j] = value;
}
}
printf("\nEnter integer values for the elements of matrix B: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_B[i][j] = value;
}
}
sum(mat_A, mat_B, mat_C);
printf("\nSum of matrices A with B: \n\n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_C[i][j];
printf("%d", value);
}
printf("\n");
}
return 0;
}
void sum(int mat_A[4][4], int mat_B[4][4], int mat_C[4][4]) {
int i, j;
int value;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_A[i][j] + mat_B[i][j];
mat_C[i][j] = value;
}
}
}

How to use a pointer (to a Matrix) as an argument in a Function in C?

I'm trying to write a code in C that sum two 4x4 matrix.
But I want my function to have a pointer as my arguments. The only error I'm getting is the time I'm trying to sum up in the function. Could someone help me?
#include <stdio.h>
#include <locale.h>
int i = 0, j = 0;
void calc_soma(int* mat_A, int* mat_B, int* mat_C)
{
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
mat_C[i][j] = mat_A[i][j] + mat_B[i][j];
printf("%d", mat_C[i][j]);
}
}
}
int main()
{
setlocale(LC_ALL, "Portuguese");
int i=0, j=0;
int mA[4][4], mB[4][4], mC[4][4];
int *mat_A, *mat_B, *mat_C;
for(i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("Type in the value for Matrix A [%d][%d]: ", i, j);
scanf_s("%d", &mA[i][j]);
}
}
i, j = 0;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("Type in the value for Matrix B [%d][%d]: ", i, j);
scanf_s("%d", &mB[i][j]);
}
}
*mat_A = &mA;
*mat_B = &mB;
return 0;
}
The types of pointers for the arguments are wrong. You want to pass (the pointer to the first elements of) arrays like int mA[4][4];, so they should be pointers to int[4].
void calc_soma(int (*mat_A)[4], int (*mat_B)[4], int (*mat_C)[4])
{
/* same as original */
}
They can simply be written like this:
void calc_soma(int mat_A[][4], int mat_B[][4], int mat_C[][4])
{
/* same as original */
}
Then the function can be called like:
calc_soma(mA, mB, mC);
The purpose of mat_A and mat_B are unclear, but if you want to get pointers to the matrice like &mA, it should be int(*)[4][4]. Note that dereferencing (like *mat_A) uninitialized pointers will invoke undefined behavior.
int main()
{
setlocale(LC_ALL, "Portuguese");
int i=0, j=0;
int mA[4][4], mB[4][4], mC[4][4];
int (*mat_A)[4][4], (*mat_B)[4][4], (*mat_C)[4][4];
/* omit */
mat_A = &mA;
mat_B = &mB;
return 0;
}
To use functions like
void calc_soma(int* mat_A, int* mat_B, int* mat_C)
you should express the matrice by 1D array to match with the format. It will be like this:
#include <stdio.h>
#include <locale.h>
#define ROWS 4
#define COLS 4
int i = 0, j = 0;
void calc_soma(int* mat_A, int* mat_B, int* mat_C)
{
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
mat_C[i * COLS + j] = mat_A[i * COLS + j] + mat_B[i * COLS + j];
printf("%d", mat_C[i * COLS + j]);
}
}
}
int main()
{
setlocale(LC_ALL, "Portuguese");
int i=0, j=0;
int mA[ROWS * COLS], mB[ROWS * COLS], mC[ROWS * COLS];
for(i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
printf("Type in the value for Matrix A [%d][%d]: ", i, j);
scanf_s("%d", &mA[i * COLS + j]);
}
}
i, j = 0;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
printf("Type in the value for Matrix B [%d][%d]: ", i, j);
scanf_s("%d", &mB[i * COLS + j]);
}
}
calc_soma(mA, mB, mC);
return 0;
}

C language - usage of struct to form Matrix

I know that object oriented is not welcoming in c language but I still trying to work my way because it is possible as later languages based on c works good with objects
so my question is when I try to write a function outside of main it doesnt give me access and doesnt change values of wanted structs , see code below (I marked every things and included working functions inside the main) :
#include <stdio.h>
#include <stdlib.h>
static int RandomNum() {
int generate = rand() / 100000000;
while (generate >= 9) {
generate = rand() / 100000000;
}
return generate;
}
// Object Definition
typedef struct {
int a, b;
int Mat[2][4];
} Matrix2x4;
typedef struct {
int a, b;
int Mat[4][5];
} Matrix4x5;
typedef struct {
int a, b;
int Mat[4][5];
} Matrix2x5;
void PrintMat2x4(Matrix2x4 a) {
int row = a.a;
int col = a.b;
for (int i = 0; i < row; i++) {
printf("{%s", " ");
for (int j = 0; j < col; j++) {
printf("%d ,", a.Mat[i][j]);
}
printf("}%s\n", " ");
}
}
void PrintMat4x5(Matrix4x5 a) {
int row = a.a;
int col = a.b;
for (int i = 0; i < row; i++) {
printf("{%s", " ");
for (int j = 0; j < col; j++) {
printf("%d ,", a.Mat[i][j]);
}
printf("}%s\n", " ");
}
}
void PrintMat2x5(Matrix2x5 a) {
int row = a.a;
int col = a.b;
for (int i = 0; i < row; i++) {
printf("{%s", " ");
for (int j = 0; j < col; j++) {
printf("%d ,", a.Mat[i][j]);
}
printf("}%s\n", " ");
}
}
// NOT WORKING AS A SEPERATE FUNCTION SO I PUT IT INSIDE THE MAIN
/*static void MatrixMultiplication (Matrix2x4 a , Matrix4x5 b , Matrix2x5 c) {
for (int i = 0; i < a.b; i++){
for (int j = 0; j < b.b; j++){
for (int k = 0; k < a.b; k++){
c.Mat[i][j] = c.Mat[i][j]+(a.Mat[i][k]*b.Mat[k][j]);}}}
}
void setRandom (Matrix a) {
int row = ((int)(sizeof (a.Mat) / sizeof (a.Mat)[0])) ;
int col = ((int)(sizeof (a.Mat)[0] / sizeof (a.Mat)[0][0])) ;
for (int i = 0; i < row ; i++){
for (int j = 0; j < col; j++){
a.Mat[i][j] = RandomNum();}}}*/
void main() {
printf("%s\n\n", "Start..");
Matrix2x4 test = {2,4,{0}}; // <----- SIZE IS 2X4
int row = ((int)(sizeof(test.Mat) / sizeof(test.Mat)[0]));
int col = ((int)(sizeof(test.Mat)[0] / sizeof(test.Mat)[0][0]));
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
test.Mat[i][j] = RandomNum();
}
}
Matrix4x5 test2 = {4,5,{0}}; // <----- SIZE IS 4X5
int row2 = ((int)(sizeof(test2.Mat) / sizeof(test2.Mat)[0]));
int col2 = ((int)(sizeof(test2.Mat)[0] / sizeof(test2.Mat)[0][0]));
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
test2.Mat[i][j] = RandomNum();
}
}
Matrix2x5 mult = {2,5,{0}}; // <----- SIZE IS 2X5
PrintMat2x4(test);
printf("X\n");
PrintMat4x5(test2);
printf("=\n");
for (int i = 0; i < test.b; i++) {
for (int j = 0; j < test2.b; j++) {
for (int k = 0; k < test.b; k++) {
mult.Mat[i][j] = mult.Mat[i][j] + (test.Mat[i][k] * test2.Mat[k][j]);
}
}
}
PrintMat2x5(mult);
printf("\n\n%s", "End ---> ");
}
In order for this function to work:
static void MatrixMultiplication (Matrix2x4 a , Matrix4x5 b , Matrix2x5 c) {
for (int i = 0; i < a.b; i++){
for (int j = 0; j < b.b; j++){
for (int k = 0; k < a.b; k++){
c.Mat[i][j] = c.Mat[i][j]+(a.Mat[i][k]*b.Mat[k][j]);}}}
}
You must pass your structs as pointers:
static void MatrixMultiplication (Matrix2x4* a , Matrix4x5* b , Matrix2x5* c) {
for (int i = 0; i < a->b; i++){
for (int j = 0; j < b->b; j++){
for (int k = 0; k < a->b; k++){
c->Mat[i][j] = c->Mat[i][j]+(a->Mat[i][k]*b->Mat[k][j]);}}}
}
Your original function doesn't work outside of main, because C is making a copy of those structs for the function's own use. Therefore the original structs never get modified. Changing those parameters to pointers means that the function is working with the original structs.

How to sort an arrays of pointers, without changing the original array in C

Given two arrays: int nums[N] and int *ptrs[N] (N is a constant number).
I have to initialize the first array with some numbers. After that, i have to initialize the second array, so every element of the second array points to the element with the same index of the first array. (ptrs[0] points to nums[0],...).
Now i have to write a function with "ptrs" as argument that modifies the pointers in such a way that the first element of the second array points to the smallest number in the first array,..)
It's not allowed to change the "nums-array", i can only change the "ptrs-array".
This is my code i already have, but when i run it, the "nums-array" changes too.
What do i do wrong?
#include <stdio.h>
#define N 6
void sort(int *ptrs);
int main()
{
int nums[N] = { 1,6,7,8,2,5 };
int(*ptrs)[N];
int i;
ptrs = nums;
sort(ptrs);
for (i = 0; i < N; i++)
printf("nummer is: %d en %d\n", (*ptrs)[i], nums[i]);
return 0;
}
void sort(int *ptrs)
{
int i, j, tmp;
for (i = 0; i < N; i++)
for (j = i + 1; j < N; j++)
if ((ptrs)[i] > (ptrs)[j])
{
tmp = (ptrs)[i];
(ptrs)[i] = (ptrs)[j];
(ptrs)[j] = tmp;
}
}
Fix for the first part:
int main()
{
int nums[N] = { 1,6,7,8,2,5 };
int *ptrs[N]; // fix
int i;
for(i = 0; i < N; i++) // fix
ptr[i] = nums+i; // fix (or ptr[i] = &nums[i])
I found the solution, thanks for helping guys!
#include <stdio.h>
#define N 6
void sort(int ptrs[], int nums[]);
int main()
{
int nums[N] = { 1,6,7,8,2,5 };
int i,j,*p, *ptrs[N];
for (i = 0; i < N; i++) {
ptrs[i] = &nums[i];
}
sort(ptrs, nums);
return 0;
}
void sort(int *ptrs[], int nums[])
{
int i, j, tmp, p[N];
for (i = 0; i < N; i++)
p[i] = *ptrs[i];
for(j = 0; j < N; j++)
for (i = 0; i <= N; i++)
if (p[i] > p[i+1])
{
tmp = (ptrs)[i];
(ptrs)[i] = (ptrs)[i+1];
(ptrs)[i+1] = tmp;
for (i = 0; i < N; i++)
p[i] = *ptrs[i];
}
for (i = 0; i < N; i++)
printf("nummer is: %d en %d\n", *ptrs[i], nums[i]);
return;
}

Pointers exit code 6

Well i m struggling with the pointers, why doesn t work the function biggest in the end (yes of that s dummy function)? (exit code 6)
Ty for help
code:
int search(int const a[], int n, int key) {
for (int *i = a; i < a + n; i++) {
if ( key == *i ) return 1;
}
return 0;
}
void print_row(int const a[], int n, int row) {
for (int *i = a + n * row; i < a + n * (row + 1); i++) {
printf("%d ", *i);
}
printf("\n");
}
void biggest(double x, long *int_part, double *frac_part) {
*int_part = (long) x;
*frac_part = x - *int_part;
}
main () {
int tempretures[7][24];
for (int *i = &tempretures[0][0]; i < &tempretures[7][24]; i++) {
static int j = 1;
*i = j;
j+=2;
}
tempretures[6][5] = 32;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 24; j++) {
printf("%d ", tempretures[i][j]);
}
printf("\n");
}
printf("Is it: %d\n", search(tempretures, 7*24, 32));
for (int i = 0; i < 7; i++) {
print_row(tempretures, 24, i);
}
long a = 0; double b = 0;
biggest(5.67, &a, &b);
printf("%li", a);
}
for (int *i = &tempretures[0][0]; i < &tempretures[7][24]; i++) {
static int j = 1;
*i = j;
j+=2;
}
This runs way past the end of the array, stopping only when it hits every element of row number 7, but there is no row number 7.

Resources