First, this is a homework assignment. I am not trying to hide that. I also don't expect anyone to do my homework for me.
But I do need help understanding HOW this works.
We are random generating an array of 100 numbers.
Then we set it to display in 5 columns.
Then we set it to display in 5 right aligned columns using setw.
Now, this is where I get lost. We are supposed to set it to display a "----" in front of any number that is less than 100 and "++" in front of any number greater than 1000.
Ex: ----99 128 ++1024 ----88 ++1001 567
The only example in our book shows how to use setfill to put the desired character in front of every number.
I attempted to use "if/else" statements, but still ended up with every number displayed with '-' filling each unused space.
ANY HELP explaining this would be greatly appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
//*************************
//* Project Name:
//* Program Name:
//* Written By:
//* Date:
//*
//* This program displays 100 random numbers in a formatted table.
//*************************
//**Preprocessor Directives
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{ //-BEGIN MAIN()
//*****Local Variables and Constants
const int SIZE = 100;
const int UPPER = 1024;
const int LOWER = 8;
int array[SIZE];
int x;
//*****Program Statements
// load the array with random numbers
for (x = 0; x < SIZE; x++)
array [x] = LOWER + rand() % (UPPER - LOWER + 1);
// display array
for (x = 0; x < 100; x++)
{ cout << setw(5) << array[x] <<" ";
if (x % 5 == 4)
cout << endl;
}
cout << endl;
return 0;
} //-END MAIN()
|