1. Create a file that contains 20 integers or download the attached filetwenty-integers.txttwenty-integers.txt reads:12 20 13 45 67 90 100 34 56 89 33 44 66 77 88 99 20 69 45 20Create a program that:2. Declares a c-style string array that will store a filename.3. Prompts the user to enter a filename. Store the file name declared inthe c-string.4. Opens the file. Write code to check the file state. If the filefails to open,display a message and exit the program.5. Declare an array that holds 20 integers. Read the 20 integers fromthe fileinto the array.6. Write a function that accepts the filled array as a parameter anddetermines the MAXIMUM value in the array.Return the maximum value from the function (the function will be oftype int).7. Print ALL the array values AND print the maximum value in the arrayusing a range-based for loop. Use informational messages.Ensure the output is readable.

Question
Asked Jan 16, 2020
4 views
1. Create a file that contains 20 integers or download the attached file
twenty-integers.txt

twenty-integers.txt reads:
12 20 13 45 67 90 100 34 56 89 33 44 66 77 88 99 20 69 45 20


Create a program that:
2. Declares a c-style string array that will store a filename.
3. Prompts the user to enter a filename. Store the file name declared in
the c-string.
4. Opens the file. Write code to check the file state. If the file
fails to open,
display a message and exit the program.
5. Declare an array that holds 20 integers. Read the 20 integers from
the file
into the array.
6. Write a function that accepts the filled array as a parameter and
determines the MAXIMUM value in the array.
Return the maximum value from the function (the function will be of
type int).
7. Print ALL the array values AND print the maximum value in the array
using a range-based for loop. Use informational messages.
Ensure the output is readable.
check_circle

Expert Answer

Step 1

The C++ program and approach are given below:

Approach: -

  • Declaring the essential headers.
  • Declaring the function arr_max to find the maximum value form the array.
  • Defining the main() function.
  • Declaring the variables as per the requirement.
  • Prompt the user to enter the name of a file and storing the file name in the c-style character array.
  • Then, the file is opened as per the name given.
  • The file is checked for existence
  • The values are taken from the file to the array.
  • The declared function is called to print the maximum value from the array.

In the loop, an auto keyword is used. An auto keyword in the loops executes the range of the loop.

Also, the name of the text file is twenty-integers.txt which stores the given values.

Step 2

Program: -

//header files

#include <stdio.h>

#include <fstream>

#include <iostream>

//namespace for using cin and cout

using namespace std;

//function having array as parameter

int arr_max(int arr[])

{

   //declaring the variable to compare the values in the array

   int final;

   //for loop to iterate over the condition

   for(int i=0;i<20;i++)

   {

       //to store max value in the array

       if(final<arr[i])

       final=arr[i];

   }

   //return final

   return final;

}

int main()

{

   //c-style character array

   char f_n[20];

   //prompt user to enter the name of file

   cout<<"Enter the name of file"<<endl;

   scanf("%s",f_n);

   ifstream f_p;

   //opening a file

   f_p.open(f_n);

   //check if file is opened or not if not print error message and exit

   if(f_p.is_open()==false)

   {

       cout<<"Error! in opening the file"<<endl;

       return 0;

   }

 &nb...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: The document must have a paragraph of at least 10 lines of text that describe you. This paragraph mu...

A: Here is the HTML code given below –&lt;html&gt;&lt;head&gt;&lt;script&gt;/* functions to get directi...

question_answer

Q: - Delete the employee who has the longest employment in the company

A: The longest employment in the company must have minimum HIREDATE so that to delete this employee dat...

question_answer

Q: Write a statement that calls the recursive function BackwardsAlphabet() with parameter startingLette...

A: Answer:The statement is used to call the recursive function “BackwardsAlphabet ()” with the “startin...

question_answer

Q: The prompt asks me to write a program that prompts the user to enter a number within the range of 1 ...

A: To input integer value from the scanner class  , you need to use Scanner  class which is declared in...

question_answer

Q: 2. Implement a function called findMinGap that will return the smallest gap betweenadjacent entries ...

A: The C++ program and approach are given below:Approach:Declaring the essential headers and the namesp...

question_answer

Q: Each device on a LAN is referred to as:     a. Router     b. Switch     c. Node     ...

A: Node: Any electronic device that is connected to a network (LAN, MAN, WAN, etc.) is said to be a nod...

question_answer

Q: Provide the code to create an input box for the country field with the default text "United States".

A: Code:&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;body&gt;Country: &lt;input name="country" value="United St...

question_answer

Q: Write a program that first gets a list of integers from input. The input begins with an integer indi...

A: Source Code#include &lt;iostream&gt;#include&lt;vector&gt;using namespace std;/* Define your functio...

question_answer

Q: What output does it produce? int funA(int x); void funB(int x); int main() { int a,b,c; a = 5; funB(...

A:  Program explanation-First, the main function will execute then funB(a) will call void funB (int n) ...