Answer to Question #216993 in C++ for Srikanth Kodamasim

Question #216993
Create a file called "DATA" to store the set of integer elements. Search whether the given element is present or not within the file. If the element is present, then write the factors of that searching element in the "FACTORS" file. Write a formatted IO file program to demonstrate the above operation and display the contents of "FACTORS" file.
1
Expert's answer
2021-07-14T04:43:37-0400
#include<iostream>
#include<fstream>
using namespace std;


int main(){
	int arr[10] = {90,100,200,400,40,60,80,30,60,70};
	ofstream file("DATA.txt", ios::out);
	for(int i=0; i<10; i++){
		file<<arr[i]<<endl; //Writing the set of integers to the file
	}
	file.close();
	ifstream file1("DATA.txt", ios::in);


	int arr1[10];
	for(int i=0; i<10; i++){
		file1>>arr1[i]; //Putting the content of the file in the array
	}
	
	for(int i=0; i<10; i++){
		cout<<"The element1  is\t"<<arr1[i]<<endl;
	}
	file1.close();
	int given_element = 400; //400 is the given element but any element can be searched
	ofstream file2("FACTORS.txt", ios::out);
	
	for(int i=1 ;i<10; i++){
		if(arr1[i] == given_element){
			for(i=0; i<given_element; i++){
				if(given_element % i == 0){
					file2<<i<<"\n";
				}
			}
		}
	
	}
	
	file2.close();


	ifstream file3("FACTORS.txt", ios::in);
	int arr2[10];
	for(int i=0; i<10; i++){
		file3>>arr2[i]; //Writing the set of integers to the file
	}
	file3.close();
	for(int i=0; i<10; i++){
		cout<<"The element is\n"<<arr2[i]<<endl;
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS