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;...

  • 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...

  • 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;...

  • 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 =...

  • // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); //...

    // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); // finds average of all //grades int findHighest (int [], int); // finds highest of all //grades int findFirstFail( int[]); int main() { int grades[100]; // the array holding 100 grades. int numberOfGrades; // the number of grades read. int pos; // index to the array. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int inderOfFail; //...

  • #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> #include <string> #include <vector> #include <sstream> using namespace...

    #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> #include <string> #include <vector> #include <sstream> using namespace std; void DisplayMenu() {    cout << "1. E games\n";    cout << "2. T games\n";    cout << "3. M games\n";    cout << "4. Total Games\n";    cout << "5. Exit\n"; } double total(double egames, double tgames, double mgames) {    int totalgames = egames + tgames + mgames;    cout << "There are " << totalgames << " games\n";    return...

  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

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
ADVERTISEMENT