Problem In C++ code must use arrays and file I/O. and functions and please send
ID: 3802414 • Letter: P
Question
Problem
In C++ code
must use arrays and file I/O. and functions and please send the output terminal window display as well, Thank you
Often, data is presented to a program in multiple files, and needs to be stored in multiple
arrays. In this lab, we will use a pair of related data files.
File data1.txt contains prices for 6 auto parts. File data2.txt contains the associated
names of those parts. The files are related in that the name on line 1 of data2.txt
corresponds to the price on line 1 of data1.txt, and similarly for each pair of lines of the
files.
File data1.txt contains the prices in dollar amounts, so choose an appropriate variable data
type for this data. data2.txt contains the names in string format.
Program
Write a program which will declare an array for the prices, and an array for the names of the
parts, open the files, and load data in the arrays.
There are exactly 6 entries in each file, so you can use static arrays to load your data.
Once the data is loaded, print out the names of the most and least expensive parts. Together
with the names of the most and least expensive parts, you should print the actual prices for
these formatted as $dollars.cents, e.g. $20.12. Please be sure the names and the prices are
aligned nicely e.g.:
The name of the most expensive part $100.45
The name of the least expensive part $1.99
File data1.txt
12.50
22.50
3500.00
90.00
30.00
30.00
File data2.txt
gasket
spark_plug
engine
spark_plug_harness
wiper_blades
gas_cap
Explanation / Answer
//Hi, you can run this cpp code
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
float price[6];
int i=0;
char item[7][21];
ifstream in;
in.open("data1.txt",ios::in);
float a;
while (in >> price[i++])
{
}
in.close();
in.clear();
in.open("data2.txt",ios::in);
i=0;
while(in >> item[i++])
{
}
int max=-1,min=35560;
int j=0,k=0;
for(i=0;i<6;i++)
{
if(max<price[i])
{
max=price[i];
j=i;
}
if(min>price[i])
{
min=price[i];
k=i;
}
}
cout<<"The most expensive thing is "<<item[j]<<" price at "<<price[j];
cout<<" The least expensive thing is "<<item[k]<<" priced at "<<price[k];
cout<<" File data1.txt ";
for(i=0;i<6;i++)
cout<<price[i]<<" ";
cout<<" File data2.txt ";
for(i=0;i<6;i++)
cout<<item[i]<<" ";
in.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.