Occurrences of the sums of two dies [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#define SIZE 13
int main(){
int die1, die2, sum, i, occurrences[SIZE], j=2;
for(i=0; i<36000; i++){
die1=1+rand()%6;
die2=1+rand()%6;
sum=die1+die2;
++occurrences[sum];
}
printf("%10s","Sums");
for(i=1; i<=12; i++){
printf("%4d", i);
}
printf("\n%10s","Occurrences");
for(i=2; i<=12; i++){
("%4d",occurrences[i]);
}
return 0;
}
Why doesn't this code work? The program has to sum all the random numbers generated by two dies and then it has to print the occurrences of the sums. Why doesn't it work? The output is this:
Sums 1 2 3 4 5 6 7 8 9 10 11 12
Occurrences

The array occurrences[SIZE] is not initialized and has indeterminate values, so the addition ++occurrences[sum]; will invoke undefined behavior. You should initialize it like occurrences[SIZE] = {0}.
The line ("%4d",occurrences[i]); is just a expression using comma operator surrounded by parenthesis, so it won't do printing. It looks like printf should be added before the parenthesis.

Related

why n mod 10 in loop doesn't show output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have solve a basic problem in c that is count the digits in integer and I have written -
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int i;
while(n!=0)
{
n %= 10;
++i;
}
printf("%d",i);
}
I already know that above code is wrong, I should write n/=10; instead of n%=10; but I wants to know why it is not printing even value of i i.e 0.
If I have written any wrong so please ignore it ,I am new here..
If the number n is not divisible by 10 then the value of this expression (the remainder of the division)
n %= 10;
will never be equal to 0.
Also the variable i is not initialized.
int i;
You should write
int i = 0;
do
{
++i;
} while ( n /= 10 );
printf( "%d\n", i );

C Why does scanf returns the false output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
here is my code.
#include <stdio.h>
/*
CALCULATING INVERSE OF A MATRIX
*/
int main(){
int matris[3][3];
int i,j;
for(i = 0; i<3; i++){
for (j=0;j<3;j++){
scanf("%d", &matris);
}
}
for(i = 0; i<3; i++){
for (j=0;j<3;j++){
printf("%d ", matris);
}
}
}
When i give the input as 1-9 numbers, i expect the program to print 3x3 matrix within 1-9 but it returns this output
Output:
1
2
3
4
5
6
7
8
9
6487536 6487536 6487536 6487536 6487536 6487536 6487536 6487536 6487536
Where is my mistake? Thx for help
scanf("%d", &matris); is wrong as long as you want to write different data in different elements of the array matris.
printf("%d ", matris); is also wrong because you want to print out different values from different elements of the array matris.
Rather do it like that:
scanf("%d",&matris[i][j]);
and
printf("%d",matris[i][j]);
You can not use the address-of operator & on the base address of the 2D array (matris in this case), you should mark the exact address that you would like to get the number to it as people mentioned here &matris[i][j], because each cell in this array has a different address.
and in case of the printf you just printed the base address of the 2D array.

for loop in c from scanf [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am attemping to solve a programming exercice on the CodeChef platform in C, but i'm having trouble reading the input...
Here what i am supposed to read:
2
3
1 4
2 1
0 1
0 1
2
1 3
0 2
1 0
Here is the link to the exercice for more details, but in summary, the first line contains a single integer N, the second line is a integer T, the following T lines all contain 2 space-separated integers X Y, followed by a last line of space separated integers. N is the number of blocks starting by a integer for T.
Here the code that i wrote in order to read this data:
int main(void) {
int T, N, i, j, x, y;
scanf("%d", &T);
for (i=0; i>T; i++) {
scanf("%d", &N);
// ....
for (j=0; j>N; j++) {
scanf("%d %d", &x, &y);
// ....
}
scanf("%d %d",&x, &y);
// ....
}
}
What seems to be happening is the code seems to be only reading the N variable after which the program just exits normally... I've been staring at this for hours and I feel lost, i can't seem to figure out what's going on. Any help is appreciated. Thank ypi

scanf() is not taking more than 6 times in a for loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
It is really very unexpected, I have looked a lot of time where can be the problem but couldn't find it.
Problem is:
Say I've given 7 integers to calculate their average, but it is taking first 6 numbers and calculating their average.
However I give any number more than 6, it will calculate only first 6's
The code is:
#include <stdio.h>
int main()
{
int n,i,total = 0;
int numArr[n];
printf("How many numbers do you want to print? => ");
scanf("%d",&n);
for(i = 0; i < n; i++)
{
scanf("%d",&numArr[i]);
total += numArr[i];
}
printf("--------------------------------\n");
printf("Average of this %d numbers: %d",n,total/n);
}
I'm not sure whether it is compiler problem or problem of my code.
initialize n first and then declare numArr[n]
Modify your code as
int n,i,total = 0;
printf("How many numbers do you want to print? => ");
scanf("%d",&n);
int numArr[n];
then scan the array.

Summing elements of a 3d array in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Im trying to sum the elements of a 3d array in C. The code recognizes that position check[1][1][0]=4 and adds 4 to sum when the loop reaches this position. However for the rest of the array it continues to add on this value again, and then it adds the total sum again for the rest of the positions of the array. Can anyone see why?
#include <stdio.h>
main() {
int check[3][3][3]={ 0 };
int size=2;
int i,j,k,sum=0;
check[1][1][0]=12;
for(k=0;k<size;k++) {
for(j=0;j<size;j++) {
for(i=0;i<size;i++) {
printf("i=%d, j=%d,k=%d, checkijk=%d ",i,j,k,check[i][j][k]);
sum+=sum+check[i][j][k];
printf("sum=%d\n", sum);
}
}
}
printf("The sum is %d\n",sum);
}
sum+=sum+check[i][j][k];
should be
sum+=check[k][j][i];
And if you want to sum all the values int size = 2; must be int size = 3;

Resources