Displaying A Multidimensional Array

This is a discussion on Displaying A Multidimensional Array within the C Programming forums, part of the General Programming Boards category; Hi guys, I just had a simple question, one of my homework assignments was to write a multidimensional array that ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    14

    Displaying A Multidimensional Array

    Hi guys, I just had a simple question, one of my homework assignments was to write a multidimensional array that displays the elements of that array that are larger than all their neighbors.
    The problem is, when I use a function I made myself to attempt to give a visual of the array, I get the number -858993460 in place of what should be there. The rest of the program however works fine. I'm kinda stumed here, any help is appreciated. Here's the code I'm using:


    Code:
    /*
    *File: neighbors.c
    *The program creates a multidimensional array, and then displays the elements of that
    *array that are larger than all their neighbors.
    */
    
    #include "stdafx.h"
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    
    #define size 5
    
    void getArray(int ne[][size]);
    bool check_neighbors(int ne[][size],int i,int j);
    void displayArray(int ne[][size]);
    
    main()
    {
    	int ne[size][size],i,j;
    	getArray(ne);
    	printf("Your array is:\n");
    	displayArray(ne);
    	for(i=1;i<size-1;i++)
    	{
    		for (j=1;j<size-1;j++)
    		{
    			if (check_neighbors(ne,i,j)==true) printf("%d in location (%d,%d)\n",ne[i][j],i-1,j-1);
    		}
    	}
    }
    
    void getArray(int ne[][size])
    {
    	int i,j;
    	for(i=1;i<size-1;i++)
    	{
    		for (j=1;j<size-1;j++)
    		{
    			printf("\nEnter next integer:  ");
    			ne[i][j]=GetInteger();
    		}
    	}
    }
    
    void displayArray(int ne[][size])
    {
    	int i,j;
    	for (i=1;i<size-1;i++);
    	{
    		for (j=1;j<=size-1;j++)
    		{
    			printf("%4d",(ne[i][j]));
    		}
    		printf("\n");
    	}
    }
    
    bool check_neighbors(int ne[][size],int i,int j)
    {
    	if ((ne[i+1][j+1]<=ne[i][j])&&(ne[i-1][j-1]<=ne[i][j])&&(ne[i+1][j]<=ne[i][j])&&(ne[i][j+1]<=ne[i][j])&&(ne[i-1][j]<=ne[i][j])&&(ne[i][j-1]<=ne[i][j])&&(ne[i+1][j-1]<=ne[i][j])&&(ne[i-1][j+1]<=ne[i][j]))
    	{
    		return (true);
    	}
    	else return(false);
    }
    Again, thanks for any help

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Arrays start at zero, not one. So for your size of 5, you have valid array elements 0,1,2,3 and 4.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    14
    Yeah, I understand that the array starts at one, but I only want to display the 3x3 set of values that is in the center of the array. The other two rows are only present to make it easier for me to use the check_neighbors function I created, so that I wouldn't have to alter it or add unnecessary ifs into it for values with less than eight neighbors, but the program still gives me the error. I was wondering more about why I might get the strange output of -858993460. Thanks again for any help.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you do not want to display the first and last entry
    Code:
    void displayArray(int ne[][size]) {
    	int i,j;
    	for (i=1;i<size-1;i++);          //remove semicolon
    	{
    		for (j=1;j<=size-1;j++) // should be j<size-1
    		{
    			printf("%4d",(ne[i][j]));
    		}
    		printf("\n");
    	}
    }
    Kurt

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > int ne[size][size]
    Unless you do something, all these array elements start off with junk values.
    So you need to initialise your perimeter elements with zero (for example).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. displaying a board with 2D array
    By axon in forum C++ Programming
    Replies: 9
    Last Post: 03-07-2003, 07:59 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21