Note that there are some explanatory texts on larger screens.

plurals
  1. POFunctions in array not displaying the correct values?
    primarykey
    data
    text
    <p>The program is suppose to display 9 numbers input by the user in a 3 x 3 board fashion. However all I got are some rather odd numbers. </p> <p>There is something wrong with my <code>disp_arr</code> function but I just don't know what is the error there. I'm fairly new to functions and pointers but I guess this is how I learn!</p> <p>This is the output that I get when running: <a href="http://s7.postimg.org/adrdscup7/Error.jpg" rel="nofollow">2686636 clone errors</a></p> <pre><code>/* File: StudentID_Surname.c - e.g. 1234567_Wilson.c * This program finds the range between highest and lowest value of a 2-D array */ #include &lt;stdio.h&gt; #define NROW 3 #define NCOL 3 /* Write a function void disp_arr(int a[NROW][NCOL]) { ... } where a[][] is the 2-D array Print the entire array to the screen. */ disp_arr( int temp ); int main(void) { /* declare needed variables or constants, e.g. */ int ar[NROW][NCOL]; int rows, cols; int temp[9] = {1,2,3,4,5,6,7,8,9}; /* Storing 9 numbers */ /* prompt for the user to enter nine positive integers to be stored into the array */ int index = 0; printf( "Please enter 9 positive integers :\n"); for ( rows = 0 ; rows &lt; 3 ; rows++ ) { for ( cols = 0 ; cols &lt; 3 ; cols++ ) { scanf( "%d", &amp;ar[rows][cols] ); /* Store values in the temp[z] = {1 2 3 4 5 6 7 8 9}*/ temp[index] = ar[rows][cols]; index += 1; /* Increase the array in temp[z] */ } } /* Call disp_arr to display the 3 x 3 board */ disp_arr( temp ); }/* end main */ disp_arr( int storedValue ) { int x,y; for ( x = 0 ; x &lt; 3 ; x++ ) { for ( y = 0 ; y &lt; 3 ; y++ ) { printf( "%d\t", storedValue ); } printf("\n"); } } </code></pre> <p>I thought about including counters like <code>storedValue[counter]</code> but it returns more error =/</p> <pre><code>disp_arr( int storedValue ) { int x,y; int counter = 0 for ( x = 0 ; x &lt; 3 ; x++ ) { for ( y = 0 ; y &lt; 3 ; y++ ) { printf( "%d\t", storedValue[counter] ); counter += 1; } printf("\n"); } } </code></pre> <p>Any help would be appreciated. </p> <p>Thanks in advance!</p> <p>Sam</p> <pre><code>/* Editted code after haccks and Rohan's advice */ #include &lt;stdio.h&gt; #define NROW 3 #define NCOL 3 /* Write a function void disp_arr(int a[NROW][NCOL]) { ... } where a[][] is the 2-D array Print the entire array to the screen. */ disp_arr( int temp ); int main(void) { /* declare needed variables or constants, e.g. */ int ar[NROW][NCOL]; int rows, cols; int temp[9] = {1,2,3,4,5,6,7,8,9}; /* Storing 9 numbers */ /* prompt for the user to enter nine positive integers to be stored into the array */ int index = 0; printf( "Please enter 9 positive integers :\n"); for ( rows = 0 ; rows &lt; 3 ; rows++ ) { for ( cols = 0 ; cols &lt; 3 ; cols++ ) { scanf( "%d", &amp;ar[rows][cols] ); /* Store values in the temp[z] = {1 2 3 4 5 6 7 8 9}*/ temp[index] = ar[rows][cols]; index += 1; /* Increase the array in temp[z] */ } } /* Call disp_arr to display the 3 x 3 board */ disp_arr( *temp ); }/* end main */ disp_arr( int storedValue ) { int x,y; int counter = 0; for ( x = 0 ; x &lt; 3 ; x++ ) { for ( y = 0 ; y &lt; 3 ; y++ ) { /* How on earth do I include the counter inside without errors??*/ printf( "%d\t", storedValue ); counter += 1; } printf("\n"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload