954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with writing function to populate the array

Sample Input:o
0 4 0 5 0 8 0 7 0
Press any key to continue . . .

This is what I have so far for the code
[

#include <stdio.h>
#include <stdlib.h>

#define ARRAY_SIZE  81

int nArray[ARRAY_SIZE];

/*Function prototypes */
void PopulateArray( int nArray[9][9]);

void PrintArray(const  int nArray[9][9]);

void PopulateArray( int nArray[9][9])
{
   int i;
   int nSize;
   for(i=0 ; i < nSize ; i++)
   {
      printf("Enter value number %d:", i+1);
      _flushall();
      scanf("%d",&nArray[i]);
      
   }
      
}

void PrintArray(const int nArray[9][9])
{
   int i,j;
   
   for(i = 0; i <= 8 ; i++)
    {
   for(j = 0; j <= 8 ; j++)
      {
        printf("%2d",nArray[i][j]);
      }    
        printf("\n");
    }      
}  

int main(void)
{
  PopulateArray( int nArray[9][9]);
 
 PrintArray(const int nArray[9][9]);

 system("PAUSE");
 
 return 0;
}

I am getting the following errors in VC++

-------------------Configuration: Test - Win32 Debug--------------------
Compiling...
myfiletesting.c
C:\Documents and Settings\HP_Administrator\myfiletesting.c(16) : warning C4029: declared formal parameter list different from definition
C:\Documents and Settings\HP_Administrator\myfiletesting.c(28) : warning C4028: formal parameter 1 different from declaration
C:\Documents and Settings\HP_Administrator\myfiletesting.c(44) : error C2143: syntax error : missing ')' before 'type'
C:\Documents and Settings\HP_Administrator\myfiletesting.c(44) : error C2198: 'PopulateArray' : too few actual parameters
C:\Documents and Settings\HP_Administrator\myfiletesting.c(44) : error C2059: syntax error : ')'
C:\Documents and Settings\HP_Administrator\myfiletesting.c(46) : error C2143: syntax error : missing ')' before 'type'
C:\Documents and Settings\HP_Administrator\myfiletesting.c(46) : error C2198: 'PrintArray' : too few actual parameters
C:\Documents and Settings\HP_Administrator\myfiletesting.c(46) : error C2059: syntax error : ')'
Error executing cl.exe.

myfiletesting.obj - 6 error(s), 2 warning(s)

appuguy
Newbie Poster
3 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

The following code should do the trick. Its not the most elagnant code as there is no validation for user inputs.

Also i have kept the interger array a 2 dimentional, if you want u can use a single dimentional array too (with approp changes), but keep it single dimentional across your as it makes the code more readable.

#include <stdio.h>

#define NROWS 3
#define NCOLS 3

static void popArray(int arr[NROWS][NCOLS])
{
	int rNo, cNo;

	for (rNo=0; rNo<NROWS; rNo++) {
		printf ("\nEnter row%d (%d values):", rNo, NCOLS);	//put approp message
		for (cNo=0; cNo<NCOLS; cNo++) {
			scanf("%d", &(arr[rNo][cNo]));
		}
	}
}


static void printArray(int arr[NROWS][NCOLS])
{
	int rNo, cNo;

	for (rNo=0; rNo<NROWS; rNo++) {
		printf("\n");
		for (cNo=0; cNo<NCOLS; cNo++) {
			printf("%d ", arr[rNo][cNo]);
		}
	}
}


int main(void)
{
	int myArr[NROWS][NCOLS];

	popArray(myArr);
	printArray(myArr);

	return 0;
}
tituscha
Newbie Poster
1 post since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
-------------------Configuration: Test - Win32 Debug-------------------- Compiling... myfiletesting.c C:\Documents and Settings\HP_Administrator\myfiletesting.c(16) : warning C4029: declared formal parameter list different from definition C:\Documents and Settings\HP_Administrator\myfiletesting.c(28) : warning C4028: formal parameter 1 different from declaration


Look at the lines specified. You are calling a function, but the parameters do not match the prototype you specified, such as calling with achar and the parameter needs a char*

C:\Documents and Settings\HP_Administrator\myfiletesting.c(44) : error C2198: 'PopulateArray' : too few actual parameters


Isn't this one obvious?C:\Documents and Settings\HP_Administrator\myfiletesting.c(44) : error C2059: syntax error : ')'
Look at the line. Why would the errer point out a ')'?


-------The following code should do the trick. Its not the most elagnant code as there is no validation for user inputs.
What grade are you expecting for finishing his code for him? We prefer tohelp them solve the problems, not prove we can do better... :icon_wink:

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You