I need a bit of help lads with writing this program. I cant seem to get it right
ID: 3621126 • Letter: I
Question
I need a bit of help lads with writing this program. I cant seem to get it right! How would i do it in C language? please help. :)Write a program that loads a 10-element array with
random integers from 1 to 1000. The program will not accept any 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.
The program should generate a new set of random numbers each time it is run. For example the program should generate something like this:
Value Characters Total Characters
142 3 3
1000 4 7
963 3 10
6 1 11
27 2 13
1000 4 17
1 1 18
500 3 21
99 2 23
500 3 26
please use all integer as the return type for main(). thank you!
Explanation / Answer
Hey pal, give this a try: #include #include #include // Returns random number between a and b int getRandNum(int a, int b) { return (a + rand()%(b-a+1)); } // Returns number of characters in n, up to 4 int getCharsInNum(int n) { if (n < 10) return 1; else if (n < 100) return 2; else if (n < 1000) return 3; else return 4; } int main() { srand(time(NULL)); int a[10]; int numChars; int totalChars = 0; int i; for (i = 0; i < 10; i++) { a[i] = getRandNum(1, 1000); numChars = getCharsInNum(a[i]); totalChars = totalChars + numChars; printf("%d %d %d ", a[i], numChars, totalChars); } return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.