My program seems all correct but when I run the program my outputs are all what they need to be but I just cant seem to figure out how to send my output into 5 seperate columns. After reading the teachers notes I am more confused with his explanation on how to output to columns.

Here is the Directions:

Write a program that will generate an unknown (between 93 and 139) count of random numbers whose values fall between 147 and 206. Calculate the average to 2 decimal places and standard deviation to 3 decimals. On the monitor display only the count of numbers, the mean, and the standard deviation. Then after you have displayed alll of that information list them again in 5 columns in order from high to low and then have skipped two spaces.

Here is what I have so far:

srand((unsigned int) time (NULL));
int first, second;
int high, low, high2, low2;
int number, number2;  
int total = 0, total2 = 0, overall = 0;
double average, deviation;
high = 139;
low = 93;
high2 = 206;
low2 = 147;

for (first=0; first<1; first++) 
  {
      number = rand()%(high-low+1)+low;
  }


      for (second=0; second < number; second++)
          {
               number2 = rand()%(high2-low2+1)+low2;
               total = number2 * number2;
               total2 = total2 + number2;
               overall = overall + total;
               cout << number2 << endl;
          }



average = ((double)total2 / (double)second);
deviation=sqrt(((double)overall/(double)second)-(((double)total2*(double)total2)/((double)second*(double)second)));

   cout << fixed << showpoint << endl;
   cout << second << endl;
   cout << setprecision ( 2 ) << average << endl;
   cout << setprecision ( 3 ) << deviation << endl;

Any assisntance you can give me on seperating my outputs into 5 seperate columns would be greatly appreciated.

Dani AI

Generated

As suggested, the simplest reliable approach is to collect every generated value in a container, sort that container descending, then print the sorted values in fixed-width columns. The program as posted computes sums and sums of squares while printing each number immediately; that prevents you from sorting later. Switch to storing the numbers first (std::vector<int>), compute sum and sum-of-squares while filling it, then compute mean and standard deviation from those aggregates.

Common pitfalls to avoid:

  • Seed the RNG once at program start, not inside a loop.
  • Use a double for sum and sum-of-squares to avoid integer overflow for large totals.
  • Floating-point rounding can make the variance slightly negative; clamp it to zero before sqrt.
  • Use fixed width (std::setw) so columns line up regardless of the number of digits.

Example printing pattern (after sorting) that produces five aligned columns and optionally pads the last row so there are always five columns:

// v is a sorted (descending) std::vector<int>
const int cols = 5;
for (size_t i = 0; i < v.size(); ++i) {
    std::cout << std::setw(6) << v[i];
    if ((i + 1) % cols == 0) std::cout << '\n';
}
if (v.size() % cols) {
    // pad remaining cells so layout stays as 5 columns
    for (int p = 0; p < cols - (v.size() % cols); ++p)
        std::cout << std::setw(6) << ' ';
    std::cout << '\n';
}
// leave two blank lines after the block if required
std::cout << '\n' << '\n';

If the assignment really requires the last printed row to always contain five entries, pad the final row with blanks as shown. Otherwise, it is perfectly normal for the last row to have fewer than five values. These small changes will let sort the numbers high-to-low and display them neatly in five columns.

Recommended Answers

All 2 Replies

Also I have had a bit of direction from the teachers handouts

EX:
int second;
if(second % 5 == 0)
putchar('\n');
cout << number2;
}

But what happens if second%5 !=0, is can it still out put like

say second is 24

2 5 6 7 11
3 5 11 6 13
5 4 2 8 12
0 9 14 8 7
12 4 5 6

and not completly fill the coulumns, by goal is to get my output to output into columns and output to 5 columns no matter how many numbers there are.

You will need to store each value of number2 in a container as it's generated. When all the values of number2 have been generated then sort the container from high to low. Then use a loop using a counter (you could use second to do that) that ranges from 1 to less than or equal to number to print all the values of number2 stored in the container 5 values per line. If the loop counter % 5 equals zero then start a new line. If number % 5 is not equal to zero then the last line won't have 5 values in it.

As a learning experience when you have completed the task change the loop counter in the print out loop to range from 0 to less than number to see what difference it makes and try to explain why there is a difference in the appearance of the print out.

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.