Home > Software design >  Why this program prints 0?
Why this program prints 0?

Time:12-29

I am supposed to write a program that prints the minimum value from vector.This is what i tried. It only prints 0. I tried to change the sign both ways but it doesnt work.

#include <stdio.h>

int read(int v[], int size)
{
    int i = 0;
    do
    {
        scanf("%i", &v[i]);
        i  ;
    } while (v[i-1] != 0 && i < size);
    int n = i;
    return n;
}
   
int minim(int v[], int n)
{
    int m;
    m = v[0];
    int i;
    for (i = 1; i <= n-1; i  )
    {
        if (v[i] < m)
        {
            m = v[i];           
        }     
    }
    return m;  
}

int main()
{
    int arr[100];
    int n = read(arr, 100);
      
    int min = minim(arr, n);
    printf("\nMinimum vrom vector is %i\n", min);
    return 0;
}   

CodePudding user response:

Since your scanf loop (I'd recommend staying away from function names like read, which are part of the C standard, even if you didn't include unistd.h) ends when 0 is entered, you need to include a check at the end to decrement the size of the array if 0 is the last entry. Basically, replace everything after your do-while loop with this:

if (v[i - 1]) {
    return i;
}
return --i;

This will return i if all 100 elements are non-zero, otherwise it will decrement to remove the 0 from your array before returning. No need to declare int n=i just to instantly return n.

Edit: I saw your comment that it worked properly for finding the maximum. This is because you almost certainly entered a number into the array that's greater than 0, so adding 0 at the end would not affect the maximum number. Try finding the max again, but only enter negative numbers. The result will be 0.

CodePudding user response:

read() uses a 0 entry to terminate reading more input. Yet that 0 is included in the array and counts toward the array length as part of the return value.

Instead, only increment the array count when input was numeric and non-zero.

int read(int v[], int size) {
    int i = 0;
    while (i < size) {
        // Also test if valid numeric input was read.
        if (scanf("%i", &v[i]) != 1) {
            break;
        }
        // Stop if a 0 was read
        if (v[i] == 0) {
            break;
        }
        // Now increment
        i  ;
    }
   
    return i;
}

Could use shorter code, yet it is less readable. Best to code for clarity.

int read(int v[], int size) {
    int i = 0;
    while (i < size && scanf("%i", &v[i]) == 1 && v[i]) {
        i  ;
    }
    return i;
}

CodePudding user response:

The test condition

while (v[i-1] != 0

checks whether the last element read was 0, after it was successfully processed and converted by scanf (but you never checked the return value). The 0 is then included in the array, and will always be the minimum unless you enter a non-negative number.

Here's the working code:

#include <stdio.h>
#include <stdlib.h> 
    
static size_t read (int v[], size_t size)
{
      size_t i = 0;
      do
      {
          /* Check if scanf was successful */ 
          if (scanf("%i",&v[i]) != 1) 
          {
              fprintf(stderr, "Error: Invalid input.\n"); 
              return EXIT_FAILURE;
          }
          i  ;
          } while (v[i - 1] != 0 && i < size);

      /* Also check whether the last element read was 0 */ 
      if (v[i - 1] == 0) {
          i--;
      } 
      return i;
 }
       
static int minim (int v[],size_t n)
{
     int min;
     min = v[0];
           
     for(size_t i = 1; i < n; i  ) {
            if (v[i] < min) {
                min = v[i]; 
            }        
     }
     return min;  
}

int main(void)
{
     int arr[100];
     size_t n = read(arr,100);
                
     int min = minim(arr,n);
     printf("\nMinimum vrom vector is %i\n", min);
     return EXIT_SUCCESS;
 }

I made some minor changes to it. Though I'm not satisfied with the design.

Sample I/O:

20
8
18
60
39
56
0
Minimum vrom vector is 8
  • Related