C++ question. Define a single dimensional array a that could have 20 elements a)
ID: 3567248 • Letter: C
Question
C++ question. Define a single dimensional array a that could have 20 elements a)Populate the array by reading from a text file, 20 integers (create your own text data file) b) find the smallest value in the array c) sort and display the values of the elements of the arrayC++ question. Define a single dimensional array a that could have 20 elements a)Populate the array by reading from a text file, 20 integers (create your own text data file) b) find the smallest value in the array c) sort and display the values of the elements of the array
C++ question. Define a single dimensional array a that could have 20 elements a)Populate the array by reading from a text file, 20 integers (create your own text data file) b) find the smallest value in the array c) sort and display the values of the elements of the array
Explanation / Answer
main.cpp
------------------
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int main()
{
ifstream myfile;
myfile.open ("input.txt");
int a;
int arr[20];
int i=0;
while (!myfile.eof()) {
myfile >> arr[i];
i++;
}
sort(arr, arr+20);
cout<<"Smallest value in the array is: "<<arr[0]<<" ";
cout<<"Sorted array: ";
for(int i=0; i<20; i++)
{
cout<<arr[i]<<" ";
}
--------
input.txt
-----------
2
3
1
4
5
3
9
0
8
4
2
3
1
4
5
3
9
0
8
4
-------
output
return 0;
}