Arrays

Trying to do a homework assignment for a class and cannot figure out how to read a file into an array. I've looked in our book and on several other forums and cant seem to find any examples of this. Below is the assignment I'm working on. I have a shell of the program that I can get to run, but getting a .txt file to read into an array is something I cant seem to figure out how to do. If anyone can help me with this I would greatly appreciate it. Thanks

Write a program to read N data items into two arrays, X and Y, of size 20. Store the product of the corresponding pairs of elements of X and Y in a third array Z, also of size 20. Print a three column table that displays the arrays X, Y, and Z. Then compute and print the square root of the sum of the items in array Z. Compute and print the average of the values in array Z and print all values above the average of array Z. Determine the smallest value in each array using only one function.

Use the two data files named DATAX.TXT and DATAY.TXT.

You must use functions for the reading of the data, computing the average, printing the three column table and printing the values above average.
boomhauer wrote:
Write a program to read N data items into two arrays, X and Y, of size 20. Store the product of the corresponding pairs of elements of X and Y in a third array Z, also of size 20.
Which is it, size N or size 20?

Here's an example of reading data from a file into an array, it works exactly the same as user input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <vector>

int main()
{
    std::vector<int> numbers;

    {
        std::ifstream in ("input.txt");
        int temp;
        while(in >> temp)
        {
            numbers.push_back(temp);
        }
    } //this closing brace destructs the std::ifstream and closes the file

    for(std::size_t i = 0; i < numbers.size(); ++i)
    {
        std::cout << numbers[i] << std::endl;
    }
}
Last edited on
The assignments in this class have been kept simple and we have not used vectors yet. Below is the shell that he gave us. Will vectors conflict with anything in the existing code? He wants us to use arrays, at a set size of 20, there are 20 sets of numbers in the .txt files, but if I can use vectors to do the same thing I dont think he will mind, as long as I can use this shell.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>
using namespace std;
void readdata(ifstream &fx, int abc[], int &ct);

int main()
{ int x[30] = {0}, y[30] = {0};
  int ctx = 0, cty =0;
  ifstream f1 ("h:\\175 winter2013\\datax.txt",ios::in);
  ifstream f2 ("h:\\175 winter2013\\datay.txt",ios::in);

  readdata(f1,x,ctx);
  system ("pause");
  readdata(f2,y,cty);
  system ("pause");

  f1.close();
  f2.close();

  return 0;
  }
  void readdata(ifstream &fx, int abc[], int &ct)
  {
     ct = 0; cout<<"\n\n";
       while (fx>>abc[ct], !fx.eof())
       {    cout<< abc[ct]<<"\n  ";
	     ct++;
     	}
	cout << " \n\n";


	   }
You probably shouldn't use vectors, then. The logic is still the same, though. Take a look at my program and see if you can translate the logic to arrays. Tip: You'll need to keep track of indices during the while(in >> temp) loop.
Topic archived. No new replies allowed.