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;...
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 { 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, 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); // 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 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; 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 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....