473,399 Members | 2,159 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,399 software developers and data experts.

How can I declare an array with an undefined size in C?

I've been trying to figure this out for hours. I need to make a program that will let the user input values and it will only stop accepting once the user has inputted a negative number then it will display its mode and frequency.

The problem is I can't find a way to let the user input values freely without asking first for the no. of values he will enter. Please help.



Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5. int i,j,k=1,p,a[20],b[20],n,cnt=1,big;
  6. clrscr();
  7. printf("Enter the number of elements:\n");
  8. scanf("%d",&n);
  9. printf("Enter the numbers: ");
  10. for(i=1; i<=n; i++)
  11. scanf("%d",&a[i]);
  12. for(i=1; i<=n; i++)
  13. {
  14. for(j=i+1;j<=n; j++)
  15. {
  16. if(a[i]==a[j])
  17. cnt++;
  18. }
  19. b[k]=cnt;
  20. k++;
  21. cnt=1;
  22. }
  23.  
  24. big=b[1];
  25. p=1;
  26. for(i=2; i<=n; i++)
  27. {
  28. if(big<b[i])
  29. {
  30. big=b[i];
  31. p=i;
  32. }
  33. }
  34. printf("\nThe mode is %d and it has occurred %d times.\n",a[p],b[p]);
  35. getch();
  36. }
Oct 7 '14 #1
2 1967
donbock
2,426 Expert 2GB
You can dynamically size the array by using malloc for the initial allocation and realloc to increase the allocation as needed. Don't forget to call free when you're done. You can take advantage of the fact that a pointer to a buffer of N items can be used as if it were an array of N items.
Oct 7 '14 #2
weaknessforcats
9,208 Expert Mod 8TB
You might also consider using malloc initially to allocate enough memory for some reasonable number of values, say 5 or 10. Then if no negative number appears you can realloc for another 5 or 10 values. realloc is expensive and you should not do it after every value entered.

I agree you should never ask the user for the number of values. Knowing the number of values is hard on the user but easy on the programmer. Just backwards. Everything should be easy for the user. If that's hard on the programmer, it's jut too bad:)
Oct 7 '14 #3

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

Similar topics

5
by: Philipp | last post by:
Hello, I've got a class Lattice which is declared like that: // file: lattice.h class Lattice:public EventChooser{ public: Lattice(LatticeParameters* params); virtual ~Lattice(); //...
5
by: Emmanuel Delahaye | last post by:
Hi, I know that C90 and C9x (N869) doesn't allow to define an array of size 0. int x; /* constraint violation */ I was under the impression that this was possible in C99, due to the common...
25
by: prashna | last post by:
Hi all, I have seen a piece of code(while doing code review) which declared an array of size 0.One of my friend told although it is not standard C,some compilers will support this..I am very...
2
by: Petez | last post by:
Hi, I want to declare array of System::Data::DataRow but how? With single variable I write System::Data::DataRow^ aRow but statement System::Data::DataRow^ - not works. System::Data::DataRow* ...
6
by: John A Grandy | last post by:
inside a procedure , i code Dim values(1) As Object .... various other lines of code ... ReDim values(2) As Object
5
by: eagle_jyjh | last post by:
For example: the msg = temp_buf; is alwawys ok? //test_msg.cpp struct msg_head { char a01;
8
by: eagle_jyjh | last post by:
For example: the msg = temp_buf; is alwawys ok? //test_msg.cpp struct msg_head { char a01;
0
by: Cedric | last post by:
Hello, I 'm a bit confused about the size of an array of byte use by getjob or setjob. For GetJob no problem, API return size needed BUT for SetJob I must pass an array with good size. It's...
7
by: SUNIL TYATA | last post by:
The java code below is well functioning code where I did was; I used nums.length as the size for the declaring the new arrary within the function body. public int reverse3(int nums) { int...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.