Help Needed

Write a program that loads a 10-element array with
random integers from 1 to 1000. There is no input from the
user. For each value, print the value, number of characters
in that value, and a running total of the number of
characters printed. Use the %n conversion specifier to
determine the number of characters output for each value.
Your program must generate a different set of values each
time it is run. The output should have the following format:

Value Characters Characters
142 3 3
1000 4 7
963 3 10
6 1 11
This is quite simple. I succeeded with 4 lines of code :P.

in order to do this you must understand the rand() function. For isntance:

randomNum = rand() % 1000

This will produce a random number between 1 and 1000 and store it in variable randomNum.

float randomNum1 = log10(randomNum);

This takes the log to the base 10 of randomNum (this seems weird, but you will see why we use this)

printf("%lf has %lf characters", randomNum, ceil(randomNum1));

ceil() function rounds a decimal number up to the whole number above it.
3.2 becomes 4, 3.7 becomes 4.

You will find this produces your desired result.

P.S. while using rand(), make sure to include srand(time(NULL)); at the beginning of your function. This way it will always use a different number each time the program is ran.

inclue math.h library and time.h library.
Topic archived. No new replies allowed.