Problems with Dynamic array problem

This part was written for me and for some crazy reason it won't work, and I have no clue why not
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<stdio.h>
#include<stdlib.h>
//array_accommodation
//linear search
//linear search recursive
//find min, max, avg
// bubble sort
void bubble_sort(int *p, int size);
void print_array(int *p, int size);
//recursive bubble sort
//adding a number to an array
//removing a number
//4.0 display distinct members of the array
//4.0 counting sort
int main()
{
	int *p;
	int size;
	int i,j;
	int temp;

/*************/

	printf("How many numbers do you want to accommodate? \n");
	scanf("%d", &size);

	p=(int*)calloc(size, sizeof(int));


	for(i=0; i<size; i++)
	{
		printf("Enter a member for box %d  \n", i);
		scanf("%d", &p[i]);

	}

	printf("\n Thank you!\n");

	print_array(p, size);

	

		bubble_sort(p, size);
		
		

		print_array(p, size);




	free(p);
	return 0;
}

void bubble_sort(int *p, int size)
{
	int i, j, temp;

	for(j=0; j<size-1; j++)
	for(i=0; i<size-1; i++)
		if(p[i]>p[i+1])
		{
			temp=p[i];
			p[i]=p[i+1];
			p[i+1]=temp;

		}

}

void print_array(int *p, int size)
{
	int i;

	printf("\n Printing array \n");
	for(i=0; i<size; i++)
		printf("%d ", p[i]);
	printf("\n");

}


Now this is the code I wrote to finish this but again I'm not sure why it wont run for me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include<stdio.h>

#include<stdlib.h>

//array_accommodation

//linear search

void linear_search (int *p, int size);

//linear search recursive
 
//find min, max, avg

void max_element(int *p, int size);

void miniumum_elements(int *p, int size);

// bubble sort

void bubble_sort(int *p, int size);

void print_array(int *p, int size);
 
//recursive bubble sort

//adding a number to an array

void add_new (int *p, int size);

//removing a number

void remove_element(int *p, int size);

//4.0 display distinct members of the array
 
//4.0 counting sort

int main()

{

	 int *p;

	 int size;

	 int i,j;
 
	int temp;

/*************/


	 printf("How many numbers do you want to accommodate? \n");
	scanf("%d", &size);

	 p=(int*)calloc(size, sizeof(int));


	for(i=0; i<size; i++)

	 {

		 printf("Enter a member for box %d  \n", i);
		scanf("%d", &p[i]);


	 }


	 printf("\n Thank you!\n");
 

	print_array(p, size);
		 bubble_sort(p, size);
		print_array(p, size);
		find_unique(p, size);
		 add_new(p, size);
		linear_search (p, size);
		 max_element(p, size);
		miniumum_elements(p, size);
		 remove_element(p, size);
 

	free(p);

	 return 0;

}

/*************/

void bubble_sort(int *p, int size)
 
{

	 int i, j, temp;


	 for(j=0; j<size-1; j++)

	 for(i=0; i<size-1; i++)
 
		if(p[i]>p[i+1])

		 {

			 temp=p[i];

			 p[i]=p[i+1];
 
			p[i+1]=temp;

		 }

}

/*************/

void print_array(int *p, int size)
 
{

	 int i;


	 printf("\n Printing array \n");

	 for(i=0; i<size; i++)
 
		printf("%d ", p[i]);

	 printf("\n");

}

/*************/
 
void find_unique(int *p, int size)

{

	 int max = p[0];

	 int i;

	 int *c;
 
	int (c-size);

	 for (i=1; i<size; i++)

		 if (max<p[i])
 
			max=p[i];

	 printf("%d \n", max);

}

/*************/

void add_new (int *p, int size)
 
{

	 int i;

	 int target;

	 int position;

 
	printf("What number do you want to add? \n");

	 scanf("%d",&target);

	 printf("indicate position \n");
 
	scanf("%d", &position);

}

/*************/

void linear_search (int *p, int size)

{

	 int search;
	 int i;
 

	printf("Enter the number to search\n");

   scanf("%d",&search);

 
   for ( i = 0 ; i<size ; i++ )
 
   {

      if ( array[i] == search )    

      {

         printf("%d is present at location %d.\n", search, i+1);

	 count++;
 
      }

   }

   if ( count == 0 )

      printf("%d is not present in array.\n", search);

   else

      printf("%d is present %d times in array.\n", search, count);
 
}

/*************/

void max_element(int *p, int size)

{

	 int i;

	 int maxium;
 

	for (i = 0; i < size; i++)

    scanf("%d", &array[i]);

  maximum = array[0];

  for (i = 1; i < size; i++)
 
  {

    if (array[i] > maximum)

    {

       maximum  = array[i];

       location = i+1;

    }

  }

  printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
 
}

/*************/

void miniumum_elements(int *p, int size)

{

	 int i;

	 int minimum;
 
	int location;


	 for ( i = 0 ; i < size ; i++ )

        scanf("%d", &array[i]);
 
    minimum = array[0];

    for ( i = 1 ; i < size ; i++ ) 

    {

        if ( array[i] < minimum ) 

        {

           minimum = array[i];
 
           location = i+1;

        }

    } 

    printf("Minimum element is present at location %d and it's value is %d.\n", location, minimum);

}
 
/**************/

void remove_element(int *p, int size)

{

	 int i;

	 int size;

 	int position;


	 for ( i = 0 ; i < size ; i++ )

      scanf("%d", &array[i]);
 
 
   printf("Enter the location where you wish to delete element\n");

   scanf("%d", &position);

   if ( position >= size+1 )

      printf("Deletion not possible.\n");
 
   else

   {

      for ( i = position - 1 ; i < size - 1 ; i++ )

         array[i] = array[i+1];

      printf("Resultant array is\n");

      for( i = 0 ; i < size - 1 ; i++ )

         printf("%d\n", array[i]);

   }

}


Any help would be greatly appreciated, I feel like crying just looking at my code.
Last edited on
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.

Also get rid of the blank lines and use proper indentation. Your code is unreadable.

Some obvious problems:
1) You don't declare functions before you call them.
2) You use variables without declaring them (array, count, maximum, location)
3) In remove_element, size is both an argument and a locally declared variable.
Thank you so much for answering my question, sorry about the format I'm pretty new to this.
1. I thought I declared my functions in lines 74 to 82.
2. To declare my variables would I put them into my function code, like so ? int array;
3. Would removing size from being a locally declared variable and leaving it as an argument fix this problem's function?
Topic archived. No new replies allowed.