As is known to all, points in three-dimensional space can be expressed via space rectangular coordinates.There is a program, Point.java, in which I define a class named Point and do some simple operations on it.
//Point.java
public class Point
{
int x, y, z; //3 coordinates of a point
Point(int _x, int _y, int _z) //Constructor
{
x = _x;
y = _y;
z = _z;
}
//To set the value of x, y and z coordinates
void setx(int _x)
{
x = _x;
}
void sety(int _y)
{
y = _y;
}
void setz(int _z)
{
z = _z;
}
//To calculate the square of distance between origin and the point
int SquareOfDistance()
{
return x * x + y * y + z * z;
}
//To test the program
public static void main(String[] args)
{
Point point = new Point(3, 4, 5);
System.out.println("The coordinate of this point is (" + point.x + "," + point.y + "," + point.z + "),and the square of distance between origin and the point is " + point.SquareOfDistance() + ".");
point.setx(1);
point.sety(2);
point.setz(3);
System.out.println("The coordinate of this new point is (" + point.x + "," + point.y + "," + point.z + "),and the square of distance between origin and the point is " + point.SquareOfDistance() + ".");
}
}
The running results are following:
The coordinate of this point is (3,4,5),and the square of distance between origin and the point is 50.
The coordinate of this new point is (1,2,3),and the square of distance between origin and the point is 14.
Have you had a try?
Saturday, November 21, 2015
Wednesday, November 18, 2015
What's Wrong With C4droid?
As an Android phone user,meanwhile a programming learner,I have got to seek for compilers which can be setup on my phone.I used C4droid to compile and run programs before,with its inside compiler TCC,GCC and G++,mainly to practice C language.However,some terrible things happened.
About the problem related to "int a[10];",which I mentioned in my blogs before,I consulted several people,but there was no exact conclusion.But I found that when I change the compiler TCC to GCC or G++,that problem disappeared.As expected, it's associated with the compiler.As to exact reason,I don't know.
What's worth,I run a C++ program by G++ successfully a few days ago,but it couldn't be run today.It's a simple program about conversion of one's height.
//Conversion Of Your Height
#include <iostream>
int main(void)
{
using namespace std;
int inchheight,foot,inch;
const int ConversionFactor=12;
cout<<"Input your height in inches(only integer allowed):__\b\b";
cin>>inchheight;
foot = inchheight / ConversionFactor;
inch = inchheight % ConversionFactor;
cout<<"You are "<<foot<<" feet(foot) "<<inch <<" inch(es) high."<<endl;
return 0;
}
Here I just display the errors shown on the screen.
What's wrong with C4droid?
About the problem related to "int a[10];",which I mentioned in my blogs before,I consulted several people,but there was no exact conclusion.But I found that when I change the compiler TCC to GCC or G++,that problem disappeared.As expected, it's associated with the compiler.As to exact reason,I don't know.
What's worth,I run a C++ program by G++ successfully a few days ago,but it couldn't be run today.It's a simple program about conversion of one's height.
//Conversion Of Your Height
#include <iostream>
int main(void)
{
using namespace std;
int inchheight,foot,inch;
const int ConversionFactor=12;
cout<<"Input your height in inches(only integer allowed):__\b\b";
cin>>inchheight;
foot = inchheight / ConversionFactor;
inch = inchheight % ConversionFactor;
cout<<"You are "<<foot<<" feet(foot) "<<inch <<" inch(es) high."<<endl;
return 0;
}
Here I just display the errors shown on the screen.
What's wrong with C4droid?
Thursday, November 12, 2015
Funny C Program:The Result Is Exactly The Same As Source Code!
Have you ever seen programs whose running results are absolutely the same as their source codes?Or do you think it is amazing for this phenomenon?Let's have a try.
//The Result Is Exactly The Same As Source Code
main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);}
Don't modify it,even adding a space,then run it.What surprise do you find?Is the result like the following?
Since finding this funny program,I try to explain it.
It seems this program is so simple,with too less code.You can say like this in a way,but in another way,do you understand this simple program?
In the main function,a character pointer,a,is defined,which points to the character string "main(){char*a=%c%s%c;printf(a,34,a,34);}".Then the statement "printf(a,34,a,34);" is run.Of course,two 'a' in the bracket point to the string.So this statement is equivalent to "printf("main(){char*a=%c%s%c;printf(a,34,a,34);}",34,"main(){char*a=%c%s%c;printf(a,34,a,34);}",34);".34 is the ASCII code of double quote,so the computer prints out the sentence:main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);},which is what you see on the screen,meanwhile the source code.
Is it funny?Have you learned something from this program?Enjoy it!
In the main function,a character pointer,a,is defined,which points to the character string "main(){char*a=%c%s%c;printf(a,34,a,34);}".Then the statement "printf(a,34,a,34);" is run.Of course,two 'a' in the bracket point to the string.So this statement is equivalent to "printf("main(){char*a=%c%s%c;printf(a,34,a,34);}",34,"main(){char*a=%c%s%c;printf(a,34,a,34);}",34);".34 is the ASCII code of double quote,so the computer prints out the sentence:main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);},which is what you see on the screen,meanwhile the source code.
Is it funny?Have you learned something from this program?Enjoy it!
Wednesday, November 11, 2015
Funny C Program:Calculating the Factorial of A Large Number
Maybe you will think,"how simple to calculate the factorial of a number!"And you just write a program like the following.
//Calculating the Factorial of A Large Number v1
#include <stdio.h>
unsigned long long int Factorial(unsigned long long int j);
int main(void)
{
unsigned long long int i;
printf("Input a nonnegative integer:");
scanf("%llu",&i);
printf("%llu!=%llu\n",i,Factorial(i));
return 0;
}
unsigned long long int Factorial(unsigned long long int j)
{
unsigned long long int k,result=1;
for(k=2;k<=j;k++)
result*=k;
return result;
}
Since we use "%llu" to control the formats of these numbers,we have acturally considered the precision.So there is priviously a problem,"as i becomes larger and larger,can the factorial of i be printed accurately?"Of course no,for numbers are stored in a computer with specific ranges the compiler allows.Here is an example of wrong result.
It's obvious that last digit of 25! should be 0,but in this result not.
Then,are there any ways to avoid accuracy losses?
Remenber how we expressed a number in the program Wenquxing Guessing Game?Through an array with enough elements,we can easily store a large number and print it.Then the improved code is following.
//Calculating the Factorial of A Large Number v2
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define N 100
//N means the maximal number of digits allowed for the value of a factorial
void OutputFactorial(int j);
int main(void)
{
int i;
do
printf("Input a nonnegative integer less than or equal to %d(maximum of int type):",INT_MAX);
while (scanf("%d",&i)!=1||i<0);
OutputFactorial(i);
printf("\n");
return 0;
}
void OutputFactorial(int j)
{
int k,m,carry=0,digit=1,array[N] ;
array[0]=1;
for(k=2;k<=j;k++)
{
for(m=0;m<digit;m++)
{
array[m]=array[m]*k+carry;
carry=array[m]/10;
array[m]%=10;
}
while (carry)
{
array[digit++]=carry%10;
carry/=10;
}
}
if (digit<=N)
{
printf("%d!=",j);
while (digit>=1)
printf("%d",array[--digit]);
}
else
printf("Sorry.The factorial has more than %d digits.It's not intended to output the result.",N);
}
Through this program,I did calculate some factorials successfully.But it seemed that the program didn't run as expected when I input a number out of range.For example,when I input the number 100,the result was beyond expection.
//Calculating the Factorial of A Large Number v3
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define N 100
//N means the maximal number of digits allowed for the value of a factorial
void OutputFactorial(int j);
int main(void)
{
int i;
do
printf("Input a nonnegative integer less than or equal to %d(maximum of int type):",INT_MAX);
while (scanf("%d",&i)!=1||i<0);
OutputFactorial(i);
printf("\n");
return 0;
}
void OutputFactorial(int j)
{
int k,m,carry=0,digit=1,array[N] ;
array[0]=1;
for(k=2;k<=j;k++)
{
for(m=0;m<digit;m++)
{
array[m]=array[m]*k+carry;
carry=array[m]/10;
array[m]%=10;
}
while (carry>0&&digit<N)
{
array[digit++]=carry%10;
carry/=10;
}
}
if (digit>=N&&carry>0)
printf("Sorry.The factorial has more than %d digits.It's not intended to output the result.",N);
else
{
printf("%d!=",j);
while (digit>=1)
printf("%d",array[--digit]);
}
}
Do you know why I modified the program like this? I defined an array with N elements in the function OutputFactorial(j) but didn't check whether the subscript was out of range in the loop of 2nd version.So trouble came.
Although the 3rd version is improved,it remains some problems.For instance,if you input a character,not a number,then the program will fall into an endless loop.Actually,it's a problem caused by buffer.What's worse,theoretically the program could calculate the factorial of a number as large as INT_MAX,but the efficiency is extremely low so it's impossible for us to wait.In the meantime,It's assumed that the number inputted is not larger than INT_MAX,so the robustness of this program needs to be improved.
Do you have any ideas better than mine?Welcome to share!
//Calculating the Factorial of A Large Number v1
#include <stdio.h>
unsigned long long int Factorial(unsigned long long int j);
int main(void)
{
unsigned long long int i;
printf("Input a nonnegative integer:");
scanf("%llu",&i);
printf("%llu!=%llu\n",i,Factorial(i));
return 0;
}
unsigned long long int Factorial(unsigned long long int j)
{
unsigned long long int k,result=1;
for(k=2;k<=j;k++)
result*=k;
return result;
}
Since we use "%llu" to control the formats of these numbers,we have acturally considered the precision.So there is priviously a problem,"as i becomes larger and larger,can the factorial of i be printed accurately?"Of course no,for numbers are stored in a computer with specific ranges the compiler allows.Here is an example of wrong result.
It's obvious that last digit of 25! should be 0,but in this result not.
Then,are there any ways to avoid accuracy losses?
Remenber how we expressed a number in the program Wenquxing Guessing Game?Through an array with enough elements,we can easily store a large number and print it.Then the improved code is following.
//Calculating the Factorial of A Large Number v2
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define N 100
//N means the maximal number of digits allowed for the value of a factorial
void OutputFactorial(int j);
int main(void)
{
int i;
do
printf("Input a nonnegative integer less than or equal to %d(maximum of int type):",INT_MAX);
while (scanf("%d",&i)!=1||i<0);
OutputFactorial(i);
printf("\n");
return 0;
}
void OutputFactorial(int j)
{
int k,m,carry=0,digit=1,array[N] ;
array[0]=1;
for(k=2;k<=j;k++)
{
for(m=0;m<digit;m++)
{
array[m]=array[m]*k+carry;
carry=array[m]/10;
array[m]%=10;
}
while (carry)
{
array[digit++]=carry%10;
carry/=10;
}
}
if (digit<=N)
{
printf("%d!=",j);
while (digit>=1)
printf("%d",array[--digit]);
}
else
printf("Sorry.The factorial has more than %d digits.It's not intended to output the result.",N);
}
Through this program,I did calculate some factorials successfully.But it seemed that the program didn't run as expected when I input a number out of range.For example,when I input the number 100,the result was beyond expection.
How can we explain the wrong result?To find the bug,I read the code carefully.And finally I
give the 3rd version.//Calculating the Factorial of A Large Number v3
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define N 100
//N means the maximal number of digits allowed for the value of a factorial
void OutputFactorial(int j);
int main(void)
{
int i;
do
printf("Input a nonnegative integer less than or equal to %d(maximum of int type):",INT_MAX);
while (scanf("%d",&i)!=1||i<0);
OutputFactorial(i);
printf("\n");
return 0;
}
void OutputFactorial(int j)
{
int k,m,carry=0,digit=1,array[N] ;
array[0]=1;
for(k=2;k<=j;k++)
{
for(m=0;m<digit;m++)
{
array[m]=array[m]*k+carry;
carry=array[m]/10;
array[m]%=10;
}
while (carry>0&&digit<N)
{
array[digit++]=carry%10;
carry/=10;
}
}
if (digit>=N&&carry>0)
printf("Sorry.The factorial has more than %d digits.It's not intended to output the result.",N);
else
{
printf("%d!=",j);
while (digit>=1)
printf("%d",array[--digit]);
}
}
Do you know why I modified the program like this? I defined an array with N elements in the function OutputFactorial(j) but didn't check whether the subscript was out of range in the loop of 2nd version.So trouble came.
Although the 3rd version is improved,it remains some problems.For instance,if you input a character,not a number,then the program will fall into an endless loop.Actually,it's a problem caused by buffer.What's worse,theoretically the program could calculate the factorial of a number as large as INT_MAX,but the efficiency is extremely low so it's impossible for us to wait.In the meantime,It's assumed that the number inputted is not larger than INT_MAX,so the robustness of this program needs to be improved.
Do you have any ideas better than mine?Welcome to share!
Subscribe to:
Posts (Atom)



