Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Passing, filling and printing 2D arrays of varaible size with functions in C language. In my first example below I declare an array[3][3] and send

Passing, filling and printing 2D arrays of varaible size with functions in C language.

In my first example below I declare an array[3][3] and send to fillArray() and printArray() and it works fine. In the second example I ask the user to give an integer wich becomes "size" and use declare array[size][size] and send it to functions but I get error (use of undeclared indetifier "size"). Can some one show me how passing 2D array with a variable size?

First example works:

#include

/* Fills array */ void fillArray(int a[][3]) { printf("Enter 9 integers "); int i, j; for (i=0; i<3; i++) { for (int j=0; j<3; j++) { scanf("%i",&a[i][j]); } } }

/* prints array */ void printArray(int a[][3]) { printf("You array is "); int i, j; for (i=0; i<3; i++) { for (int j=0; j<3; j++) { printf("%i ",a[i][j]); } printf(" "); } }

int main(int argc, char **argv) { int myArray[3][3]; fillArray(myArray); printArray(myArray); return 0; }

Second example doesn't work

#include

/* get size input */ int getSize() { int input; printf("Enter integer N for your N X N array: "); scanf("%i",&input); printf("Size = %i ", input); return input; } /* Fills array */ void fillArray(int a[][size], int size) { printf("Enter your array data as integers "); int i, j; for (i=0; i { for (int j=0; j { scanf("%i",&a[i][j]); } } }

/* prints array */ void printArray(int a[][size], int size) { printf("You array is "); int i, j; for (i=0; i { for (int j=0; j { printf("%i ",a[i][j]); } printf(" "); } }

int main(int argc, char **argv) { /* get size */ int size; size = getSize(); int myArray[size][size]; fillArray(myArray, size); printArray(myArray, size); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2005 For Windows Mobile Web Office And Database Applications Comprehensive

Authors: Gary B. Shelly, Thomas J. Cashman, Corinne Hoisington

1st Edition

0619254823, 978-0619254827

More Books

Students also viewed these Databases questions

Question

int fib ( int n ) { if ( n Answered: 1 week ago

Answered: 1 week ago

Question

107 MA ammeter 56 resistor ? V voltmeter

Answered: 1 week ago

Question

Generally If Drug A is an inducer of Drug B , Drug B levels will

Answered: 1 week ago