Question

In: Computer Science

C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the...

C Language
NO ARRAY UTILIZATION OR SORTING

Create a .txt file with 20 integers in the range of 0 to 100. There may be repeats. The numbers must
not be ordered/sorted. The task is to find and print the two smallest numbers. You must accomplish
this task without sorting the file and without using arrays for any purpose.


It is possible that the smallest numbers are repeated – you should print the number of occurrences of
the two smallest numbers.


The reading of the numbers, the comparisons, the printing of the smallest numbers should all be in
separate functions. You MUST employ PASS BY REFRENCE for this problem. The counting of the two
smallest numbers occurrences functionality must be implemented with static counter.

Solutions

Expert Solution

Program Code Screenshot



Program Sample Console Input/Output Screenshot



Program Code to Copy

#include <stdlib.h>
#include <stdio.h>

void updateMinimum(int value, int *min1, int *min2, int *min1_count, int *min2_count)
{
    //update count if this is a repeated element
    if (*min1 == value)
    {
        (*min1_count)++;
        return;
    }
    else if (*min2 == value)
    {
        (*min2_count)++;
    }
    //initialize element if its the first element being read
    else if (*min1_count == 0)
    {
        *min1 = value;
        *min1_count = 1;
    }
    else if (*min2_count == 0)
    {
        //make sure the min1 is less than min2 always
        if (*min1 < value)
        {
            *min2 = value;
            *min2_count = 1;
        }
        else
        {
            *min2 = *min1;
            *min2_count = *min1_count;
            *min1 = value;
            *min1_count = 1;
        }
    }
    else
    {
        //update values on min1 and min2
        if (value < *min1)
        {
            *min2_count = *min1_count;
            *min2 = *min1;
            *min1_count = 1;
            *min1 = value;
        }
        else if (value < *min2)
        {
            *min2_count = 1;
            *min2 = value;
        }
    }
}
int read_num(FILE *file)
{
    int val;
    fscanf(file, "%d", &val);
    return val;
}
void print_result(int min1, int min2, int min1_count, int min2_count)
{
    //print theṇ results
    printf("Minimum element 1: %d occured %d times\n", min1, min1_count);
    printf("Minimum element 2: %d occured %d times\n", min2, min2_count);
}
int main(int argc, char *argv[])
{
    //declare variable to read from file
    FILE *file = fopen("input.txt", "r");

    //declare variable to read integers
    int val;

    //declare static counter variables to store two minimum numbers
    //and there count
    static int min1 = 101, min2 = 101;
    int min1_count = 0, min2_count = 0;

    //read 20 integers from the file
    for (int i = 0; i < 20; i++)
    {
        int val = read_num(file); //separate function to read file

        //passing variables by reference
        updateMinimum(val, &min1, &min2, &min1_count, &min2_count); //separate function for comparisions
    }

    //separate function to print result
    print_result(min1, min2, min1_count, min2_count);
}

Related Solutions

c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
C++ Language Create Login Register program without open .txt file simple program also if you can...
C++ Language Create Login Register program without open .txt file simple program also if you can hide password input option menu 1) Login 2) Register 3) exit
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...
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
Create a c++ program that: Create an input file using notepad ( .txt ). When testing...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File” marker. Input date...
Write a recursive method to sum the values in an array of integers. Create a file...
Write a recursive method to sum the values in an array of integers. Create a file ArraySum.java and add the recursive method public int sumOfArray (). Use the driver class ArraySumDriver.java to populate your array and demonstrate that your method works. ////ArraySumDriver.java/////// public class ArraySumDriver { private final static int ARRAY_SIZE = 6; /** * @param args */ public static void main(String[] args) { int index = 0; Integer[] myArray = new Integer[ARRAY_SIZE]; ArraySum arraySum = new ArraySum(); myArray[index++] =...
In C++ First create the txt file given below. Then complete the main that is given....
In C++ First create the txt file given below. Then complete the main that is given. There are comments to help you. An output is also given You can assume that the file has numbers in it Create this text file: data1.txt -59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42 Main #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; const int MAXSIZE = 100; // Prototypes int...
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance.
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance. Use the following file:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT