A calculator for big numbers - c

i have to code a calculator that does adding or multiplication to numbers with maximum digits of 30 .(maximum number of operands i can enter is 20).
and this is what i came up with (it is long):
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void Adding(int Result[31],int temp[30][31],int cnt)
{
int remainder,i,j,ResultsA[60]={0};
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
ResultsA[j]=(ResultsA[j]+temp[i][j])%10;
remainder=(ResultsA[j]+temp[i][j])/10;
ResultsA[j+1] += remainder;
if(ResultsA[30])
{
cnt++;
}
}
}
for(i=0;i<30;i++)
{
Result[i]=ResultsA[i];
}
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
temp[i][j]=0;
}
}
return;
}
void Mul(int num,int Results[31],int Num_int[30][31],int cnt)
{
int remainder=0,i,j,rows,x;
for(i=0;i<1;i++)
{
Results[i]=1;
}
int temp[30][31]={0};
for(rows=0;rows<num;rows++)
{
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
temp[i][i+j]=remainder+(Num_int[rows][j]*Results[i])%10;
remainder=(Num_int[rows][j]*Results[i])/10;
}
}
Adding(Results,temp,cnt);
}
return;
}
void Plus(int num,int Nums_int[30][31],int Results[31],int cnt)
{
int i,j,remainder,len=0;
for(i=0;i<num;i++)
{
for(j=0;j<30;j++)
{
Results[j]=(Results[j]+Nums_int[i][j])%10;
remainder=(Results[j]+Nums_int[i][j])/10;
Results[j+1]+=remainder;
if(Results[30])
cnt++;
}
}
return;
}
void str_to_int(int Nums_int[20][31],char Nums_str[20][31])
{
int length[20]={0},i,j;
for(i=0;i<20;i++)
{
for(j=19;0<=j;j--)
{
while(Nums_str[i][j]<48||57<Nums_str[i][j])
length[i]++;
}
}
for(i=0;i<20;i++)
{
for(j=0;j<length[i];j++)
{
Nums_int[i][length[i]-j]=Nums_str[i][j]-'0';
}
}
return;
}
void calcBig(char op, int operands[][31], int numOperands, int result[31],int cnt)
{
if (op=='+')
{
Plus(numOperands,operands,result,cnt);
}
if(op=='*')
{
Mul(numOperands,result,operands,cnt);
}
return;
}
int main()
{
int i,cnt=0,y;
char x;
char Nums_str[20][31]={0};
int num, Nums_int[30][31]={0};
int Results[31]={0};
printf("select operator:\n");
scanf("%c",&x);
printf("enter number of operands:\n");
scanf("%d",&num);
printf("enter numbers <seperated by new line>:\n");
for(i=0;i<num;i++)
{
scanf("%s",Nums_str[i]);
}
str_to_int(Nums_int,Nums_str);
calcBig(x,Nums_int,num,Results,cnt);
printf("result:\n");
if(cnt)
{
printf("overflow!");
}
else{
for(i=29;0<=i;i--)
{
while(Results==0)
{
y++;
}
}
for(i=y;0<=i;i--)
{
printf("%d",&Results[i]);
}
}
return 0;
}
Num_int is the 2d array containing numbers from user , num is the number of numbers entered by user.
Mul is function for multiplying numbers in Nums_int.
str_to_int converts the number recieved from user from string to int and reverse the order.
Plus is a function to add numbers in Nums_int.
Adding is function to add numbers that are the result of multiplying a number by ever digit of the other numbers (it gives the result of multiplying two numbers).
the problem is that when i run the code i get this:
select the operation:
*/+
select number of operands:
num
enter numbers:
//entering numbers
and after entering the numbers nothing happens and the program doesnt even end (no return 0). simply nothing happens after entering the numbers

Related

why this show() function produce two * and the element will stop sometimes .There is no error in the code

This is a plane game function I wrote. I use a two-dimensional array to represent the game variables but The running result is abnormal, and * will jump suddenly.
And there will be two * , at the same time, and the plane also will stop
There should be no two * in the process of traversing the two-dimensional array. I tried to modify the position of * but I still couldn't.It's OK to run part of the code alone, but when you use the key operation, the program makes an error, but I don't know what's wrong
#include<stdio.h>
#include <windows.h>
#include<stdlib.h>
#include<conio.h>
#define enemynum 3
int element [20][30];
int position_x,position_y;
int enemy_x[enemynum],enemy_y[enemynum];
int score;
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup()
{ element[20][30]={0};
position_x=10;position_y=15;
element[position_x][position_y]=1;
for(int k=0;k<enemynum;k++)
{
enemy_x[k]=rand()%3;enemy_y[k]=rand()%20;
element[enemy_x[k]][enemy_y[k]]=3;
}
HideCursor();
}
This is an encapsulated pointer callback function. I don't think we need to consider the above functions
void show()
{ int i,j;
gotoxy(0,0);
for(i=0;i<20;i++)
{
for(j=0;j<30;j++)
{ if(element[i][j]==1)
{
printf("*");
}else if(element[i][j]==3)
{
printf("#");
}
else
printf(" ");
}
printf("\n");
}
}
void updatewhithout()
{
int i,j;
for(i=0;i<20;i++)
{
for(j=0;j<30;j++)
{
if(element[i][j]==2)
{
element[i][j]=0;
if(i>0)
element[i-1][j]=2;
}
}
} static int flag;
if(flag<20)
flag++;
if(flag==20)
{ for(int k=0;k<enemynum;k++)
{
if(enemy_x[k]==20)
{
enemy_x[k]=rand()%3;
enemy_y[k]=rand()%20;
}
element[enemy_x[k]][enemy_y[k]]=0;
enemy_x[k]++;
element[enemy_x[k]][enemy_y[k]]=3;
flag=0;
}
}
}
void updatewhith()
{ char ch;
if( kbhit())
ch=getch();
if(ch=='a')
{ element[position_x][position_y]=0;
position_y--;
element[position_x][position_y]=1;
}
if(ch=='d')
{ element[position_x][position_y]=0;
position_y++;
element[position_x][position_y]=1;
}
}
int main()
{startup();
while(1)
{show();
updatewhithout();
updatewhith();
}
}

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

Encode printing of alphabets in C

In this program i have encoded
Using the numerical value of each letter (A=1, B=2, ... Z= 26) the rules are as follows:
A–E Multiply its numerical value by 2
F–J Divide its numerical value by 3. Multiply the integer remainder by 5
K–O Divide its numerical value by 4. Multiply the integer remainder by 8.
P–T Add 10.
U- Z Find the largest integer factor of its numerical value less than the value itself. Multiply it by 12.
As an example if the letter to encode is a B, the B has a numerical value of 2 and encodes to a 4 and becomes a D, the 4th letter of the alphabet.
The G has a numerical value of 7. It encodes to a 5 and becomes an E.
The numerical value of Z is 26. Its largest factor is 13. You must count 156 (13*12) letters. This has the effect of wrapping around the alphabet 6 complete times and ending at Z. If a numerical value of zero is evaluated print a # symbol.
Problem with the code
- I am not able to print #.
- whenever i am entering Y i am getting J why???????
Here is my code:
#include<stdio.h>
void main()
{
char c;
int n,i,max=0,j,y;
int a[]={65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90};
scanf("%c",&c);
n=c;
for(i=0;i<5;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
}
if(flag==1)
{
printf("%c",a[((i+1)*2)-1]);
break;
}
}
for(i=5;i<10;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
}
if(flag==1)
{
printf("%c",a[(((i+1)%3)*5)-1]);
break;
}
}
for(i=10;i<15;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
}
if(flag==1)
{
printf("%c",a[(((i+1)%4)*8)-1]);
break;
}
}
for(i=15;i<20;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
}
if(flag==1)
{
printf("%c",a[((i+1)+10)-1]);
break;
}
}
for(i=15;i<26;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
for(j=1;j<=(i+1)/2;j++)
{
if((i+1)%j==0)
{
if(max<j)
{
max=j;
}
}
}
}
if(flag==1)
{
y=(max*12)/6;
if(y>=0)
{
printf("%c",a[y-1]);
}
else
{
printf("#");
}
break;
}
}
}
Help me out guy's.
You have a variety of rules for encoding the characters, but at the end of the day, each letter of the alphabet maps to a single output character. So you can simply create a table that contains the outputs that correspond to each letter. Thus the code reduces to the following
int main( void )
{
char table[] = "BDFHJ#EJ#EX#HPXZABCDFBLJHZ";
char c;
if ( scanf("%c",&c) == 1 && c >= 'A' && c <= 'Z' )
printf( "%c\n", table[c - 'A'] );
}
Note that the table in this code attempts to follow the rules that you specified. Those rules don’t create a 1-to-1 mapping of input to output, but of course it’s trivial to change the table to achieve any mapping you like.
#include<stdio.h>
int large(int n);
int main()
{
char c;
int n,y;
scanf("%c",&c);
n=c;
if(n>=65 && n<70)
{
int val;
val=(((n-65)+1)*2)+64;
printf("%c\n",val);
}
else if(n>=70 && n<75)
{
int val=(n-65+1);
if((val)%3!=0)
{
printf("%c\n",(((val)%3)*5)+64);
}
else printf("#\n");
}
else if(n>=75 && n<80)
{
int val=(n-65+1);
if((val)%4!=0)
{
printf("%c\n",(((val)%4)*8)+64);
}
else
printf("#\n");
}
else if(n>=80 && n<85)
{
int val=n-65+1;
if(val>16)
{
val=val-26;
printf("%c",val+10+64);
}
else
printf("%c\n",(val+10)+64);
}
if(n>=85 && n<91)
{
int d,b=0,m;
int val=n-65+1;
d=large(val);
y=(d*12);
while(b<y)
{
b=b+26;
}
b=b-26;
m=y-b;
printf("%c\n",m+64);
}
return 0;
}
int large(int n)
{
int j,max=0;
for(j=1;j<=n/2;j++)
{
if(n%j==0)
{
if(max<j)
{
max=j;
}
}
}
return max;
}
Try this code:
#include<stdio.h>
int maxFac(int n)
{
int i,max=0;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
if(max<i)
max=i;
}
}
return max;
}
int main()
{
char c;
scanf("%c",&c);
if((int)c>=65 && (int)c<=69)
{
c=(((int)c-64)*2)+64;
}
else if ((int)c>=70 && (int)c<=74)
{
c= (((int)c-64)%3)*5;
if(c==0)
c='#';
else
c=(int)c+64;
}
else if((int)c>=75 && (int)c<=79)
{
c= (((int)c-64)%4)*8;
if(c==0)
c='#';
else
c=(int)c+64;
}
else if((int)c>=80 && (int)c<=84)
{
c= c+10;
if(c>90)
c=c-26;
}
else if((int)c>=85 && (int)c<=90)
{
int z;
c=c-64;
z=maxFac(c);
c=z;
c=(z*12)%26 + 64;
if(c=='#')
c=c+26;
}
printf("%c",c);
}

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

What is causing my blackjack program to crash before doing anything?

When I run my program, it will print "Welcome to Blackjack! Rules: " and then an error window will come up saying "Blackjack.exe has stopped working and I have to close the program. In the compiler window it says "Process terminated". How do I stop this from happening?
Is one of the loops not working correctly?
My code:
#include <stdio.h>
#include <stdlib.h>
void Initializedeck(int[]);
void Shuffledeck(int[]);
void displayscore(int, int);
int hdrawcards(int[], int[]);
char getsuit(int);
int cdrawcards(int[], int[]);
int main()
{
int deck[52];
char cont;
int hcherries=10;
int ccherries=10;
int hcards[10];
int ccards[10];
int htotal;
int ctotal;
printf("Welcome to Blackjack!\n");
printf("Rules: \n ");
Initializedeck(deck);
cont='y';
while(cont=='y')
{
Shuffledeck(deck);
system("cls");
displayscore(hcherries, ccherries);
htotal=hdrawcards(hcards, deck);
ctotal=cdrawcards(ccards, deck);
if(htotal>ctotal)
{
printf("You Win!");
}
else
{
printf("Computer Wins. :( ");
}
printf("Do you want to continue?(y/n)");
scanf("%c", &cont);
}
return 0;
}
void Initializedeck(int deck[])
{
int i=0;
while(i<52)
{
deck[i]=i;
i++;
}
}
void Shuffledeck(int deck[])
{
int hold;
int max=51;
int random;
while(max>=0)
{
random=rand()%(max)+1;
hold=deck[max];
deck[max]=random;
random=hold;
max--;
}
}
void displayscore(int hcherries, int ccherries)
{
printf("Human: %i(cherries) Computer: %i(cherries)", hcherries, ccherries);
}
int hdrawcards(int hcards[], int deck[])
{
char answer;
int i=0;
int score=0;
int total=0;
char rank;
int worth;
char suit;
int card;
printf("Do you want to draw a card? (y/n)");
scanf("%c", &answer);
while(answer=='y' && total<21)
{
score=score+deck[i];
hcards[i]=deck[i];
card=hcards[i];
worth=hcards[i]%13;
if(worth==10)
{
rank='T';
}
else if (worth==11)
{
rank='J';
}
else if (worth==12)
{
rank='Q';
}
else if (worth==13)
{
rank='K';
}
else if(worth==1)
{
rank='A';
}
else
{
rank=worth;
}
if(worth>10)
{
worth=10;
}
total=total+worth;
suit=getsuit(card);
printf("%c%c\n", rank, suit);
printf("Total: %i", total);
i++;
scanf("%c", &answer);
}
return total;
}
int cdrawcards(int ccards[], int deck[])
{
int i=26;
int total;
int score;
int worth;
char rank;
char suit;
int card;
while(total<18)
{
score=score+deck[i];
ccards[i]=deck[i];
card=ccards[i];
worth=ccards[i]%13;
if(worth==10)
{
rank='T';
}
else if (worth==11)
{
rank='J';
}
else if (worth==12)
{
rank='Q';
}
else if (worth==13)
{
rank='K';
}
else if(worth==1)
{
rank='A';
}
else
{
rank=worth;
}
if(worth>10)
{
worth=10;
}
total=total+worth;
suit=getsuit(card);
printf(" %c%c\n", rank, suit);
printf(" Total: %i", total);
i++;
}
return total;
}
char getsuit(int card)
{
char suit;
if(card<13)
{
suit='S';
}
else if(card>=13 && card<26)
{
suit='H';
}
else if(card>=26 && card<39)
{
suit='D';
}
else if(card>=39 && card<52)
{
suit='C';
}
return suit;
}
You have an array overflow in cdrawcards():
int ccards[10];
...
ctotal = cdrawcards(ccards, deck);
...
int cdrawcards(int ccards[], int deck[])
{
int i = 26;
...
ccards[i] = deck[i]; //Overflow, ccards[] only has 10 elements
Either change the initial value of i or make the array larger int ccards[52]. I would also add an explicit check in cdrawcards() and hdrawcards() to ensure you don't overflow the arrays, like:
while (total < 18 && i < 10)
For example, in hdrawcards() you can overflow the array if you get a bunch of low value cards in a row (ex: 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3(overflow), 3, ...).

Resources