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
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 3 years ago.
Improve this question
Write a C program to calculate (x^n)/(n!) where x is a floating point number and n is an integer greater than or equal to zero.
I coded the following:
#include<stdio.h>
#include<math.h>
void main()
{
float x,p;
int i,n,f=1;
printf("Enter the value of x,n\n");
scanf("%d %d",&x,&n);
if(n>0)
{
for(i=1;i<=n;i++)
{
f=f*i;
}
p=(float)pow(x,n)/f;
printf("The value of p is %.3f",p);
}
if(n==0)
{
p=(float)pow(x,n)/1;
printf("The value of p is %d",p);
}
getch();
}
But this is not running well. Where have I gone wrong?
PS: Edit
In your question I have recognize 3 problems.
main problem is scanf("%d %d",&x,&n); should be change into scanf("%f %d",&x,&n);
because x is `float type #dragosht has mentioned it.
printf("The value of p is %d",p); should be correct as printf("The value of p is %f",p); beacause p is also float type.
It is better to set p = 0; at the beginning because you did not assign value to p using keyboard. There for some times you will get corrupted values because of this.
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.
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 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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want make a program to print the number sequence from beginning to end with a negative increment specified.
Input format:
I Put a single line consisting of three integers: the beginning, the negative increment, and end sequence. (ex: scan 9 -2 3
Output format:
Output a sequence of numbers from start to finish is printed per line. End output with a newline character.(it will be:
9
7
5
3
Here is the solution:
#include<stdio.h>
int main(void)
{
int start,step,end,i;
scanf("%d %d %d",&start,&step,&end);
for(i=start;i>=end;i+=step)
printf("%d ",i)
printf("\n");
}
#include<stdio.h>
int main(void)
{
int start,step,end,i;
scanf("%d %d %d",&start,&step,&end);
for(i=start;i>=end;i+=step)
printf("%d ",i)
printf("\n");
}
int start,end,inc;
scanf("%d %d %d",&start,&inc,&end);
for(;start>=end;start-=inc)
printf("%d ",start);