0
votes

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.

3
int nums[N]; - if you want a variable length array - you will probably need to malloc() it. Also, try to add \n to your "checks".amit
When you print for quick-and-dirty tracing, add \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 Kalinichenko
@amit I never learned about malloc(), what do you mean? @dasblinkelight you mean something like printf("1"); \n ?JA3N
printf("%d", &N); is undefined behaviour, and nothing can be said about the code past this point. Perhaps you want printf("%d", N);?Kerrek SB
@JA3N: he means: printf("1\n");. malloc() is used when you need an array of size that will be known only on run-time, since N is unkown to the compiler!amit

3 Answers

5
votes
if (i=0 || nums[i] < n) n = nums[i];

you are assigning i = 0, so the loop never realy advances! You probably wanted i == 0. This is causing the infinite loop.

Other issue: int nums[N]; - if you want an array of dynamic [determined in run-time] size, you will probably need to malloc() it.

Update: note that int nums[N] is valid in C99, so if your assigment is assuming C99 you should not worry about this issue. Otherwise - malloc() will be needed:

int* nums = (int*) malloc(sizeof(int) * N)

And don't forget to free(nums) before the program ends, or you will get memory leak.

1
votes
if (i=0 || nums[i] < n) n = nums[i]; 

This is the culprit. You are making an assignment i=0 when you should do a comparison : i==0

Your loop goes to infinity because everytime you are i to 0.

Moreover, the code you gave us, gave me an error, because you were creating new variables during runtime.

#include <stdio.h> 
#include "stdlib.h"
int main(void) { 
    int N, a, n; 
    int x=0; 
    int t=0; 
    int i; 
    int *nums;

    printf("1"); //Check 
    scanf("%d", &N); 
    printf("2"); //Check 

    nums = malloc(N*sizeof(int));

....

1
votes

There are numerous problems with this code.

You mistook assignment for comparison:

i=0

sets i to zero. You probably meant

i==0

which checks whether i is equal to zero.

You should check the value returned by scanf(). When data read by scanf() doesn't fit the format you specified it leaves the data in the buffer and the next call to scanf() sees the same data again.

The reason that the first printf() doesn't print anything is most likely that you are not printing any newline. Note that on some output devices like terminals output is line-buffered.

You are passing a pointer to a variable to printf(), but your format specifies %d. You probably meant the variable itself, not a pointer to it.

Also, if you need an array of length dependent on a value known only at runtime, you need to allocate it on the heap, e.g. using malloc().