How to do arithmetic operation on very large numbers [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to add several 50-digit numbers from an array of strings and print first 10 digit of the summation,but I am getting the wrong answer.What is wrong with my code?
The answer should be: 5537376230
My answer is :3737623039
p.s. i have used the array to store single digits of the summation
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string num[100] = {
"37107287533902102798797998220837590246510135740250",
"46376937677490009712648124896970078050417018260538",
"74324986199524741059474233309513058123726617309629",
"91942213363574161572522430563301811072406154908250",
"23067588207539346171171980310421047513778063246676",
"89261670696623633820136378418383684178734361726757",
"28112879812849979408065481931592621691275889832738",
"44274228917432520321923589422876796487670272189318",
"47451445736001306439091167216856844588711603153276",
"70386486105843025439939619828917593665686757934951",
"62176457141856560629502157223196586755079324193331",
"64906352462741904929101432445813822663347944758178",
"92575867718337217661963751590579239728245598838407",
"58203565325359399008402633568948830189458628227828",
"80181199384826282014278194139940567587151170094390",
"35398664372827112653829987240784473053190104293586",
"86515506006295864861532075273371959191420517255829",
"71693888707715466499115593487603532921714970056938",
"54370070576826684624621495650076471787294438377604",
"53282654108756828443191190634694037855217779295145",
"36123272525000296071075082563815656710885258350721",
"45876576172410976447339110607218265236877223636045",
"17423706905851860660448207621209813287860733969412",
"81142660418086830619328460811191061556940512689692",
"51934325451728388641918047049293215058642563049483",
"62467221648435076201727918039944693004732956340691",
"15732444386908125794514089057706229429197107928209",
"55037687525678773091862540744969844508330393682126",
"18336384825330154686196124348767681297534375946515",
"80386287592878490201521685554828717201219257766954",
"78182833757993103614740356856449095527097864797581",
"16726320100436897842553539920931837441497806860984",
"48403098129077791799088218795327364475675590848030",
"87086987551392711854517078544161852424320693150332",
"59959406895756536782107074926966537676326235447210",
"69793950679652694742597709739166693763042633987085",
"41052684708299085211399427365734116182760315001271",
"65378607361501080857009149939512557028198746004375",
"35829035317434717326932123578154982629742552737307",
"94953759765105305946966067683156574377167401875275",
"88902802571733229619176668713819931811048770190271",
"25267680276078003013678680992525463401061632866526",
"36270218540497705585629946580636237993140746255962",
"24074486908231174977792365466257246923322810917141",
"91430288197103288597806669760892938638285025333403",
"34413065578016127815921815005561868836468420090470",
"23053081172816430487623791969842487255036638784583",
"11487696932154902810424020138335124462181441773470",
"63783299490636259666498587618221225225512486764533",
"67720186971698544312419572409913959008952310058822",
"95548255300263520781532296796249481641953868218774",
"76085327132285723110424803456124867697064507995236",
"37774242535411291684276865538926205024910326572967",
"23701913275725675285653248258265463092207058596522",
"29798860272258331913126375147341994889534765745501",
"18495701454879288984856827726077713721403798879715",
"38298203783031473527721580348144513491373226651381",
"34829543829199918180278916522431027392251122869539",
"40957953066405232632538044100059654939159879593635",
"29746152185502371307642255121183693803580388584903",
"41698116222072977186158236678424689157993532961922",
"62467957194401269043877107275048102390895523597457",
"23189706772547915061505504953922979530901129967519",
"86188088225875314529584099251203829009407770775672",
"11306739708304724483816533873502340845647058077308",
"82959174767140363198008187129011875491310547126581",
"97623331044818386269515456334926366572897563400500",
"42846280183517070527831839425882145521227251250327",
"55121603546981200581762165212827652751691296897789",
"32238195734329339946437501907836945765883352399886",
"75506164965184775180738168837861091527357929701337",
"62177842752192623401942399639168044983993173312731",
"32924185707147349566916674687634660915035914677504",
"99518671430235219628894890102423325116913619626622",
"73267460800591547471830798392868535206946944540724",
"76841822524674417161514036427982273348055556214818",
"97142617910342598647204516893989422179826088076852",
"87783646182799346313767754307809363333018982642090",
"10848802521674670883215120185883543223812876952786",
"71329612474782464538636993009049310363619763878039",
"62184073572399794223406235393808339651327408011116",
"66627891981488087797941876876144230030984490851411",
"60661826293682836764744779239180335110989069790714",
"85786944089552990653640447425576083659976645795096",
"66024396409905389607120198219976047599490197230297",
"64913982680032973156037120041377903785566085089252",
"16730939319872750275468906903707539413042652315011",
"94809377245048795150954100921645863754710598436791",
"78639167021187492431995700641917969777599028300699",
"15368713711936614952811305876380278410754449733078",
"40789923115535562561142322423255033685442488917353",
"44889911501440648020369068063960672322193204149535",
"41503128880339536053299340368006977710650566631954",
"81234880673210146739058568557934581403627822703280",
"82616570773948327592232845941706525094512325230608",
"22918802058777319719839450180888072429661980811197",
"77158542502016545090413245809786882778948721859617",
"72107838435069186155435662884062257473692284509516",
"20849603980134001723930671666823555245252804609722",
"53503534226472524250874054075591789781264330331690",
};
long long sum[100];
int runner=0;
long long carry=0;
for(int l=49;l>=0;l--)
{
int total=0;
for(int k=0;k<100;k++ )
{
total+=(int)num[k][l]-48;
}
total=total+carry;
sum[runner]=total%10;
if(total>9)carry=total/10;
else carry=0;
runner++;
}
//cout<<runner<<endl;
for(int l=runner-1;l>runner-11;l--)
{
cout<<sum[l];
}
}

After your last loop, the carry is 55, but you never insert it into the result.

Since you only need the first 10 digits
int main(int argc, char **argv)
{
int i;
char *num[100] = {
"37107287533902102798797998220837590246510135740250",
"46376937677490009712648124896970078050417018260538",
"74324986199524741059474233309513058123726617309629",
"91942213363574161572522430563301811072406154908250",
"23067588207539346171171980310421047513778063246676",
"89261670696623633820136378418383684178734361726757",
"28112879812849979408065481931592621691275889832738",
"44274228917432520321923589422876796487670272189318",
"47451445736001306439091167216856844588711603153276",
"70386486105843025439939619828917593665686757934951",
"62176457141856560629502157223196586755079324193331",
"64906352462741904929101432445813822663347944758178",
"92575867718337217661963751590579239728245598838407",
"58203565325359399008402633568948830189458628227828",
"80181199384826282014278194139940567587151170094390",
"35398664372827112653829987240784473053190104293586",
"86515506006295864861532075273371959191420517255829",
"71693888707715466499115593487603532921714970056938",
"54370070576826684624621495650076471787294438377604",
"53282654108756828443191190634694037855217779295145",
"36123272525000296071075082563815656710885258350721",
"45876576172410976447339110607218265236877223636045",
"17423706905851860660448207621209813287860733969412",
"81142660418086830619328460811191061556940512689692",
"51934325451728388641918047049293215058642563049483",
"62467221648435076201727918039944693004732956340691",
"15732444386908125794514089057706229429197107928209",
"55037687525678773091862540744969844508330393682126",
"18336384825330154686196124348767681297534375946515",
"80386287592878490201521685554828717201219257766954",
"78182833757993103614740356856449095527097864797581",
"16726320100436897842553539920931837441497806860984",
"48403098129077791799088218795327364475675590848030",
"87086987551392711854517078544161852424320693150332",
"59959406895756536782107074926966537676326235447210",
"69793950679652694742597709739166693763042633987085",
"41052684708299085211399427365734116182760315001271",
"65378607361501080857009149939512557028198746004375",
"35829035317434717326932123578154982629742552737307",
"94953759765105305946966067683156574377167401875275",
"88902802571733229619176668713819931811048770190271",
"25267680276078003013678680992525463401061632866526",
"36270218540497705585629946580636237993140746255962",
"24074486908231174977792365466257246923322810917141",
"91430288197103288597806669760892938638285025333403",
"34413065578016127815921815005561868836468420090470",
"23053081172816430487623791969842487255036638784583",
"11487696932154902810424020138335124462181441773470",
"63783299490636259666498587618221225225512486764533",
"67720186971698544312419572409913959008952310058822",
"95548255300263520781532296796249481641953868218774",
"76085327132285723110424803456124867697064507995236",
"37774242535411291684276865538926205024910326572967",
"23701913275725675285653248258265463092207058596522",
"29798860272258331913126375147341994889534765745501",
"18495701454879288984856827726077713721403798879715",
"38298203783031473527721580348144513491373226651381",
"34829543829199918180278916522431027392251122869539",
"40957953066405232632538044100059654939159879593635",
"29746152185502371307642255121183693803580388584903",
"41698116222072977186158236678424689157993532961922",
"62467957194401269043877107275048102390895523597457",
"23189706772547915061505504953922979530901129967519",
"86188088225875314529584099251203829009407770775672",
"11306739708304724483816533873502340845647058077308",
"82959174767140363198008187129011875491310547126581",
"97623331044818386269515456334926366572897563400500",
"42846280183517070527831839425882145521227251250327",
"55121603546981200581762165212827652751691296897789",
"32238195734329339946437501907836945765883352399886",
"75506164965184775180738168837861091527357929701337",
"62177842752192623401942399639168044983993173312731",
"32924185707147349566916674687634660915035914677504",
"99518671430235219628894890102423325116913619626622",
"73267460800591547471830798392868535206946944540724",
"76841822524674417161514036427982273348055556214818",
"97142617910342598647204516893989422179826088076852",
"87783646182799346313767754307809363333018982642090",
"10848802521674670883215120185883543223812876952786",
"71329612474782464538636993009049310363619763878039",
"62184073572399794223406235393808339651327408011116",
"66627891981488087797941876876144230030984490851411",
"60661826293682836764744779239180335110989069790714",
"85786944089552990653640447425576083659976645795096",
"66024396409905389607120198219976047599490197230297",
"64913982680032973156037120041377903785566085089252",
"16730939319872750275468906903707539413042652315011",
"94809377245048795150954100921645863754710598436791",
"78639167021187492431995700641917969777599028300699",
"15368713711936614952811305876380278410754449733078",
"40789923115535562561142322423255033685442488917353",
"44889911501440648020369068063960672322193204149535",
"41503128880339536053299340368006977710650566631954",
"81234880673210146739058568557934581403627822703280",
"82616570773948327592232845941706525094512325230608",
"22918802058777319719839450180888072429661980811197",
"77158542502016545090413245809786882778948721859617",
"72107838435069186155435662884062257473692284509516",
"20849603980134001723930671666823555245252804609722",
"53503534226472524250874054075591789781264330331690",
};
long int sum = 0;
for (i = 0 ; i < 100 ; i++)
{
char firstDigits[15];
memcpy(firstDigits, num[i], 14);
firstDigits[14] = 0;
sum += strtol(firstDigits, NULL, 10);
}
char string[11];
snprintf(string, 11, "%ld", sum);
fprintf(stderr, "%s\n", string);
return 0;
}

I don't know if this is the only problem, but you are using 'int' which has the following range:
–2,147,483,648 to 2,147,483,647
Your numbers are to big and will therefore not work as desired.
Check http://msdn.microsoft.com/en-us/library/s3f49ktz.aspx to se the available data-types

You're implicitly assuming that the answer will have no more digits than each of the inputs, which clearly doesn't hold in this case. You do allocate 100 digits for the output, which is more than sufficient, but you never fill more than 50 of them. I've had a go at modifying your code to remedy this.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
const int nnum = 100, idigits = 50, odigits = 100;
string num[nnum] = {
"37107287533902102798797998220837590246510135740250",
"46376937677490009712648124896970078050417018260538",
"74324986199524741059474233309513058123726617309629",
"91942213363574161572522430563301811072406154908250",
"23067588207539346171171980310421047513778063246676",
"89261670696623633820136378418383684178734361726757",
"28112879812849979408065481931592621691275889832738",
"44274228917432520321923589422876796487670272189318",
"47451445736001306439091167216856844588711603153276",
"70386486105843025439939619828917593665686757934951",
"62176457141856560629502157223196586755079324193331",
"64906352462741904929101432445813822663347944758178",
"92575867718337217661963751590579239728245598838407",
"58203565325359399008402633568948830189458628227828",
"80181199384826282014278194139940567587151170094390",
"35398664372827112653829987240784473053190104293586",
"86515506006295864861532075273371959191420517255829",
"71693888707715466499115593487603532921714970056938",
"54370070576826684624621495650076471787294438377604",
"53282654108756828443191190634694037855217779295145",
"36123272525000296071075082563815656710885258350721",
"45876576172410976447339110607218265236877223636045",
"17423706905851860660448207621209813287860733969412",
"81142660418086830619328460811191061556940512689692",
"51934325451728388641918047049293215058642563049483",
"62467221648435076201727918039944693004732956340691",
"15732444386908125794514089057706229429197107928209",
"55037687525678773091862540744969844508330393682126",
"18336384825330154686196124348767681297534375946515",
"80386287592878490201521685554828717201219257766954",
"78182833757993103614740356856449095527097864797581",
"16726320100436897842553539920931837441497806860984",
"48403098129077791799088218795327364475675590848030",
"87086987551392711854517078544161852424320693150332",
"59959406895756536782107074926966537676326235447210",
"69793950679652694742597709739166693763042633987085",
"41052684708299085211399427365734116182760315001271",
"65378607361501080857009149939512557028198746004375",
"35829035317434717326932123578154982629742552737307",
"94953759765105305946966067683156574377167401875275",
"88902802571733229619176668713819931811048770190271",
"25267680276078003013678680992525463401061632866526",
"36270218540497705585629946580636237993140746255962",
"24074486908231174977792365466257246923322810917141",
"91430288197103288597806669760892938638285025333403",
"34413065578016127815921815005561868836468420090470",
"23053081172816430487623791969842487255036638784583",
"11487696932154902810424020138335124462181441773470",
"63783299490636259666498587618221225225512486764533",
"67720186971698544312419572409913959008952310058822",
"95548255300263520781532296796249481641953868218774",
"76085327132285723110424803456124867697064507995236",
"37774242535411291684276865538926205024910326572967",
"23701913275725675285653248258265463092207058596522",
"29798860272258331913126375147341994889534765745501",
"18495701454879288984856827726077713721403798879715",
"38298203783031473527721580348144513491373226651381",
"34829543829199918180278916522431027392251122869539",
"40957953066405232632538044100059654939159879593635",
"29746152185502371307642255121183693803580388584903",
"41698116222072977186158236678424689157993532961922",
"62467957194401269043877107275048102390895523597457",
"23189706772547915061505504953922979530901129967519",
"86188088225875314529584099251203829009407770775672",
"11306739708304724483816533873502340845647058077308",
"82959174767140363198008187129011875491310547126581",
"97623331044818386269515456334926366572897563400500",
"42846280183517070527831839425882145521227251250327",
"55121603546981200581762165212827652751691296897789",
"32238195734329339946437501907836945765883352399886",
"75506164965184775180738168837861091527357929701337",
"62177842752192623401942399639168044983993173312731",
"32924185707147349566916674687634660915035914677504",
"99518671430235219628894890102423325116913619626622",
"73267460800591547471830798392868535206946944540724",
"76841822524674417161514036427982273348055556214818",
"97142617910342598647204516893989422179826088076852",
"87783646182799346313767754307809363333018982642090",
"10848802521674670883215120185883543223812876952786",
"71329612474782464538636993009049310363619763878039",
"62184073572399794223406235393808339651327408011116",
"66627891981488087797941876876144230030984490851411",
"60661826293682836764744779239180335110989069790714",
"85786944089552990653640447425576083659976645795096",
"66024396409905389607120198219976047599490197230297",
"64913982680032973156037120041377903785566085089252",
"16730939319872750275468906903707539413042652315011",
"94809377245048795150954100921645863754710598436791",
"78639167021187492431995700641917969777599028300699",
"15368713711936614952811305876380278410754449733078",
"40789923115535562561142322423255033685442488917353",
"44889911501440648020369068063960672322193204149535",
"41503128880339536053299340368006977710650566631954",
"81234880673210146739058568557934581403627822703280",
"82616570773948327592232845941706525094512325230608",
"22918802058777319719839450180888072429661980811197",
"77158542502016545090413245809786882778948721859617",
"72107838435069186155435662884062257473692284509516",
"20849603980134001723930671666823555245252804609722",
"53503534226472524250874054075591789781264330331690",
};
int sum[odigits];
int carry=0, d;
for(d=0;d<odigits;d++)
{
int ipos = idigits-1-d;
int opos = odigits-1-d;
int total = carry;
if(ipos >= 0)
for(int k = 0; k < nnum; k++)
total += num[k][ipos]-48;
else if(carry == 0) break;
sum[opos] = total%10;
carry = total/10;
}
for(int i=0; i < 10; i++)
cout << sum[odigits-d+i];
cout << "\n";
}
This produces the desired result 5537376230.
Since an int can hold much more than just 1 decimal digit, a more efficient approach would be to process larger groups of digits at once.

Related

Perform binary trees search based a collection of data

I was asked to write a BTS code for a collection of data with have the x, y and z coordinate, which has negative and positive number, and the requirement are below:
That program is capable to perform binary trees search based a collection of data.
Please use the data provided within this email. It is collection of coordinates in X,Y, and Z
That program must capable of:
FInding positive X, negative X, positive y, negative y, positive z, negative z.
User can search all positive numbers, all negative numbers, etc.
So the user will input a X coordinate, it will display the Y and Z together in the output, and another is when user want to search for all Positive/Negative of X, it will also show the Y and Z with the X.
So far as I only know binary search only can perform a search on a single number, but not for a string of element with 3 numbers inside.
This is the few of the collection of X, Y and Z data for example:
-0.090729 0.122568 0.030209 <--- one line of X Y Z coordinate
-0.179660 0.154953 0.033881
-0.335793 0.244996 0.269589
0.075957 0.149626 0.114472
also I will going to use this code as the base
#include <cstdlib>
#include <iostream>
using namespace std;
int binary_search(int array[],int first,int last, int value);
int main() {
int list[10];
for (int k=0; k<11; k++)
list[k]=2*k+1;
cout<< "binary search results: "<< binary_search(list,1,21,11)<<endl;
return 0;
}//end of main
int binary_search(int array[],int first,int last, int search_key)
{
int index;
if (first > last)
index = -1;
else
{
int mid = (first + last)/2;
if (search_key == array[mid])
index = mid;
else
if (search_key < array[mid])
index = binary_search(array,first, mid-1, search_key);
else
index = binary_search(array, mid+1, last, search_key);
} // end if
return index;
}// end binarySearch
I was also thinking that Using array is not enough for this, and I will have to make a separate file like a database that user can search the data with one input.
So I'm having an idea that I only do the binary tree for X coordinate but when the user input the X, it will show the Y and Z with the X from the data collection, also the data collection is a lot, any expert's help will be appreciated :) still wondering does binary search tree able to do that or not....
So this is the collections of data I need to used, can someone please tell me how to make that function that able to call the data from this huge collection?
0.075957 0.149626 0.114472
0.000905 0.131220 0.031000
0.334059 -0.004790 0.207368
0.380561 -0.016845 0.110792
-0.083847 -0.009495 0.260374
0.316664 0.071467 0.120474
-0.307909 0.341245 -0.115581
0.411077 -0.098945 0.029724
-0.093728 0.413972 -0.045792
-0.445182 0.173948 0.306924
-0.339452 0.094466 0.338246
0.154974 0.153764 0.113986
0.383043 -0.017294 0.038540
0.371618 -0.036996 -0.034430
-0.488796 0.174331 0.109211
-0.425485 -0.003917 0.201166
-0.328634 0.319858 0.114390
-0.410262 0.298180 0.193996
0.155009 0.097454 0.225033
-0.225622 0.246139 0.118317
-0.399749 0.163849 0.335552
0.076578 0.133341 0.177679
-0.201047 0.142376 0.174390
-0.429153 -0.098775 -0.040573
0.230882 0.131755 0.118676
-0.483743 0.082920 0.040329
-0.078715 0.471042 -0.055900
-0.411718 0.237937 0.278383
-0.005249 0.003769 0.270521
-0.018101 0.489653 -0.124951
-0.133572 0.398057 -0.090000
0.154095 0.134174 0.180583
-0.172136 0.147088 0.112057
0.240332 0.079992 0.210373
-0.175250 0.391765 0.011504
-0.477955 0.162864 0.042821
-0.263429 0.246060 0.204495
0.408153 -0.094551 0.112774
0.388142 -0.103007 0.184936
-0.257106 0.309349 0.107726
-0.020619 0.343770 -0.051245
-0.366778 -0.278556 0.116261
-0.329815 0.313916 0.034602
-0.482418 0.071724 0.268572
0.296142 0.067455 0.185325
0.392588 -0.106540 -0.040632
-0.348415 0.380318 -0.132477
0.074743 0.098941 0.226089
0.313984 -0.095701 0.274736
-0.000127 0.359321 -0.099978
-0.389304 0.419894 -0.275472
-0.448952 -0.015681 0.018614
0.360441 0.017741 0.133138
0.072016 -0.429793 0.306404
0.157903 -0.423369 0.305657
-0.253995 0.329448 0.039563
-0.486770 0.073079 0.193422
-0.277697 0.136104 0.260296
-0.000924 0.131167 0.114723
-0.324768 0.304498 0.200203
-0.172816 0.407647 -0.040188
-0.262926 -0.426393 0.279458
-0.389169 0.326903 -0.115444
-0.171523 0.319164 0.036876
-0.166670 -0.433521 0.278390
-0.052386 -0.126211 0.329163
0.021908 0.122344 0.172256
-0.467692 -0.118237 0.042218
-0.339549 0.330551 -0.049267
-0.333589 0.232102 -0.053896
0.240226 -0.009687 0.271713
-0.125529 0.419457 -0.000220
-0.323461 0.062386 -0.069065
-0.335525 0.413992 -0.292639
-0.485317 0.253072 0.122003
0.160470 0.053417 0.252663
0.320594 -0.097082 -0.121452
0.153275 -0.012350 0.284012
-0.351329 0.151160 0.328284
0.155106 0.152666 0.034476
0.237370 -0.432505 0.276914
0.213352 -0.110928 0.333017
0.076081 0.050562 0.261882
-0.420589 0.067501 -0.027579
-0.404542 0.241335 -0.029536
0.154083 -0.010130 -0.141933
-0.011780 0.079933 0.212997
0.076419 0.148417 0.035528
-0.474790 0.231229 0.188058
0.065221 0.102536 -0.068695
-0.341908 0.470328 -0.362529
-0.081834 0.109006 0.134754
-0.445012 0.272483 0.212757
-0.471614 -0.110944 0.114503
-0.086541 0.096661 -0.045842
-0.087413 0.067613 0.198527
-0.400299 0.085220 0.335886
0.335514 0.004886 -0.052942
-0.331894 -0.098257 -0.115394
-0.384468 -0.109560 -0.094346
-0.429120 -0.083583 0.201641
-0.450774 0.192619 0.008875
-0.077900 -0.436097 0.335867
-0.001575 -0.432054 0.337764
-0.318593 0.418017 -0.378191
-0.081679 0.417342 -0.113276
-0.256147 0.180127 0.214579
-0.324634 0.166962 0.291635
-0.165520 0.315139 -0.053349
-0.244783 -0.239901 0.258588
-0.157444 -0.112794 -0.153111
0.001362 -0.102979 0.350567
0.076088 -0.090730 0.354817
0.395664 -0.267686 0.191676
0.304469 0.060476 -0.029716
-0.103680 -0.414289 0.294346
-0.240880 0.071427 0.229963
0.395003 -0.186405 -0.042120
0.312090 -0.430972 0.197739
-0.092992 -0.078157 -0.160328
0.367958 -0.017433 0.172606
-0.072734 -0.114118 -0.196052
0.001156 -0.422750 0.282151
0.140519 -0.396572 0.327980
0.401578 -0.409301 0.180077
-0.038587 0.485641 -0.078435
0.078437 -0.338751 0.352289
0.150609 -0.344197 0.348366
-0.468113 -0.013229 0.110504
-0.258706 -0.401441 0.198291
-0.080173 -0.353192 0.259849
0.076135 -0.012304 0.287421
-0.025898 -0.350688 0.291216
0.008889 -0.332588 0.332609
-0.259982 0.255333 -0.021288
-0.500000 0.074738 0.115107
-0.293236 0.334808 -0.197488
-0.450030 -0.006072 0.139787
0.338110 -0.354940 0.228053
0.398298 -0.341848 0.204755
-0.171607 0.062339 0.205716
-0.334439 0.323599 -0.285487
0.295641 -0.030420 0.258598
-0.078645 0.467777 -0.100027
-0.388458 -0.109277 0.249584
-0.010804 -0.060563 0.298001
-0.235267 -0.337188 0.190461
-0.169882 -0.344871 0.254487
-0.434959 0.052108 0.305670
0.211018 0.127704 0.169978
-0.141419 0.105761 0.161259
-0.277201 0.080277 0.260381
-0.348562 0.307347 -0.210293
-0.264058 0.296204 0.184212
-0.201645 -0.339661 0.235866
0.044728 0.475219 -0.187208
-0.467501 -0.011187 0.052141
-0.406464 0.305509 0.039260
-0.079797 0.121035 0.092894
-0.424583 0.308193 0.113378
-0.410118 -0.023560 -0.044817
-0.048690 0.100157 0.158385
-0.323503 0.344986 -0.356561
-0.403118 -0.246061 0.111481
0.051702 0.432676 -0.178928
-0.258646 -0.350652 0.045387
-0.005999 0.095570 -0.055052
-0.344346 0.255465 -0.122999
-0.157020 0.102027 -0.067144
-0.337089 0.267242 -0.193677
-0.241299 -0.345101 0.113604
0.003790 -0.261338 0.347452
0.075487 -0.264686 0.369264
0.162459 -0.267253 0.361906
0.020993 0.060885 0.246241
0.471800 -0.343702 0.172142
0.149844 -0.096394 0.350343
-0.169260 -0.266480 0.265656
-0.078075 -0.262286 0.267258
-0.135235 0.358184 -0.088433
-0.255263 -0.279737 0.222196
-0.387804 0.315919 -0.042996
-0.249325 0.097467 -0.066377
0.232603 -0.011736 -0.126844
0.468932 -0.278385 0.170881
-0.458816 -0.088186 0.015550
-0.389489 0.356552 -0.200456
-0.175817 0.130162 -0.025492
-0.091194 -0.184586 0.283638
-0.247519 -0.009323 -0.126505
-0.362708 0.015190 -0.066164
-0.250302 -0.104264 -0.143793
-0.206967 -0.275905 0.243332
-0.197050 0.247164 0.041679
-0.415699 0.144577 -0.034869
-0.160657 -0.012060 -0.135141
0.073840 -0.096450 -0.221220
0.076158 -0.010711 -0.150732
-0.079237 0.333988 -0.044751
-0.001993 -0.009713 -0.147751
-0.469462 0.198805 0.256553
-0.330455 0.136368 -0.060891
0.422080 -0.239554 0.139433
-0.155069 0.062187 -0.100190
0.045128 0.395109 -0.135353
-0.236349 0.328456 -0.022454
0.150712 -0.095474 -0.203321
-0.395556 -0.231558 0.172341
-0.297510 0.278742 -0.103171
-0.339099 -0.258986 0.198846
-0.244746 -0.177858 -0.137134
-0.173047 -0.190625 0.279613
-0.007145 -0.190266 0.364539
0.075964 -0.187064 0.378191
0.162311 -0.175585 0.365091
-0.446336 0.252967 0.034904
0.360230 0.017437 0.013711
-0.335841 -0.181269 0.265241
-0.253792 -0.188356 0.283896
0.367728 -0.129890 -0.095475
-0.309241 -0.450194 0.258058
-0.346159 0.401686 -0.210900
-0.292800 0.317122 -0.141242
-0.296030 0.360867 -0.265735
-0.338339 -0.101703 0.275068
-0.210562 0.208448 0.093963
-0.402655 0.015646 0.272364
0.234197 -0.188255 0.339100
0.150828 0.105100 -0.069723
-0.252361 -0.094102 0.288347
-0.169846 -0.103389 0.282147
-0.081747 -0.090851 0.290155
0.251737 -0.136450 0.321562
0.229362 0.131550 0.030055
-0.076129 -0.006871 -0.140948
0.238180 -0.085572 0.311821
0.076712 0.058579 -0.109601
-0.233470 0.362778 0.015787
0.352277 -0.078644 -0.083349
-0.487177 0.156348 0.191350
-0.332970 -0.005332 0.263005
-0.176265 -0.018775 0.261670
0.165160 -0.062183 0.318872
-0.251163 -0.025516 0.268080
-0.326562 0.052598 0.309106
-0.215845 0.215242 0.013283
-0.250863 0.162990 -0.035906
0.018121 0.489990 -0.177498
-0.079734 0.357054 -0.098577
-0.255718 0.023253 0.243169
-0.155250 0.018091 0.241299
-0.095910 0.017906 0.241005
0.280869 0.018851 0.239697
-0.194925 0.262219 -0.018530
-0.005598 -0.479311 0.331880
-0.002292 0.414560 -0.123543
-0.260753 -0.481258 0.276218
0.154172 -0.480161 0.274365
-0.167268 -0.479832 0.271694
-0.323638 -0.019706 -0.102023
0.285772 -0.408742 0.248108
0.366403 -0.172115 -0.097373
-0.442366 -0.184972 0.119327
0.467558 -0.397663 0.116399
0.238829 -0.357919 0.301619
0.164031 0.069292 -0.104950
-0.092374 0.325879 0.002629
0.304791 -0.336291 0.265589
0.500000 -0.348679 0.115310
0.227075 -0.117799 -0.181677
0.211965 -0.325049 0.327752
0.358338 -0.105352 0.238236
-0.015772 0.387595 -0.078307
0.214950 -0.264517 0.340296
0.254757 -0.262003 0.316288
0.313084 -0.262813 0.273214
0.359071 -0.263765 0.238343
0.495477 -0.261305 0.109474
0.325024 -0.193317 -0.130771
-0.386855 0.449060 -0.338864
-0.317542 -0.310432 0.112304
-0.417095 -0.173898 0.198758
0.241026 0.090858 -0.049727
0.318413 -0.186461 0.281366
0.361404 -0.188428 0.239352
0.392783 -0.186163 0.191749
0.419850 -0.187629 0.113618
-0.405129 -0.244415 0.041488
-0.078681 0.053919 -0.101020
-0.459326 0.038064 0.023302
0.021646 0.437884 -0.175533
0.231143 -0.479464 -0.043005
0.222780 -0.475271 -0.110977
0.243339 -0.475372 0.203179
0.403476 -0.440490 0.034505
0.265111 -0.443827 -0.056269
0.242083 -0.429440 -0.130781
0.389655 -0.410616 -0.027253
0.404388 -0.439917 0.113291
0.454583 -0.405555 0.034545
0.304492 -0.017036 -0.097924
0.318352 -0.418003 -0.044667
-0.006068 -0.085607 -0.207010
-0.092889 -0.485483 -0.191576
0.287708 -0.397662 -0.113398
0.481895 -0.258555 0.035148
0.459541 -0.348207 -0.020224
0.490604 -0.353829 0.030048
0.398522 -0.346518 -0.043788
-0.294172 0.394323 -0.352233
0.341075 -0.354125 -0.066500
0.310707 -0.341708 -0.120061
-0.001949 0.053880 -0.108140
-0.160536 -0.475848 -0.178375
0.081321 -0.427695 -0.184472
-0.323267 -0.485070 -0.031361
0.429826 -0.241506 0.032040
0.280277 -0.166830 -0.166326
-0.082011 -0.430622 -0.190980
0.259778 -0.371250 -0.160124
-0.063844 -0.479809 0.332759
0.222145 0.052023 -0.099953
0.325379 -0.262782 -0.125122
0.392030 -0.265942 -0.040276
0.455904 -0.291006 -0.015375
-0.474778 0.135820 0.279064
0.419232 -0.185902 0.034486
-0.350612 0.278498 -0.259807
-0.168321 -0.427839 -0.120334
-0.360026 0.463230 -0.317495
-0.387025 0.461928 -0.315415
0.316651 0.070958 0.034101
-0.394244 0.396200 -0.201302
0.249565 -0.098461 -0.160699
-0.052792 -0.184322 0.333604
-0.314333 -0.478718 0.103402
-0.312439 -0.480349 0.190804
-0.310186 -0.438183 0.197410
-0.309481 -0.479378 0.252555
-0.331159 -0.487284 0.035204
-0.333495 -0.435541 0.040352
-0.263413 -0.426976 0.128511
-0.303818 -0.438361 0.090061
-0.277050 -0.400649 0.037718
-0.038925 -0.278358 0.294233
-0.304313 -0.238548 0.246970
-0.011962 -0.176767 -0.257824
-0.166728 -0.485130 -0.130257
0.070000 -0.483251 -0.137440
-0.318461 -0.442320 -0.030378
-0.138111 -0.435125 -0.178330
-0.252236 -0.426105 -0.053975
-0.159840 -0.434426 -0.069893
-0.097390 -0.417033 -0.114525
-0.087536 -0.339895 -0.126204
-0.167931 -0.340827 -0.078534
-0.248072 -0.337747 -0.038942
-0.057447 -0.336240 -0.190659
-0.249331 0.064124 -0.092216
-0.010390 -0.350874 -0.214420
-0.338845 -0.258346 -0.047109
-0.367182 -0.279822 0.032717
-0.021345 -0.267534 -0.245406
-0.076570 -0.266564 -0.195832
-0.101198 -0.272924 -0.132843
-0.001765 -0.485288 -0.175093
-0.395722 -0.228026 -0.030905
0.215824 0.121247 -0.021429
-0.443022 -0.183083 0.031252
-0.408828 -0.184413 -0.046159
-0.089085 -0.186963 -0.210342
-0.119101 -0.171558 -0.153775
-0.334735 -0.182650 -0.112396
-0.223385 -0.485981 -0.089089
0.222636 -0.180295 -0.196924
0.020131 -0.131068 -0.255166
0.001177 -0.426232 -0.190799
-0.012155 -0.064992 -0.167676
-0.251505 -0.489586 -0.044734
0.162056 -0.065542 -0.166444
0.017214 0.121009 -0.020486
0.231414 -0.347292 -0.184091
0.052590 -0.187358 -0.254877
0.150268 -0.453553 -0.148873
0.231710 -0.263207 -0.195083
0.077347 0.134286 -0.022666
0.157028 0.134088 -0.024777
0.152628 -0.483117 -0.122336
0.312729 -0.474830 0.028494
0.156938 -0.408421 -0.178160
0.210296 -0.399260 -0.170051
0.155543 -0.345337 -0.204948
0.075077 -0.351087 -0.219512
-0.113516 -0.334652 -0.079797
0.070376 -0.262941 -0.241359
0.152999 -0.265258 -0.221395
-0.260925 -0.281522 -0.067153
-0.163813 -0.249589 -0.110536
-0.000517 -0.245935 -0.256464
-0.251757 -0.234363 -0.110374
-0.305968 -0.241161 -0.095382
0.153196 -0.189709 -0.227997
0.093251 -0.180480 -0.246527
-0.048941 -0.173545 -0.249073
-0.157017 -0.189159 -0.134516
-0.370694 -0.198645 -0.084469
-0.003756 -0.479136 -0.132966
-0.093078 -0.475263 -0.122568
0.075829 -0.460227 -0.099777
-0.001899 -0.459362 -0.105936
0.165415 -0.477174 -0.046786
0.144067 -0.458390 -0.051246
0.076365 -0.454779 -0.042957
-0.000796 -0.457043 -0.042454
-0.168778 -0.486562 -0.041229
0.240678 -0.480161 0.029208
-0.082080 -0.458459 -0.042333
0.149462 -0.481931 0.033089
0.122461 -0.463743 0.002471
0.076974 -0.458785 0.035224
-0.165019 -0.485403 0.035211
-0.002507 -0.458952 0.035797
0.305050 -0.475313 0.120096
-0.248056 -0.489990 0.034763
-0.072565 -0.460061 0.025380
0.109360 -0.475999 0.064561
0.243323 -0.479673 0.126123
-0.110629 -0.489074 0.043599
0.151045 -0.483545 0.115056
0.113069 -0.479908 0.084853
0.336442 -0.463570 0.135023
0.076638 -0.459867 0.113200
-0.117317 -0.480495 0.113990
-0.002076 -0.459748 0.113895
-0.057473 -0.462636 0.113188
-0.256639 -0.489621 0.112012
0.123161 -0.463285 0.143835
0.181877 -0.477548 0.192178
0.151977 -0.457373 0.192210
0.076190 -0.455132 0.193957
-0.102975 -0.477424 0.189320
-0.067928 -0.459351 0.209563
-0.002745 -0.457144 0.191918
-0.161571 -0.485037 0.194232
0.216362 -0.476830 0.268931
-0.250727 -0.485793 0.193098
-0.320925 -0.308279 0.034877
0.072334 -0.480626 0.296205
-0.087828 -0.482461 0.282326
-0.003897 -0.482100 0.299386
does this code will work if i place all the data into a data.txt file and read it in the main.cpp?
int main ()
{
{
ifstream myReadFile;
myReadFile.open("Data.txt");
char output[100];
if (myReadFile.is_open())
{
while (!myReadFile.eof())
{
myReadFile >> output;
cout<<output;
}
}
myReadFile.close();
return 0;
}
and this is the bubble sort for the vertex X, I know it will take a really long time to sort it, but time doesn't matter as long as the code works.
for (i=0; i<500-1; i++)
for (int j=i+1; j<500; j++)
if (X[i] > X[j])
{
int temp = X[i];
X[i] = X [j];
X[j] = temp;
temp = Y[i];
Y[i] = Y [j];
Y[j] = temp;
temp = Z[i];
Z[i] = Z [j];
Z[j] = temp;
}
You talk about binary search trees but the code you have shown does a binary search on a sorted array. That is not the same thing.
One way of using your binary search would be to put the X,Y and Z values together in an array or custom struct. Something like:
struct Vertex {
float X;
float Y;
float Z;
};
Modify your binary search function so that it takes an array of these structs instead of an array of integers. You will have to make your search key an X float value:
int binary_search(Vertex array[],int first,int last, float x_value);
Load your X, Y, Z values into an array of Vertex and then sort by X value:
bool vertexXComparator(const Vertex& lhs, const Vertex& rhs) {
return lhs.X < rhs.X;
}
std::sort(list, list+number_of_vertexes, vertexXComparator);
Then you should be able to use your binary search (or std::binary_search) on the sorted list.
I would be a bit wary of using a float as a search key. It is generally a bad idea to test floating point numbers for equality.
Live demo.

Gauss-Jordan elimination returning only zeros

My code is returning zero for all values and I'm not sure why. I'm supposed to solve using Gauss-Jordan elimination. Does anyone have any suggestions?
#include <stdio.h>
#define N 10
int main()
{
double a[N][N+1]={{3.55618, 5.87317, 7.84934, 5.6951, 3.84642, 9.15038, -1.68539, 5.03067, 7.63384, -1.75626},
{-4.82893, 8.38177, -0.301221, 5.10182, -4.1169, -6.09145, -3.95675, -2.33365, 1.3969, 6.54555},
{-7.64196, 5.66605, 3.20481, 1.55619, -1.19814, 9.79288, 5.35547, 5.86109, 4.95544, -9.35749},
{-2.95914, -9.16958, 7.3216, 2.39876, -8.1302, -7.55135, -2.37718, 7.29694, 5.9867, 8.5401},
{-8.42043, -0.369407, -5.4102, -8.00545, 9.22153, 3.96454, 5.38499, 0.438365, 0.419677, 4.17166},
{6.02952, 4.57728, 5.46424, 3.52915, -1.01135, -3.74686, 8.14264, -8.86961, -2.88114, 1.29821},
{0.519819, -6.16655, 1.13216, 2.75811, -1.05975, 4.20286, -3.45764, 0.763558, -0.281287, -9.76168},
{5.15737, -9.67481, 9.29904, -3.93334, 9.12785, -4.25208, -6.1652, 2.5375, 0.139195, 2.00106},
{-4.30784, 1.40711, -6.97966, -9.29715, 5.17234, 2.42634, 1.88818, -2.05526, -3.7679, 3.3708},
{-4.65418, 7.18118, 6.51338, 3.13249, 0.188456, -16.85599, 7.21435, -2.93417, 1.06061, 1.10807}};
double pivot,d;
int i,j,k;
for(k=0; k<N; k++)
{
pivot=a[k][k];
for(j=k; j<N+1; j++)
a[k][j]=a[k][j]/pivot;
for(i=0; i<N; i++)
{
if(i != k)
{
d=a[i][k];
for(j=k; j<N+1; j++) a[i][j]=a[i][j]-d*a[k][j];
}
}
}
for(i=0; i<N; i++)
printf("x[%d]=%lf\n", i+1, a[i][N]);
return 0;
}
Your input array is of size [10][11] but your input values are of size [10][10]. Therefore your rightmost column is full of zeroes, which means the output will be all zeroes.
Try this :- (N in place of N+1) :-)
double a[N][N]={{3.55618, 5.87317, 7.84934, 5.6951, 3.84642, 9.15038, -1.68539,
5.03067, 7.63384, -1.75626},
{-4.82893, 8.38177, -0.301221, 5.10182, -4.1169, -6.09145, -3.95675,
-2.33365, 1.3969, 6.54555},
{-7.64196, 5.66605, 3.20481, 1.55619, -1.19814, 9.79288, 5.35547,
5.86109, 4.95544, -9.35749},
{-2.95914, -9.16958, 7.3216, 2.39876, -8.1302, -7.55135, -2.37718,
7.29694, 5.9867, 8.5401},
{-8.42043, -0.369407, -5.4102, -8.00545, 9.22153, 3.96454, 5.38499,
0.438365, 0.419677, 4.17166},
{6.02952, 4.57728, 5.46424, 3.52915, -1.01135, -3.74686, 8.14264,
-8.86961, -2.88114, 1.29821},
{0.519819, -6.16655, 1.13216, 2.75811, -1.05975, 4.20286, -3.45764,
0.763558, -0.281287, -9.76168},
{5.15737, -9.67481, 9.29904, -3.93334, 9.12785, -4.25208, -6.1652,
2.5375, 0.139195, 2.00106},
{-4.30784, 1.40711, -6.97966, -9.29715, 5.17234, 2.42634, 1.88818,
-2.05526, -3.7679, 3.3708},
{-4.65418, 7.18118, 6.51338, 3.13249, 0.188456, -16.85599, 7.21435,
-2.93417, 1.06061, 1.10807}};

Unterminating c++ Program [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have recently read an enormous book on c++, but I have not tested what I have learned until now. Now, this "enormous book" of mine was actually made in 1995, so I obviously cannot trust everything that this book states. In other words, I know not whether the compiler, enormous book, or I did something wrong.
In order to test my knowledge of c++, I made a class (without pointer syntax due to fear of potential disaster) that I called MathContext. As a quick summary, this class of mine has eight (!) constructors in order to be user-friendly (if I can get this to work). It has three private fields (or attributes; I know not what one would prefer to call them in c++) called base, precision, and mode. The base field of type short stores the requested base (from two, inclusive, to thirty-six, inclusive), the precision field stores the requested decimal digit count (according to the base; I'll worry about that later), and the mode field is actually an instance of a nested enumeration called RoundingMode.
As you may see, this class is somewhat similar to the java.math.MathContext class except that I include the "java.math.RoundingMode" enumeration within the "java.math.MathContext" class.
Here is the complete syntax of all files that I have written in order to test this MathContext class:
MathContext.h:
#pragma once
class MathContext sealed
{
public:
enum RoundingMode
{
DOWN,
UP,
FLOOR,
CEILING,
HALF_DOWN,
HALF_UP,
HALF_EVEN
};
MathContext(void);
MathContext(unsigned short);
MathContext(unsigned int);
MathContext(RoundingMode);
MathContext(unsigned short, unsigned int);
MathContext(unsigned short, RoundingMode);
MathContext(unsigned int, RoundingMode);
MathContext(unsigned short, unsigned int, RoundingMode);
~MathContext(void);
unsigned short get_base(void);
void set_base(unsigned short);
unsigned int get_precision(void);
void set_precision(unsigned int);
RoundingMode get_rounding_mode(void);
void set_rounding_mode(RoundingMode);
bool operator==(MathContext);
bool operator!=(MathContext);
bool operator>(MathContext);
bool operator<(MathContext);
bool operator>=(MathContext);
bool operator<=(MathContext);
private:
unsigned short base;
unsigned int precision;
unsigned RoundingMode mode;
}
MathContext.cpp:
#include "stdafx.h"
#include "MathContext.h"
MathContext::MathContext(void)
{
base = 10;
precision = 15;
mode = RoundingMode::HALF_EVEN;
}
MathContext::MathContext(unsigned short base)
{
set_base(base);
precision = 15;
mode = RoundingMode::HALF_EVEN;
}
MathContext::MathContext(unsigned int precision)
{
base = 10;
set_precision(precision);
mode = RoundingMode::HALF_EVEN;
}
MathContext::MathContext(RoundingMode rounding_mode)
{
base = 10;
precision = 15;
mode = rounding_mode;
}
MathContext::MathContext(unsigned short base, unsigned int precision)
{
set_base(base);
set_precision(precision);
mode = RoundingMode::HALF_EVEN;
}
MathContext::MathContext(unsigned short base, RoundingMode rounding_mode)
{
set_base(base);
precision = 15;
mode = rounding_mode;
}
MathContext::MathContext(unsigned int precision, RoundingMode rounding_mode)
{
base = 10;
set_precision(precision);
mode = rounding_mode;
}
MathContext::MathContext(unsigned short base, unsigned int precision, RoundingMode rounding_mode)
{
set_base(base);
set_precision(precision);
mode = rounding_mode;
}
MathContext::~MathContext(void)
{
delete &base;
delete &precision;
delete &mode;
}
unsigned short MathContext::get_base(void)
{
return base;
}
void MathContext::set_base(unsigned short value)
{
base = value == 0 || value % 36 == 1 ? 2 : (value % 36 == 0 ? 36 : value % 36);
}
unsigned int MathContext::get_precision(void)
{
return precision;
}
void MathContext::set_precision(unsigned int value)
{
precision = value;
}
MathContext::RoundingMode MathContext::get_rounding_mode(void)
{
return mode;
}
void MathContext::set_rounding_mode(RoundingMode rounding_mode)
{
mode = rounding_mode;
}
bool MathContext::operator==(MathContext context)
{
return base == context.base && precision == context.precision && mode == context.mode;
}
bool MathContext::operator!=(MathContext context)
{
return base != context.base || precision != context.precision || mode != context.mode;
}
bool MathContext::operator>(MathContext context)
{
return precision == context.precision ? mode > context.mode : precision > context.precision;
}
bool MathContext::operator<(MathContext context)
{
return precision == context.precision ? mode < context.mode : precision < context.precision;
}
bool MathContext::operator>=(MathContext context)
{
return precision == context.precision ? mode >= context.mode : precision > context.precision;
}
bool MathContext::operator<=(MathContext context)
{
return precision == context.precision ? mode <= context.mode : precision < context.precision;
}
Graphics.cpp (the test class; please don't ask why I called it that):
#include "stdafx.h"
#include "MathContext.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
MathContext context[8];
context[0] = MathContext::MathContext();
context[1] = MathContext::MathContext((unsigned short) 46);
context[2] = MathContext::MathContext((unsigned int) 20);
context[3] = MathContext::MathContext(MathContext::RoundingMode::UP);
context[4] = MathContext::MathContext((unsigned short) 36, (unsigned int) 30);
context[5] = MathContext::MathContext((unsigned short) 36, MathContext::RoundingMode::HALF_DOWN);
context[6] = MathContext::MathContext((unsigned int) 25, MathContext::RoundingMode::HALF_EVEN);
context[7] = MathContext::MathContext((unsigned short) 26, (unsigned int) 29, MathContext::RoundingMode::HALF_EVEN);
int best = 0;
for(int i = 1; i < 8; i++)
{
if(context[best] < context[i])
{
best = i;
}
}
std::cout << "Context #" << best + 1 << " is the best!\n";
return 0;
}
I hoped that, Context #5 is the best! would show on the console (my Windows 8.1 Command Prompt), but I could not get anywhere near that point because after I compiled those three files and began to debug them, the command prompt actually did not terminate (I assume that some would call it a freeze), so I had to force its exit through the Windows 8.1 Task Manager. Now, that's what I call a problem!
Here are two essential details: My OS is Windows 8.1 (I hope that it won't throw anyone off), and I use the Microsoft Visual Studio Express 2012 for Windows Desktop Update 4. As miscellaneous information, I consider myself as an expert Java programmer (it may or may not help; perhaps someone could point out some relevant differences between Java and c++ so that this won't happen ever again), and the source code that I typed actually removed unnecessary whitespace; for example, instead of base = 10;, I typed base=10; Well, the compiler apparently thinks that my code is fine, but it apparently is not for some reason. Could anyone point out to me what is obsolete that I use, why the debugging apparently freezes the command prompt, and any essential differences between Java and c++ that I should take note of as I program in c++? I sincerely thank you!
You are calling the MathContext constructor-methods directly, change that to to the below
by dropping the first part of MathContext::MathContext to just MathContext
int main(int argc, _TCHAR* argv[])
{
MathContext context[8];
context[0] = MathContext();
context[1] = MathContext((unsigned short) 46);
context[2] = MathContext((unsigned int) 20);
context[3] = MathContext(MathContext::UP);
context[4] = MathContext((unsigned short) 36, (unsigned int) 30);
context[5] = MathContext((unsigned short) 36, MathContext::HALF_DOWN);
context[6] = MathContext((unsigned int) 25, MathContext::HALF_EVEN);
context[7] = MathContext((unsigned short) 26, (unsigned int) 29, MathContext::HALF_EVEN);
You are deleting memeory which is not allocated (newed) by you (remove that)
MathContext::~MathContext(void)
{
//delete &base;
//delete &precision;
//delete &mode;
}
In short, your program was hanging due to the weird memory errors you had -- direct invocation of a constructor will assume that memory for the object is already allocated + the deallocation of memory which you had not allocated.

How to pass array of integers from Tcl to C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
How do I send Tcl array to C++? I have written the following code:
Tcl:
set ns [new Simulator]
set n [$ns node]
$n set X_ 100
$n set Y_ 30
$n set Z_ 0
set x [$n set X_]
set y [$n set Y_]
set z [$n set Z_]
#after 2000
set b {12 2 3 4 5}
set aa [new "Application/Trust/ITLeach"]
$aa set bufer_ 1
$aa set allnode_ $n
$aa set X_ $x
$aa set Y_ $y
$aa set Z_ $z
$aa set ClausterHeadID_ [array get b] **#send array to c++**
$ns at 0.0 "$aa start"
puts $b
$ns run
ITLEACH.h:
#ifndef ns_ITLeach_h
#define ns_ITLeach_h
#include "app.h"
#include "node.h"
#include "tcl.h"
#include "mobilenode.h"
#include <iostream>
#include <fstream>
class ITLeach;
#define TCL_OK 0
class ITLeach : public Application {
public:
ITLeach();
virtual int command(int argc, const char*const* argv);
protected:
// need to define recv and timeout
void start();
int Buffer;
MobileNode * node ;
ofstream nodeSetting;
double XPos ;
double YPos ;
double ZPos ;
int ClausterHeadID [] ; //int array that passed from tcl file
int ClausterID [] ;
int id_node;
};
#endif
ITLEACH.cc:
/*
* ITLeach.cc
*
* Created on: Oct 29, 2013
* Author: root
*/
#include "ITLeach.h"
static class ITLeachClass : public TclClass {
public:
ITLeachClass() : TclClass("Application/Trust/ITLeach") {}
TclObject* create(int, const char*const*) {
return (new ITLeach());
}
} class_app_ITLeach;
ITLeach::ITLeach() : Application() {
Tcl_Obj *baObj = Tcl_NewObj();
bind("bufer_",&Buffer);
bind("allnode_",&node);
bind("X_",&XPos);
bind("Y_",&YPos);
bind("Z_",&ZPos);
bind("ClausterHeadID_",(int *) &ClausterHeadID); // call array from tcl
bind("ClausterID_",ClausterID);
bind("id_",&id_node);
}
int ITLeach::command(int argc, const char*const* argv) {
if (strcmp(argv[1], "start") == 0) {
ITLeach::start();
return(TCL_OK);
}
return(ITLeach::command(argc, argv));
}
void ITLeach::start()
{
//double x=0, y =0 , z =0;
nodeSetting.open("./leachnode.txt",fstream::app);
//node = (MobileNode*)Node::get_node_by_address(i);
//node->location()->getLocation(x,y,z);
//node->getLoc(&x,&y,&z);
nodeSetting << "id " << id_node << " x "<< XPos << " y " << YPos << " z " << ZPos <<"\n";
nodeSetting.close();
printf(" claster head number : %d \n" ,ClausterHeadID[1]);
printf("node number is : %d \n",Buffer);
}
I send array from Tcl with this code:
$aa set ClausterHeadID_ [array get b] **#send array to c++**
and receive array from C++ with this code:
bind("ClausterHeadID_",(int *) &ClausterHeadID); // call array from tcl
But it doesn't work, please help me.
If you've got that command bound to the string interface (i.e., the arguments arrive via int argc, char **argv) then you use Tcl_SplitList() to take apart the relevant argument (which might be argv[argc-1], i.e., the last argument) and then Tcl_GetInt() to retrieve an integer from each of those values. Those integers are the members of that Tcl list.
int listc;
char **listv;
if (Tcl_SplitList(interp, argv[argc-1], &listc, &listv) != TCL_OK) {
// wasn't a valid list!
return TCL_ERROR;
}
std::vector<int> theArray(listc, 0);
for (int i=0 ; i<listc ; i++) {
if (Tcl_GetInt(interp, listv[i], &theArray[i]) != TCL_OK) {
// wasn't an int in the list!
return TCL_ERROR;
}
}
This isn't very fast! For a faster way, you need to use the Tcl_Obj-based API (the Tcl_Obj is the fundamental Tcl first-class value type), starting with registering your implementation function correctly. After that, it's fairly easy to convert the above code:
int listc;
Tcl_Obj **listv;
if (Tcl_ListObjGetElements(interp, argv[argc-1], &listc, &listv) != TCL_OK) {
// wasn't a valid list!
return TCL_ERROR;
}
std::vector<int> theArray(listc, 0);
for (int i=0 ; i<listc ; i++) {
if (Tcl_GetIntFromObj(interp, listv[i], &theArray[i]) != TCL_OK) {
// wasn't an int in the list!
return TCL_ERROR;
}
}
The big difference? A Tcl_Obj knows whether it is holding a string or an integer (or a float or any number of other things) and so the Tcl runtime doesn't normally need to reparse or type-convert values, whereas if everything is a string, you do a lot of conversions. (It's common to say “Everything is a string” in Tcl, but that's inaccurate; the correct version is “Everything has a perfect string serialization, or is a named entity” but that's rather more verbose.)

How to pause a program for a few milliseconds?

How to pause a program for a few milliseconds using C++ managed code?
I tried Sleep() but it didn't work when I included the winbase.h file, I got lots of compile errors!
for(int i=0; i<10; i++)
{
simpleOpenGlControl1->MakeCurrent();
System::Threading::Thread::Sleep(100);
theta_rotated += 2.0* 3.141592653/(double)Model->size()/10.0;
simpleOpenGlControl1->Invalidate();
}
private: System::Void simpleOpenGlControl1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e)
{
Gl::glMatrixMode(Gl::GL_MODELVIEW);
Gl::glLoadIdentity();
Gl::glClearColor(1, 1, 1, 1);
Gl::glClear(Gl::GL_COLOR_BUFFER_BIT | Gl::GL_DEPTH_BUFFER_BIT);
camera->Tellgl();
Gl::glRotated(Angle,X,Y,Z);
RenderModels();
}
void RenderModels(void)
{
Points = gcnew System::Collections::Generic::List<Point>();
Point p(0,0) ;
int t = -5;
double r = 5;
double x, z, theta;
for(int i = 0;i < Model->size();i++)
{
theta = i*2* 3.141592653/ (double)Model->size();
x = r*sin(theta + theta_rotated);
z = r*cos(theta + theta_rotated);
Gl::glPushMatrix();
Gl::glTranslated(x,0,z);
Model->at(i).Render();
p.X=x;
p.Y=0;
Points->Add(p);
t +=1;
Gl::glPopMatrix();
}
//simpleOpenGlControl1->Invalidate();
}
You want System::Threading::Thread::Sleep().
http://msdn.microsoft.com/en-us/library/system.threading.thread.sleep.aspx
System::Threading::Thread::Sleep() as mentioned in another answer. But let me warn you, it is not precise, and for small (milliseconds) Sleep is extremely imprecise. consider that your app run together with other apps and threads and they all want processor time.
#include <unistd.h>
main() {
usleep(2000); // 2 msecs
}
Read the man pages ;-)
man 3 usleep