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”...
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...
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:...
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
(C++) Create a data file and name it "input.txt". manually save 10 integers into the file....
(C++) Create a data file and name it "input.txt". manually save 10 integers into the file. Write a program to read the data and calculate the average of events and odds, separately. Print out the average values.
C++ sort a file with 140 integers where only 20 integers maybe placed into memory at...
C++ sort a file with 140 integers where only 20 integers maybe placed into memory at any one time Main idea: break data into blocks, sort blocks into runs, merge runs less. Call inFile1 our source file (the one with the initial 140 records to be sorted. You will also need an inFile2, and 2 other files outFile1 and outFile2. Break the file into blocks of size 20: in this case there will be 7 blocks ( 140/20 ) Sort...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT