tag:blogger.com,1999:blog-220178302019-05-19T11:12:13.431+02:00C Lessons ProjectLearn C programming from these daily lessons. They're twelve days ahead of you already so get busy...Vurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.comBlogger36125tag:blogger.com,1999:blog-22017830.post-1143084879787951182006-03-23T04:33:00.000+01:002007-01-12T12:37:07.150+01:00Lesson 19: C Programming ExamplesI’ve based this lesson on a single program example. This part of C programming tutorial has dual purpose: firstly to teach you how to apply previously learned C/C++ knowledge, second: to show you how pointers, arrays, functions and matrixes can be combined together in one single program. Example: Maximal number of rows and columns matrix can have is predefined. Write your own main program which reads given number of matrix’s rows and columns, and additionally reads matrix’s given elements. Main program prints: Matrix’s elements sum (calls upon a function that calculates elements sum) Maximal value in every row of a matrix (calls upon a function that finds the biggest element in a flow) Matrix mat is declared in a way of two dimensional array (2D array): float mat[MAXROW][MAXCOL]; Variables nrRow and nrCol store matrix’s dimensions provided by user. The image below shows you this in a visual way. Click on the image to enlarge it. Memory of a computer allocates MAXROW * MAXCOL * sizeof(float) byte. Matrix’s rows follow one after another, in a way where every next row is placed right after the previous one; and so on. Picture below describes this perfectly. Click on the image to enlarge it. Generally speaking, for mat[i][j] – number of elements from the beginning of an array is: i * MAXCOL + j #include &lt;stdio.h&gt; #define MAXROW 100 #define MAXCOL 100 float max( int len, float *p ) { // OR: float max( int len, float p[] ) float res; int i; printf("\nIn function max :"); printf("\nRow’s beginning address in memory : %ld", p); res = p; for( i=1; i&lt;len; i++ ) if( p[i] &gt; res ) res = p[i]; return res; } float sumEl(int nrRow,int nrCol, int maxCol,float *mat) { int i, j; float sum; printf("\nIn function sumEl:"); printf("\nMatrix’s beginning address in memory: %ld", mat); printf("\nBegin. of 2nd row’s addr. in mem.: %ld", &mat[maxCol]); ...<br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com11tag:blogger.com,1999:blog-22017830.post-1142799852520456942006-03-19T21:23:00.000+01:002006-10-03T07:48:05.996+02:00Lesson 18: Pointers and Stacks in CToday's lesson goes more into details about pointers and their usage as function's arguments. Additional tutorial about stacks in C and C++ is provided. Be sure to read this lesson carefully in order to understand it, since pointers are most important part of C programming language. Transferring Argument’s Address into a Function (call by reference) Write your own function that changes polar coordinates into Cartesian coordinates, and show how this function is called upon from main program. #include &lt;math.h&gt; void cart(float r, float fi, float *x, float *y) { *x = r*cos(fi); // x address:1244956,value:1245052 //*x address:1245052,value:1.755165 *y = r*sin(fi); // y address:1244960,value:1245048 //*y address:1245048,value:0.958851 } Main program slice: float x, y; // x address:1245052,value:? // y address:1245048,value:? float fi = 0.5, r = 2; //fi address:1245044,value:0.5 // r address:1245040,value:2 cart(r, fi, &x, &amp;y); printf("x=%f y=%f",x,y); // x address:1245052, value:1.755165 // y address:1245048, value:0.958851 Question: What would happen if the function looked like this? void cart(float r, float fi, float *x, float *y) { x = 10000; *x = r * sin(fi); *y = r * cos(fi); } Important: Function is able to return single value through its “return” order. In order to return more values (in this case x & y), we can return them through arguments (Call by Reference). Example: Write your own function that returns numerator and denominator of a fraction. This fraction is a sum of two fractions, whose two numerators and two denominators are given by user. Function also shortens the fraction if possible, before returning values. Augends numerators and denominators are integers. (Fraction example: 5/9, Numerator: 5, Denominator: 9) void addFractions(int nrator1,int denom1, int...<br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com3tag:blogger.com,1999:blog-22017830.post-1142449098746005822006-03-15T19:58:00.000+01:002006-12-20T08:18:04.836+01:00Lesson 17: Pointers in CAfter three days of waiting, C++ Maniac brings you another interesting lesson. This one is labeled no. 17, and I think moment has come when I can proudly say we have crossed a half-way of my complete C Tutorial; at least first part of it, “C Programming In General”. This Lesson is about Pointers and their useful implementation in Your future C programs. Let’s start… Important: Examples are translated using Microsoft Visual C++ compiler. Example: What will be printed after execution of a following program block? int a = 5; //a address:1245052, value:5 int *b; //a address:1245052, value:5 //b address:1245048, value:? b = &a; //a address:1245052, value:5 //b address:1245048, value:1245052 *b = 6; //a address:1245052, value:6 //b address:1245048, value:1245052 printf("a = %d *b = %d\n", a, *b); printf("a = %d b = %ld\n", a, b); printf("&a = %ld &amp;b = %ld\n", &a, &amp;b); Result: a = 6 *b = 6 a = 6 b = 1245052 &a = 1245052 &amp;b = 1245048 Important: Value that is pointed by variable b is on the same address (b = &a) as the value assigned to variable a. What would happen if we left out this line: b = &amp;a; ? Result: Before assigning value to it, pointer b points to undefined address. This makes it possible for program to store value 6 to an address previously reserved for some other variable or code. This would result in an unexpected behavior or cause an error in program’s execution (because of unauthorized access to memory’s section). Typical Mistakes: scanf(“%d", n); printf(“%d", &n); Connection between Pointers and Arrays: Any declared array’s name can also be used as a pointer. Any pointer can also be used as an array. What will be printed as a result of following program block? int x[] = {0, 1, 2}; //x address:1245044, value:0 //x address:1245048, value:1 //x address:1245052, value:2 printf("&x = %ld x =...<br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com1tag:blogger.com,1999:blog-22017830.post-1142124954107068102006-03-12T01:54:00.000+01:002006-11-30T03:13:57.176+01:00Lesson 16: Functions in CFunctions are essential part of code in C and C++ language, so be sure to carefully read this lesson. You’ll notice there’s nothing to be afraid of – they are really easy to understand, and sometimes, can lighten up our program code significantly. In a way, they remind us of our main program. Functions usually return value that we use in our main block, but sometimes they return nothing. Either way, they do their task: like printing on screen or calculating equations. C++ Maniac presents you another interesting tutorial! Example: Write your own function that calculates arithmetic middle of three real numbers. Write additional main program that stores given three numbers and calls on your previous function, and then prints calculated arithmetic middle. #include &lt;stdio.h&gt; float arit_midd( float a, float b, float c ){ float ar; ar = (a + b + c) / 3; return ar; // How many values “return” may return? } int main(void) { float x, y, z, midd; printf("\nInput three real numbers : "); scanf("%f %f %f", &x, &amp;y, &z ); midd = arit_midd(x,y,z); printf("\nArithmetic middle : %f", midd); } Example: What will be printed after execution of a program that calls on a function? void twotimes(int x) { printf ("\nF:Argument before change took place %d",x); x *= 2; printf ("\nF:Argument after tempering with it %d",x); } int main(void) { int number=10; printf ("\nM:Number before function-call %d",number); twotimes(number); printf("\nM:Number after function-call is %d",number); } Result on-screen: M:Number before function-call 10 F:Argument before change took place 10 F:Argument after tempering with it 20 M:Number after function-call 10 Change inside a function wasn’t saved after execution and return to main program! Why? int twotimes(int x){ printf ("\nF:Argument before change took place %d",x); x *= 2; printf ("\nF:Argument after tempering with it %d",x); return x; } int main(void) { int...<br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com6tag:blogger.com,1999:blog-22017830.post-1142020135059349272006-03-10T20:48:00.000+01:002006-11-01T04:50:15.976+01:00Lesson 15: Matrixes and 2D ArraysThere was a slight pause in my C++ Maniac programming tutorial, due to me answering some of your C & C++ lesson-related questions. You can find some explanations on previous programming materials in my sidebar now, along with other new C & C++ stuff. It seems to me finally, after all troubleshoots are answered (or are they?!), we can bravely continue. This 15th lesson is about matrixes and two-dimensional C++ arrays. Although this one may look significantly more complicated, if you read through it carefully, you will conquer programming matrixes with no problem – I promise you that. Declaration of a 2D array in C language: int x - matrix 3X2 (3 rows, 2 columns), elements: integers char myascii - array of characters (2 rows & 4 columns) float sequence[MAXROW][MAXCOL] - MAXROW X MAXCOL matrix Example of declaration with initialization: int array = {1, 2, 3, 4, 5}; array = 1 array = 2 array = 3 array = 4 array = 5 array = 0 // even though nowhere stated array = 0 array = 0 array = 0 char cmaniac = {'C', 'M', 'A', 'N', 'I', 'A', 'C'} /* array of characters */ int y = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; y= 1 y= 2 y= 3 y= 0 y= 4 y= 5 y= 6 y= 0 y= 7 y= 8 y= 9 y= 0 Declaration of multidimensional array: int x 3D array of integer numbers float x 4D array of real numbers Example: Write your own C program that reads through real matrix, 10x10 dimensioned and finds the smallest element in main diagonal and smallest element in secondary diagonal. #include &lt;stdio.h&gt; #define NR_ROW 10 #define NR_COL 10 int main(void) { int i, j; float mat[NR_ROW][NR_COL], min_maindg, min_secdg; printf("Input matrix elements :"); for (i = 0; i &lt; NR_ROW; i++) { for (j =...<br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com5tag:blogger.com,1999:blog-22017830.post-1141783704967248582006-03-08T03:05:00.000+01:002006-03-11T14:10:40.500+01:00C++ IntermezzoBefore I continue lecturing and post my 15th Lesson, I’ll give you a slight C++ break. This will be in order to provide you with some additional explanations to previous chapters. This decision is based on comments and questions you've submitted to some of my C and C++ topics (yes I read your posts :) You can see there are some fresh lectures regarding C Numerical Systems and Numeric System Conversions, while further explanations and examples regarding Numeric Data Storage and #include library references will also be provided soon. Remember, some of these chapters aren't necessary for you to obtain C & C++ knowledge, but are great material to understand computer’s logic and “behind curtains” view, of how things work inside the machine. This topic is also a great chance for you to post more questions and proposals for my future in-depth C++ articles. Sorry if the site is gonna look a little messy these days… It’s because I’m adding tons of new C++ stuff ;) Comments section is temporarly moderated, but feel free to post your questions, It just takes some time for them to show up (after I approve them by hand). Technorati Tags: Numeric, Numerical, Redesign, Transformation, Feedback, Questions, Intermezzo, Conversion, Numbers, News, Convert, Method <br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com0tag:blogger.com,1999:blog-22017830.post-1141782766440875402006-03-08T02:48:00.000+01:002007-01-15T14:17:20.666+01:00Hex 2 Binary 2 OctalGeneral Method for Transforming Numbers - Number we want to transform uses base X: we would like to transform this number into new one, that uses Y base. In this case we will use method of continuous dividing and multiplying (dividing numbers left of comma (,) with Y and multiplying numbers on the right side of comma, by Y - rational numbers). This method is best understood looking at these examples: Octal System: Base: 8 Digits: 0, 1, 2, 3, 4, 5, 6, 7 Example: 27 (decimal) = 33 8 (octal) Hexadecimal (hex) System: Base: 16 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Example: 27 (decimal) = 1B 16 (hex) Binary System: Base: 2 Digits: 0, 1 Example: 27 (decimal) = 11011 2 (binary) Transforming Binary, Hex and Octal - Quickly Binary Hex Binary Octal 0000 0 000 0 0001 1 001 1 0010 2 010 2 0011 3 011 3 0100 4 100 4 0101 5 101 5 0110 6 110 6 0111 7 111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F Example Transform -6F,A 16 into binary number -6F,A 16 = - 0110 1111 , 1010 2 = - 1101111 , 101 2 Example Transform 11,000011001 2 into hex number 11,000011001 2 = 11 , 0000 1100 1 2 = 0011 , 0000 1100 1000 2 ...<br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com6tag:blogger.com,1999:blog-22017830.post-1141782492178666512006-03-08T02:44:00.000+01:002006-03-08T02:56:33.206+01:00Hexadecimal 2 DecimalNumber we want to transform uses base X: we would like to transform this number into new one, that uses Y base. In this case we will use method of continuous dividing and multiplying (dividing numbers left of comma (,) with Y and multiplying numbers on the right side of comma, by Y - rational numbers). This method is best understood looking at these examples, where X = 16 (source is hex base, B1=16) and Y = 10 (destination is decimal base, B2=10). Hexadecimal (hex) System: Base: 16 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Example: 27 (decimal) = 1B 16 (hex) Decimal System: Base: 10 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Hex 2 Decimal Example Transform 9EC,570A3 (hex) into decimal number 9EC, 570A3 16 = 9*162 + 14*161 + 12*160 + 5*16-1 + 7*16-2 + 0*16-3 + 10*16-4 + 3*16-5 = 2304 + 224 +12 + 0,3125 + 0,02734375 + 0,0001525878... + 0,00000286102... = 2540 , 33999919891357421875 Technorati Tags: Numeric, Numerical, Transform, Transformation, Binary, Decimal, Octal, Hexadecimal, Hex, Base, Complement, Method <br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com0tag:blogger.com,1999:blog-22017830.post-1141782228323521092006-03-08T02:39:00.000+01:002006-11-04T14:19:15.773+01:00Binary 2 DecimalNumber we want to transform uses base X: we would like to transform this number into new one, that uses Y base. In this case we will use method of continuous dividing and multiplying (dividing numbers left of comma (,) with Y and multiplying numbers on the right side of comma, by Y - rational numbers). This method is best understood looking at these examples, where X = 2, and Y = 10 (binary and decimal base, B1=2, B2=10): Binary System: Base: 2 Digits: 0, 1 Example: 27 (decimal) = 11011 2 (binary) Decimal System: Base: 10 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Binary 2 Decimal Example Transform 110101 (binary) into decimal number 110101 2 = 1*25 + 1*24 + 0*23 + 1*22 + 0*21 + 1*20 = 1*25 + 1*24 + 1*22 + 1*20 = 1*32 + 1*16 + 1*4 + 1*1 = 53 10 Example Transform -11,101 (binary) into decimal number -11,101 2 = - (1*21 + 1*20 + 1*2-1 + 0*2-2 + 1*2-3) = - (1*21 + 1*20 + 1*2-1 + 1*2-3) = - (1*2 + 1*1 + 1*0,5 + 1*0,125) = - 3 , 625 Technorati Tags: Numeric, Numerical, Transform, Transformation, Binary, Decimal, Octal, Hexadecimal, Hex, Base, Complement, Method <br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com1tag:blogger.com,1999:blog-22017830.post-1141781881218336952006-03-08T02:33:00.000+01:002006-07-26T11:06:55.586+02:00Decimal 2 HexadecimalNumber we want to transform uses base X: we would like to transform this number into new one, that uses Y base. In this case we will use method of continuous dividing and multiplying (dividing numbers left of comma (,) with Y and multiplying numbers on the right side of comma, by Y - rational numbers). This method is best understood looking at these examples, where X equals 10 and Y equals 16 (hex base, B=16) Decimal System: Base: 10 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Hexadecimal (hex) System: Base: 16 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Example: 27 (decimal) = 1B 16 (hex) Decimal 2 Hex Example Transform 2540,34 (decimal) into hex number-result ~ 9EC, 570A316 a) left from”,” 2540 : 16 = 158 , remaining 12 => C 160 last digit 158 : 16 = 9 , remaining 14 => E 161 9 : 16 = 0 , remaining 9 => 9 162 first digit end of procedure => 9EC 16 b) right from “,” 0,34 * 16 = 5,44 5 => 5 16-1 first digit after zero 0,44 * 16 = 7,04 7 => 7 16-2 0,04 * 16 = 0,64 0 => 0 16-3 0,64 * 16 = 10,24 10 => A 16-4 0,24 * 16 = 3,84 3 => 3 16-5 … … procedure could be continued => 0, 570A3… 16 Final result ~ 9EC , 570A316 Technorati Tags: Numeric, Numerical, Transform, Transformation, Binary, Decimal, Octal, Hexadecimal, Hex, Base, Complement, Method <br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com1tag:blogger.com,1999:blog-22017830.post-1141780931064115912006-03-08T02:21:00.000+01:002006-10-07T11:33:05.483+02:00Decimal 2 BinaryNumber we want to transform uses base X: we would like to transform this number into new one, that uses Y base. In this case we will use method of continuous dividing and multiplying (dividing numbers left of comma (,) with Y and multiplying numbers on the right side of comma, by Y - rational numbers). This method is best understood looking at these examples where X equals 10 (decimal base, B=10), and Y eqauls 2 (binary base, B=2) Decimal System: Base: 10 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Binary System: Base: 2 Digits: 0, 1 Example: 27 (decimal) = 11011 2 (binary) Decimal 2 Binary Example Transform 29 (decimal) into binary number - reading upwards, result is 111012 29 : 2 = 14 , remaining 1 20 last (smallest) digit 14 : 2 = 7 , remaining 0 21 7 : 2 = 3 , remaining 1 22 3 : 2 = 1 , remaining 1 23 1 : 2 = 0 , remaining 1 24 first digit - end of procedure Example Transform 0,8125 (decimal) into binary number – reading downwards, result is 0,11012 0,8125 * 2 = 1, 625 2-1 first digit after zero 0,625 * 2 = 1, 25 2-2 0,25 * 2 = 0, 5 2-3 0,5 * 2 = 1, 0 2-4 last digit 0,0 end of procedure Example Transform 0,3 (decimal) into binary number – result is 0,01001 1001 1001… 2 shortly rounded = 0,010012 0,3 * 2 = 0, 6 2-1 first digit after zero 0,6 * 2 = 1, 2 2-2 -------- 0,2 * 2 = 0, 4 2-3 0,4 * 2 = 0, 8 2-4 0,8 * 2 = 1, 6 2-5 0,6 * 2 = 1, 2 2-6 -------- … … procedure never ends Example – Quick Method Transform 53(decimal) into binary number using quick method 53 25 + 24 + 22 + 20 = - 32 -&gt; 25 1*25 + 1*24 + 0*23 + 1*22 + 0*21 + 1*20 = -------- ...<br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com1tag:blogger.com,1999:blog-22017830.post-1141780625880099442006-03-08T02:16:00.000+01:002006-03-08T05:16:31.813+01:00Decimal 2 DecimalNumber we want to transform uses base X: we would like to transform this number into new one, that uses Y base. In this case we will use method of continuous dividing and multiplying (dividing numbers left of comma (,) with Y and multiplying numbers on the right side of comma, by Y - rational numbers). This method is best understood looking at these exampleswhere both X & Y equal to 10 (decimal base, B=10) Decimal System: Base: 10 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Example Transform 5324 (decimal) into decimal number (?!) using method of continuous dividing – result 5324 5324 : 10 = 532 , remaining 4 100 last digit 532 : 10 = 53 , remaining 2 101 53 : 10 = 5 , remaining 3 102 5 : 10 = 0 , remaining 5 103 first digit - end of procedure Example Transform 0,8125 (decimal) into decimal number (?!) using method of continuous multiplying – result 0,8125 0,8125 * 10 = 8, 125 10-1 first digit after zero 0,125 * 10 = 1, 25 10-2 0,25 * 10 = 2, 5 10-3 0,5 * 10 = 5, 0 10-4 last digit 0,0 end of procedure Technorati Tags: Numeric, Numerical, Transform, Transformation, Binary, Decimal, Octal, Hexadecimal, Hex, Base, Complement, Method <br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com0tag:blogger.com,1999:blog-22017830.post-1141780123879196472006-03-08T02:08:00.000+01:002006-03-08T02:08:43.983+01:00Numerical SystemsOur society uses numerical system with base 10. Simple explanation why this system is used - simply because people have ten fingers, thus this is the easiest way for us to calculate numbers. If things were gone different, and something had messed up primordial soup -&gt; developing Homo sapiens with eight fingers, it would be more likely we would use octal numeric system in present time. Infinite number of numerical systems exist, but following are most commonly used... Decimal System: Base: 10 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Octal System: Base: 8 Digits: 0, 1, 2, 3, 4, 5, 6, 7 Example: 27 (decimal) = 33 8 (octal) Hexadecimal (hex) System: Base: 16 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Example: 27 (decimal) = 1B 16 (hex) Binary System: Base: 2 Digits: 0, 1 Example: 27 (decimal) = 11011 2 (binary) To learn more about numerical systems and their transformation (decimal2binary, dec2hex, dec2octal, hex2octal,...) continue to next lecture. Technorati Tags: Numeric, Numerical, Transform, Transformation, Binary, Decimal, Octal, Hexadecimal, Hex, Base, Complement, Method <br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com0tag:blogger.com,1999:blog-22017830.post-1141779666349464372006-03-08T01:59:00.000+01:002006-12-28T22:54:04.873+01:00Numeric TransformationsGeneral Method of Transforming Numbers Number we want to transform uses base X: we would like to transform this number into new one, that uses Y base. In this case we will use method of continuous dividing and multiplying (dividing numbers left of comma (,) with Y and multiplying numbers on the right side of comma, by Y - rational numbers). This method is best understood looking at these examples: Example Transform 5324 (decimal) into decimal number (?!) using method of continuous dividing – result 5324 5324 : 10 = 532 , remaining 4 100 last digit 532 : 10 = 53 , remaining 2 101 53 : 10 = 5 , remaining 3 102 5 : 10 = 0 , remaining 5 103 first digit - end of procedure Example Transform 0,8125 (decimal) into decimal number (?!) using method of continuous multiplying – result 0,8125 0,8125 * 10 = 8, 125 10-1 first digit after zero 0,125 * 10 = 1, 25 10-2 0,25 * 10 = 2, 5 10-3 0,5 * 10 = 5, 0 10-4 last digit 0,0 end of procedure Decimal 2 Binary Example Transform 29 (decimal) into binary number - reading upwards, result is 111012 29 : 2 = 14 , remaining 1 20 last (smallest) digit 14 : 2 = 7 , remaining 0 21 7 : 2 = 3 , remaining 1 22 3 : 2 = 1 , remaining 1 23 1 : 2 = 0 , remaining 1 24 first digit - end of procedure Example Transform 0,8125 (decimal) into binary number – reading downwards, result is 0,11012 0,8125 * 2 = 1, 625 2-1 first digit after zero 0,625 * 2 = 1, 25 2-2 0,25 * 2 = 0, 5 2-3 0,5 * 2 = 1, 0 2-4 last digit 0,0 end of procedure Example Transform 0,3 (decimal) into binary number – result is 0,01001 1001 1001… 2 shortly rounded = 0,010012 0,3 * 2 = 0, 6 2-1 first digit after zero 0,6 * 2...<br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com0tag:blogger.com,1999:blog-22017830.post-1141774451663483042006-03-08T00:33:00.000+01:002006-12-28T22:58:33.050+01:00Numerical Systems and TransformationsOur society uses numerical system based on number 10. There is a nutorious explanation why this system is used - it's simple, people have ten fingers, thus makes them easier to calculate numbers. If things were gone different, and something had messed up primordial soup -> developing Homo sapiens with eight fingers, it would be more likely we would use octal numeric system at present time. Infinite number of numerical systems exist, but following are most commonly used, and their conversion from one to another, is simple. Decimal System: Base: 10 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Octal System: Base: 8 Digits: 0, 1, 2, 3, 4, 5, 6, 7 Example: 27 (decimal) = 33 8 (octal) Hexadecimal (hex) System: Base: 16 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Example: 27 (decimal) = 1B 16 (hex) Binary System: Base: 2 Digits: 0, 1 Example: 27 (decimal) = 11011 2 (binary) General Method of Transforming Numbers Number we want to transform uses base X: we would like to transform this number into new one, that uses Y base. In this case we will use method of continuous dividing and multiplying (dividing numbers left of comma (,) with Y and multiplying numbers on the right side of comma, by Y - rational numbers). This method is best understood looking at these examples: Example Transform 5324 (decimal) into decimal number (?!) using method of continuous dividing – result 5324 5324 : 10 = 532 , remaining 4 100 last digit 532 : 10 = 53 , remaining 2 101 53 : 10 = 5 , remaining 3 102 5 : 10 = 0 , remaining 5 103 first digit - end of procedure Example Transform 0,8125 (decimal) into decimal number (?!) using method of continuous multiplying – result 0,8125 0,8125 * 10 = 8, 125 10-1 first digit after zero 0,125 * 10 = 1, 25 10-2 0,25 * 10 = 2, 5 10-3 0,5 * 10 = 5, 0 10-4 last digit 0,0 end of...<br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com2tag:blogger.com,1999:blog-22017830.post-1141536097037274442006-03-05T06:20:00.000+01:002006-11-01T05:05:33.993+01:00Lesson 14: ArraysIn C language arrays are very popular. They can be found in almost any program code and are pretty helpful and easy to understand. To understand them, just visualize a sequence of numbers and try assigning them to only one declaration. It can be done if the declaration is properly stated as an array including index brackets (example int MySequence). Arrays Array is data structure used to share multiple data elements under a single name declaration. It’s important that every single element of data, we wish to assign to array, belongs to the same data type. Array’s elements are easily accessed – we use index; a number that must be nonnegative integer (constant, variable, integer expression). Element’s index is a number between 0 and the number of elements minus one, including. In short: Index ( [0, NrOfElements – 1]. Declaration of an array: data_type array[index]; Array’s definition in C: int x – array consisted of 20 integer numbers char symbols – array consisted of 2 characters float sequence[MAX] – MAX is constant Assigning element’s values definition: int array[ ] = {1, 2, 3}; array = 1 array = 2 array = 3 int array  = {1, 2}; array = 1 array = 2 array = 0 array = 0 Accessing array’s elements: x – first element of an array sequence[i] – i. element of an array, 0 &lt;= i &lt;= NrOfElements – 1 sequence[MAX – 1] – last element of an array Common Wrong access to array’s elements: int array = {0}; int x = sequence; float a = 1.; int x = array[a]; int a = 1, b = 0, c = 1; int x = array[(a &&amp; b) - c]; Example: Write your own program that asks user to input sequence of numbers, afterwards it calculates arithmetic middle of the given sequence. Program also prints numbers smaller than arithmetic middle, and afterwards prints numbers bigger than arithmetic middle. #include &lt;stdio.h&gt; #define DIMENSION 10 int main(void) { int i; float sum = 0., arit_midd = 0.,...<br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com1tag:blogger.com,1999:blog-22017830.post-1141534919680929832006-03-05T06:01:00.000+01:002007-01-18T00:10:39.393+01:00AboutPurpose: "This website will provide you with lessons and quality material to learn basics of C language programming in just a few days. All you have to do is visit it here and then and read trough my lessons. You will notice I'm putting new lessons every day or two. It would be practical if you had Visual Studio installed on your computer and used it parallel to this lessons, but again it isn't neccessary... just follow and read theese C/C++ programming lessons and examples, and you'll be on your way! Let's not waste time, here we go..."<br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com3tag:blogger.com,1999:blog-22017830.post-1141530935135034822006-03-05T04:55:00.000+01:002006-10-27T19:50:23.946+02:00alloc.h#include &lt;alloc.h&gt;, syntax at the beginning of our code, means we automatically included these (pre-defined) functions in our program: void *malloc (size_t size); NULL error void free (void *block); void *realloc(void *block, size_t size); NULL error Technorati Tags: alloc.h, Void, Malloc, Realloc, Memory, Cache, RAM <br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com0tag:blogger.com,1999:blog-22017830.post-1141530860850592272006-03-05T04:54:00.000+01:002006-08-13T20:40:00.546+02:00ctype.h#include &lt;ctype.h&gt;, syntax at the beginning of our code, means we automatically included these (pre-defined) functions in our program: int toupper(int ch); int tolower(int ch); int isdigit(int c); figure (0-9) int isalpha(int c); letter (A-Z or a-z) int isalnum(int c); letter (A-Z or a-z) or figure (0-9) int isprint(int c); character which can be printed (0x20-0x7E) int iscntrl(int c); control char (0x7F or 0x00-0x1F) int isspace(int c); empty space int islower(int c); letter (a-z) int isupper(int c); letter (A-Z) Technorati Tags: ctype.h, Figure, #include, toupper, tolower, isalpha, iscntrl <br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com2tag:blogger.com,1999:blog-22017830.post-1141530830072203122006-03-05T04:53:00.001+01:002006-12-03T22:40:22.346+01:00string.h#include &lt;string.h&gt;, syntax at the beginning of our code, means we automatically included these (pre-defined) functions in our program: char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t maxlen); char *strcat(char *dest, const char *src); size_t strlen(const char *s); char *strlwr(char *s); char *strupr(char *s); int strcmp(const char *s1, const char *s2); int strcmpi(const char *s1, const char *s2); int stricmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t maxlen); int strncmpi(const char *s1, const char *s2, size_t maxlen); int strnicmp(const char *s1, const char *s2, size_t maxlen); char *strchr(const char *s, int c); char *strstr(const char *string, const char *substring); Technorati Tags: math.h, Library, #include, strlen, strcmp, strcat, strchr <br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com2tag:blogger.com,1999:blog-22017830.post-1141530799150772242006-03-05T04:53:00.000+01:002006-03-09T20:38:48.100+01:00stdlib.h#include &lt;stdlib.h&gt;, syntax at the beginning of our code, means we automatically included these (pre-defined) functions in our program: void exit (int status); void randomize (void); or void srand (unsigned int seed); int rand (void); Technorati Tags: stdlib.h, Rand, Randomise, Exit, Number, Function, C <br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com0tag:blogger.com,1999:blog-22017830.post-1141530755304511172006-03-05T04:52:00.000+01:002006-03-09T23:44:07.516+01:00math.h#include &lt;math.h&gt;, syntax at the beginning of our code, means we automatically included these (pre-defined) functions in our program: int abs (int x); IxI long labs (long x); double fabs (double x); double sin (double x); double cos (double x); double tan (double x); double asin (double x); double acos (double x); double atan (double x); double sinh (double x); double cosh (double x); double tanh (double x); double exp (double x); ex double log (double x); ln x double log10 (double x); log x double pow (double x,double y); xy double sqrt(double x); sqare root of x double fmod(double x, double y); x mod y double ceil (double x); double floor(double x); Technorati Tags: math.h, Library, #include, abs, long, double, mod <br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com1tag:blogger.com,1999:blog-22017830.post-1141450018167358152006-03-04T06:26:00.000+01:002006-10-14T21:48:39.690+02:00Lesson 13: Hello World! - A Classic -This is a true classic. I covered "Hello World!" example in this lesson, which is the one everybody encounters when learning to program – It's just sooner or later. Well, in my lessons –later-. Most of other tutorials start with this example, but I think I'm on the right path of introducing you to it only now. Not much of a new stuff covered here. More like repeating and affirmation. Loops Example: What is the printed result of following program block? i = 1; while (i &lt; 5) { if (i==3) { printf (“\n Hello world %d.x!”, i); continue; } else if (i==4) { printf (“\n Goodbye %d.x!”, i); continue; } i++; } Result: Hello world 3.x! Hello world 3.x! ... ... … and so on for infinite number of times. Even after value 3 is reached, orders under i==3 are executed. Because of “continue” command, i doesn’t increase, but the program branches on conditional phrase (i &lt; 5). Example: Write your own program block that prints multiplying table of numbers up to 100. /*1*/ int main(void) { /*2*/ int i,j; /*3*/ /*4*/ for (i = 1; i &lt;= 10; ++i) { /*5*/ for (j = 1; j &lt;= 10; ++j) /*6*/ printf("%3d",i*j); /*7*/ printf("\n"); /*8*/ } /*9*/} By adding single line of code, we accomplish that new table is made from even numbers only: /*6*/ if ( (i%2!=0) &&amp;amp; (j%2!=0) ) continue; // both odd numbers Same thing: /*6*/ if ( (i%2==0) || (j%2==0) ) // at least one number is even Example: Write your own program block that prints first N Fibonacci numbers. N is given by keyboard. Algorithm to calculate Fibonacci Numbers: Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2) Fibonacci(0) = Fibonacci(1) = 1 1, 1, 2, 3, 5, 8, 13, 21,... #include &lt;stdio.h&gt; int main () { int N, i, f0 = 1, f1 = 1,f = 1; printf ("\n Input amount of Fibonacci Numbers (N) : \n"); scanf ("%d",&N); for (i = 0; i &lt;= N; i++) { if (i &gt; 1) { f = f1 + f0; f0...<br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com1tag:blogger.com,1999:blog-22017830.post-1141266084770873512006-03-02T03:21:00.000+01:002006-11-05T00:44:30.910+01:00Lesson 12: Switch-Case, Break; and Continue;New lesson is up. I’ve composed this one, based on common examples where controls: break; and continue; are used. Another explanation on branching - using “switch – case” order is provided. Enjoy it! Hope you’ve noticed by now I also added some appendixes (more to be added soon); and for some time now, I post regular News regarding C++ Maniac homepage development. You can find these links in my sidebar. Loop flow controls: break; and continue; C uses two different orders to control loop’s flow break – escapes from the nearest outer loop continue – inside “while” and “do” loop: switches program execution to test condition, inside “for” loop: switches program execution to “for” loop step and then to condition test (also applies for nearest outer loop) - this can sound little messy, so better check the examples Example: Write your own program that tests if the given number is prime number. #include &lt;stdio.h&gt; #include &lt;math.h&gt; void main() { int i, n, prime=1; // prime is true printf("Input natural number :"); scanf("%d", &n); for( i=2; i&lt;= n-1; i++) { if( n % i == 0 ) { // also possible to state if(!(n % i)) prime =0; // prime is now false break; } } if( prime ) printf("%d is prime number !\n", n); else printf("%d isn’t prime number!\n", n); } Possible algorithm enhancements (decreasing number of loop’s iterations/repeats): It is enough to loop n/2 times, better, only till square root(n) (C function sqrt(n)) Test if the number is dividable with 2, and if it isn’t, test inside loop if the number is dividable with odd numbers bigger than 2 Example: Write your own program that reads integers given by keyboard and applies following rule above them: if the given number is smaller than zero, program should print error message and stop...<br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com8tag:blogger.com,1999:blog-22017830.post-1140923541192075532006-02-26T04:12:00.000+01:002006-11-05T08:44:12.563+01:00Lesson 11: Infinite and Finite LoopsSeems to me that submitting new lessons every two days became a rutine lately. Let’s hope it stays that way. Today I posted new lesson which will put some light on programming loops - you surely have heard about those. So from now on, when you watch Futurama and hear Beneder blabring something about being stuck in an infinite loop, you’ll understand his problem in details: from one – to zero. Hehe, let’s start... Iteration with previously known number of repeats. Syntax: for (expression1; expression2; expression3) { . . . } expression1 is an expression which will be executed only once, before entering into the first iteration. It’s most commonly used for counters initialization. If more then one expression is needed in this field, they are separated by comma. expression2 is calculated as logical condition (0 - false, !=0 - true), and the iteration is executed number of times expression2 is true. Testing of condition is done before any iteration is processed. expression3 is executed after each iteration (executed loop). It’s most common use is to increase counter’s value. If more then one expression is needed to be executed, they are separated by comma. either one of expressions (expression1; expression2; expression3) can be left out. If expression 2 is left out, loop is executed as if logical value of the expression were TRUE. Common use: for (i = start; i &lt;= end; i = i + k) { . . . } Example of “for” loop with 2 counters: for (hi = 100, lo = 0; hi &gt;= lo; hi--, lo++) { . . . } Example: Write your own program that will calculate arithmetic middle of n given numbers. #include &lt;stdio.h&gt; void main() { int i, n, sum, x; float arit_midd; printf("For how many numbers do you wish to calc. their arithmetic middle : "); scanf("%d", &n ); sum = 0; for(i=0; i&lt;n; i++) { printf("Give %d. number : ", i); scanf("%d",...<br/> <br/> Visit the link for full lessonVurdlakhttp://www.blogger.com/profile/08849999795183163370noreply@blogger.com1