A Few Useful Notes on C for Assignment 8 1. Arrays - which you'll need for sorting: To declare an integer array: int A[100]; To declare a real array: double Y[100]; To declare a character array, which is a string: char S[100]; - Arrays are numbered from 0, so there are elements A[0], A[1], ... A[99]. - That is also how you access the elements: e.g. T = A[34]; - The only difference from Turing is the square brackets and the enforced numbering of the index from 0. 2. Formatted output: (the equivalent of put) Be sure to put: #include at the top of your program. Output is done with the printf function which has the rough syntax: printf("", var1, var2, ....); To print an integer: int Lvar; Lvar = 5; printf("This is an integer: %d\n", Lvar); - this will print: This is an integer: 5 - the %d in the format string tells the function to look for an integer variable following the , and put it where the %d was in the format string. - for floats an doubles, use %f - for strings use %s To print an integer and then a real and then a string: int Intvar = 4; double Fvar = 2.3; char Svar[100] = "String Example"; printf("The integer is %d, the real is %d and the string is %s\n", Intvar, Fvar, Svar); This will print: The integer is 4, the real is 2.3 and the string is String Example 3. Input: - also need to include stdio.h as above - use the scanf function: scanf("", pvar1, pvar2 ..); - as with printf the format string indicates the type of variables to read from the terminal. - the tricky part, which I'll explain later next week, is that the "pvar1,.." have to be *pointers* to the variables you want to input, not the variables themselves. - to get a pointer to a variable X, you just put an ampersand in front of it: e.g. &X - so to input an integer X using scanf: int X; scanf("%d",&X); - this is equivalent to "get X" in Turing. If you want to input a real, then use %f, and a string, then use %s. - to input an integer and then a real and then a string: int X; double Y; char S[100]; /* a string is an array of char */ scanf ("%d %f %s", &X, &Y, S); Note a special case here: if you use the name of an array without an index, it is actually a pointer to the first element of the array. Instead of S above, I could have used &(S[1]), which points to the same thing I hope this helps! Jonathan