Return to the BobbyGriggs.com C++ Nook


Chapter 8 - Square a number
© 1998 by Bobby Griggs.



// Program name:  Squares

// Purpose:  To display the square of a number between 1 and 32 on

// the monitor using an array for storage of the squares.



// Win32 console application



#pragma hdrstop



#include < iostream.h >

#include < math.h >

#include "myprocs.h"



const MAX = 33;



void Init_array(double squares[MAX]);

int User_input();

void Display(double squares[MAX],int number);



void main()

{

	double squares[MAX];

	int number;

	bool doagain = true;



	Init_array(squares);

	do

	{

		number = User_input();

		Display(squares,number);

	}

	while (doagain = Again());

	return;

}



void Init_array(double squares[MAX])

{

	int i;

	

	for (i = 1;i < MAX;i++)

		squares[i] = pow((double)i,2);

	return;

}



int User_input()

{

	int c = 0;



	while ((c < 1) || (c > MAX - 1))

	{

		cout << "Square this number ... ";

		cin >> c;

		cout << endl;

		if ((c < 1) || (c > MAX - 1))

		{

			cout << "Entry must be between 1 and " << MAX - 1 << "!";

			cout << endl << endl;

		}

	}

	return c;

}



void Display(double squares[MAX],int number)

{

	cout << "The square of " << number << " is " << squares[number];

	cout << "!" << endl << endl;

	return;

}