In: Computer Science
C++ language
There are 100 integers in the inputfile.txt. Read each integer in the inputfile.txt and store each one in the array named myArray. After that, complete the code in the function printArray(...) to output the elements in the array with the following format:
- Each output line should consist of 10 integers
- Each integer should be separated by | The output should be exactly as shown
Array contains the following elements 12 | 32 | 34 | 56 | 78 | 21 | 14 | 19 | 92 | 14 | 67 | 83 | 95 | 70 | 24 | 29 | 58 | 34 | 69 | 32 | 42 | 73 | 24 | 26 | 32 | 90 | 32 | 37 | 19 | 77 | 26 | 62 | 31 | 84 | 51 | 54 | 19 | 33 | 59 | 31 | 86 | 54 | 93 | 46 | 52 | 44 | 79 | 69 | 42 | 64 | 14 | 34 | 15 | 89 | 34 | 76 | 10 | 44 | 53 | 81 | 16 | 83 | 85 | 77 | 56 | 10 | 43 | 11 | 88 | 15 | 67 | 94 | 61 | 26 | 95 | 41 | 28 | 91 | 31 | 94 | 29 | 77 | 32 | 58 | 32 | 35 | 72 | 69 | 75 | 56 | 12 | 99 | 59 | 71 | 90 | 47 | 70 | 71 | 16 | 64 |
then find the minimum and the maximum elements in the array
Complete the code to find the min and max values of all the integers in myArray. The output should look exactly as shown:
The min value of the elements in the array is : 10 The max value of the elements in the array is : 99
Code:
#include<iostream>
#include<fstream>
using namespace std;
void printarray(int a[]){
for (int i = 0; i < 100; i++){
cout << a[i]<<"|"
;
if((i+1)%10==0)
//printing array
cout<<endl;
}
}
int main() {
ifstream j;
j.open("list.txt"); //you give
your file name here with 100 numbers by space or newline
int arr[100],k=0,i;
while (j >> arr[k]){
k=k+1;
//reading integers from file.
}
j.close();
printarray(arr);
//calling function printarray
int max=-1,min=1000000000;
for(i=0;i<100;i++){
if(min>=arr[i]){
//finding minmum element in array.
min=arr[i];
}
}
for(i=0;i<100;i++){
if(max<=arr[i]){
//finding maximum element in array.
max=arr[i];
}
}
cout<<"The min value of the elements in the array is
:"<<min<<endl;
cout<<"The max value of the elements in the array is
:"<<max;
}
Output:
12 32 34 56 78 21 141992 14 67|83|95|70|24|29|58|34|69|32| 42|73|24|26|32|90|32|37|19|77| 26|62|31|84|51|54|19|33|59|31| 86|5493|46|52|44|79|69|42|64| 14|34|15|89|34|76|10|44|53|81| 16|83|85|77|56|10|43|11|88|15|| 67|94|61|26|95|41|28|91|31|94|| 29|77|32|58|32|35|72|69|75|56|| 12|99|59|71|90|47|70|71|16|64|| The min value of the elements in the array is :10 The max value of the elements in the array is :99