2

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.

There is something wrong with my disp_arr 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!

This is the output that I get when running: 2686636 clone errors

/* 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 <stdio.h>

#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 < 3 ; rows++ )
    {
        for ( cols = 0 ; cols < 3 ; cols++ )
            {
                scanf( "%d", &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 < 3 ; x++ )
    {
        for (  y = 0 ; y < 3 ; y++ )
        {
            printf( "%d\t", storedValue );
        }
        printf("\n");
    }
}

I thought about including counters like storedValue[counter] but it returns more error =/

disp_arr( int storedValue )
    {
        int x,y;
        int counter = 0
        for (  x = 0 ; x < 3 ; x++ )
        {
            for (  y = 0 ; y < 3 ; y++ )
            {
                printf( "%d\t", storedValue[counter] );

                counter += 1;
            }
            printf("\n");
        }
    }

Any help would be appreciated.

Thanks in advance!

Sam

/* Editted code after haccks and Rohan's advice */
#include <stdio.h>

#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 < 3 ; rows++ )
    {
        for ( cols = 0 ; cols < 3 ; cols++ )
            {
                scanf( "%d", &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 < 3 ; x++ )
        {
            for (  y = 0 ; y < 3 ; y++ )
            {
                /* How on earth do I include the counter inside without errors??*/
                printf( "%d\t", storedValue );

                counter += 1;
            }
            printf("\n");
        }
    }
Sam Liew
  • 157
  • 2
  • 4
  • 15

3 Answers3

1

By passing temp to disp_array means you are passing the address of first element of array temp (as it will decay to pointer in this case) to your function. For this, the function parameter must be of type int *.

Declare your disp_array as

void disp_arr( int *temp);   

Call it as

disp_array(temp);  

And change your function definition to

void disp_arr( int *storedValue )
{
    int i;
    for (  i = 0 ; i < NROW*NCOL ; i++ )
    {
            printf( "%d\t", storedValue[i] );
                printf("\n");
    }
}
haccks
  • 104,019
  • 25
  • 176
  • 264
  • hey good to hear from you again! Well back to the code. When I include void infront, it gives an error :/ and the only number it prints out is the first number that I input. Any ideas? – Sam Liew Nov 11 '13 at 15:59
  • thanks! I still have some questions though. If an array is to be passed to a function/decay into pointer, the global scope function & the function definition argument must include a *(pointer)? Does the decaying process only apply to arrays? – Sam Liew Nov 11 '13 at 16:16
  • Yes. Any expression of array type is, in most but not all contexts, implicitly converted to ("decays" to) a pointer to the array object's first element, unless it's the operand of unary `&` or `sizeof` operator. – haccks Nov 11 '13 at 16:34
  • Any other queries ? :) – haccks Nov 11 '13 at 16:55
  • Oh I see! Just two more. You mentioned that an array's object first element is decayed into pointers. How about the subsequent elements? Secondly the term `void` means "does not return any value". Is it necessary to place the code `void` infront of the global scope function and function definition? I have heard from my lecturer but I just don't seem to understand.. – Sam Liew Nov 11 '13 at 17:05
  • For your first question I would suggest to read [this](http://stackoverflow.com/a/18364638/2455888) and [this](http://stackoverflow.com/a/18361686/2455888). For second, In C89 it is legal to define a function without any return type and implicitly its return type is converted to `int`. But C99 and latter standards drop this feature and omitting the return type of a function is not legal (**6.9.1 Function definitions**: `The return type of a function shall be void or a complete object type other than array type.`), so it is best to avoid this practice. – haccks Nov 11 '13 at 18:56
1

disp_arr() should take int * parameter rather than int.

And print it as in your 2nd function.

printf( "%d\t", storedValue[counter] );
Rohan
  • 52,392
  • 12
  • 90
  • 87
  • I got tons of errors when I print it as in my 2nd function. I editted the main code. Please have a look Sam – Sam Liew Nov 11 '13 at 16:00
0

Not directly related to your function issue, but I think you can clean up your main for loop some. Since it looks like you do nothing with your ar array, you can do something like the following instead (double check me for syntax errors since I'm not at a compiler right now, but the idea should hopefully be clear):

int main(void)
{
    /*declare needed variables or constants, e.g. */
    int total = NROW * NCOL;
    int temp[total];
    printf("Please enter %d positive integers :\n", total);
    for(int i = 0; i < total; i++)
    {
        scanf("%d", temp[i]);
    }

    // Print the array.
    disp_arr(temp);
 }
Dillon Welch
  • 481
  • 4
  • 15