Hey, I'm pretty new to programming and I'm writing a program for my class. I can't figure out why my <iomanip> isn't working.. I'll post a copy of the code (I'm aware it's bugged, as it's not returning the proper values, but the format of the output is the issue). I'm sure I called everything I need to but it reads 'fixed' as an undefined value, which leads me to believe I might not have the proper library on my computer. If this is the case, how can I get it?? Thanks.

//**********************************************
//**********************************************
// N/A   Prof: N/A
// Assignment # 6        Question # 2
// Program: Arrays and Functions
// Purpose: Calculate and display the largest
//         element, mean, standard deviation,
//         and median of a user-defined set
//         of integers.
// Author: Cold_Demon388
// Date: 16 November 2009
//**********************************************
//**********************************************

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int indexMax(const int x[], int length);
void remove(int x[], int &length, int index);
int readSeq(int x[]);
void writeArray(int x[], int length);
double mean(const int x[], int length);
double stdDeviation(const int x[], int length, double mean);
double median(int x[], int length);

int main()
{
   // Variable Declarations
  const int MAX_SIZE = 512;
  int x[MAX_SIZE];

   // Get User Input
  int length = readSeq(x);

   // Calcualtions and Output
  cout << "Largest: " << x[indexMax(x, length)] << endl;
  cout << fixed << setprecision(1) << "Mean: " << mean(x, length) << endl;
  cout << setprecision(3) << "Std Deviation: " << stdDeviation(x, length, mean(x, length)) << endl;
  cout << setprecision(1) << "Median: " << median(x, length) << endl;

   return 0;
}

int indexMax(const int x[], int length)
{
  int i = 0;
  int max = x[0];
  int index = 0;

  //finds max value in array and returns the index to that val
  while (i < length - 1)
    {
      if (max < x[i+1])
	{
	  max = x[i+1];
	  index++;
	}

      i++;
    }

  return index;
}

void remove(int x[], int &length, int index)
{
  //shifts value at x[index+1] to x[index] and decrements length by 1
  while (index <= length - 1)
    {
      x[index] = x[index+1];
      index++;
    }
  length--;
}

int readSeq(int x[])
{
  int input;
  int length = 0;
  cin >> input;

  while(input != 0 && length <= 512)
    {
      x[length] = input;
      length++;
      cin >> input;
    }
  
  return length;
}

void writeArray(int x[], int length)
{
  int i = 0;
  while (i < length)
    {
      cout << x[i] << " ";
      i++;
    }
}

double mean(const int x[], int length)
{
  int i = 0;
  int sum;
  while (i < length)
    {
      sum += x[i];
      i++;
    }

  return sum / i;
}

double stdDeviation(const int x[], int length, double mean)
{
  int i = 0;
  double sum;

  while (i < length)
    {
      sum += (x[i] - mean) / length;
      i++;
    }

  return sqrt(sum);
}

double median(int x[], int length)
{
  int i = static_cast <int> (length / 2);
  int originalLength = length;
  int largerNum;

  if (originalLength % 2 == 1)
    {
      while (originalLength - i < length)
	remove(x, length, indexMax(x, length));
 
      return x[indexMax(x, length)];
    }
  else
    {
      while (originalLength - i + 1 < length)
	remove(x, length, indexMax(x, length));

      largerNum = indexMax(x, length);
      remove(x, length, largerNum);
      
      return (x[largerNum] + x[indexMax(x, length)]) / 2;
    }
 
}

Thanks again everyone. Take care.

Recommended Answers

All 3 Replies

Try it as:

cout.setf(ios::fixed);
cout << setprecision(1) << "Mean: ";

Instead of: cout << fixed << setprecision(1) << "Mean: ";

Hey thanks!
That did the trick. I don't fully understand why though, lol. What does each part of that command represent? And is there a way I can apply the same general concept to other libraries that are giving me similar headaches?

My guess would be that you needed to qualify fixed in your code with the ios:: namespace if you didn't have using namespace std; in your code (which you shouldn't do anyway). I'm not entirely up on how ios and iostream are woven together, though I think ios is the base class.
In this example, using setf in place of the manipulator "fixed" basically accomplishes the same thing, changing the flags to reflect that the output should be fixed-point.

In terms of the general concept, look around about namespaces and the using directive (there's a lot on this site as to why you shouldn't bring the entire std namespace in). There are lots of examples of how to use it more effectively with e.g., using std::cout; so you don't have to qualify the often-used method.

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.