Variables
Naming
1. Can a variable name start with a number?
2. Can a variable name start with a typographical symbol (e.g. #, *, _)?
3. Give an example of a C variable name that would not work. Why doesn't it work?
Solution[ Show ]
1. No, the name of a variable must begin with a letter (minuscule or majuscule), or a
underscore.
underscore.
2. Only the underscore can be used.
3. for example, p$t is not allowed because $ is not a valid character for the name of a variable.
Data Types
1. List at least three data types in C
1. On your computer, how much memory does each require?
2. Which ones can be used in place of another? Why?
1. Are there any limitations on these uses?
3. If so, what are they?
4. Is it necessary to do anything special to use the alternative?
5. Can the name we use for a data type (e.g. 'int', 'float') be used as a variable?
•
3 data types : char, int,double.
• On my computer :
• char : 1 byte
• int : 4 bytes
• double : 8 bytes
• we can not use 'int' or 'float' as a variable 's name.
Assignment
1. How would you assign the value 3.14 to a variable called pi?
2. Is it possible to assign an int to a double?
|
1. Is the reverse possible?
doublepi;pi=3.14;
• Yes, for example :
int a=67;
doubleb;b=a;
• Yes, but a cast is necessary and the double is truncated :
double a=89;
int b;
b=(int) a;
Referencing
1. A common mistake for new students is reversing the assignment statement. Suppose you
want to assign the value stored in the variable "pi" to another variable, say "pi2":
want to assign the value stored in the variable "pi" to another variable, say "pi2":
1. What is the correct statement?
2. What is the reverse? Is this a valid C statement (even if it gives incorrect results)?
3. What if you wanted to assign a constant value (like 3.1415) to "pi2":
1. What would the correct statement look like?
4. Would the reverse be a valid or invalid C statement?
Simple I/O
Input
1. scanf() is a very powerful function. Describe some features that make it so versatile.
2. Write the scanf() function call that will read into the variable "var":
1. a float
3. an int
4. a double
Output
1. Write a program that reverses a string that is input to the system.
2. Write a program that prints each word of a sentence on a new line
3. Write a program that outputs this stopping at n, so n = 6 would look like *
**
***
****
***********
***********
4. Write a program that outputs a sideways pyramid, so this if n = 4 *
**
***
*******
**
*
5. Write a program to do a right side up pyramid taking input n
Program Flow
1. Build a program where control passes from main to three different functions with 3 calls
2. Now make a while loop in main with the function calls inside it. Ask for input at the beginning of the loop. End the while loop if the user hits Q
3. Next add conditionals to call the functions when the user enters numbers, so 1 goes to function1,
2 goes to function 2, etc
4. Have function 1 call function a, which calls function b, which calls function c
5. Draw out a diagram of program flow, with arrows to indicate where control goes
Functions
1. Write a function to check if an integer is negative, the declaration should look like bool is_positive(int i);
2. Write a function to raise a floating point number to an integer power, so for example to when you use it
float a = raise_to_power(2,3);//a gets 8 float b = raise_to_power(9,2);//b gets 81
float raise_to_power(float f, int power);//make this your declaration
Math
1. Write a function to calculate if a number is prime.
2. Write a function to determine the number of prime numbers below n.
3. Write a function to find the square root by using Newton's method.
4. Write functions to do trigonometric functions.
5. Try to write a random number generator.
Indepth C ideas
Arrays & Strings
Arrays in C act to store related data under a single variable name with an index, also known as a
subscript. It is easiest to think of an array as simply a list or ordered grouping of variables. As such, arrays often help a programmer organize collections of data efficiently and intuitively.
Later we will consider the concept of a pointer, fundamental to C, which extends the nature of the array. For now, we will consider just their declaration and their use.
Arrays
If we want an array of six integers , called "numbers", we write in C
int numbers[6];
For a character array called letters,
char letters[6];
and so on.
If we wish to initialize as we declare, we write
int vector[6]={0,0,1,0,0,0};
If we want to access a variable stored in an array, for example with the above declaration, the following code will store a 1 in the variable x
int x;
x = vector[2];
Arrays in C are indexed starting at 0, as opposed to starting at 1. The first element of the array above is vector[0]. The index to the last value in the array is the array size minus one. In the example above the subscripts run from 0 through 5. C does not do bounds checking on array accesses. The compiler will not complain about the following:
char y;
int z = 9;
char vector[6] = { 1, 2, 3, 4, 5, 6 };
//examples of accessing outside the array. A compile error is not raised y = vector[15];
y=vector[4];y=vector[z];
During program execution, an out of bounds array access does not always cause a run time error. Your program may happily continue after retrieving a value from vector[1]. To alleviate indexing problems, the sizeof() expression is commonly used when coding loops that process arrays.
int ix;
short anArray[]= { 3, 6, 9, 12, 15 };
for (ix=0; ix< (sizeof(anArray)/sizeof(short)); ++ix) {
DoSomethingWith( anArray[ix] );
}
Notice in the above example, the size of the array was not explicitly specified. The compiler knows to size it at 5 because of the five values in the initializer list. Adding an additional value to the list
will cause it to be sized to six, and because of the sizeof expression in the for loop, the code automatically adjusts to this change. This technique is often used by experienced C programmers.
will cause it to be sized to six, and because of the sizeof expression in the for loop, the code automatically adjusts to this change. This technique is often used by experienced C programmers.
C also supports multi dimensional arrays. The simplest type is a two dimensional array. This creates
a rectangular array each row has the same number of columns. To get a char array with 3 rows and
a rectangular array each row has the same number of columns. To get a char array with 3 rows and
5 columns we write in C
char two_d[3][5];
To access/modify a value in this array we need two subscripts:
char ch;
ch = two_d[2][4];
or
two_d[0][0] = 'x';
Similarly, a multidimensional array can be initialized like this:
int two_d[2][3] = {{ 5, 2, 1 },
{ 6, 7, 8 }};
There are also weird notations possible:
int a[100];
int i = 0;
if(a[i]==i[a]){
printf("Hello World!\n");
}
a[i] and i[a] point to the same location. (This is explained later in the next Chapter.)
Strings
C has no string handling facilities built in; consequently, strings are defined as arrays of characters.
char string[30];
However, there is a useful library of string handling routines which you can use by including another header file.
#include <stdio.h>
#include <string.h> //new header file
int main(void) {
}
0 comments:
Post a Comment