Untitled

 avatar
unknown
plain_text
a year ago
3.8 kB
7
Indexable
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstdlib>

using namespace std;

// Q4. Write a program that: 

// a) Declares a dynamic 1D array A of N doubles, where N 
// is input from the keyboard, and initializes the array 
// elements to A[i] = sin(1.1*i)

// b) Declares a dynamic 1D array B of N doubles and 
// initializes the array elements to B[i] = 0

// c) Saves/writes the array elements A[i] into a file called 
// "A.txt". Use scientific notation with 10 digits of precision.

// d) Loads/reads the array elements B[i] from "A.txt".

// e) Calculate delta = A[i] - B[i] and print out
// the delta values to the screen.
// Why are the values of delta not zero in general ?

int main() 
{ 

// a) Declares a dynamic 1D array A of N doubles, where N 
// is input from the keyboard, and initializes the array 
// elements to A[i] = sin(1.1*i)

	int N, i;
	double *A, *B, delta;

	cout << "\ninput N (>0) ? ";
	cin >> N;

	A = new double [N];
	
	// * NOTE: if we don't say you have to check for errors
	// in a question then you don't have to, except for
	// dynamic array allocation error and file I/O errors
	// -- the reason is because these errors are very
	// significant and checking is always the same so
	// it's not too time hard/consuming.
	// also the errors are run-time so they can be hard
	// to find
	// file I/O and dynamic arrays have special procedures
	// for checking for errors so we want to make sure you
	// know them.
	
	if ( A == NULL ) {
		cout << "\ndynamic allocation error for A";
		return 1;
	}
	
	for(i=0;i<N;i++) A[i] = sin(1.1*i);
	
// b) Declares a dynamic 1D array B of N doubles and 
// initializes the array elements to B[i] = 0

	B = new double [N];
	
	if ( B == NULL ) {
		cout << "\ndynamic allocation error for B";
		return 1;
	}
	
	for(i=0;i<N;i++) B[i] = 0.0;

// c) Saves/writes the array elements A[i] into a file called 
// "A.txt". Use scientific notation with 10 digits of precision.

	ofstream fout("A.txt");

	if ( !fout ) {
		cout << "\noutput file open error";
		return 1;
	}
	
	fout << scientific; // analgous to cout

//	fout.precision(9); // 9 decimals = 10 digits
	
//	fout.precision(15); // 15 decimals = 16 digits
	
	fout.precision(16); // 16 decimals = 17 digits
	// -- this seems to work for this example, but
	// may not work in general because computers
	// use a different number system than base 10 
	// scientific notation to store numbers (ie base 2/binary)
	
	for(i=0;i<N;i++) fout << A[i] << "\n";

	// *** you need to close "A.txt" before it can be opened
	// in the next part
	// -- closing a file finishes the writing and releases
	// the file so it can be used by other programs or
	// parts of the program in this 
	// -- if you don't close a file and your program
	// crashes or terminates prematurely then you
	// can lose your file data
	// -- normally when you are done using a file you 
	// should close it as soon as possible
	fout.close();

// d) Loads/reads the array elements B[i] from "A.txt".

	ifstream fin("A.txt");
	
	if( !fin ) {
		cout << "\ninput file open error";
		return 1;
	}

	for(i=0;i<N;i++) fin >> B[i];

	fin.close(); 

// e) Calculate delta = A[i] - B[i] and print out
// the delta values to the screen.
// Why are the values of delta not zero in general ?

// -- we lost the last 6 significant digits (ie we lost data)
// because only 10 digits were wrote to the file

	cout << "\ndelta = \n";
	for(i=0;i<N;i++) {
		delta = A[i] - B[i];
		cout << delta << "\n";
	}

	// NOTE: always delete your dynamic array when you
	// are done using it -- typically at the end of the function
	delete A;
	delete B;

	cout << "\n\ndone.\n";
	getchar();

	return 0;
Editor is loading...
Leave a Comment