473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

numeric constant

3
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

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define sz 10
  3.  
  4. double read_double(const char* prompt)
  5. {
  6.     double result;
  7.     printf("%s", prompt);
  8.         while(scanf(" %lf", &result) != 1)
  9.         {
  10.             scanf("%*[\n]");
  11.             printf("%s\n", prompt);
  12.         }
  13.     return result;
  14. }
  15.  
  16. void print_arr(const double data[], const int sz)
  17. {
  18.     int i;
  19.     for (i = 0; i < sz; i++)
  20.     {
  21.         printf("%lf\n", data[]);
  22.     }
  23. }
  24.  
  25. void pop_arr(double data[], const int sz)
  26. {
  27.     int i;
  28.     for (i = 0; i < sz; i++)
  29.     {
  30.         data[i] = read_double("Enter a value");
  31.     }
  32. }
  33.  
  34. int main()
  35. {
  36.     double data[sz];
  37.     pop_arr(data, sz);
  38.     print_arr(data, sz);
  39.  
  40.     // return 0;
  41. }
May 16 '12 #1
4 5630
Banfa
9,065 Expert Mod 8TB
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.
May 16 '12 #2
Jay566
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 .
May 16 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
What the compiler sees is:

Expand|Select|Wrap|Line Numbers
  1. void print_arr(const double data[], const int 10)
  2. { 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[].
May 16 '12 #4
Jay566
3
Thanks for the help guys finally worked it out ..


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define data_size 10
  3.  
  4. double read_double(const char* prompt)
  5. {
  6.     double result;
  7.     printf("%s", prompt);
  8.         while(scanf(" %lf", &result) != 1)
  9.         {
  10.             scanf("%*[\n]");
  11.             printf("%s\n", prompt);
  12.         }
  13.     return result;
  14. }
  15.  
  16. double sum(const double data[], const int size)
  17. {
  18.     int i;
  19.     double result;
  20.  
  21.     for (i = 0; i < size; ++i)
  22.     {
  23.         result += data[i];
  24.     }
  25.     return result;
  26. }
  27.  
  28. double mean(const double data[], const int size)
  29. {
  30.     return sum(data, size)/ size;
  31. }
  32.  
  33. double max(const double data[], const int size)
  34. {
  35.     int i;
  36.     int max= data[0];
  37.     for (i = 0; i < 10; i++)
  38.     {
  39.       if (data[i] > max)
  40.         {
  41.            max = data[i];
  42.         }
  43.      }
  44.     return max;
  45. }
  46.  
  47. void print_arr(const double data[], const int size)
  48. {
  49.     int i;
  50.     for (i = 0; i < size; i++)
  51.     {
  52.         printf("%lf\n", data[i]);
  53.     }
  54. }
  55.  
  56. void pop_arr(double data[], const int size)
  57. {
  58.     int i;
  59.     for (i = 0; i < size; i++)
  60.     {
  61.         data[i] = read_double("Enter a value");
  62.     }
  63. }
  64.  
  65. int main()
  66. {
  67.     double data[data_size];
  68.     pop_arr(data, data_size);
  69.     print_arr(data, data_size);
  70.     printf("Sum:     %4.2f\n", sum(data, data_size));
  71.     printf("Mean:     %4.2f\n", mean(data, data_size));
  72.     printf("Max:     %4.2f\n", max(data, data_size));
  73.     return 0;
  74. }
  75.  
May 17 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: William Payne | last post by:
Hello, I am starting to steer away from the practice of using "using namespace std;" in my code. Instead I am qualifying each name in the source when I use them, for example: std::cout << "Hello";...
6
by: Tom Torfs | last post by:
Hello All, I've been missing the lack of support for binary numeric literals in C. To get around it I wrote the following handy macros, which allows you to simply write something like: ...
6
by: fctk | last post by:
hello, i'm trying to compile this small program: int main(void) { unsigned long int max; max = 4000000000;
3
by: ollii | last post by:
im creating a iterrative server and i get this error error: syntax error before numeric constant and this errors occurs near this line: int listen(sock,5); need help
26
by: kerravon | last post by:
The following C program: int main(void) { int x = -2147483648; return (0); } Produces the following warning:
10
by: aarklon | last post by:
Hi all, what exactly is the purpose of multi-character constant..???
1
by: Allen | last post by:
In my app, there is a namespace definition. .... namespace CMD { ... const int YT_UP = 1; // line 149 ... }; g++ tells expected unqualified-id before numeric constant error at
2
by: dissectcode | last post by:
If I have a function funct( uint32 x ) { stuff } and call it:
2
by: tvnaidu | last post by:
I am getting this error for line 108, I kept C code also below, any idea?. main.c:108: underscore in number main.c:108: syntax error before numeric constant 104: #define...
3
by: lingjun | last post by:
Hi, I am taking my first programing course in college... and I am completely lost on this assignment. I am not sure what is wrong with my current code. Any help will be appreciate it... thanks! ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.