populateing an array with for loops

This is a discussion on populateing an array with for loops within the C++ Programming forums, part of the General Programming Boards category; Hi there I'm just starting out with c++, its difficult but I'm getting there. Anyway here's my problem. the code ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    27

    populateing an array with for loops

    Hi there

    I'm just starting out with c++, its difficult but I'm getting there. Anyway here's my problem.

    the code i'm writing is meant to declare an uninitiilized array and then populate it to make a pattern and thern print it out. Heres my code

    Code:
    #include<iostream>
    
    using namespace std;
    
    char populate(char, int, int);
    char display(char, int, int);
    
    int main()
    {
    
    	//declare the i and j variables for use in the array
    	//declare the array and leave it uninitalized
    	//Call the functions populate() and display()	
    	
    	int i, j;
    
    	char array1[7][7];
    
    	char populate(array1, i, j);
    
    	char display(array1, i, j);
    
    	return 0;
    
    }
    
    
    //function that uses a for loop to add a 1 the the element of the array if i is greater
    //that j and a 0 if the opposite is true
    char populate(char array2, int i, int j)
    {
    	for (i=0; i<7; i++)
    	{
    		for (j=0; j<7; j++)
    		{
    			if (i<j)
    				array2[i][j]=1;
    			else
    				array2[i][j]=0;
    		}
    	}
    
    }
    
    
    //function that prints out the contents of the array
    char display(char array3, int i, int j)
    {
    	for(i=0; i<7; i++)
    	{
    		for(j=0; j<7; j++)
    		{
    			cout<< array3[i][j];
    		}
    	}
    }
    and heres the error messages the compiler is throwing out;

    In function ‘int main()’:
    21: error: initializer expression list treated as compound expression
    23: error: initializer expression list treated as compound expression
    In function ‘char populate(char, int, int)’:
    39: error: invalid types ‘char[int]’ for array subscript
    41: error: invalid types ‘char[int]’ for array subscript
    In function ‘char display(char, int, int)’:
    57: error: invalid types ‘char[int]’ for array subscript

    I've been told that I'm not calling/defining my functions correctly, but they didn't say how. Also i'm not sure if saying 'array2' and 'array3' would affect it in any way, but it throws out the error messages regardless of what i call them

    thanks in advance

    ES

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    The biggest issue is how you are defining your functions and passing the array.

    You've declared and defined your functions as being passed a "char" - which is a single character. However, you should have declared and defined your functions to pass a 2 dimensional char array.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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