If I don't now the size of the array?!

Write a program that reads integers, finds the largest of them, and counts its occurrences. Assume that the input ends with number 0. Here are sample runs of the program:
Sample Output
Enter numbers: 3 5 2 5 5 5 0
The largest number is 5
The occurrence count of the largest number is 4
Sample Output
Enter numbers: 3 6 5 4 2 4 5 4 5 5 0
The largest number is 6
The occurrence count of the largest number is 1
*Solve it by using array


The question is how can i make the array if i don't now the size????!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 #include <stdio.h>
int main ()
{
	int n=0,largest=0,occu=0,i=0;
	int array1[]={0};
	for(i=0;i<array1[];i++)
	{
	printf("enter numbers:");
	scanf("%d",&n);
	if(n>largest)
		largest=n;
	if(largest==n)
		++occu;
	}
		printf("The largest is: %d\n",largest);
	printf("the occurrence count of the largest number is :%d\n",occu);
}
you're gonna have to implement a vector or a dynamic array
To expand on that, you declare an array with initial capacity X and length 0. The length will represent how many elements are actually in use. If the length ever reaches the capacity, you double the capacity, and create a new array with the new capacity then copy everything over and delete the old array. This results in amortized O(1) time for adding in new elements.

Of course, you could just use a vector which does this all for you.
Topic archived. No new replies allowed.