Presentation is loading. Please wait.

Presentation is loading. Please wait.

And c++?. What will we do? C vs JAVA Dev-C++ tutorial and your first program C++ structure Reading input/output Flow Control (if, for, while) array function.

Similar presentations


Presentation on theme: "And c++?. What will we do? C vs JAVA Dev-C++ tutorial and your first program C++ structure Reading input/output Flow Control (if, for, while) array function."— Presentation transcript:

1 And c++?

2 What will we do? C vs JAVA Dev-C++ tutorial and your first program C++ structure Reading input/output Flow Control (if, for, while) array function (method) debugging

3 Hello World (Day 1 Morning) C JAVA import java.io.*; class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); } import java.io.*; class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); } #include int main(int argc, char* argv[]) { printf("Hello, CP!\n"); return 0; } #include int main(int argc, char* argv[]) { printf("Hello, CP!\n"); return 0; }

4 C vs JAVA C code DOES NOT need to be in a class No filename restriction C and JAVA have very similar syntax if, for, while is the same Data type is almost the same (int, char, bool, float, double, etc) Function and method is the same For c++, function does not need to be in a class We don’t really use any class in algorithm :D

5 Output (printf) printf(format_string, value…); commandWhat you get printf(“YES”);YES printf(“YES %d”,12);YES 12 printf(“%d is more than%d”,24,12);24 is more than12 printf(“floating point %f”,2.3457);floating point 2.3457 printf(“test\ntest\nhaha”);test haha printf(“string %s is possible”,”haha”);string haha is possible

6 input(scanf) scanf(format_string, &variable…); Use the same format string #include int main() { int age, w; printf("Please enter your age and weight”); scanf("%d %d", &age, &w); printf(“your age = %d \n your weight is %d\n”,age,w); }

7 Condition Statement If statement The same as java #include int main() { int age; printf( "Please enter your age" ); scanf( "%d", &age ); if ( age < 100 ) { printf ("You are NOT old!\n" ); } else { printf ("You are old!\n" ); } return 0; } #include int main() { int age; printf( "Please enter your age" ); scanf( "%d", &age ); if ( age < 100 ) { printf ("You are NOT old!\n" ); } else { printf ("You are old!\n" ); } return 0; }

8 While loop The same as java #include int main() { int x = 0; while ( x < 10 ) { printf("x = %d\n",x); x++; } return 0; } #include int main() { int x = 0; while ( x < 10 ) { printf("x = %d\n",x); x++; } return 0; }

9 For loop The same as java #include int main() { for (int i = 0;i < 10;i++) { printf(“i = %d\n",i); } return 0; } #include int main() { for (int i = 0;i < 10;i++) { printf(“i = %d\n",i); } return 0; }

10 Practice Lab 1 (http://www.nattee.net/files-dae/Algo-Lab1.pdf) Prob 0,1 Lab 2 (http://www.nattee.net/files-dae/Algo-Lab2.pdf) Prob 0,1 hw00e (BMI) http://www.nattee.net/~dae/algo/prob/hw00b_fibo/problem.pdf hw00c (drawbox) http://www.nattee.net/~dae/algo/prob/hw00c_drawbox/problem.pd f hw00d (time after) http://www.nattee.net/~dae/algo/prob/hw00d_timeafter/problem.pd f

11 Array in C++ (Day 1 Afternoon) (almost) the same as java #include int main() { int fibo[100]; fibo[0] = 1; fibo[1] = 1; for (int i = 2;i < 100;i++) { fibo[i] = fibo[i-1] + fibo[i-2]; } printf("%d\n",fibo[99]); return 0; }

12 2D Array Example #include int main() { int x; int y; int array[8][8]; for ( x = 0; x < 8; x++ ) { for ( y = 0; y < 8; y++ ) array[x][y] = x * y; } printf("Array Indices\n"); for ( x = 0; x < 8;x++ ) { for ( y = 0; y < 8; y++ ) printf("[%d][%d] = %d\n",x,y,array[x][y]); printf("\n"); } return 0; } #include int main() { int x; int y; int array[8][8]; for ( x = 0; x < 8; x++ ) { for ( y = 0; y < 8; y++ ) array[x][y] = x * y; } printf("Array Indices\n"); for ( x = 0; x < 8;x++ ) { for ( y = 0; y < 8; y++ ) printf("[%d][%d] = %d\n",x,y,array[x][y]); printf("\n"); } return 0; }

13 More practice Lab 2 Prob 2 hw00f http://www.nattee.net/~dae/algo/prob/hw00f_aminmax/problem.pdf

14 Debugging (Day 2 Morning) Follow guide in Lab 3 (http://www.nattee.net/files-dae/Algo-Lab3.pdf)


Download ppt "And c++?. What will we do? C vs JAVA Dev-C++ tutorial and your first program C++ structure Reading input/output Flow Control (if, for, while) array function."

Similar presentations


Ads by Google