-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode3.c
1205 lines (949 loc) · 22.9 KB
/
code3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<%
int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
int main() /* Most important part of the program! */
{
int age; /* Need a variable... */
printf( "Please enter your age" ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in age */
if ( age < 100 ) { /* If the age is less than 100 */
printf ("You are pretty young!\n" ); /* Just to show you it works... */
}
else if ( age == 100 ) { /* I use else just to show an example */
printf( "You are old\n" );
}
else {
printf( "You are really old\n" ); /* Executed if no other statement is */
}
return 0;
}
int DoSomethingNice( int aVariable, int aFunction, void *dataPointer )
{
int rv = 0;
if (aVariable < THE_BIGGEST) {
rv = aFunction(aVariable, dataPointer );
} else {
rv = aFunction(aVariable, dataPointer );
}
return rv;
}
// typedef struct {
// int colorSpec;
// char *phrase;
// } DataINeed;
int TalkJive( int myNumber, void *someStuff )
{
/* recast void * to pointer type specifically needed for this function */
int *myData = someStuff;
/* talk jive. */
return 5;
}
int FunctTwo( int *pStruct, int *mValue )
{
int j;
long AnArray[25];
long *pAA;
pAA = &AnArray[13];
j = FunctOne( pStruct, *mValue, pAA );
return j;
}
float A[6][8];
float *pf;
pf = &A[0][0];
//*(pf+1) = 1.3; /* assigns 1.3 to A[0][1] */
//*(pf+8) = 2.3; /* assigns 2.3 to A[1][0] */
long myArray[20];
long *pArray;
int i;
/* Assign values to the entries of myArray */
pArray = myArray;
for (i=0; i<10; ++i) {
*pArray++ = 5 + 3*i + 12*i*i;
*pArray++ = 6 + 2*i + 7*i*i;
}
float linearA[30];
float *A[6];
A[0] = linearA; /* 5 - 0 = 5 elements in row */
A[1] = linearA + 5; /* 11 - 5 = 6 elements in row */
A[2] = linearA + 11; /* 15 - 11 = 4 elements in row */
A[3] = linearA + 15; /* 21 - 15 = 6 elements */
A[4] = linearA + 21; /* 25 - 21 = 4 elements */
A[5] = linearA + 25; /* 30 - 25 = 5 elements */
*A[3][2] = 3.66; /* assigns 3.66 to linearA[17]; */
*A[3][3] = 1.44; /* refers to linearA[12];
negative indices are sometimes useful. But avoid using them as much as possible. */
float KrazyFunction( int *parm1, int p1size, int bb )
{
int ix; //declaring an integer variable//
for (ix=0; ix<p1size; ix++) {
if (parm1[ix].m_aNumber == bb )
return parm1[ix].num2;
}
return 0;
}
/* ... */
//struct MyStruct myArray[4];
//#define MY_ARRAY_SIZE (sizeof(myArray)/sizeof(*myArray))
float v3;
//struct MyStruct *secondArray;
int someSize;
int ix;
/* initialization of myArray ... */
v3 = KrazyFunction( myArray, MY_ARRAY_SIZE, 4 );
/* ... */
//secondArray = calloc( someSize, sizeof(myArray) );
for (ix=0; ix<someSize; ix++) {
secondArray[ix].m_aNumber = ix *2;
secondArray[ix].num2 = 304 * ix * ix;
}
int c, d;
int *pj;
// struct MyStruct astruct;
// struct MyStruct *bb;
c = 10;
pj = &c; /* pj points to c */
d = *pj; /* d is assigned the value to which pj points, 10 */
pj = &d; /* now points to d */
*pj = 12; /* d is now 12 */
bb = &astruct;
(*bb).m_aNumber = 3; /* assigns 3 to the m_aNumber member of astruct */
bb->num2 = 44.3; /* assigns 44.3 to the num2 member of astruct */
*pj = bb->m_aNumber; /* eqivalent to d = astruct.m_aNumber; */
int myInt;
int *pPointer;
// struct MyStruct dvorak;
// struct MyStruct *pKeyboard;
pPointer = &myInt;
pKeyboard = &dvorak;
struct MyStruct {
int m_aNumber;
float num2;
};
int main()
{
int *pJ2;
// struct MyStruct *pAnItem;
}
long * var1, var2;
int ** p3;
void copy_array(float *src, float *dst, int n)
{
while (n-- > 0) {
// Loop that counts down from n to zero
*dst++ = *src++; // Copies element *(src) to *(dst),
// then increments both pointers
}
}
#include "stdio.h"
const char x = 'x';
c <<= 2;
int i, j , k;
x = a * b - c ;
y = b / c * a ;
z = a - b / c + d;
head.x=25;
head->y=20;
head.direction=RIGHT;
bool x= !true;
int main ()
{
float a, b, c, x, y, z;
a = 9;
b = 12;
c = 3;
x = a - b / 3 + c * 2 - 1;
y = a - b / (3 + c) * (2 - 1);
z = a - ( b / (3 + c) * 2) - 1;
printf ("x = %fn",x);
printf ("y = %fn",y);
printf ("z = %fn",z);
}
int a =2;
int mult (int x, int y)
{
return x * y;
}
int main () {
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a == 10 ) {
/* if condition is true then print the following */
printf("Value of a is 10\n" );
}
else if( a == 20 ) {
/* if else if condition is true */
printf("Value of a is 20\n" );
}
else if( a == 30 ) {
/* if else if condition is true */
printf("Value of a is 30\n" );
}
else {
/* if none of the conditions is true */
printf("None of the values is matching\n" );
}
printf("Exact value of a is: %d\n", a );
return 0;
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" \nvalues after swap m = %d\n and n = %d", a, b);
}
int max(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 == num2)
result = num1;
else
result = num2;
return result;
}
/* check the boolean condition */
if( a < 20 ) {
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
else {
/* if condition is false then print the following */
printf("a is not less than 20\n" );
}
for( a = 10; a < 20; a = a + 1 ){
//printf("value of a: %d\n", a);
result = num1;
}
int main () {
int a;
/* for loop execution */
for( a = 10; a < 20; a = a + 1 ){
printf("value of a: %d\n", a);
}
return 0;
}
if(boolean_expression ) {
/* Executes when the boolean expression 1 is true */
}
else if( boolean_expression ) {
/* Executes when the boolean expression 2 is true */
}
else if( boolean_expression ) {
/* Executes when the boolean expression 3 is true */
}
else {
/* executes when the none of the above condition is true */
}
int *length;
int bend_no;
int a[5] ;
//int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
int len [] [10] ;
//int len2 [] = {4};
int a[][2];
int a[5] ;
char key;
int i, j , k;
char c;
float f, salary;
double d;
int a[5][2];
enum colors {RED=1, YELLOW, GREEN=6, BLUE };
//const int a =2;
int a =2;
int d = 3, f = 5; // definition and initializing d and f.
const char x = 'x'; // the variable x has the value 'x'.
/* function returning the max between two numbers */
int max(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 == num2)
result = num1;
else
result = num2;
return result;
}
void record(){
int x;
}
int life;
void Delay(long car){
int c;
}
void gotoxy(int x, int y){
}
int Scoreonly(){
int z;
}
struct coordinate{
int x;
int y;
int direction;
};
for (string item : someList) {
char key;
Print();
// system("cls");
loaduno(key);
}
int main () {
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 ) {
printf("value of a: %d\n", a);
//a++;
int tmp;
}
return 0;
}
int main () {
/* local variable definition */
int a = 10;
/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}
int main()
{
int x;
x = 0;
do {
/* "Hello, world!" is printed at least one time
even though the condition is false */
printf( "Hello, world!\n" );
} while ( x != 0 );
getchar();
}
int main () {
/* local variable definition */
char grade = 'B';
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
char grade = 'B';
break;
default :
printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade );
return 0;
}
while (true)
{
take_turn(player1);
take_turn(player2);
}
take_turn(uno,dos,tres);
while(true)
{
if (someone_has_won() || someone_wants_to_quit() == TRUE)
{break;}
take_turn(player1);
if (someone_has_won() || someone_wants_to_quit() == TRUE)
{break;}
take_turn(player2);
}
a= suma();
b=suma(x,y);
int main() {
int rad;
float PI = 3.14, area, ci;
printf("\nEnter radius of circle: ");
scanf("%d", &rad);
area = PI * rad * rad;
printf("\nArea of circle : %f ", area);
ci = 2 * PI * rad;
printf("\nCircumference : %f ", ci);
return (0);
}
int main () {
/* local variable definition */
int a = 100;
int b = 200;
/* check the boolean condition */
if( a == 100 ) {
/* if condition is true then check the following */
if( b == 200 ) {
/* if condition is true then print the following */
printf("Value of a is 100 and b is 200\n" );
}
}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );
return 0;
}
int main () {
/* local variable definition */
int a = 100;
int b = 200;
switch(a) {
case 100:
printf("This is part of outer switch\n", a );
switch(b) {
case 200:
printf("This is part of inner switch\n", a );
}
}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );
return 0;
}
int main() {
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
int main () {
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i < 10; i ) {
n[ i ] = i + 100; /* set element at location i to i + 100 */
}
/* output each array element's value */
for (j = 0; j < 10; j) {
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}
int main () {
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i < 10; i ) {
n[ i ] = i + 100; /* set element at location i to i + 100 */
}
/* output each array element's value */
for (j = 0; j < 10; j) {
printf("Element[%d] = %d\n", j, head.hoja );
}
return 0;
}
// Sum the elements of an array
float sum_elements(float arr, int n)
{
float sum = 12.4;
int i = 0;
while (i < n){
sum += arr[i++]; // Post-increment of i, which steps
// through n elements of the array
}
return sum;
}
int main()
{
int i=0;
while(i++ < 5 )
{
printf("%d ",i);
}
return 0;
}
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
}
}
int main()
{
int i=20;
while(i>10)
{
printf("%d ",i);
i--;
}
}
int main()
{
int i=10;
while(--i > 5 )
{
printf("%d ",i);
}
return 0;
}
int main()
{
int i=10;
while(i-- > 5 )
{
printf("%d ",i);
}
return 0;
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("\n values after swap a = %d \nand b = %d", *a, *b);
}
int main()
{
int num = 45 , *ptr , **ptr2ptr ;
ptr = #
ptr2ptr = &ptr;
printf("%d",**ptr2ptr);
return(0);
}
void main() {
int* x; // Allocate the pointers x and y
int* y; // (but not the pointees)
//x = malloc(sizeof(int)); // Allocate an int pointee,
// and set x to point to it
*x = 42; // Dereference x to store 42 in its pointee
*y = 13; // CRASH -- y does not have a pointee yet
y = x; // Pointer assignment sets y to point to x's pointee
*y = 13; // Dereference y to store 13 in its (shared) pointee
}
for (player = 1; someone_has_won == FALSE; player++)
{
if (player > total_number_of_players)
{player = 1;}
if (is_bankrupt(player))
{continue;}
take_turn(player);
}
for (i = 0; i < test; i) {
int intValue = test[i];
// do some work here on intValue
}
for ( i = 0; i < test.length; i++) {
int intValue = test[i];
// do some work here on intValue
}
for (i = 0; i < test; i++) {
int intValue = test[i];
// do some work here on intValue
}
int main()
{
int i;
int j = 10;
for( i = 0; i <= j; i ++ )
{
if( i == 5 )
{
continue;
}
printf("Hello %d\n", i );
}
}
int main()
{
char key;
Print();
// system("cls");
loaduno(key);
load(key,x,y);
length=5;
head.x=25;
head.y=20;
head.direction=RIGHT;
Boarder();
Food(); //to generate food coordinates initially
life=3; //number of extra lives
bend[0]=head;
Move(); //initialing initial bend coordinate
return 0;
}
void Move()
{
int a,i;
do{
Food();
fflush(stdin);
len=0;
for(i=0;i<30;i++)
{
body[i].x=0;
body[i].y=0;
if(i==length)
break;
}
Delay(length);
Boarder();
if(head.direction==RIGHT)
Right();
else if(head.direction==LEFT)
Left();
else if(head.direction==DOWN)
Down();
else if(head.direction==UP)
Up();
ExitGame();
}while(!kbhit());
a=getch();
if(a==27)
{
// system("cls");
exit(0);
}
key=getch();
if((key==RIGHT&&head.direction!=LEFT&&head.direction!=RIGHT)||(key==LEFT&&head.direction!=RIGHT&&head.direction!=LEFT)||(key==UP&&head.direction!=DOWN&&head.direction!=UP)||(key==DOWN&&head.direction!=UP&&head.direction!=DOWN))
{
bend_no++;
bend[bend_no]=head;
head.direction=key;
if(key==UP)
head.y--;
if(key==DOWN)
head.y++;
if(key==RIGHT)
head.x++;
if(key==LEFT)
head.x--;
Move();
}
else if(key==27)
{
// system("cls");
exit(0);
}
else
{
printf("\a");
Move();
}
}
GotoXY(head.x,head.y-i);
void load(){
int row,col,r,c,q;
gotoxy(36,14);
printf("loading...");
gotoxy(30,15);
for(r=1;r<=20;r++){
for(q=0;q<=100000000;q++){
}//to display the character slowly
printf("%c",177);}
getch();
}
void Down()
{
int i;
for(i=0;i<=(head.y-bend[bend_no].y)&&len<length;i++)
{
GotoXY(head.x,head.y-i);
// {
// if(len==0)
// printf("v");
// else
// printf("*");
// }
body[len].x=head.x;
body[len].y=head.y-i;
len++;
}
Bend();
if(!kbhit())
head.y++;
}
void Delay(long k)
{
Score();
long i;
//for(i=0;i<=(10000000);i++);
}
void ExitGame()
{
int i,check=0;
for(i=4;i<length;i++) //starts with 4 because it needs minimum 4 element to touch its own body
{
if(body[0].x==body[i].x&&body[0].y==body[i].y)
{
check++; //check's value increases as the coordinates of head is equal to any other body coordinate
}
if(i==length||check!=0)
break;
}
if(head.x<=10||head.x>=70||head.y<=10||head.y>=30||check!=0)
{
life--;
if(life>=0)
{
head.x=25;
head.y=20;
bend_no=0;
head.direction=RIGHT;
Move();
}
else
{
system("cls");
printf("All lives completed\nBetter Luck Next Time!!!\nPress any key to quit the game\n");
record();
exit(0);
}
}
}
/*************************************************************************
Por probar, todos estos casos para abajo no funcionan
***********************************************************************/
//coordinate head, bend[500],food,body[30];
void Food()
{
if(head.x==food.x&&head.y==food.y)
{
length++;
// time_t a;
a=time(0);
srand(a);
food.x=rand()%70;
if(food.x<=10)
food.x+=11;
food.y=rand()%30;
if(food.y<=10)
food.y+=11;
}
else if(food.x==0)/*to create food for the first time coz global variable are initialized with 0*/
{
food.x=rand()%70;
if(food.x<=10)
food.x+=11;
food.y=rand()%30;
if(food.y<=10)
food.y+=11;
}
}
void Left()
{
int i;
for(i=0;i<=(bend[bend_no].x-head.x)&&len<length;i++)
{
body[len].x=head.x+i;
body[len].y=head.y;
len++;