In my program so far, i call a function to generate 100 pairs of random numbers, i then send it to a file with two numbers on one time, meaning i have 100 lines of 100 pairs of numbers. my problem now is that i have to call another function to read the pair of numbers on a line from the file, and then find the greatest common divisor and record it in a new file. Im confused on how I can read the pair of numbers from the file, find the greatest common divisor, then record that number and do that 100 times. heres what im workign with so far
#include<iostream>
#include<fstream>
#include<cmath>
#include <cstdlib>
#include"myheader.h"
using namespace std;
void main()
{
ofstream outfile;
outfile.open("numbers.txt");
outfile.open("results.txt");
int number1;
int number2;
int count=0;
srand((unsigned)time(0));
1. first complete the writting to the file and then start reading. you are doing it together in the same loop that will not work as the output file stream is still open.
2. this will not give you 100 line with each line having 100 numbers as you said. this will give you 100 lines with each line have two numbers. for 100 by 100 you need two loops.
3. after your outfile is complete, start reading it in an array and find the average. see how you will be doing it:
//how you will read
void second_function(ifstream &ifs)
{
//this will read first two numbers from the file you wrote in the first_function
//keep on reading how you want to do that.
ifs >> k1;
ifs >> k2;
cout << "Average: " << (k1+k2) / 2;
}