This program takes the first number in a file and indicates how many numbers are going to be after it, then does various other things with the numbers that follow.
It seems like scanf is causing an infinite loop when trying to read from the file. WHen I run the program not even the check at 1 works
Here is the code:
#include <stdio.h>
int main(void) {
int N, a, n;
int x=0;
int t=0;
printf("1"); //Check
scanf("%d", &N);
printf("2"); //Check
int nums[N];
int i;
printf("%d", &N); //Check
for (i=0; i<N; i++)
{
scanf("%d", &nums[i]);
t+=nums[i];
if (nums[i] > x) x=nums[i];
if (i=0 || nums[i] < n) n = nums[i];
}
a = t/N;
printf("Number Processed: \t%d\n", &N);
printf("Maximum: \t%d\n", &x);
printf("Minimum: \t%d\n", &n);
printf("Total: \t%d\n", &t);
printf("Average: \t%d\n", &a);
}
The way i run the program is
gcc -lab16
./a.out <in1
where in1 is text and has the numbers
7
6
-30
90
3903
-934
443
445
Thanks for your time.
int nums[N];
- if you want a variable length array - you will probably need tomalloc()
it. Also, try to add\n
to your "checks". – amit\n
at the end to force the flush, otherwise the output will not appear immediately. Make sure that your input file has an end-of-line marker on the last line. – Sergey Kalinichenkoprintf("1"); \n
? – JA3Nprintf("%d", &N);
is undefined behaviour, and nothing can be said about the code past this point. Perhaps you wantprintf("%d", N);
? – Kerrek SBprintf("1\n");
. malloc() is used when you need an array of size that will be known only on run-time, sinceN
is unkown to the compiler! – amit