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

(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.
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 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,...
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...
You need to write a program that reads in two integers from cin and outputs an...
You need to write a program that reads in two integers from cin and outputs an horribly inefficent calculation of the median value. First count from the first number to the second, but stop one short. Then, count from that number back towards the first, again stopping one short. Continue until you reach a single number. Enter 3 and 9 solution: 3456789 987654 45678 8765 567 76 6
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The program then calls the foo function. The function will modify the array x of size n by doubling any values in x that are less than the threshold. The function will count how many values were changed and how many were not changed via two reference parameters. The function signature is: void foo(int n, int x[], int threshold, int *pChanged, int *pUnchanged); The function...
Code in C Write a program whose inputs are three integers, and whose outputs are the...
Code in C Write a program whose inputs are three integers, and whose outputs are the largest of the three values and the smallest of the three values. Ex: If the input is: 7 15 3 the output is: largest: 15 smallest: 3 Your program must define and call the following two functions. The LargestNumber function should return the largest number of the three input values. The SmallestNumber function should return the smallest number of the three input values. int...
C++ programing Write a main function that  reads a list of integers from a user, adds to...
C++ programing Write a main function that  reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation.
Convert the following text into a code format: The program reads an unspecified number of integers...
Convert the following text into a code format: The program reads an unspecified number of integers until a zero is entered. While the program reads each number it counts the number of positive numbers and the number of negative numbers that have been entered and sum up the values of the numbers entered. After the user enters zero, the program computes the average of the input values, not counting the zero. At the end, the program displays the average, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT