Why is it reading from the file randomly? - c

#include <stdio.h>
/*this puts the numbers fomr the file into two matrices*/
void readMatrices(FILE*numbers, int array[4][4], int array2[4][4])
{
int i,j,num;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
fscanf(numbers,"%d",&array[i][j]);
}
}
for(i=0;i<=4;i++)
{
for(j=0;j<4;j++)
{
fscanf(numbers,"%d",&array2[i][j]);
}
}
}
void printMatrices(int array[4][4],int array2[4][4]) /* prints out the matrices*/
{
int i,j,num;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%d ",array[i][j]);
}
printf("\n");
}
printf("\n");
for(i=0;i<=4;i++)
{
for(j=0;j<4;j++)
{
printf("%d ",array2[i][j]);
}
printf("\n");
}
printf("\n");
}
/*multiplies the matrices*/
void multiplyMatrices(int array[4][4],int array2[4][4],int result[4][4])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
result[i][j] = (array[i][0]*array2[0][j])+(array[i][1]*array2[1][j])+(array[i][2]*array2[2][j])+(array[i][3]*array2[3][j]);
printf("%d ",result[i][j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
int array[4][4],array2[4][4],results[4][4];
FILE*numbers;
numbers = fopen("numbers.txt", "r");
readMatrices(numbers,array,array2);
while(array[0][0]!=0)
{
printMatrices(array,array2);
multiplyMatrices(array,array2,results);
readMatrices(numbers,array,array2);
}
fclose(numbers);
return 0;
}
Here is the file. This reads the file but in random groups of fours rather than in order like it should be, this is an issue because it's wrong and it also won't read the 0 in the file which causes it to loop infinitely.

well i figured it out, the equals signs in the for loop cause some errors, i don't really know why it did that but they were causing issues, thanks for the suggestions.

Related

Why is my code giving right output in codeBlocks but wrong output in HackerRank?

I am trying to solve a problem of HackerRank. The problem wants me to find out if a string can be changed into a palindrome string or not with a certain number of changing characters of that string. That certain number will be given by input. I am giving the link of full description of the problem below.
In my code, I first checked if the string is already palindrome or not. Then I checked some cases under the first checking for the given number of changes to find out if that string can be changed into palindrome after changing the characters.
But I am getting different output for my code. Output that I found from code blocks is not the same as the output which I found in HackerRank.
Here is the link of the full problem statement
my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print(char str[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%c",str[i]);
}
}
void cpy(char qtr[],char str[],int n)
{
int i;
for(i=0;i<n;i++)
{
qtr[i]=str[i];
}
}
void rev(char str[],int n)
{
int i;
char qtr[100000];
for(i=0;i<n;i++)
{
qtr[i]=str[n-1-i];
}
for(i=0;i<n;i++)
{
str[i]=qtr[i];
}
}
int main()
{
char str[100],str2[100000],qtr[100000],c;
int n,k,i;
scanf("%d",&n);
scanf("%d",&k);
fflush(stdin);
gets(str);
cpy(qtr,str,n);
rev(str,n);
cpy(str2,str,n);
int same=0;
if(strcmp(str2,qtr)==0)
{
same=1;
}
if(k==n)
{
c=qtr[i]+1;
for(i=0;i<k;i++)
{
if(qtr[i]==c)
{
c=c+1;
i=-1;
continue;
}
}
for(i=0;i<k;i++){qtr[i]=c;}
print(qtr,n);
return 0;
}
if(same==1)
{
if(n%2!=0)
{
if(qtr[n/2]!='9'){qtr[n/2]='9';}
else
{qtr[n/2]='1';}
k--;
for(i=0;i<n;i++)
{
if(k<0){printf("-1");break;}
if(k==0){printf(qtr,n);break;}
if(qtr[i]=='9')
{
qtr[i]='1';
qtr[n-1-i]='1';
k=k-2;
}
else
{
qtr[i]='9';
qtr[n-1-i]='9';
k=k-2;
}
}
}
else
{
for(i=0;i<n;i++)
{
if(k<0){printf("-1");break;}
if(k==0){print(qtr,n);break;}
if(qtr[i]=='9')
{
qtr[i]='1';
qtr[n-1-i]='1';
k=k-2;
}
else
{
qtr[i]='9';
qtr[n-1-i]='9';
k=k-2;
}
}
}
}
else
{
if(n%2==0)
{
int arr[n];
for(i=0;i<n;i++){arr[i]=0;}
while(k>=0)
{
int g=0;
for(i=0;i<n/2;i++)
{
if(qtr[i]!=qtr[n-i-1])
{
arr[i]=1;
arr[n-i-1]=1;
qtr[i]=qtr[n-i-1];
k--;
g=1;
}
}
if(k==0){print(qtr,n);return 0;}
if(k%2!=0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==1)
{
arr[i]=2;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k--;g=1;
break;
}
}
}
if(k==0){print(qtr,n);return 0;}
if(k%2==0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==0)
{
arr[i]=3;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k=k-2;g=1;
break;
}
}
}
if(k==0){print(qtr,n);return 0;}
if(g==1){printf("-1");return 0;}
}
printf("-1");return 0;
}
else
{
int arr[n];
for(i=0;i<n;i++){arr[i]=0;}
while(k>=0)
{
int g=0;
for(i=0;i<n/2;i++)
{
if(qtr[i]!=qtr[n-i-1])
{
arr[i]=1;
arr[n-i-1]=1;
qtr[i]=qtr[n-i-1];
k--;
g=1;
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(k%2!=0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==1)
{
arr[i]=2;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k--;g=1;
break;
}
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(k%2==0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==0)
{
arr[i]=3;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k=k-2;g=1;
break;
}
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(g==1){printf("-1");return 0;}
}
printf("-1");return 0;
}
}
}

Getting negative numbers or zero as output of factorial in C

I've written a very simple program which calculate the factorial of a number, the problem is that from 30 to 60 approximately, it returns a negative number and from 70 it returns 0.
I don't what I've done wrong. Could this problem depend on the computing power of my computer?
Here's the code:
#include <stdio.h>
int main(){
int x, i;
long long int f = 1;
printf("Insert a number:");
scanf("%d", &x);
if (x == 0){
printf("0! = 1");
}
else {
for (i = 1; i <= x; i++){
f *= i;
}
printf("%d! รจ = %lli", x, f);
}
return 0;
}
Here is my code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void Carry(int bit[],int pos)
{
int i,carray=0;
for(i=0;i<=pos;i++)
{
bit[i]+=carray;
if(bit[i]<=9)
{
carray=0;
}
else if(bit[i]>9&&i<pos)
{
carray=bit[i]/10;
bit[i]%=10;
}
else if(bit[i]>9&&i>=pos)
{
while(bit[i]>9)
{
carray=bit[i]/10;
bit[i]%=10;
i++;
bit[i]=carray;
}
}
}
}
int main()
{
int num,pos,digit,i,j,m,n;
double sum=0;
int *fact;
printf("input want to calculute factorial num:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
sum+=log10(i);
}
digit=(int)sum+1;
if(!(fact=(int *)malloc((digit+1)*sizeof(int))))
{
printf("malloc failed\n");
return 0;
}
for(i=0;i<=digit;i++)
{
fact[i]=0;
}
fact[0]=1;
for(i=2;i<=num;i++)
{
for(j=digit;j>=0;j--)
{
if(fact[j]!=0)
{
pos=j;
break;
}
}
for(j=0;j<=pos;j++)
{
fact[j]*=i;
}
Carry(fact,pos);
}
for(j=digit;j>=0;j--)
{
if(fact[j]!=0)
{
pos=j;
break;
}
}
m=0;
n=0;
for(i=pos;i>=0;i--)
{
printf("%d",fact[i]);
m++;
if(m%4==0)
{
printf(" ");
}
if(m==40)
{
printf("\n");
m=0;
n++;
if(n==10)
{
printf("\n");
n=0;
}
}
}
printf("\n\n");
return 0;
}

Star Pattern in C- 2nd half is not printing

I've been trying to print star pattern but it's not working after
"if(i>4)" as you can see the second image there's a gap in between which implies that the spaces are getting printing or the new line but the star pattern is not getting printed
enter code here
//the first half
int main() {
int i,j,k,m;
for (i=1;i<=4;i++) {
for (j=1;j<=4-i;j++) {
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
printf("\n"); // to print the first half
}
//the second half
if(i>4){
for(j=1;j<=i-2;j++)
{
for (k=1;k<=j;k++) {
printf(" ");
}
for(m=1;m<=4-i;m++)
{
printf("*");
}
printf("\n");
}
}
return 0;
}
Try this
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
n = n/2+1;
for(int i=1;i<=n;i++)
{
for(int j=n-i;j>=1;j--)
{
printf(" ");
}
for(int k=1;k<=i;k++)
{
printf("*");
}
printf("\n");
}
for(int i=1;i<=n;i++)
{
for(int k=1;k<=i;k++)
{
printf(" ");
}
for(int j=n-i;j>=1;j--)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output

Struct with pointers changes value without reassigning

I debugged the program below. It finds the correct result but then mmat in the recursive function ri gets changed without reassigning it by this part of the code pmat[q][h].ptes=NULL; and i get as final result that all the non pre-assigned pointers are null. I got the same problem with a global variable mat **mmat instead of passing it by reference in ri. Does anybody know the reason?
typedef struct tes_{
struct seg_{
char c;
int v;
}seg[2];
}tes;
typedef struct mat_{
tes *ptes;
int rot;
} mat;
int max=0;
int main()
{
FILE *f; char frase[M], buff[M]; int n,i,j,r,c,q=0,z=0,e=0,*pos,*esc; tes *vtes; mat **pmat, **mmat;
printf("Inserire nome file input tessere: "); scanf("%s",frase);
f=fopen(frase,"r");
if (f==NULL) exit(1);
fgets(frase,50,f); sscanf(frase,"%d",&n);
vtes=malloc(n*sizeof(tes));
for (i=0;i<n;i++) {fgets(frase,M,f); sscanf(frase,"%c %d %c %d",&(vtes[i].seg[0].c),&(vtes[i].seg[0].v),&(vtes[i].seg[1].c),&(vtes[i].seg[1].v));}
fclose(f);
printf("Inserire nome file input scacchiera: "); scanf("%s",frase);
f=fopen(frase,"r");
if (f==NULL) exit(1);
fgets(frase,M,f); sscanf(frase,"%d %d",&r,&c);
esc=(int*)malloc(r*c*sizeof(int));
pmat=(mat **)malloc(r*sizeof(mat *));
mmat=(mat **)malloc(r*sizeof(mat *));
for (i=0;i<r;i++) {pmat[i]=(mat *)malloc(c*sizeof(mat)); mmat[i]=(mat *)malloc(c*sizeof(mat));}
for (i=0;i<r;i++) {
fgets(frase,M,f); q=0;
for (j=0;j<c;j++) {
z=0;
while (frase[q]!='/') {
buff[z]=frase[q]; q++; z++;
}
q++; buff[z]='\0'; z=0;
if (atoi(buff)==-1) pmat[i][j].ptes=NULL;
else {pmat[i][j].ptes=&(vtes[atoi(buff)]); esc[e]=atoi(buff); e++;}
while (frase[q]!=' ' || frase[q]=='\n' || frase[q]=='\0') {
buff[z]=frase[q]; q++; z++;
}
q++; buff[z]='\0'; pmat[i][j].rot=atoi(buff);
}
}
pos=(int *)malloc((n-e)*sizeof(int)); q=0;
for(i=0;i<n;i++) { // pos contiene tutte le tessere inseribili
for (j=0;j<e;j++) if(esc[j]==i) break;
if (j>=e) {pos[q]=i; q++;}
}
ri(pmat,&mmat,vtes,pos,n-e,r,c);
printf("Max valore %d\n",max);
for (i=0;i<r;i++) {
for (j=0;j<c;j++) {
printf("%c %d // %c %d\t",mmat[i][j].ptes->seg[(0+mmat[i][j].rot)%2].c,mmat[i][j].ptes->seg[(0+mmat[i][j].rot)%2].v,mmat[i][j].ptes->seg[(1+mmat[i][j].rot)%2].c,mmat[i][j].ptes->seg[(1+mmat[i][j].rot)%2].v);
}
printf("\n");
}
return 0;
}
void ri(mat **pmat, mat ***mmat, tes *vtes, int *pos, int i, int r, int c){
int j,q,h,z,*pos2;
if (i==0) {
if(check(pmat,vtes,r,c)) {
*mmat=pmat;
}
return;
}
for (j=0;j<i;j++) {
for (q=0;q<r;q++) {
for (h=0;h<c;h++) {
if (pmat[q][h].ptes==NULL) break;
}
if (h<c) break;
}
if (q==r && h==c) return;
pos2=malloc((i-1)*sizeof(int));
for (z=0;z<i;z++){
if (z<j) pos2[z]=pos[z];
else if (z>j) pos2[z-1]=pos[z];
}
pmat[q][h].ptes=&(vtes[pos[j]]);
pmat[q][h].rot=0;
ri(pmat,mmat,vtes,pos2,i-1,r,c);
pmat[q][h].rot=1;
ri(pmat,mmat,vtes,pos2,i-1,r,c);
free(pos2);
if ((*mmat)[0][1].ptes==NULL) {
printf("prob");
}
pmat[q][h].ptes=NULL;
if ((*mmat)[0][1].ptes==NULL) {
printf("prob");
}
}
}
int check(mat **pmat,tes *vtes, int r, int c){
int i,j, cont=0;
for (i=0;i<r;i++) {
for (j=0;j<c-1;j++) {
if (pmat[i][j].ptes->seg[(0+pmat[i][j].rot)%2].c!=pmat[i][j+1].ptes->seg[(0+pmat[i][j+1].rot)%2].c) break;
}
if (j==c-1) {for (j=0;j<c;j++) cont+=pmat[i][j].ptes->seg[(0+pmat[i][j].rot)%2].v;}
}
for (i=0;i<c;i++) {
for (j=0;j<r-1;j++) {
if (pmat[j][i].ptes->seg[(1+pmat[j][i].rot)%2].c!=pmat[j+1][i].ptes->seg[(1+pmat[j+1][i].rot)%2].c) break;
}
if (j==r-1) {for (j=0;j<r;j++) cont+=pmat[j][i].ptes->seg[(1+pmat[j][i].rot)%2].v;}
}
if (cont>max) {
max=cont;
return 1;
}
return 0;
}

Prime Generator(spoj)

For exact question see this link
Here I defined three function, called them selves one within another.
Function call is not being done
#include<stdio.h>
int primegen(int x1,int x2);
int isprime(int j);
int main(){
int x,n1,n2,i;
printf("Enter the number of test cases:");
scanf("%d",&x);
for(i=0;i<x;i++){
printf("enter the starting point and ending point:");
scanf("%d %d",&n1,&n2);
primegen(n1,n2);
}
return 0;
}
int primegen(int x1,int x2){
int k;
if(x2>x1){
for(k=x1;k<x2;k++){
if(isprime(k))
{
printf("%d",k);
}
}
return 0;
}
}
int isprime(int j){
int i,c=0;
for (i=1;i<=j;i++)
{
if(j%i==0){
c++;
}
if(c!=2){
return 0;
}
else{
return 1;
}
}
}
Output
There is no output for this code.
Take following outside loop:
if(c!=2){
return 0;
}
else{
return 1;
}
The problem is in your loop in isprime().
In this you are using the value of c when it is still 0. So, c!=2 would result in true and 0 will be returned and you would not get any answer.
Remove this particular statement from the for loop as you need to calculate total no. of divisors.
for (i=1;i<=j;i++)
{
if(j%i==0)
c++;
}
if(c!=2)
return 0;
else
return 1;
Or you can do like this:
int isprime(int j){
int i,k;
k=sqrt(j); //include math.h for this
for(i=2;i<=k;i++)
{
if(j%i==0)
return 1;
}
return 0;
}
You only need to find any divisor up to the square root of the number.
It's better to return 0 if it is found to be divisible instead of using counter and incrementing it.
int isprime(int j)
{
int i;
for(i=2;i<j;i++)
if((j%i)==0)
return 0;
return 1;
}
#include <stdio.h>
#include <math.h>
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
unsigned int low,high,i=0,j=2,k,x=0,y=0,z;
unsigned long int a[200000],b[200000];
scanf("%d",&low);
scanf("%d",&high);
for(i=low;i<=high;i++)
a[x++]=i;
for(i=2;i<=32000;i++)
b[y++]=i;
i=0;
while(b[i]*b[i]<=high)
{
if(b[i]!=0)
{
k=i;
for(;k<y;k+=j)
{
if(k!=i)
{
b[k]=0;
}
}
}
i+=1;j+=1;
}
for(i=0;i<y;i++)
{
if(b[i]!=0 && (b[i]>=low && b[i]<=sqrt(high)))
printf("%d\n",b[i]);
}
int c=0;
for(i=0;i<y;i++)
{
if(b[i]!=0 && (b[i]>=1 && b[i]<=sqrt(high)))
b[c++]=b[i];
}
int m=a[0];
for(i=0;i<c;i++)
{
z=(m/b[i])*b[i];k=z-m;
if(k!=0)
k += b[i];
for(;k<x;)
{
if(a[k]!=0)
{
a[k]=0;
}
k+=b[i];
}
}
for(i=0;i<x;i++)
{
if(a[i]!=0 && (a[i]>=2 && a[i]<=(high)))
printf("%d\n",a[i]);
}
printf("\n");
}
return 0;
}

Resources