hi everyone , i have worked on this 2 days now and cant find my mistakes , the program runs but it stops ( waiting ) after i enter 3 or 4 . can anyone help me where are my mistakes please? thanks allot.
the program should do the following :
user will be asked how many numbers he wants to enter ( 3 or 4 ).
3 or 4 integer values will be entered from user and saved inside an array.
the program counts how many zeros the user entered.
the program then doubles the array to the double size , where each element has a copy of it self next to it.
print out the new array at the end.
#include <stdio.h>
#include <stdlib.h>
static int cnt = 0;
/* Read <cnt> Integers into given array.
Return number of entered zeros */
int* readInts(int *arr) {
int i=0, *zeros = (int*)malloc(sizeof(int));
while(i <= cnt) {
scanf("%d", arr++);
if (arr[i++] == 0) *zeros++;
}
return zeros;
}
/* Create "double-sized" copy of array (duplicate each value)*/
void extend(int *arr, int *newarr) {
int i,j;
newarr = (int*) malloc(2 * cnt * sizeof(int*));
for (i=0,j=0; i <= cnt; i++) {
newarr[j++] = arr[i];
newarr[j++] = arr[i];
}
}
int main(void)
{
int arr[4], *zeros, i;
printf("How many integers (3 or 4)?\n");
scanf("%d", &cnt);
zeros = readInts(arr);
printf("You entered %d zero(s)!\n", *zeros);
int *newarr;
extend(arr, newarr);
for (i=0; i < cnt*2; i++) printf("%d ", newarr[i]);
printf("\n");
return 0;
}