C++ Programming, Arrays. Please explain! Psuedocode provided The text file looks
ID: 641602 • Letter: C
Question
C++ Programming, Arrays. Please explain! Psuedocode provided
The text file looks like this:
The end result should look like this:
Use one dimensional array to solve the following problem. Read in a number form a file (numbers.txt), each of which is between 10 and 100 both inclusive As each number is read, store it in the array only if it is not already in the array If a particular number is in the file 4 times, we only want to keep it once in the array once After reading all the values, display the array by using a function. Function should have 2 arguments - const array and int size The file format is Number (int the first record is a header Steps Declare an array of 91 integers- Initialize to 0. const int SIZE-91; would be a good thing Pre-read While not eof For loop to go thru the array If element[il is 0 element-inNumber else if elementlil is eaual to inNumber break alreay in the array end of loop ost-read end of while call the function to display the array; Function should have 2 arguments const array and int sizeExplanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
void display(const int arr[], int size){
cout << " List of unique Numbers " << endl;
for(int i = 0; i < size; ++i){
if(arr[i] == 0){
break;
}
else{
cout << arr[i] << endl;
}
}
}
int main(){
const int SIZE = 91;
int arr[SIZE] = {0};
ifstream in;
int value;
in.open("numbers.txt");
if(in.is_open()){
while(in >> value){
for(int i = 0; i < SIZE; ++i){
if(arr[i] == 0){
arr[i] = value;
break;
}
else if(arr[i] == value){
break;
}
}
}
}
else{
cout << "Can not open the file" << endl;
}
display(arr, SIZE);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.