C Programming/Beginning exercises

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search
Previous: File IO Index Next: Arrays

Contents

[edit] Variables

[edit] 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?


[edit] 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?
      2. If so, what are they?
      3. Is it necessary to do anything special to use the alternative?
  2. Can the name we use for a data type (e.g. 'int', 'float') be used as a variable?


[edit] 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?


[edit] 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":
    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":
      a. What would the correct statement look like?
      b. Would the reverse be a valid or invalid C statement?


[edit] Simple I/O

[edit] 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
    2. an int
    3. a double


[edit] String manipulation

1. Write a program that prompts the user for a string, and prints its reverse.

2. Write a program that prompts the user for a sentence, and prints each word on its own line.


[edit] Loops

1. Write a function that outputs a right isosceles triangle of height and width n, so n = 6 would look like

*
**
***
****
*****
******

2. Write a function that outputs a sideways triangle of height 2n-1 and width n, so the output for n = 4 would be:

*
**
***
****
***
**
*


3. Write a function that outputs a right-side-up triangle of height n and width 2n-1; the output for n = 6 would be:

     *
    ***
   *****
  *******
 *********
***********


[edit] 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

[edit] 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

[edit] Math

1. Write a function to calculate if a number is prime. Return 1 if it is prime and 0 if it is not a 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 evaluate the trigonometric functions:

5. Try to write a random number generator.

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,n;
Previous: File IO Index Next: Arrays
In other languages