getting a runtime error in the code - c

GETTING A RUNTIME ERROR IN THE given code
INITIALISING VARIABLES
#include <stdio.h>
#include <stdlib.h>
int main(){
int k;
int n;
int i;
int *a;
int b;
int t;
int f=0;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&k);
dynami
a=malloc(sizeof(int)*n);
for(i=0;i<n;i++){
scanf("%d",a[i]);
a[i]=(k/a[i]);
}
this is the point where the execution fails in runtime
for(i=0;i<n;i++){
scanf("%d",b);
a[i]=(a[i]*b);
finding the max element
if(f==0){
n=a[0];
f=1;
}
if(a[i]>n)
n=a[i];
}//END FOR LOOP
printf("%d\n",n);
}
return 0;
}

Change
scanf("%d",b);
and
scanf("%d",a[i]);
to
scanf("%d",&b);
and
scanf("%d",&a[i]);
respectively and the code will work. scanf expects an argument of type int* but you provide an argument of type int. The & is the address-of operator and it gives the address of the variable,which in your case is an int* since both a[i] and b are ints.

scanf("%d",b);
should be :
scanf("%d",&b);
you miss a '&' that's why runtime error.

this
scanf("%d",b);
should be
scanf("%d",&b);

Related

Null pointer assignment error in C, segmentation fault error in code

I using a old version of Borland for C lang.
At the beginning of the program you enter the name (full name, FIO), then 4 digits (as grades). The program calculates the average among 5 entered FIO and back a average number.
#include <stdio.h>
#include <conio.h>
int main(){
struct nya{
char a[100];
int x[4];
}A[5];
int i;
for(i=0;i<5;i++){
puts("FIO");
scanf("%s", A[i].a);
puts("4 ocenki");
for(int g=0;g<4;g++){
scanf("%i", A[i].x[g]);
}
clrscr();
}
float bird=0, comme[5];
for(i=0;i<5;i++){
comme[i]=0;
}
for(i=0;i<5;i++){
for(int g=0;g<4;g++){
comme[i]+=A[i].x[g];
}
comme[i]=comme[i]/4;
bird+=comme[i];
}
bird=bird/5;
printf("Sredny = %f", bird);
}
change scanf("%i", A[i].x[g]); to scanf("%i", &A[i].x[g]); (with a &)
thanks to #Blaze

How to read a bidimensional array inside a function?

i have this code how to read the bidimensional array using a function?
i write this function it works read all the numbers but when i output to console the array there are not the values that i entered
ex
Input:
2 1 2 3 4
Output:
16 256
1 4525376
#include <stdio.h>
#include <stdlib.h>
void citMat(int a, int n) {
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("a[%d][%d]",i,j);
scanf("%d", &a);
}
}
int main()
{ int i,j;
int a[10][10],n;
printf("Introdu n:");
scanf("%d", &n);
citMat(a[10][10],n);
for(i=1;i<=n;i++){
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
You need to change the prototype to (Here array dimension is important)
void citMat(int a[10][10], int n)
Other changes are explained by others (The whole code is below)
#include <stdio.h>
#include <stdlib.h>
void citMat(int a[10][10], int n) {
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("a[%d][%d]:",i,j);
fflush(stdout);
scanf("%d", &a[i][j]);
}
}
int main()
{ int i,j;
int a[10][10],n;
printf("Introdu n:");
scanf("%d", &n);
if (n > 10)
{
fprintf(stderr, "Invalid input %d\n", n);
return 1;
}
citMat(a,n);
for(i=0;i<n;i++){
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
1. If you want to pass a 2-d array to function .Change your function definition to -
void citMat(int a[10][10], int n) { // first parameter to take a 2-d int array
2. And then inside function citMat to take input-
scanf("%d", &a[i][j]); // you need to write like this
Note -
1. Array indexing starts from 0 , so if you have array a[n] then it have valid index from 0 to n-1 .
So start reading from 0 and till n in all for loops . If you include n then you would access index out of bound and writing to it will cause undefined behaviour.
So, look out for that .
2. int main() -> int main(void) or int main(int argc,char **argv)
You need to change few things in your program to make it work
1) Call the function with the base address of the array, lik
citMat(a,n);
2) Change your function definition to,
void citMat(int a[10][10], int n)
to make it accept 2D array as parameter.
3) Change the scanf() to read for each element,
scanf("%d", &a[i][j]);
4) Since the array index starts from 0, change all the for loops termination condition to
for(i=1;i<n;i++)

Can't find error in my c code

can't find an error please help, this is a C code to find minimum number of possible quadrangles on co-ordinate plane
#include <stdio.h>
int quadrangle(int *,int);
int min(int,int);
int main(){
int t,i,j,n,p[n][n];
printf("\nEnter the number of test cases");
scanf("%d",&t);
for(i=0;i<t;i++){
scanf("%d",&n);
for(j=0;j<n;j++){
scanf("%d %d",&p[j][0],&p[j][1]);
}
printf("%d",quadrangle(&p[0][0],n));
}
return 0;
}
int quadrangle(int *p,int len){
int f=0,s=0,t=0,fo=0,i;
for(i=0;i<len;i++){
if(*(p+i*len)>0&&*(p+i*len+1)>0)
f++;
if(*(p+i*len)>0&&*(p+i*len+1)<0)
s++;
if(*(p+i*len)<0&&*(p+i*len+1)<0)
t++;
if(*(p+i*len)<0&&*(p+i*len+1)>0)
fo++;
}
return min(min(f,s),min(t,fo));
}
int min(int a,int b){
if(a>b) return b;
else return a;}
I tested it on codeblocks software it is compiling okay but ends abruptly when I execute it . The control doesn't even enter main() .
n is uninitialized and you create an array of size n (guess what's the value of n) here:
int t,i,j,n,p[n][n];
Declare the array after n gets initialized, i.e, change
int t,i,j,n,p[n][n];
to
int t,i,j,n;
and add
int p[n][n];
after
scanf("%d",&n);

I am getting an Memory fault(coredump) error message in my c program?

I am not able to understand why it is showing this error.
I have never encountered such an error before.
Here is my code, can you identify the mistake or the cause of it :
#include<stdio.h>
#include<math.h>
void dec2bin(int n,int bin[1000]){
int num = 0, index = 0, i;
while (n != 0){
bin[index] = n%2;
index++;
n = n/2;
}
}
int Sub(int a[100],int b[1000],int ac[100],int siz){
int i=siz-1,k=0;
for(;i>=0;i--){
if(b[i]){
a[k++]=ac[siz-i-1];
}
}
return k;
}
int sum(int a[100],int s){
int i,sum=0;
for(i=0;i<s;i++)
sum+=a[i];
return sum;
}
main(){
int b[1000],sub[100],a[100],n,i,s,count=0;
printf("Enter n: ");
scanf("%d",n);
for(i=0;i<n;i++){
printf("Enter number %d",i+1);
scanf("%d",&a[i]);
}
printf("Enter S: ");
scanf("%d",s);
int no=(int)pow(2.0,(float)n);
for (i=0;i<1000;i++)
b[i]=0;
for(i=0;i<no;i++){
dec2bin(i,b);
int siz=Sub(sub,b,a,n);
if(sum(sub,siz)==s)
count++;
}
printf("Subsets: %d",count);
}
And this code shows memory fault error immediately after entering the value of n.
You're using scanf slightly incorrectly.
The arguments following the format string need to be pointers to your objects, not the objects themselves.
scanf("%d", &n);
Do remember though, that scanf is very dangerous to use. Behavior is undefined for instance if the integer would overflow. Better to read a line safely then use strtol to parse it, since you can detect errors properly.
when taking input for n.
scanf("%d", &n);
You forgot a &

Can't find max and min in my code

I have written two code for max and min. The first is without, the second with an extra function.
The first one works, but for second one minimum is always 1. Why?
What causes the problem?
(1) Code without function:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int x,n,min=x,max=1,cnt;
while(1){
cnt=0;
printf("how many numbers do you want to enter\n");
scanf("%d",&n);
printf("enter your numbers\n");
while (cnt!=n){
scanf("%d",&x);
cnt++;
if(x>max)
max=x;
if(x<min)
min=x;
}
printf("maximum is:%d\n",max);
printf("minimum is:%d",min);
getch();
system("cls");
}
return 0;
}
(2) Code with function:
#include <stdio.h>
#include <stdlib.h>
int maximum(int);
int main(int argc, char *argv[]) {
int n;
printf("how many numbers do you want to enter\n");
scanf("%d",&n);
maximum(n);
return 0;
}
//*****************************************
int maximum(int n){
int i,a,max=1,min=a;
printf("enter your number\n");
for(i=1;i<=n;i++){
scanf("%d",&a);
if(a>max)
max=a;
if(a<min)
min=a;
}
printf("maximum is:%d\n",max);
printf("minimum is:%d",min);
getch();
}
Your code causes undefined behavior. Undefined behavior means, anything could happen.
You have not initialized the variable a and you are doing min=a;
C99 section 6.7.8 Initialization:
If an object that has automatic storage duration is not initialized explicitly,
its value is indeterminate.
C99 section 3.18 Un-deļ¬ned behavior:
Behavior, upon use of a non-portable or erroneous program construct, of erroneous
data, or of indeterminately valued objects, for which this International Standard
imposes no requirements.
This means, compiler is free to do anything. It can crash your program, your program might print JLo pic on your monitor...Etc...
Your code invokes undefined behavior because the assignment min=x in first and min=a in second done without initializing x and a. In this case you may get anything, either expected or unexpected result or segmentation fault or program crash.
To fix this issue you can modify your code as
Code 1.
int x,n,min,max=1,cnt;
while(1){
cnt=1;
printf("how many numbers do you want to enter\n");
scanf("%d",&n);
printf("enter your numbers\n");
scanf("%d",&x);
min = x;
while (cnt!=n){
scanf("%d",&x);
...
...
Code 2.
int maximum(int n){
int i,a,max=1,min;
printf("enter your number\n");
scanf("%d",&a);
min = a;
for(i=1;i<n;i++){
scanf("%d",&a);
...
...

Resources