Looking for a Tutor Near You?

Post Learning Requirement »
x

Choose Country Code

x

Direction

x

Ask a Question

x

x
x
x
Hire a Tutor

C Programming

Loading...

Published in: C / C++ | Data Structures
3,359 Views

C Programming

Akhilesh K / Lucknow

4 years of teaching experience

Qualification: M.Sc (NIT Rourkela - 2019)

Teaches: All Subjects, English, Mathematics, Science, Chemistry, Physics, Algebra, IIT JEE Mains, AIPMT, NEET

Contact this Tutor
  1. C Programming lecture 2 Beautiful programs Greek algorithms ' Arrays of numbers Strings of characters ' Pointers (the really difficult bit of C)
  2. Beauty is truth and truth beauty A program which is neatly formatted is easier to understand ' If you're staring in confusion at a load of {s and } s and don't know whether you need another } your program is probably UGLY ' Indenting consistently is especially important. (in functions, for, while if and else). One of our most important goals as a programmer is to write something that other people can read.
  3. Layout of a program (brace style) int main() Right bracket level with statement which started it NOTE: Not all code on these overheads follows this style for space reasons float if (x y for z; 3; if 5; 7 1; i for ( < 200; 1; Always indent after a left bracket Start a left bracket after a statement 200; both loops * / the first loop * / return / * Inside Inside only O;
  4. MAGIC numbers in programs A lot of programs contain what programmers sometimes call MAGIC nos for (i = 7; i < 103; i +=2) print f ("Ood\n" , i* 7) This makes code look UGLY and it is confusing to the reader. It is better to give some idea of what these numbers mean in the program.
  5. enum We can use the enum command to define int s and chars. It looks like this: enum { MAX LEN= LETTERX- NEWLINE= 100, x By convention we use all capitals for enum constants. That way we can tell them from variables. We can now use the enum constant wherever we could use an int or char char a [MAX LEN] ; for O; i < MAX LEN; i++) a [i] = LETTERX,•
  6. #define This preprocessor command replaces one thing with another before compilation NOTE - NO semi #define PI 3.14 colon here ! #define GRAV CONST 9.807 #define HELLO WORLD "Hello World'. Now, anywhere in the program we use PI or GRAV CONST it will be replaced with the replacement string BEFORE the real compiler starts (that's why we call it pre-processing) 2.0 * PI * r; (ml*m2)/ (r r); GRAV CONST * print f (HELLO WORLD) ;
  7. This lecture's program The sieve of Eratosthenes is an algorithm of greek origin. ' It is a relatively efficient way of constructing a list of small prime numbers ' It is defined in your notes in the section ' going from algorithm to program' It is also going to be used in the next worksheet.
  8. 11 16 21 The sieve 12 17 22 23 10 20 25 After appropriate crossing out we have prime numbers But how can we represent the sieve in C? The goal of this is to find another way to write our is _ prime function.
  9. Choosing a data representation When going from an algorithm to a program, one of the first questions is "How can I represent my data?' If the program has to store anything then what is the best way to store it? In the case of the sieve of Eratosthenes, we need to store a table of numbers. In this case, a good storage mechanism is an array — a list of numbers.
  10. The ARRAY ' An array in C is a group of similar variables. For example 200 ints or 45 chars Arrays use square brackets like so: int some nums [200] char bunch o chars [45] some numbers [3] 5; print f ("Element 3 is Ood\n" , some nums [3] ) bunch o chars [0] Unfortunately, in C, we must give the length when we declare the array (see lecture notes). ARRAYS CAN BE MODIFIED BY FUNCTIONS! For the reason see later in the lecture
  11. Great C muck-ups: #5 in a series of 100 ' If we declare an array in C to be 100 elements the array is numbered from 0 to 99. EVERYBODY at some point makes this mistake: int a [100]; 3; a [1001= 5; / *Declare 100 int s * / * This is a common error There is NO e 100 but your program will compile and run* /
  12. Passing arrays to functions We prototype a function which accepts an array like this: void process array (int C] ) int calc array (char[]); And write the function like this: void process array (int all nums [ ] ) all nums [1] 3; And call the function like this: int some numbers [100] process array (some numbers) ' Note that we CAN'T return an array from a function (but see later).
  13. So what does this mean for our sieve? We could use an enum to define how many primes we want our array to be. We can use an array to store whether or not it has been crossed (it only needs to be a ID array - think about this) We can have an enum to say whether it is crossed or not crossed (instead of storing the number) Write something to uncross all the array
  14. Look for some obvious sub-tasks which might be functions What bits of our algorithm are obvious candidates for becoming functions? What data do we need to pass to and from each function? Write the bits that call the function and the prototype Then write the function
  15. Look for some obvious LOOPS Two of the most used workhorses in C are the for and while loops. Which is clearer in this case? A for or while loop? What indeed is the difference? for (initialiser condi t ion in cremen t) code; initial i ser; while ( condi t ion) { Generally we use for when the initialiser, condition and increment are all on the same variable. code; There is ONE difference — can you in cremen t ; work out what?
  16. Great C muck-ups: #51 in a series of 100 We must get the order right in a for statement. for (i < 7; i++; i = 0) print f ("i is ; And don't use commas instead of ; for (i = 0, i < 7, i++) wrong ops - wrong order And remember, if you miss out the { } s then only the one next statement will be executed. for (i = 0; i < 7; i++) print f ("i is Ood\n" i); print f ("i squared Ood\n" i Oops - only see this once i);
  17. Checking the sieve works ' Don't just TRUST your program - check it. In a big program it is good to check functions as they are written - not the whole program. Check as you write. The best way to check something is working is to get some print out from it. ' So add something to the sieve to print all the primes which have been found so far.
  18. Strings in C When we want to store and print text in C we use a string A string is an array of type char which has '\0' as the last character. '\0' is a special character (like '\n') which means "stop now' We can initialise a string using = but we can't set a string using this at other times (use sprintf) ! We can print a string with %s: "Hello World'. char hi there [l print f ("0 hi there) ;
  19. Useful functions with strings We can find a lot of useful functions in the library string . h # include < string.h> int main() char test [100]; "World! \ n" char test 2 strcpy (test, "Hello") ; strcat (test, test 2) if (strcmp (test, "dave") 0) print f ("Test is same as Dave \ n") ' print f ("Length of test is Ood\n" strlen (test));
  20. Great C muck-ups: #91 in a series of 100 Remember, our string must be big enough to HOLD what we put in it. char small string [l "Hey there" sprint f (small string, "Hello World! \ n") ; This will almost certainly cause problems. The first statement sets up a string which is only big enough to hold 10 characters [Why 10? Think about it.] The second statement tries to put 14 characters into it. Disaster!
  21. The scanf statement ' Scanf can be used like printf but to read instead of write. It is a confusing way to read data from the user (we will see why in later lectures) ' But look — number CHANGED — we said that didn't happen! int number, check; check= scan f ("Ood" , &number) ; if (check ! 1) print f ("Error! \ n") ; return —1;
  22. Pass by reference/Pass by value ' Normally when we send a variable to a function we make a COPY of the variable. This is called pass by value. A value is passed and this copy of the variable arrives at the function. ' Sometimes, like in scanf we want to change the variable inside the function. In this case we need to do something different: pass by reference. This is what the & character is for.
  23. What are pointers? ' Pointers are one of the most difficult topics to understand in C. ' Pointers "point at" areas of your computer's memory. int says p is a pointer to an int Imagine your computer's memory as a series of boxes which all hold ints 56 71 12 3 21 7 p points at one of the ints
  24. & means "address of" or "point at me" means value or "what am I * 5; int int q— print f pointing at?" p is a pointer to an int q is an int p now points at q ("p is Ood\n" * p); Therefore p has the same value of 5 We use *p to mean "the value p is pointing at" =5
  25. What's the point of pointers? When we use & to pass a pointer to a variable into a function we CAN change the value of the variable within the function. This is called pass by reference. This is what was going on when we use & in scan f. In the next lecture we will hear more about pointers and how to use them. We will also learn that arrays are nearly the same thing as pointers.
  26. How we can change an argument within a function void square num (int * ) int main() Prototype a function taking a POINTER int P— 5; Pass the address of p to our function square num (&p) • print f ( 'P is now • return 0; Now the function has changed p to 25 void square ( *num) num (int *num) ( *num) ( *num) , Remember * gives the value the pointer points at
  27. Pointers ! Pointers are confusing ' But they are also very useful With pointers we can effectively return more than one thing from a function ' Arrays are a form of pointer really We will (unfortunately) be hearing a lot more about them in subsequent lectures