blockchain in c

Solutions on MaxInterview for blockchain in c by the best coders in the world

showing results for - "blockchain in c"
Cassius
30 Mar 2019
1int hash();
2void printStruct();
3void printChain();
4void printWallet();
5void setLocation();
6int Random();
7int rand();
8int getBlock();
9void printNewChain();
10struct Block{
11int locationBefore;
12int machineID;
13int locationCurrent;
14};
15int Random(int lower, int upper)
16{
17int num = (rand() % (upper - lower + 1)) + lower;
18return num;
19}
20// void setLocation(int previousLocation , int currentLocation){
21// previousLocation = currentLocation;
22// }
23void main(){
24//initialize
25struct Block hashArray[100];
26int walletMachine1 = 0, walletMachine2 = 0, z =0 , y =
270,machine1Code,machine2Code,check=1,manualCheck,automaticIterations,i=0,blocks=0;
28//solution
29int solution = Random(0,10);
30printf("manual or automatic simulation?( 1:manual 0:automatic):");
31scanf("%d",&manualCheck);
32if(manualCheck == 0){
33printf("please enter the number of iterations for the simulations:");
34scanf("%d",&automaticIterations);
35}
36do{
37//mining
38machine1Code = Random(0,10);
39machine2Code = Random(0,10);
40if(machine1Code == solution){
41//machine 1 has the code
42walletMachine1++;
43blocks++;
44//set in block
45hashArray[z].machineID = 1;
46hashArray[z].locationBefore = y;
47hashArray[z].locationCurrent = z;
48//set previous location for next block
49y=z;
50//set hash for new block
51//z now location of next block
52z = Random(0,100);
53z = hash(z,1,hashArray);
54//new code
55solution = Random(0,10);
56}
57if(machine2Code == solution){
58//machine 2 has the code
59walletMachine2++;
60blocks++;
61//set in block
62hashArray[z].machineID = 2;
63hashArray[z].locationBefore = y;
64hashArray[z].locationCurrent = z;
65//set previous location for next block
66y=z;
67//set hash for new block
68//z now location of next block
69z = Random(0,100);
70z = hash(z,2,hashArray);
71//new code
72solution = Random(0,10);
73}
74if(manualCheck == 1){
75printf("continue?( 1:yes , 0:no ):");
76scanf("%d",&check);
77}
78i++;
79if(i == automaticIterations){
80check = 0;
81}
82}while(check == 1);
83//print wallet for each machine
84printWallet(walletMachine1,walletMachine2,i);
85//to print blockChain
86printf("\n\n");
87printf("the blockchain has been depicted as shown:-\n");
88printf("------------------------Block Chain-------------------------\n");
89printf("------------------------------------------------------------\n");
90//print chain
91printChain(y,hashArray);
92printf("\n\n");
93printf("change a block?( 1:y 0:n):");
94scanf("%d",&manualCheck);
95if(manualCheck == 1){
96do{
97printf("please enter the block to be changed (from top):");
98scanf("%d",&i);
99}while(i > blocks);
100i--;
101//get block
102int LocationNow = getBlock(i,y,hashArray);
103int locationBefore = hashArray[LocationNow].locationBefore;
104printf("\n");
105printf("please enter the data for the block :");
106scanf("%d",&i);
107hashArray[LocationNow].machineID = i;
108//getData
109LocationNow = hash(LocationNow,i,hashArray);
110//set data in block
111hashArray[LocationNow].machineID = i;
112hashArray[LocationNow].locationCurrent = LocationNow;
113// int locationBefore = hashArray[LocationNow].locationBefore;
114hashArray[LocationNow].locationBefore = locationBefore;
115printStruct(hashArray[LocationNow]);
116//to change data
117printNewChain(i,y,hashArray);
118}
119}
120void printNewChain(int num,int y,struct Block Array[100]){
121do{
122y = Array[y].locationBefore;
123num--;
124}while(num != 0);
125printChain(y,Array);
126}
127int getBlock(int num,int y,struct Block Array[100]){
128if(num == 0){
129printStruct(Array[y]);
130}
131do{
132y = Array[y].locationBefore;
133num--;
134}while(num != 0);
135printStruct(Array[y]);
136return y;
137}
138void printWallet(int a,int b,int iterations){
139printf("wallet 1 has earned %d latinum after %d simulations\n",a,iterations);
140printf("wallet 2 has earned %d latinum after %d simulations\n",b,iterations);
141}
142void printChain(int y,struct Block Array[100]){
143if(Array[y].locationCurrent != 0){
144printStruct(Array[y]);
145printChain(Array[y].locationBefore,Array);
146}
147if (Array[y].locationCurrent == 0){
148printStruct(Array[y]);
149}
150}
151void printStruct(struct Block t){
152printf("------------------------------------------------------------\n");
153printf("locationCurrent :- %d\n",t.locationCurrent);
154printf("machineID :- %d\n",t.machineID);
155printf("locationBefore :- %d\n" , t.locationBefore);
156}
157int hash(int num,int data,struct Block Array[100]){
158int location=0,i=0;
159do{
160location = (num%100 + i%100)%100 ;
161i ++;
162}while(Array[location].machineID == 1 || Array[location].machineID == 2 || i != 100);
163return location;
164}