Relevant

Saturday, 1 November 2014

Solutions No. 003: EEG 206: Program that reads a file and does some work with it. (Our last classwork)

The Program where you read data from a file

Code Notes: 
  • The program's name is "ExtFileOps" (External File Operations) (cool, huh?).
  • It revolves around using a standard header file called "fstream".
  • This somehow involves using classes, but you don't need to bother yourselves.
  • I have chosen to upload the code this time in a zip format because firstly, there is an external data file and secondly, to illustrate how the lecturer wants us to do it.
  • If you are downloading with your phone, you can open the ".cpp" files easily. Just open it like a normal text file (.txt). If your phone is stubborn, rename the file and add ".txt" to the end of the name.
  • I scattered the order of the data in order to prove that it works well (That is, the biggest isn't the last).
Code Process:
  1. We include the fstream header the same way we include iostream.
  2. Normally, to use cin and cout for input and output, we include iostream, we are just doing the same thing here.
  3. We can perform OUTPUT TO the file FROM the program by writing stuff into it, and we can get INPUT FROM the file by reading stuff from it. We'll be focusing on input here as that is our main objective.
  4. To do input, we use the keyword "ifstream" (input file stream) (we use "ofstream" for output).
  5. So, to use ifstream and ofstream for input and output respectively, we include fstream.
  6. And now, the magic (not really).
  7. We create a data file to store the data in question. How?. In visual studio, just right-click "Resource Files" and then select "Add new item" (the same way you add a new source file). Then scroll down and select "text file" and of course specify a name (in this case, I used "leaf.txt").
  8. When the data file opens, you then enter the values into the file line by line as shown.
  9. Now save the file (CTRL+S). Back to code.
  10. Now, just the way you say: "int x", we now say "ifstream data". The name "data" can be any valid c++ name, Just be consistent. (Remember, "ifstream" is like a data type).
  11. Now, we open the data file we created (the EXACT name of the file is in the double quotes). Also, when you declared that object with ifstream, the name you used must be the one used as: name.open("file_name"); in this case, I used "data".
  12. With the data file opened, we can now read freely from the file. To read, we use the object we declared earlier ("data"). We use it like "cin" (cin puts stuff in the program, so does data). I this case, we use a for loop because we are reading into an array named lengths.
  13. That is, the first line of data goes into the first element of lengths[] (lengths[0]), second line into second element, e.t.c.
  14. With the data now in the array, we no longer need the file, so we can now close the file. But we don't need to specify the name of the file this time.
  15. Finding the average is very simple. Just declare one sum variable and use a for loop to add the elements of the array one by one to the sum.
  16. For the big three, its like a ranking. We have a "big three" array for the three biggest. the first element will be the biggest, last element the smallest. We first initialize all the elements to zero.
  17. Then, we set the first data element as the biggest (big3[0]). Now, the work.
  18. We now iterate for each element using a for loop (arrays and loops, they really go well together). 
  19. For each element, we start checking: 
  20. Is it bigger than the biggest, if it is, then the second biggest becomes third, first becomes second and the element becomes first. 
  21. If not, is it bigger then the second, if it is, second becomes third, and the element becomes second. 
  22. If not again, is it bigger than third, if so, the element becomes third, and third is discarded).
  23. If not, the element is discarded. We go through all the elements in the data array like this.
  24. That's all.

And now, the code:
#include <iostream>
#include <fstream>

using namespace std;

void Big3(float[], float[]);
float FindAve(float[]);

int main()
{
ifstream data;
data.open("leaf.txt");
float lengths[25];
for (int i = 0; i < 25; i++)
data >> lengths[i];
       data.close();
float big3[3] = {};
Big3(lengths, big3);
cout << "The average of all the data is: " << FindAve(lengths) << endl;
cout << "The three biggest lengths are: ";
for (int i = 0; i < 3; i++)
cout << big3[i] << ((i<2)?(", "):(""));
cout << endl;
return 0;
}
void Big3(float data[],float big3[])
{
big3[0] = data[0];
for (int i = 1; i < 25; i++)
{
if (data[i] > big3[0])
{
big3[2] = big3[1];
big3[1] = big3[0];
big3[0] = data[i];
}
else if (data[i] > big3[1])
{
big3[2] = big3[1];
big3[1] = data[i];
}

else if (data[i] > big3[2])
{
big3[2] = data[i];
}
}

}
float FindAve(float data[])
{
float sum = 0, ave;
for (int i = 0; i < 25; i++)
sum += data[i];
ave = sum / 25;
return ave;

}
* Keep in mind that the ZIP file has everything.

Download the code (ZIP): ExtFileOps.zip
Download the code (CPP)ExtFileOps.cpp
Download the data file: ExtFileOps_data.txt

No comments:

Post a Comment