1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define length 100

void print_array();

int main()
{
    int m,n,i,j;
    int A[length][length];
    printf("Give dimensions of array (up to 100x100):\ni:\n");
    scanf("%d",&i);
    printf("j:\n");
    scanf("%d",&j);
    srand(time(NULL));
    for (m=0;m<i;m++)
    {
        for (n=0;n<j;n++)
        {
            A[m][n]=rand()%45+1;
            printf("A[%d,%d]=%d\n",m,n,A[m][n]);
        }
    }
    print_array(i,j,A);
    return 0;
}

void print_array(int i,int j,int A[][j])
{
    printf("\n");
    int m,n;
    for (m=0;m<i;m++)
        {
            for (n=0;n<j;n++)
            {
                printf("A[%d,%d]=%d\n",m,n,A[m][n]);
            }
        }
}

Hello. I am trying to print a 2d array by calling a function print but when I run the program I get:

For the first printf() the correct values:

A[0,0]=25
A[0,1]=19
A[0,2]=13
A[1,0]=4
A[1,1]=17
A[1,2]=43
A[2,0]=7
A[2,1]=37
A[2,2]=20

But when with the 2nd printf() within the function call of print_array I get:

A[0,0]=25
A[0,1]=19
A[0,2]=13
A[1,0]=0
A[1,1]=0
A[1,2]=0
A[2,0]=0
A[2,1]=0
A[2,2]=0

Seems like I miss something with pointers... Thanks.

0

7 Answers 7

3

This is C99, right?

The problem is that you're confusing the array size.

The main program has int A[length][length], but then you call the function with a dynamic size for the final dimension, A[][j]. If j != length, then the function will index the array incorrectly.

I would recommend representing the array in the function call as a bare pointer to the first element, and doing the indexing manually:

void print_array(const int *A, size_t width, size_t height)
{
  for(size_t i = 0; i < height; ++i)
  {
    for(size_t j = 0; j < width; ++j)
      printf("A[%zu][%zu] = %d\n", i, j, A[i * width + j]);
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Wooo so many answers in 1 minute! Thanks! that should work! :)
Would it be correct to apply the same print_array for a malloced 2d array? would it be something like : void print_array(int i,int j,int A[][j*sizeof(int)])
my wrong its print_array(int i,int j,int **A) since we have array of pointers pointing to 1d arrays. If i missed something plz correct me :)
0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <time.h>
#define length 100

void print_array(int i,int j,int A[length][length]);

int main()
{
    int m,n,i,j;
    int A[length][length];
    printf("Give dimensions of array (up to 100x100):\ni:\n");
    scanf("%d",&i);
    printf("j:\n");
    scanf("%d",&j);
    srand(time(NULL));
    for (m=0;m<i;m++)
    {   
        for (n=0;n<j;n++)
        {   
            A[m][n]=rand()%45+1;
            printf("A[%d,%d]=%d\n",m,n,A[m][n]);
        }   
    }   
    print_array(i,j,A);
    return 0;
}

void print_array(int i,int j,int A[length][length])
{
    printf("\n");
    int m,n;
    for (m=0;m<i;m++)
        {   
            for (n=0;n<j;n++)
            {   
                printf("A[%d,%d]=%d\n",m,n,A[m][n]);
            }   
        }   
}

Apart from the above, you need to do some input validation for the dimension (i and j). They should not exceed the length. If it exceeds the length then you will run into problems.

2 Comments

Yup i just removed some lines when i put the code here so that it wont get too long. Thanks again!
@GeoPapas Cool. If you find my answer useful, please feel free to upvote! Thanks!! :)
0
void print_array(int i,int j,int A[][j])

should be

void print_array(int i,int j,int A[][length])

When you tell the compiler that the actual array has different dimensions than what you earlier specified, it accesses the wrong elements. A is always 100x100, you just don't fill all of it.

Comments

0

You need to define the size of the array int A[][] in the function signature. Use this function instead :

void print_array(int i,int j,int A[length][length])
{
    printf("\n");
    int m,n;
    for (m=0;m<i;m++)
        {
            for (n=0;n<j;n++)
            {
                printf("A[%d,%d]=%d\n",m,n,A[m][n]);
            }
        }
}

Comments

0

First you should forward declare your function with the correct prototype

void print_array(int n,int m, int A[n][m]);

And then use it with the correct dimensions

print_array(length, length, A);

Other than that:

  • use size_t for sizes instead of int
  • usual naming conventions have i and j as indices and n and m as sizes. Sticking to such conventions makes code easier to read for others.

Comments

0

You've declared A as int A[length][length], But in the print_array function you've declare it as A[][j] which means in your function A have a different number of colones which leads to this error.

change this line

void print_array(int i,int j,int A[][j])

with this one

void print_array(int i,int j,int A[][length])

http://www.cplusplus.com/doc/tutorial/arrays/

Comments

0

array and matrix printing functions are little similar but tricky.. but i have worked on it and found out some thing and i think it will be useful for all here is a simple C code for Function for printing matrix

#include<stdio.h>
void printmatrix(int l,int x,int y,int *p_arr);
void putvalue(int l,int x,int y,int *r_arr);
/*  ******** Function to insert some random value in to a Matrix ************ */
void putvalue(int l,int x,int y,int *r_arr)
{
  int i,j;
  for(i=0;i<x;i++)
  {
    for(j=0;j<y;j++)
    {
        r_arr[(l*i)+j]=i+j+10;
    }
  }
}
/* *********Function to Print any Matrix ********** */
void printmatrix(int l,int x, int y,int *p_arr)
{
  int i,j;
  printf("\n");
  for(i=0;i<x;i++)
  {
    printf("\n");
    for(j=0;j<y;j++)
    {
        printf(" %d",p_arr[(l*i)+j]);
    }
  }
}
/* ****** Main ****** */
void main()
{
  int i,j,l;
  int Big_arr[80][100],Small_arr[20][40];   

     //  ****** I have taken two different size arrays  *****

  int x,y;
  clrscr();
  printf("\n Enter x and y values under 20X40:"); // Because of screen size of Output
  scanf("%d %d",&x,&y);
  if((x!=0)&&(y!=0))
  {
    printf("\n %dX%d",x,y);

    l=sizeof(Big_arr[0])/sizeof(Big_arr[0][0]);  // **** l is the length of a single row of matrix
    putvalue(l,x,y,(int*)Big_arr);
    printf("\n Printing Big_arr");
    printmatrix(l,x,y,(int*)Big_arr);


    l=sizeof(Small_arr[0])/sizeof(Small_arr[0][0]);
    putvalue(l,x,y,(int*)Small_arr);
    printf("\n Printing Small_arr");
    printmatrix(l,x,y,(int*)Small_arr);
  }
  else
    printf("\n ***** Enter valied x and y values *****");
  getch();

}

hope u like it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.