In Short: I am learning array's so I did this test. The goal was to generate a random number from 1-100. Then divided that by 10 so 67 would be 6 as an example. Then with a for loop fill the array with 6 'X''s and then print it delivering 6 X's.
The code works but it prints 1 more X then it should. I tried to fix this by changing the for loop from i=0 to i=1. But when I do that it stops printing the X's when I run the program?
I know the code might look funky and not what you guys would write. But this I understand with what I have learned so far in the book. What am I doing wrong?
Thanks
The code works but it prints 1 more X then it should. I tried to fix this by changing the for loop from i=0 to i=1. But when I do that it stops printing the X's when I run the program?
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main (int argc, const char * argv[]) {
int guess, toPrint, i, terminate; // my int's
char array[11]; //my array
srand( clock() ); //my random seed
guess = (rand() % 100) + 1; // my random num 1-100
printf("The random number was %d\n",guess); // test to tell me what num it is
toPrint = (guess / 10); // rounds the rand num ie. 67 would be 6
printf("The rounded is %d\n",toPrint); //tests to see if the nuber is right
terminate = (toPrint + 1); // adding 1 number above my toPrint for array terminate
printf("The terminate array num is %d\n",array[terminate]); //Test this number
array[terminate] = '\0'; //set my 0 in my array to 1 number higher then toPrint.
for(i=0; i<=toPrint; toPrint--){
array[toPrint] = 'X'; // adds an 'X' to each Char array
printf("The i counter is %d and toPrint is %d\n",i,toPrint); //tests this
}
printf("%s", array); // prints the X'x
printf("\n");
return 0;
}
I know the code might look funky and not what you guys would write. But this I understand with what I have learned so far in the book. What am I doing wrong?
Thanks