How would you solve these questions mainly questions #3 and #4? Write a function
ID: 3821735 • Letter: H
Question
How would you solve these questions mainly questions #3 and #4? 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 array filled with values and a variable whose value is the actual number of elements read in. Write a function to output all the integers in an array to an output file, with 10 values per line. Values on a line arc separated by a single space. Write a function similar to the function indexLargestElement (page 535 in Example 8- 6) except that this function is called indexSmallestElement and it returns the index of the minimum value found in an array. Write a complete C++ program which includes both functions 1. and 2. above, as well as indexLargestElement 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.Explanation / Answer
Here is the answer for question. Since the text book implementation for indexLargestElement is not given here... I have coded it. Please take a look at the output file. Remembers index starts from 0 and not 1. Please don't forget to rate the answer if it helped. Thank you very much.
#include <iostream>
#include <fstream>
#define MAX_SIZE 200
using namespace std;
//returns the index of largest element in an array of numbers having count element.
//return -1 if the count is 0
int indexLargestElement(int numbers[],int count)
{
if(count == 0) //if no elements
return -1;
int max_idx = 0; //assume the first element is largest
for(int i=1; i<count; i++) //starting from 2nd till last elemnet
{
if(numbers[i] > numbers[max_idx]) //if current array element is greater than previous max element
max_idx = i; //update the max_idx
}
return max_idx;
}
//returns the index of the smallest element in the array of numbers with count elements.
//returns -1 if count is 0;
int indexSmallestElement(int numbers[],int count )
{
if(count == 0) //if no elements
return -1;
int min_idx = 0; //assume 1st element is the smallest
for(int i=1; i<count; i++) //startin from 2nd number till last element
{
if(numbers[i] < numbers[min_idx]) //if current array element is smaller than min element update min_idx
min_idx = i;
}
return min_idx;
}
//function to write array elements having count elements into output specified. Note: ofstream object should be
// passed by reference (& in the function definition). Write 10 numbers in each line separated by space
int write_to_file(int numbers[],int count,ofstream &out_file)
{
for(int i=0; i<count; i++)
{
if(i % 10 == 0) //10 elemennts are done
out_file<<endl; //put a newline to output file stream object
out_file<<" "<<numbers[i]; //write to output file separated by space
}
out_file<<endl<<endl;
return 0;
}
//ask user for input filename and output filename. reads the numbers in the file and then finds min, max index and writes all numbers to output file
//also writes min and max numbers along with index to output file.
int main()
{
ifstream in_file;
ofstream out_file;
string out_filename, in_filename;
int numbers[MAX_SIZE], count=0, min_idx, max_idx;
cout<<"Enter input filename: ";
cin>>in_filename;
in_file.open(in_filename);
if(!in_file.is_open())
{
cout<<"File not found - "<<in_filename<<endl;
return 1;
}
while(in_file >> numbers[count])
{
count++;
}
in_file.close();
cout<<"Enter output filename: ";
cin>>out_filename;
out_file.open(out_filename);
if(!out_file.is_open())
{
cout<<"Could not open file for writing - "<<out_filename<<endl;
return 1;
}
write_to_file(numbers,count,out_file);
min_idx = indexSmallestElement(numbers,count);
max_idx = indexLargestElement(numbers, count);
out_file<<"Index of Minimum Number = "<<min_idx<<endl;
out_file<<"Minimum Number = "<<numbers[min_idx]<<endl<<endl;
out_file<<"Index of Maximum Number = "<<max_idx<<endl;
out_file<<"Maximum Number = "<<numbers[max_idx]<<endl;
out_file.close();
}
sample input file numbers.txt
12 4 20 4 5 11
16 34 12 78 39
75 86 15 32 12
34 29 80 99 16
14 53 28 65 68
92 49 78 98 3
55 94 10 33 61
70
sample run
Enter input filename: numbers.txt
Enter output filename: numbers-out.txt
output file numbers-out.txt
12 4 20 4 5 11 16 34 12 78
39 75 86 15 32 12 34 29 80 99
16 14 53 28 65 68 92 49 78 98
3 55 94 10 33 61 70
Index of Minimum Number = 30
Minimum Number = 3
Index of Maximum Number = 19
Maximum Number = 99
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.