Hey guys,
I am particularly new to coding I tried with C while giving a solution to this problem but I'm always getting an SIGSEGV error
Pleas Help!!
Here is my code
#include <stdio.h>
int main() {
int a[20], x, count=0;
do{
scanf("%d\n", &x);
a[count]=x;
count++;
}while(x!=42);
x=0;
for(x; x<count-1; x++){
printf("%d\n", a[x]);
}
return 0;
}
is there any problem with this code.??it gives the output but still its showing error.
#include<stdio.h>
int main()
{
int i,n,j;
int ch[5];
scanf("%d",&n);
for(j=0;j<n;j++)
{
scanf("%d",&ch[j]);
}
for(i=0;i<n;i++)
{
if(ch[i]<ch[i+1])
printf("\n%d",ch[i]);
else
break;
}
if(ch[i]>ch[i+1])
printf("\n%d",ch[i]);
return 0;
}
Hello atifasr,
A few remarks about your code:
What do you think happens to your program if the input contains more than 100 numbers ?
Your variables size and i are effectively the same thing. They’re both initialized to 1, and incremented at the same time.
The variable ans is used uninitialized on the first iteration of the loop.
You’re checking if ans == 42 at the start of a loop that will be entered only if ans != 42. That condition in your if can never be true.
You are THEN storing the input into ans, and not checking if it is now == 42. When 42 is encountered, it is going to be put in your array, supposing less than 100 numbers have been read so far.
I suggest you think about what you want to do exactly and what the constraints are, sort everything out so that it makes sense, and then start typing code.