how would i be able to get this program to run? please help? #include <iostream>
ID: 3832686 • Letter: H
Question
how would i be able to get this program to run? please help?
#include <iostream>
#include<fstream>
using namespace std;
//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<<" MENU";
cout<<" 1.Accept elements of array";
cout<< 2.output to a file
cout<< 3.output maximun element to a file
cout << 4.output mininum element to a file
cout<< 5.exit
cout<<" 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;
}
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;
}
Explanation / Answer
Hi, I have fixed all compile time issue :)
#include <iostream>
#include <fstream>
using namespace std;
// function prototype
void read(int arr[]);
void output(int arr[]);
int indexLargestElement(int arr[], int );
int indexSmallestElement(int arr[], int );
int main()
{
int arr[100],n,choice;
cout<<"enter size of Array";
cin>>n;
int x;
ofstream myfile2;
do
{
cout<<" MENU";
cout<<" 1.Accept elements of array";
cout<<" 2.output to a file";
cout<<" 3.output maximun element to a file";
cout <<" 4.output mininum element to a file";
cout<<" 5.exit";
cout<<" enter your choice 1-5:";
cin>>choice;
switch(choice)
{
case 1:
read(arr);
break;
case 2:
output(arr);
break;
case 3:
x=indexLargestElement(arr, n);
myfile2.open("outputmax.txt");
myfile2<<x;
myfile2.close();
break;
case 4 :
x=indexSmallestElement(arr, n);
myfile2.open("outputmin.txt");
myfile2<<x;
myfile2.close();
break;
case 5:
break;
default:
cout<<"invalid choice"<<endl;
}
} while(choice!=5);
return 0;
}
void read(int arr[])
{
int num;
ifstream myfile("input.txt");
int i = 0;
if (myfile.is_open())
{
while (myfile>>num)
{
arr[i] = num;
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 indexLargestElement(int arr[], int n)
{
int maxIndex = 0;
for (int i = 1; i<n; i++) {
if (arr[maxIndex] < arr[i])
maxIndex = i;
}
return maxIndex;
}
int indexSmallestElement(int arr[], int n)
{
int minIndex = 0;
for (int i = 1; i<n; i++) {
if (arr[minIndex] > arr[i])
minIndex = i;
}
return minIndex;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.