Question


1. Write a function to read into an array all the integers in an input file of unknown size. When the function ends, the calling program has the amay filled with values and a variable whose value is the actual number of read in. 2. Write a function to output all the integers in an array to an output file, with 10 values per line. Values on a line are separated by a single space. 3. Write a function similar to the function indexLargestElement (page 535 in Example 8- 60 except that this function is called indexSmallestElement and it retums the index of the minimum value found in an array. 4. Write a complete C++ program which includes both functions l. and 2. above, as well as index LargestElement and indexSmallestElement. The main program will declare variables and call functions to get array input, find and output to a file the maximum value in the array, find and output to the file the minimum value in the array, and output all the values in the array. Output will have appropriate labels.

How would answer question #4 of this ?

#1 and 2

#include<iostream>
#include <fstream>

using namespace std;

void read(int arr[])
{
   string line;
   ifstream myfile("input.txt");
   int i = 0;
   if (myfile.is_open())
   {
       while (getline(myfile, line))
       {
           arr[i];
           i++;
       }
       myfile.close();
   }
}
void output(int arr[])
{
   ofstream myfile("output.txt");
   if (myfile.is_open())
   {
       int i = 0;
       int k = 0;
       while (i<10)
       {
           myfile << arr[k] << " ";
           k++;
           i++;
       }

       myfile.close();
   }
}
int main(int argc, char argv[])
{
   int arr[100];
   read(arr);
   output(arr);
   return 0;
}

#3

int indexLargestElement(int[] arr)
{

   int maxIndex = 0;

   for (int i = 1; i<arr.length; i++) {

       if (arr[maxIndex] < arr[i])

           maxIndex = i;

   }

   return maxIndex;

}

int indexSmallestElement(int[] arr)
{

   int minIndex = 0;

   for (int i = 1; i<arr.length; i++) {

       if (arr[minIndex] > arr[i])

           minIndex = i;

   }

   return minIndex;

}

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

#include <iostream>

void read(int arr[]);

void output(int arr[]);

int indexLargestElement(int arr[]);

int indexSmallestElement(int arr[]);

int main()

{

int arr[100],n,choice;

cout<<"enter size of Array";

cin>>n;

do

{

cout<<"\n MENU";

cout<<"\n 1.Accept elements of array";

cout<<\n 2.output to a file

cout<<\n 3.output maximun element to a file

cout <<\n 4.output mininum element to a file

cout<<\n 5.exit

cout<<"\n enter your choice 1-5:";

cin>>choice;

switch(choice)

{

case 1:read(arr);

break;

case 2:output(arr);

break;

case 3: int x;

x=indexLargestElement(arr);

ofstrean myfile2;

myfile2.open(outputmax.txt);

myfile2<<x;

myfile.close;

break;

case 4 :int x;

x=indexSmallestElement(arr);

ofstream myfile2

myfile2.open(outputmin.txt);

myfile2<<x;

myfile2.close;

break;

case 5:break.

defult:cout<<"invalid choice";

}

} while(choice!=5);

return 0;

}

// write your functions here.....

Add a comment
Know the answer?
Add Answer to:
How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace...
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
  • Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std;...

    Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std; int read_function(int array[], int, int); int main() { int array[300], numba; read_function(array[300]); cout << "Enter a whole number between 2-20: " << endl; cin >> numba; read_function(numba); return 0; } int read_funtion (int arr[300], int num, int Values) { int sum=0; ifstream array_file; array_file.open("Lab8.dat"); for(int i=0; i < 300; i++) {   array_file >> arr[i];   cout << arr[i];   sum += i; } cout << sum;...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> //...

    in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...

  • Having issues using binary search on a pointer array. 1. Write 1000 random ints to file...

    Having issues using binary search on a pointer array. 1. Write 1000 random ints to file 2. Binary Search to find if number exists in element from file. int main() { ArrayActions action;   int count; int userInput; int num = 1000; int array[num]; ofstream myFile ("/Users/chan/Desktop/LANEY_CIS27/Assignemtn2_CIS27/Assignemtn2_CIS27/File.txt");   //Set srand with time to generate unique random numbers srand((unsigned)time(0));   //Format random num up to 999 for(count = 0; count < num; count++) { array[count] = rand() % 1000; }          //Condition...

  • Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person {...

    Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person { public: Person() { setData("unknown-first", "unknown-last"); } Person(string first, string last) { setData(first, last); } void setData(string first, string last) { firstName = first; lastName = last; } void printData() const { cout << "\nName: " << firstName << " " << lastName << endl; } private: string firstName; string lastName; }; class Musician : public Person { public: Musician() { // TODO: set this...

  • can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...

    can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block {    std::string word;    int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) {    string filename="input.txt";    //declare array of struct word_block    word_block arr[SIZE];    int count = 0;    if (argc < 2)    {        cout << "Usage: " << argv[0] << "...

  • //Find two smallest integers #include <iostream> #include <string> using namespace std; // implement printArray here //...

    //Find two smallest integers #include <iostream> #include <string> using namespace std; // implement printArray here // implement findTwoSmallest here // DO NOT WRITE ANY CODE BELOW THIS LINE (EXCEPT FOR TESTING - REMOVE BEFORE SUBMITTING) int main() {     int n;     int arr[100];        cout << "Enter number of integers: ";     cin >> n;        cout << "Enter " << n << " integers: ";       for(int i = 0; i < n; i++) {        ...

  • This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType...

    This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *next;    double value; }; void createList(nodeType*& first, nodeType*& last, ifstream& inf); void printList(nodeType* first); int main() {    nodeType *first, *last;    int num;    ifstream infile;    infile.open("InputIntegers.txt");    createList(first, last, infile);    printList(first);    infile.close();    system("pause");    return 0; } void createList(nodeType*& first, nodeType*& last, ifstream& infile) {    int number;...

  • use c++ and complete this lab #include <iostream> using namespace std; class FibObj { public: void...

    use c++ and complete this lab #include <iostream> using namespace std; class FibObj { public: void setSize() { cout << "Enter an integer larger than 2: "; cin >> size; } int getSize() { return size; } void fibTerms(int x) // Write a recursive function { } explicit FibObj() // Default constructor was made explicit to avoid possible implicit type conversion { setSize(); int *arr; arr = new int[getSize()]; arr[0] = 1; arr[1] = 1; // Call recursive function here...

  • Step 4: Write a Sum Function Since we can write, compile and run simple c files,...

    Step 4: Write a Sum Function Since we can write, compile and run simple c files, lets add a bit to make a program that will sum an entire array of integers. To do this we are going to simply overwrite the main.c file to include the new function. The sum function below is not complete and must be finished before the program will execute properly. %%file main.c #include <stdio.h> int sum(int array[], int arrayLength) {     int i =...

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