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;
}
#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.....
How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace...
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 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> // 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 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 { 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 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 // 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
{
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 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, 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 =...