How do you create these 3 functions and program in c++ using no global variables
ID: 3823407 • Letter: H
Question
How do you create these 3 functions and program in c++ using no global variables, using visual studios? 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 array filled with values and a variable whose value is the actual number of elements 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 index Largest Element (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. 4. 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
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//this structure is used to return 2 variables at a time
struct return_values{
int arr[100];//to store array elements
int n;
};
//this read_data function will read integers in a file and stores it into
//an array
struct return_values read_data()
{
struct return_values x;
int i=0,length;
int num;
int arr[100];
ifstream file;
file.open("integers.txt");//read file
while(!file.eof())
{
file>>arr[i];//storing into file
i++;
}
file.close();//closing file
length=i-1;//finding no of elements
for(i=0;i<length;i++)
x.arr[i]=arr[i];//store into structure member of array
x.n=length;
return x;//returning both array and size of the array
}
//This function will write into a specified file
//data that will write is the array elements
void write_data(struct return_values x)
{
int i;
ofstream myfile ("example.txt");
if(myfile.is_open())
{
for(i=0;i<x.n-1;i++){
myfile<<x.arr[i]<<" ";//writing into a file with a space between two integers
if(i%10==0&&i!=0)//for every 10 elements it will give a new line
printf(" ");
}
myfile.close();//closing file
}
else
cout<<"Unable to open file";
}
//finding smallest element index
int indexSmallestElement(struct return_values x)
{
int min_index,i;
min_index=0;
for(i=1;i<x.n;i++)
{
if(x.arr[i]<x.arr[min_index])
min_index=i;//smallest element index will stores here
}
return min_index;
}
int main()
{
int i,smallestElementIndex;
struct return_values x;
x=read_data();//calling function to read data from a file
write_data(x);//calling function to write into a file
smallestElementIndex=indexSmallestElement(x);//calling function to find smallest element
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.