Thread: Difficulty With Defining an Array

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    6

    Difficulty With Defining an Array

    Basically I'm attempting to write this program but it refuses to compile, giving the error message:

    Code:
    Compiling and linking file: Project2.c
    D:\C PROJECT\PROJECT2.C(24) : error 185 - A constant expression is required here
    *** Compilation failed
    Compilation failed
    The program is as follows:

    Code:
    /*This program was written by Chris Tayler. Work started on 4/11/05*/
    
    /*The purpose of this program is to calculate the gradient, y-intercept and the uncertainty in the gradient for a line
    of best fit determined by a series of points input by the user of the program. The line of best fit is drawn using the
    "least squares fitting" method.*/
    
    #include<stdio.h>
    #include<math.h>
    
    int main (void)
    
    {
    
    	int N; /*Declares N to be an interger*/     
            
    	printf("Please enter the number of (x,y) coordinates you recorded: ");
    
        scanf("%d",&N); /*Determines value for N (user specified)*/
    
        printf("\nThe number of values you recorded is: %d\n\n", N); /*Prints value of N*/ 
       
    	int x, y; /*Declares x and y to be intergers*/
    
    	int results[N][2]; /*(attempts to) declare an array with N rows and 2 columns*/
      	
        printf("Enter your %d pairs of values in the form (x,y), pressing return after each pair\n\n", N);
      
    	return 0;
        
    }
    The error message is referring to line 24 which is:

    Code:
    int results[N][2]; /*Declares an array with N rows and 2 columns using the pointer *n*/
    Any help on this one is appreciated, i'm still having issues with C!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't use variable sized arrays unless you're compiling as C99. You should consider using something like malloc for dynamic memory allocation, since that's what it's for. Also, scattering your variable declarations all around your code is illegal unless you're compiling as C99.


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

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    The error message is referring to line 24 which is:
    Code:
    int results[N][2];
    You can't do that, your array bounds must be a constant value and since N is an integer it cant be used for defining the size of your array.

    You could solve this issue by either doing this:-

    Code:
    #include <stdio.h>
    #define MAX 100
    
    int main(void)
    {
    
    	int arr[MAX][2];
    	int n=10;
    	int i,j;
    
    	for(i=0;i<n;i++)   /*User defined for size*/
    	{
    		for(j=0;j<2;j++)
    		{
    		  /*Your array scanning code goes here */
    		}
    	}
    
    	return 0;
    }
    Or you could dynamically allocate your array, which I dont think you would want to get into at this point, if your still curious, search the forums and you'll find your answer.
    Thanks,
    Sahil

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    Thanks for the help Sahil, i think i understand how the #define command works but i'm still having difficulty assigning the value of n using scanf. This is that section of code as it stands at the moment:

    Code:
    #include <stdio.h>
    #define N 100
    
    int main(void)
    
    {printf("Please enter the number of (x,y) coordinates you recorded: ");
    
        scanf("%d",&N); /*Determines value for N (user specified)*/
    
        printf("\nThe number of values you recorded is: %d\n\n", N); /*Prints value of N*/ 
      }
     
    {
    
    	int arr[MAX][2];
    	int n=10;
    	int i,j;
    
    	for(i=0;i<n;i++)   /*User defined for size*/
    	{
    		for(j=0;j<2;j++)
    		{
    		  scanf("%d %d", i, j, &arr[i], &arr[j]);
    		}
    	}
    
    	return 0;
    }
    This code returns the following error message:

    Code:
    Compiling and linking file: tester.c
    F:\C PROJECT\TESTER.C(8) : error 99 - & Requires an lvalue operand
    F:\C PROJECT\TESTER.C(13) : error 199 - '{' found where a type specifier was expected
    F:\C PROJECT\TESTER.C(28) : fatal 233 - Compilation abandoned
    Compilation failed
    The other issue with the code in this form is that the values to be input into the array will need to be floats but the compiler didn't like it when i tried to use scanf with %f. I think this is down to the fact that the array needs to be defined using intergers? How do i get around this?

    Again any help is appreciated, i've got a help class tomorrow but i'd like to make some progress before then.

    Thanks again people.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #define N 100
    With the above define, all occurances, outside of strings, in the code where N appears will be replaced with 100. It's a text replace, as if you went through your code in a text editor and did a "search and replace" for N, subsituting it with 100.

    It's not defining a variable. It's aliasing the macro N as the value 100.


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

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    That's whats causing my problem. I need to have an array that uses a user defined number of columns. The rest of the program is easy enough its just getting that bit sorted that's causing issues!

    I also think that my syntax is wrong for using scanf to get data into my array? I'm currently trying to use:

    Code:
    	int n=10;
    	int i,j;
    
    	for(i=0;i<n;i++)   /*User defined for size*/
    	{
    		for(j=0;j<2;j++)
    		{
    		  scanf("%d %d", i, j, &arr[i], &arr[j]);
    		}
    But it seems to me that the %d's should really be %f's so that the values input into the array can be decimals? Could anyone confirm/correct me on this one?

    Thanks guys, i'm getting there (albeit slowly)!

  7. #7
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    >>That's whats causing my problem. I need to have an array that uses a user defined number of columns.
    So, as already told above.Use a c99 compiler or allocate memory dynamically using malloc.


    Code:
    scanf("%d %d", i, j, &arr[i], &arr[j]);
    You want to store four integer values so increase the no. of %d's by two.Since you have two dimensional array,so you should give whole address like &a[x][y].

    >>But it seems to me that the %d's should really be %f's so that the values input into the array can be decimals?
    yes,Because int cant store decimal values.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    Quote Originally Posted by cbastard
    So, as already told above.Use a c99 compiler or allocate memory dynamically using malloc.
    Unfortunatley i'm restricted to using Salford Plato3 as my compiler so that's not an option and our course hasn't covered dynamic memory allocation so i'm thinking there must be a way to do it using the #define command...?


    Quote Originally Posted by cbastard
    You want to store four integer values so increase the no. of %d's by two.Since you have two dimensional array,so you should give whole address like &a[x][y]
    So this would give me
    Code:
    {	for(x=0;x<N;x++)  
    
    	{	for(y=0;y<2;y++)
    		
    scanf("%d, %d,", x, y, &arr[x][y]);
    
    }
      
             }
    as my syntax?

    Quote Originally Posted by cbastard
    yes,Because int cant store decimal values.
    The problem i'm having with this is that the position in the array cannot be a float so if the input from the user is going to define both the recorded value and the position in the array then neither %f or %d will work?

    This is beginning to drive me crazy! Thanks again guys.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dirtbiker
    Unfortunatley i'm restricted to using Salford Plato3 as my compiler so that's not an option and our course hasn't covered dynamic memory allocation so i'm thinking there must be a way to do it using the #define command...?
    No. You've already been told how to you have to do it. Preprocessor commands are not run time items. They're compile time only.

    So, you either make a really big array, and hope no one wants an array bigger than what you've set up, or you allocate it dynamically.

    Also, if you want decimal places, then you use a type that supports it. Otherwise you don't get them.


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

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    After some fiddling about i've managed to get it to work, thanks guys!

    The code i used is:

    Code:
    #include <stdio.h>
    #define MAXSIZE 100
    
    void main()
    	
    {	int i, size;
    		
    int A[MAXSIZE], B[MAXSIZE];
    		
    {
    	printf("How many (x,y) values did you collect?: ");
    	scanf("%d",&size);
    }
    		while (size < 1 || size > MAXSIZE);
            		
    				printf("\nPlease enter the %d x-values:\n\n", size);
    		        for (i = 0; i < size; i++)         /* reads values for x */
        		    scanf ("%d", &A[i]);
    	
    					printf("\nPlease enter the %d y-values\n\n", size);
    					for (i = 0; i < size; i++)         /* reads values for y */
      		   			scanf ("%d", &B[i]);
    		
    						for (i = 0; i < size; i++)        
        		    		printf("\nThe data points you entered: (%d,%d)", A[i], B[i]); /* displays (x,y) cooridinates */
                            printf("\n");
    
                          
    	}
    Happy now, its probably not the most elegant soloution but at least it works properly!

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM