A beginner question [ C ]
Hey guys I jst strted learning C and was studying arrays, in which i got this program regarding dice roll probabilities ( 2 dices)
It was like this
#include <stdio.h>
#include <stdlib.h>
main()
{
int i;
int d1, d2;
int a[13]; /* uses [2..12] */
for(i = 2; i <= 12; i = i + 1)
a[i] = 0;
for(i = 0; i < 100; i = i + 1)
{
d1 = rand() % 6 + 1;
d2 = rand() % 6 + 1;
a[d1 + d2] = a[d1 + d2] + 1;
}
for(i = 2; i <= 12; i = i + 1)
printf("%d: %d\n", i, a[i]);
return 0;
}
I got everything, except the 3rd for loop used
now my questions r
1. Why did we use the for loop for the third time to print??Why didnt we printf in the 2nd loop (which we used fr the iterations)??
2. Why do we even printf there? I mean wont this print i = 2 -12 instead of the 1-100 matrix we want to print?
How r we relating the 2nd loop with the printf statement with another for loop between them (with totally different conditions)
It would be nice if u cud explain clearly.. coz i m jst strted into this language
thnx
It was like this
#include <stdio.h>
#include <stdlib.h>
main()
{
int i;
int d1, d2;
int a[13]; /* uses [2..12] */
for(i = 2; i <= 12; i = i + 1)
a[i] = 0;
for(i = 0; i < 100; i = i + 1)
{
d1 = rand() % 6 + 1;
d2 = rand() % 6 + 1;
a[d1 + d2] = a[d1 + d2] + 1;
}
for(i = 2; i <= 12; i = i + 1)
printf("%d: %d\n", i, a[i]);
return 0;
}
I got everything, except the 3rd for loop used
now my questions r
1. Why did we use the for loop for the third time to print??Why didnt we printf in the 2nd loop (which we used fr the iterations)??
2. Why do we even printf there? I mean wont this print i = 2 -12 instead of the 1-100 matrix we want to print?
How r we relating the 2nd loop with the printf statement with another for loop between them (with totally different conditions)
It would be nice if u cud explain clearly.. coz i m jst strted into this language
thnx

Get a book, read it 