numeric constant

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jay566
    New Member
    • May 2012
    • 3

    numeric constant

    could anyone help I just started learning c and I couldn't fix this this error in the program

    errors are

    stat.c:16: error: expected ‘;’, ‘,’ or ‘)’ before numeric constant
    stat.c:25: error: expected ‘;’, ‘,’ or ‘)’ before numeric constant

    line 16 void pop_arr(double data[], const int sz)
    line 25 void print_arr(const double data[], const int sz)

    the code

    Code:
    #include <stdio.h>
    #define sz 10
    
    double read_double(const char* prompt)
    {
    	double result;
    	printf("%s", prompt);
    		while(scanf(" %lf", &result) != 1)
    		{
    			scanf("%*[\n]");
    			printf("%s\n", prompt);
    		}
    	return result;
    }
    
    void print_arr(const double data[], const int sz)
    {
    	int i;
    	for (i = 0; i < sz; i++)
    	{
    		printf("%lf\n", data[]);
    	}
    }
    
    void pop_arr(double data[], const int sz)
    {
    	int i;
    	for (i = 0; i < sz; i++)
    	{
    		data[i] = read_double("Enter a value");
    	}
    }
    
    int main()
    {
    	double data[sz];
    	pop_arr(data, sz);
    	print_arr(data, sz);
    
    	// return 0;
    }
    Last edited by Meetee; May 16 '12, 09:00 AM. Reason: code tags added
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    On line 2 you have #defined sz to 10. This is a text substitution performed by the preprocessor before the compiler runs on all the code so the line void print_arr(const double data[], const int sz) is modified to void print_arr(const double data[], const int 10) by the time the compiler sees it and that produces your error.

    You need to be much more careful about the names you give to you #defined symbols and make the much more meaningful so that you don't accidentally get clashes like this with parameter/variable/other symbol names.

    Comment

    • Jay566
      New Member
      • May 2012
      • 3

      #3
      Sorry but I knew this already I dont understand the error message tho. I tried changing the define
      to size instead of sz but still doesn't work .

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        What the compiler sees is:

        Code:
        void print_arr(const double data[], const int 10)
        { etc...
        The "const int 10" is meaningless so the compiler just puts out a generic error message that maybe you have left out some syntax.

        You have to decide whether sz ia an int or a 10. It can't be both in C but it could be both in C++ where you are allowed default values.

        BTW changing sz to size does work. BUT-- you need to fix an error in print_arr. printf needs to show data[i] rather than data[].

        Comment

        • Jay566
          New Member
          • May 2012
          • 3

          #5
          Thanks for the help guys finally worked it out ..


          Code:
          #include <stdio.h>
          #define data_size 10
          
          double read_double(const char* prompt)
          {
          	double result;
          	printf("%s", prompt);
          		while(scanf(" %lf", &result) != 1)
          		{
          			scanf("%*[\n]");
          			printf("%s\n", prompt);
          		}
          	return result;
          }
          
          double sum(const double data[], const int size)
          {
          	int i;
          	double result;
          
          	for (i = 0; i < size; ++i)
          	{
          		result += data[i];
          	}
          	return result;
          }
          
          double mean(const double data[], const int size)
          {
          	return sum(data, size)/ size;
          }
          
          double max(const double data[], const int size)
          {
          	int i;
          	int max= data[0];
          	for (i = 0; i < 10; i++)
          	{
                if (data[i] > max)
                  {
                     max = data[i];
                  }
               }
          	return max;
          }
          
          void print_arr(const double data[], const int size)
          {
          	int i;
          	for (i = 0; i < size; i++)
          	{
          		printf("%lf\n", data[i]);
          	}
          }
          
          void pop_arr(double data[], const int size)
          {
          	int i;
          	for (i = 0; i < size; i++)
          	{
          		data[i] = read_double("Enter a value");
          	}
          }
          
          int main()
          {
          	double data[data_size];
          	pop_arr(data, data_size);
          	print_arr(data, data_size);
          	printf("Sum:     %4.2f\n", sum(data, data_size));
          	printf("Mean:     %4.2f\n", mean(data, data_size));
          	printf("Max:     %4.2f\n", max(data, data_size));
          	return 0;
          }

          Comment

          Working...