Question

Make sure to create the code with the given functions, and do not overlook them, do...

Make sure to create the code with the given functions, and do not overlook them, do not create your own functions. This is an assignment that I did on my own and that's passed due, therefore, for educational purposes, I am interested to know what are other ways to properly program this. Please follow instructions as I am doing my best to fully understand every crucial detail in order to become a better programmer who is able to come up with different ways of programming. Your help is highly appreciated. Make sure this is created using a menu. Thank you.

Code is for C++

const int MAX_SIZE = 100;
double theArray[MAX_SIZE];

const string INPUT_FILE = "INPUT.DAT";
const string OUTPUT_FILE = "OUTPUT.DAT";

/* Pass in the INPUT_FILE, create the file with randomly from 1 to 100 generated double value, return the number of generated elements */
void CreateDataFile(const string inputFileName, int& numberOfElements);

/* Pass in the INPUT_FILE, read all elements and store them in the partially filled array */
void ReadDataFileIntoArray(const string inputFileName, double theArray[], int numberOfElements);

/* Display all elements in the partially filled array */
void DisplayAllElementsOfArray(const double theArray[], int numberOfElements);

/* Sort the partially filled array in ascending order */
void SortArrayAscendingOrder(double theArray[], int numberOfElements);

/* Sort the partially filled array in descending order */
void SortArrayDescendingOrder(double theArray[], int numberOfElements);

/* Display the smallest and largest values of the partially filled array */
void DisplaySmallestLargestElementOfArray(const double theArray[], int numberOfElements, double& smallest, double& largest);

/* Display the calculated average of elements in the partially filled array */
void DisplayAverageOfArray(const double theArray[], int numberOfElements, double& average);

/* Write the values of the smallest, largest and calculated average to the OUTPUT.DAT */
void WriteDataFile(string outputFileName, double smallest, double largest, double average);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: The language used is C++.

Code:

#include<bits/stdc++.h>
using namespace std;

const int MAX_SIZE = 100;
double theArray[MAX_SIZE];

const string INPUT_FILE = "INPUT.DAT";
const string OUTPUT_FILE = "OUTPUT.DAT";

/* Pass in the INPUT_FILE, create the file with randomly from 1 to 100 generated double value, return the number of generated elements */
void CreateDataFile(const string inputFileName, int& numberOfElements)
{
   ofstream file;
   file.open(INPUT_FILE);
  
   srand(time(0));
  
   numberOfElements = rand()%MAX_SIZE + 1;
  
   for(int i=0;i<numberOfElements;i++){
       double random_double = ((double)rand()/RAND_MAX) * (MAX_SIZE - 1) + 1;
       file<<random_double<<"\n";
   }
  
   file.close();
}

/* Pass in the INPUT_FILE, read all elements and store them in the partially filled array */
void ReadDataFileIntoArray(const string inputFileName, double theArray[], int numberOfElements)
{
   ifstream file;
   file.open(INPUT_FILE);
  
   double num;
  
   for(int i=0;i<numberOfElements;i++){
       file>>num;
       theArray[i] = num;
   }

   file.close();
}

/* Display all elements in the partially filled array */
void DisplayAllElementsOfArray(const double theArray[], int numberOfElements)
{
   cout<<"The array elements are:\n";
   for(int i=0;i<numberOfElements;i++)
       cout<<theArray[i]<<" ";
   cout<<endl;
}

/* Sort the partially filled array in ascending order */
void SortArrayAscendingOrder(double theArray[], int numberOfElements)
{
   for(int i=0;i<numberOfElements;i++){
       int min_index = i;
       for(int j=i;j<numberOfElements;j++){
           if(theArray[j] < theArray[min_index])
               min_index = j;
       }
       swap(theArray[i], theArray[min_index]);
   }
}

/* Sort the partially filled array in descending order */
void SortArrayDescendingOrder(double theArray[], int numberOfElements)
{
   for(int i=0;i<numberOfElements;i++){
       int max_index = i;
       for(int j=i;j<numberOfElements;j++){
           if(theArray[j] > theArray[max_index])
               max_index = j;
       }
       swap(theArray[i], theArray[max_index]);
   }
}

/* Display the smallest and largest values of the partially filled array */
void DisplaySmallestLargestElementOfArray(const double theArray[], int numberOfElements, double& smallest, double& largest)
{
   smallest = theArray[0];
   largest = theArray[0];
  
   for(int i=1;i<numberOfElements;i++){
       if(theArray[i] < smallest)
           smallest = theArray[i];
   }
  
   for(int i=1;i<numberOfElements;i++){
       if(theArray[i] > largest)
           largest = theArray[i];
   }
  
   cout<<"The smallest value in the array is "<<smallest<<" and the largest value in the array is "<<largest<<"\n";
}

/* Display the calculated average of elements in the partially filled array */
void DisplayAverageOfArray(const double theArray[], int numberOfElements, double& average)
{
   double sum = 0;
   for(int i=0;i<numberOfElements;i++)
       sum += theArray[i];
  
   average = sum/numberOfElements;
  
   cout<<"The average of the array elements is "<<average<<endl;
}

/* Write the values of the smallest, largest and calculated average to the OUTPUT.DAT */
void WriteDataFile(string outputFileName, double smallest, double largest, double average)
{
   ofstream file;
   file.open(OUTPUT_FILE);
  
   file<<"The smallest element in the array is "<<smallest<<"\n";
   file<<"The largest element in the array is "<<largest<<"\n";
   file<<"The average of the array is "<<average<<"\n";
  
   file.close();
}

int main()
{
   int numberOfElements;
   double largest, smallest, average;
   CreateDataFile(INPUT_FILE, numberOfElements);
   ReadDataFileIntoArray(INPUT_FILE, theArray, numberOfElements);
  
   DisplayAllElementsOfArray(theArray, numberOfElements);
  
   SortArrayAscendingOrder(theArray, numberOfElements);
   cout<<"\nAfter sorting in ascending order\n";
   DisplayAllElementsOfArray(theArray, numberOfElements);
  
   SortArrayDescendingOrder(theArray, numberOfElements);
   cout<<"\nAfter sorting in descending order\n";
   DisplayAllElementsOfArray(theArray, numberOfElements);
  
   cout<<endl;
   DisplaySmallestLargestElementOfArray(theArray, numberOfElements, smallest, largest);
  
   cout<<endl;
   DisplayAverageOfArray(theArray, numberOfElements, average);
  
   WriteDataFile(OUTPUT_FILE, smallest, largest, average);
  
   return 0;

}

Code Snippets:

Sample Output:

Console:

OUTPUT.DAT file:

Add a comment
Know the answer?
Add Answer to:
Make sure to create the code with the given functions, and do not overlook them, do...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...

    C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...

  • I have to a C++ program I need help with. Can you break it into the...

    I have to a C++ program I need help with. Can you break it into the two steps provided below. Suppose MAX_SIZE is a global constant int variable that has been declared and initialized to an appropriate positive value. 1. int maxValue(const array<int, MAX_SIZE>&, int); is the prototype for a function that returns the value of the largest element in the array parameter. The array may be only partially filled. The int parameter indicates how many items are actually in...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • First create the two text file given below. Then complete the main that is given. There...

    First create the two text 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: data.txt (remember blank line at end) Mickey 90 Minnie 85 Goofy 70 Pluto 75 Daisy 63 Donald 80 Create this text file: data0.txt (remember blank line at end) PeterPan 18 Wendy 32 Michael 28 John 21 Nana 12 Main #include...

  • Hello, I have written a code that I now need to make changes so that arrays...

    Hello, I have written a code that I now need to make changes so that arrays are transferred to the functions as pointer variable. Below is my code I already have. #include<iostream> #include<string> #include<iomanip> using namespace std; //functions prototypes void getData(string id[], int correct[], int NUM_STUDENT); void calculate(int correct[], int incorrect[], int score[], int NUM_STUDENT); double average(int score[], int NUM_STUDENT); void Display(string id[], int correct[], int incorrect[], int score[], double average, int NUM_STUDENT); int findHigh(string id[], int score[], int NUM_STUDENT);...

  • Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same...

    Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same way it is used in the GeneralizedSelectionSort. You should use the attached ComparableDemo to test your program. public class ComparableDemo { public static void main(String[] args) { Double[] d = new Double[10]; for (int i = 0; i < d.length; i++) d[i] = new Double(d.length - i); System.out.println("Before sorting:"); int i; for (i = 0; i < d.length; i++) System.out.print(d[i].doubleValue( ) + ", ");...

  • #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...

    #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...

  • Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the...

    Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the size of an array. Then create an integer array of that exact size. Ask the user to enter a maximum value and then write a loop to fill the array with random numbers with value in the range of 1 to the maximum value. For example, if the maximum value is 100, random numbers must have value 1 to 100 inclusive. Input size of...

  • Please follow the instructions below to create a code. The instructions are broken down into a...

    Please follow the instructions below to create a code. The instructions are broken down into a few steps. Create the following functions along with a driver for each one confirming it works. The driver needs to test the function with at least 5 different inputs, although more is always better. Be sure to use the function display_startup_banner( ) in all of your drivers. Please create separate files for each code. That is, if you're creating a function called learning_online_is_a_trip( )...

  • THIS IS FOR C++ PROGRAMMING USING VISUAL STUDIO THE PROGRAM NEEDS TO BE IN C++ PROGRAMMING #inclu...

    THIS IS FOR C++ PROGRAMMING USING VISUAL STUDIO THE PROGRAM NEEDS TO BE IN C++ PROGRAMMING #include "pch.h" #include #include using namespace std; // Function prototype void displayMessage(void); void totalFees(void); double calculateFees(int); double calculateFees(int bags) {    return bags * 30.0; } void displayMessage(void) {    cout << "This program calculates the total amount of checked bag fees." << endl; } void totalFees() {    double bags = 0;    cout << "Enter the amount of checked bags you have." << endl;    cout <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT