Question

In: Computer Science

Code is in C Write a program that reads integers from stdin. Once it reaches the...

Code is in C

Write a program that reads integers from stdin. Once it reaches the

* end of the input, it prints the smallest absolute value among those

* of the numbers it read.

*

* For example, if

* 4, 6 -3, 3, -2, 13, -4

* are read from stdin, the program should print 2.

*

* If the end of file is reached before any integer is seen, the

* number printed should be INT_MAX (defined in limits.h).

*

* The program should return 0 if successful and -1 if there are

* errors in the input.

*

* You may assume that the absolute value of all negative integers

* successfully read by your program may be stored in an int.

Solutions

Expert Solution

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int solve(){

    int num ; // the variable where input is going to be taken upto End Of File
    int smallestAbsValue = INT_MAX; // the variable declared as the smallestAbsVal which will store the smallest Absolute vlaue till the EOF

    int error = 0;

    while(scanf("%d", &num)!=EOF){ // iterating till the EOF (end of file)
    
        if(abs(num) < smallestAbsValue){ // checking the codition for the final Ans
            smallestAbsValue = abs(num); // upadte the answer variable
        }
    }

    printf("Smallest Absolute value is %d ", smallestAbsValue);
    
    return error;
    // if there is an error it should retunr -1
}
int main() {
    
    
    int flag = solve();
    if(flag ==0 ) printf("\nProgramme executed successfully");
    else if(flag == -1) printf("There is Error input");
    return 0;
}


Related Solutions

1) Using either emacs or vi write a C program that reads 100 integers from stdin...
1) Using either emacs or vi write a C program that reads 100 integers from stdin into a 2-dimensional 10x10 array. The program will read one additional integer which is used to multiply each element of the array. The final array should be printed to stdout as follows: Run 1: Enter 100 integers: 1 2 3 4 5 6 7 8 9 10 11 …. 100 Enter factor: 3 Result: 3 6 9 12 … 30 33 36 39 42...
Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
write these programs in c++ using getch() function Write a program that reads 20 integers from...
write these programs in c++ using getch() function Write a program that reads 20 integers from a user using a while loop and determines and prints whether each of those integers is an even number. Write a program that uses a while statement to read integers from a user and prints the sum, average, and largest of these numbers. The input should terminate if the user enters -1.
Do not use arrays and code a program that reads a sequence of positive integers from...
Do not use arrays and code a program that reads a sequence of positive integers from the keyboard and finds the largest and the smallest of them along with their number of occurrences. The user enters zero to terminate the input. If the user enters a negative number, the program displays an error and continues. Sample 1: Enter numbers: 3 2 6 2 2 6 6 6 5 6 0 Largest Occurrences Smallest Occurrences 6 5 2 3. Do not...
Write a C program that reads three integers and then prints them in the order read...
Write a C program that reads three integers and then prints them in the order read and reversed. Use four functions: main, one to read the data, one to print them in the order read, and one to print them reversed.
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. Remember, try not to do the entire job all at once! First try input of a single number and make sure it works....
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop Remember, try not to do the entire job all at once! First try input of a single number and make sure it works....
C++ code please: Write a program that first gets a list of integers from input. The...
C++ code please: Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates how much to multiply the array by. Finally, print out the entire array with each element multiplied by the last input. Assume that the list will always contain less than 20 integers. Ex: If the input is 4 4 8 -4 12...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT