just a small issue with my program...I am finishing it up and the issue I have is I need to make me decimal points line up...the array is up and perfect just need some assistance in setting up the widths

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void Fill_Array(double Size[], int& count);
void Print_Array(double Size[], int count);
double Calc_Average(double Size[], int count);
void Sort(double Size[], int count);
void Swap(double& v1, double& v2);
double index_of_smallest(const double Size[], int start_index, int number_used);
double Calc_Median(int count, double Size[]);
void Print_Array_and_Calculations(double median, double average, int count, double Size[]);

const int MAX_SIZE = 100;

int main()
{
	ifstream in_size;
	int count;
	int numbers_used = 0;
	int small_index;
	double Size[MAX_SIZE];
	double median = 0.0;
	double average = 0.0;

	Fill_Array(Size, count);
	Print_Array(Size, count);
	small_index = index_of_smallest(Size, count, numbers_used);
	
	average = Calc_Average(Size, count);
	Sort(Size, count);
	median = Calc_Median(count, Size);
	Print_Array_and_Calculations(median, average, count, Size);
	
	in_size.close();
	return 0;
}

void Fill_Array(double Size[], int& count)
{
	double size;
	ifstream in_size;
	string text_file;

	cout << "Enter the file to read in: ";
	getline(cin, text_file);
	
	cout << "The numbers in the array are:" << endl << endl;
	in_size.open (text_file.c_str ());
	if(in_size.fail())
	{
		cerr  << "Error opening file" << endl;
		exit(1);
	}
	count = 0;
	in_size >> size;
	while((!in_size.eof()) && (count <= MAX_SIZE))
	{
		Size[count] = size;
		count++;
		in_size >> size;

	}
	in_size.close();
}

void Print_Array(double Size[], int count)
{

	int tempcount = 1;
	for(int i = 0; i < count; ++i)
	{
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(1);
	
	cout << Size[i] << " ";
	
	if (tempcount == 4)
	{
		cout << endl;
		tempcount = 1;
	
		}
		else
		tempcount++;
	}
}

double Calc_Average(double Size[], int count)
{
	double total = 0.0;
	double average = 0.0;
	for (int i = 0; i < count; i++)
	{
		total = total + Size[i];
	}
	average = double(total) / count;

	return average;
}

void Sort(double Size[], int count)
{
	int index_of_next_smallest;
	
	for (int index = 0; index < count - 1; index++)
	{
		index_of_next_smallest = index_of_smallest(Size, index, count);
		Swap(Size[index], Size[index_of_next_smallest]);
	}
}

void Swap(double& v1, double& v2)
{
	double temp;
	temp = v1;
	v1 = v2;
	v2 = temp;
}

double index_of_smallest(const double Size[], int start_index, int number_used)
{
	double min = Size[start_index],
		index_of_min = start_index;
	for (int index = start_index + 1; index < number_used; index++)
		if(Size[index] < min)
		{
			min = Size[index];
			index_of_min = index;
		}
		return index_of_min;
}

double Calc_Median(int count, double Size[])
{
	double median = 0.0;
	
	
	

	if ( count % 2 == 1)
	{
		median = Size[count / 2];
	}
	else
	{	
		median = (Size[(count / 2) - 1] + Size[count / 2]) / 2; 
	}

	
	return (median);
}

void Print_Array_and_Calculations(double median, double average, int count, double Size[])
{
	cout << endl << endl << endl << "The numbers in the array are:" << endl << endl;
	Print_Array(Size, count);
	cout << endl << endl << endl<< "The average of the numbers is " << average;
	cout << endl << "The median of the numbers is " << median;
	cout << endl << endl;
}

Recommended Answers

All 2 Replies

let me know if u guys do not know what i mean

You're on the right track. What you're missing is setting the width of the field in which the values will be displayed, using setw( ) manipulator.

The .setf( ) calls don't need to be inside the loop, as they are "sticky" settings. Width, however, applies only to the very next item to output, so it must be repeated.

void Print_Array(double Size[], int count)
{
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(1);
	int tempcount = 1;

        cout << right;   //sets right alignment in the fields
	for(int i = 0; i < count; ++i)
	{
	     cout << setw(6) << Size[i] ;   //use size correct for your data
	
	     if (tempcount == 4)
	     {
		cout << endl;
		tempcount = 1;
	     }
	     else
		tempcount++;
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.