Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m working on a C program and I was wondering if I can input less numbers into

ID: 3597828 • Letter: I

Question

I'm working on a C program and I was wondering if I can input less numbers into an array I initialized. I have tried multiple attempts but none of it seeems to be working. Example, I initialize a variable as; a[14]; and I want to scan user input with a maximum of 14, AT THE SAME TIME I want the array to be able to take less than 14 numbers if the user happens to input less than 14. If this is not possible, how can I scan user input into an array without knowing the size. IMPORTANT: I can't ask the user to input the size of an element because the data is going to be read from a text file.

Explanation / Answer

There are two ways of doing it.

int a[14];
int i = 0;
while (i < 14 && (fscanf(fp,"%d", a[i]) != EOF)) {
i = i + 1;
}

I will strictly advised to go for first approach as second is more work and your problem doesn't seems to fit for the second one.