Can't find error in my c code - c

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);

Related

program doesn't read a file properly

For homework I have to write a program where I am typing the string of n integer and then the program that prints the string,calculates the arithmetic mean and makes a new file only with even numbers and at the end prints the new file on screen.
And here are 2 programs
#include<stdio.h>
main()
{
int x,n,i;
FILE *p;
p=fopen("podaci.dat","wb");
printf("n=");
scanf("%d",&n);
fwrite(&n,sizeof(int),1,p);
for(i=0;i<n;i++)
{
printf("x=");
scanf("%d",&x);
fwrite(&x,sizeof(int),1,p);
}
fclose(p);
}
#include<stdio.h>
void stampa(int n,int a[])
{
int i;
for(i=0;i<n;i++)
printf("%5d",a[i]);
printf("\n");
}
float ars(int n,int a[])
{
int i,s=0;
float ars=0;
for(i=0;i<n;i++)
s+=a[i];
return 1.0*s/n;
}
main()
{
int i,n;
FILE *p,*u;
u=fopen("niz.dat","wb");
p=fopen("podaci.dat","rb");
fread(&n,sizeof(int),1,p);
printf("n=%d\n",n);
int a[n],m=0;
for(i=0;i<n;i++)
{
fread(&a[i],sizeof(int),1,p);
if(a[i]%2==0)
{
m+=1;
}
fwrite(&m,sizeof(int),1,u);
for(i=0;i<n;i++)
if(a[i]%2==0)
{
fwrite(&a[i],sizeof(int),1,u);
}
stampa(n,a);
printf("ars=%.2f",ars(n,a));
fclose(p);
fclose(u);
}
}
When I type more than 2 numbers the program instead of those numbers reads them as 0 and sometimes it adds numbers.
You cannot declare an array a[n] where n is a variable (number that you don't know at compile time but only once you read the file).
You can ether declare a[N] where N is a number big enough defined in with #DEFINE
#include <stdio.h>
#include ...
#DEFINE N 10000
//...
int a[N];
or you have to allocate a[] dynamically
int * a;
a = (int*)malloc(sizeof(int)*n);
You can read more about this here and here

Identify an Armstrong number

I Wrote this code which can identify whether a number is a Armstrong number or not
#include <stdio.h>
#include <stdlib.h>
int n;
const int input()
{
printf("insert n:");
scanf("%d",&n);
return n;
}
int Num_amount()
{
int amount=0;
while(n>=10)
{
amount++;
n=n/10;
if(n<10)
amount++;
}
return amount;
}
int Armstrong()
{
n=input();
int v;
int z=0;
int y=10
int x=Num_amount();
int m[100]={};
int i;
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
return z;
}
int main()
{
int z=Armstrong();
printf("%d",z);
}
When run with n=153 i always get 0.After several debugging,I found out the problem is somewhere in the Armstrong function(most likely)
int Armstrong()
{
n=input();
int v;
int z=0;
int y=10
int x=Num_amount();
int m[100]={};
int i;
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
return z;
}
The debug watches indicate that instead of execute the for loop,it went straight to the return z line,I have tried everything but still can't figure it out.Can you tell me what the problem is?
You are getting the wrong result because of some logical error. When you are choosing a variable to be global, you need to consider that the variable value can be modified by any function and in this case, you have already modified its value in num_amount function. You have also made some logical error in Num_amount and Armstrong function.
You haven't included math.h header file for pow.
Here is your modified code,
#include <stdio.h>
#include <stdlib.h>
#include<math.h> //<-------------Should have included
int n;
const int input()
{
printf("insert n:");
scanf("%d",&n);
return n;
}
int Num_amount() //<------------modified
{
int z = n; //<--------take a copy of global n
int amount=0;
while(z>0)
{
amount++;
z=z/10;
}
return amount;
}
int Armstrong() //<------------modified
{
n=input();
int v;
int z=0;
int x=Num_amount();
int i;
while(n>0)
{
v=n%10;
z+=pow(v,x);
n/=10; //<-------modification of global n
}
return z;
}
int main()
{
int z=Armstrong();
printf("%d",z);
}
Found a lot problems with the code. Here is a modified version.
1. Do not use a global variable.
2. Make calculation for power easier.
3. Return the status of result, not the result. You want to check whether number is Armstrong or not.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int no_digits(int n){
int digits = 0;
while(n){
digits++;
n = n/10;
}
return digits;
}
int armstrong(){
int n;
printf("insert n:");
scanf("%d",&n);
int digits = no_digits(n);
int curnum = 0,original = n;
while(n){
curnum += pow(n%10,digits);
n /= 10;
}
if(original == curnum)
return 1;
return 0;
}
int main(){
if(armstrong())
printf("Is Armstrong\n");
else printf("Not Armstrong\n");
}
Let's take a look at your loop:
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
What's the value of n at this point? You've set it in the previous call to Num_amount like so:
while(n>=10)
{
amount++;
n=n/10;
if(n<10)
amount++;
}
So, after Num_amount has finished executing, n must be less than 10, meaning the loop in Armstrong won't execute.
This is a big reason why you shouldn't use globals, even in a toy program like this. If you use it for different purposes in different places, you just create headaches like this.
At the very least, you should change your code such that n is passed as a parameter to Num_amount and Armstrong.
Your function Num_amount() return "n" value is already less than 10 and for loop never run.

getting a runtime error in the code

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);

C program keeps crashing

My program keeps crashing. The codes seems legit and correct. Wonder what's the problem.
#include <stdio.h>
void queue(int length,int turns){
int permutations,totalTurns;
turns++;
if (length>0){
queue(length-1,turns);
if (length>1){
queue(length-2,turns);
}
}
else{
permutations++;
totalTurns+=turns;
}
}
int main()
{
while(true){
int length;
float average;
int permutations=0;
int totalTurns=0;
printf("Queue length: ");
scanf("%d", &length);
queue(length,-1);
average=totalTurns/permutations;
printf("The average turns are %f", average);
}
}
int permutations=0;
average=totalTurns/permutations;
You're dividing by zero.
Note that the permutations variable you've declared in main() is different from the one in queue().
You should probably return the permutations value from queue(), like this:
int queue(int length,int turns){
int permutations = 0;
...
return permutations;
}
int main(void) {
...
int permutations = queue(length,-1);
}
You should declare permutations as a global variable, i.e. outside main function as well as totalTurns because as others mentioned it's always 0 because even thought you declare it in function queue it's being forgotten outside it.
#include <stdio.h>
static int permutations=0;
static int totalTurns=0;
void queue(int length,int turns){
turns++;
if (length>0){
queue(length-1,turns);
if (length>1){
queue(length-2,turns);
}
}
else{
permutations++;
totalTurns+=turns;
}
}
int main()
{
while(true){
int length;
float average;
int totalTurns=0;
printf("Queue length: ");
scanf("%d", &length);
queue(length,-1);
average=totalTurns/permutations;
printf("The average turns are %f", average);
}
}

Calculate Reverse Matrix?

Why should i get crash with this , where did i wrong !? :(
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
const volatile max=15;
int read(float[][max],float[][max]);
void compute1(float[][max],float[][max],float[][max],int,int,int,float);
float compute2(float[][max],int);
void display(float[][max],float[][max],int,float);
int main(){
float num[max][max],g[max][max],v[max][max],a[max][max];
int dn,u;
float det;
int register i,j;
dn=read(num,a);
det=compute2(num,dn);
for(i=0;i<dn;i++)
for(j=0;j<dn;j++){
compute1(a,g,v,dn,i,j,det);
}
display(a,v,dn,det);
getch();
return 0;
}
//****************************************************************************
int read(float num[][max],float a[][max]){
int dn;
clrscr();
int register i,j;
printf("\nenter degree of matrix:");
scanf("%d",&dn);
clrscr();
for(i=0;i<dn;i++){
printf("\n\n\nenter arguments of row[%d]:\n\n",i);
for(j=0;j<dn;j++){
scanf("%f",*(num+i)+j);
*(*(a+i)+j)=*(*(num+i)+j);
}
}
return dn;
}
//****************************************************************************
void display(float c[][max],float inv[][max],int dn,float det){
int register i,j;
clrscr();
printf("\n\n\n\n\n\t\t\t --ORIGINAL MATRIX--\n");
for(i=0;i<dn;i++){
printf("\n\t\t");
for(j=0;j<dn;j++)
printf("%10.3f",c[i][j]);
}
printf("\n\n\n\t\t\t --INVERSE MATRIX--\n");
for(i=0;i<dn;i++){
printf("\n\t\t");
for(j=0;j<dn;j++)
printf("%10.6f ",inv[i][j]);
}
printf("\n\n\n\t\tعؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤ؟");
printf("\n\t\t³ determinan of matrix= %19.7f ³ ",det);
printf("\n\t\tہؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤؤظ");
}
//****************************************************************************
void compute1(float g[][max],float v[][max],float inv1[][max],int dn,int r,int c,float e){
int col=0,row=0,add=r+c,y;
float y1=1;
if(add%2)
y1=-1;
int register i,j;
for(i=0;i<dn-1;i++){
if(i==r)
row=1;
col=0;
for(j=0;j<dn-1;j++){
if(j==c)
col=1;
v[i][j]=g[i+row][j+col];
}
}
inv1[c][r]=y1*compute2(v,dn-1)/e;
}
//****************************************************************************
float compute2(float c[][max],int s){
float *h=(float*)malloc(sizeof(float)),h1=1;int y=s-1,k=s;
int register i,j;
while(y>0){
for(i=1;i<k;i++){
if(c[y][y]!=0)
h[i-1]=c[y-i][y]/c[y][y];
else{
for(j=0;j<s;j++)
c[y][j]+=c[y-i][j];
h[i-1]=c[y-i][y]/c[y][y];
}
}
y--;
k--;
for(i=0;i<k;i++)
for(j=s-1;j>=0;j--)
c[y-i][j]=c[y-i][j]-h[i]*c[y+1][j];
}
for(i=0;i<s;i++)
h1*=c[i][i];
return h1;
}
There's quite a number of problems with your code. In compute2 you're allocating memory for.. a row?
float *h=(float*)malloc(sizeof(float)),h1=1;int y=s-1,k=s;
You're allocating space for just one float though. That could be a possible source of crashes.
You never actually deallocate the memory either, too.
You need to use debugging and logging tools rather than eyeballing the code. We don't even know what your crash is.
However here is one suspicious place
if(c[y][y]!=0)
h[i-1]=c[y-i][y]/c[y][y];
else{
for(j=0;j<s;j++)
c[y][j]+=c[y-i][j];
h[i-1]=c[y-i][y]/c[y][y];
}
In both branches of the if...else you are dividing by c[y][y]. You know when it enters the else that is zero. Unless the for loop changes it you will have divide by zero. So I suggest you test it.

Resources