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;
Related
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 months ago.
Improve this question
sum += (i\*8);
Can some one explain the problem? I, as a beginner, I am getting confused.
#include<stdio.h>
int main(){
int sum = 0;
for (int i == 1; i < 11; i ++)
{
sum += (i*8);
} I
printf("The value of sum is %d", sum);
return 0;
}
can someone explain
Instead of write i == 1, write i = 1. That assigns 1 to the variable i.
i == 1 : comparison
i = 1 : assignment
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 );
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.
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
(Write a C function that should check if a matrix (4x5) is sparse or not. Knowing that: sparse matrix is a matrix that has zeros more than the half of its size.)
That's a problem for a sheet in out subject
here is my code:
#include <stdio.h>
#include <stdlib.h>
int Spare(int [4][5]);
int main()
{
int arr[4][5];
int m,n;
for(m=0;m<4;m++){
for(n=0;n<5;n++){
scanf("%d ",&arr[4][5]);
}
}
Spare(arr[4][5]);
return 0;
}
int Spare(int Arr[4][5]){
int i,j;
int zerocount=0;
for(i=0;i<4;i++){
for(j=0;j<5;j++){
if(Arr[i][j]==0){
zerocount++;
}
}
}
if(zerocount>=10) return 1;
else return 0;
}
Its Running but after the user enter inputs of array it stops working!
Any Help Guys?
Change scanf("%d",&arr[4][5]) into scanf("%d",&arr[m][n]). You are just storing the input in arr[4][5] everytime in the loop.
The Spare(arr[4][5]) pass the element of fifth row and sixth column only which doesn't exist.
The Spare function expects an array as parameter and you are passing the single element only.
The function call must be done in this way: Spare(arr)
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
In the following program when the value of N is less than 100 the program is executing perfectly but for bigger values of N its showing segmentation fault.I sit because of less memory or anything wrong with program??
#include<stdio.h>
int main()
{
int N,iteration,MAX_ITERATONS;
int i,j,k,n,index,boundary1,boundary2;
double h[2][100][100];
int current = 0;
int next = 1;
printf("Enter the number of points\n ");
scanf("%d", &N);
boundary1=0.4*N;
boundary2=(0.6*N);
printf("The boundary is between %d and %d .\n",boundary1,boundary2);
for(k=0;k<2;k++)
{
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if((i==0)&&(j>=boundary1&&j<boundary2))
h[k][i][j]=100;
else
h[k][i][j]=20;
}
}
}
printf("Initial Values\n");
index = N/10;
for(i=0;i<N;)
{
for(j=0;j<N;)
{
printf("%.2f\t",h[0][i][j]);
j=j+index;
}
i=i+index;
printf("\n");
}
}
When N > 100, h is accessed to an index greater than 100, inside the nested for loop
h[k][i][j]=100;
but h is defined as
double h[2][100][100];
You are going out of bounds for h
If you want N as greater than 100 you need to redefine h or malloc it.