CP2

Cards (85)

  • Array
    In C programming, a data structure that allows you to store a fixed-size sequence of elements of the same type
  • Array
    • Provides a way to group related data items under a single name
    • Elements in an array are accessed using an index, which represents their positions in the array
  • How to Declare an Array
    1. type arrayName[size]
    2. type specifies the data type of the elements in the array
    3. arrayName is the identifier for the array
    4. size represents the number of elements that the array can hold
  • Declaring an Array

    • int myArray[5]
  • How to Initialize an Array
    1. Assign values to the array elements using the assignment operator (=)
    2. Initialize the array at the time of declaration
  • Initializing an Array
    • int myArray[5] = {10, 20, 30, 40, 50}
    • int myArray[] = {10, 20, 30, 40, 50}
  • How to Access Array Elements
    1. Use the array name followed by the index in square brackets
    2. Use a loop to process all the elements of the array
  • Arrays in C have a fixed size determined at compile time. Once an array is declared, its size cannot be changed.
  • Arrays are fundamental part of C programming and widely used to store and manipulate collections of data efficiently.
  • Arrays provide a convenient way to work with multiple values of the same type in a contiguous block of memory.
  • How to Change Value of Array Elements

    Access the element using the index and assign a new value
  • How to Input and Output Array Elements
    Use loops and the scanf() and printf() functions
  • Accessing an element of an array beyond its bounds leads to undefined behavior.
  • Two-dimensional Array
    A matrix-like data structure that consists of rows and columns
  • How to Declare Two-Dimensional Arrays
    1. type arrayName[rowSize][columnSize]
    2. rowSize represents the number of rows
    3. columnSize represents the number of columns
  • Declaring a Two-Dimensional Array
    • int matrix[3][3]
  • How to Initialize Two-Dimensional Arrays
    1. Assign values to the array elements using the assignment operator (=)
    2. Initialize the array at the time of declaration
  • Initializing a Two-Dimensional Array
    • int matrix[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}
    • int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}}
  • How to Access and Manipulate Two-Dimensional Array Elements
    1. Use nested loops to iterate over the rows and columns
    2. Access the elements using the row and column indices
  • Exceeding the bounds of a two-dimensional array can lead to undefined behavior.
  • C programming does not provide built-in runtime checks to detect array access out of bounds.
  • It is the programmer's responsibility to ensure that array access remains within its valid range.
  • "Functions without arguments"
  • Building Programs from Existing Information
    • Programmers seldom start off with a blank slate (or empty screen) when they develop a program
    • Often some – or all – of the solution can be developed from information that already exists or from the solution to another problem
    • Carefully following the software development method generates important system documentation before you even begin to code a program
    • This system documentation, consisting of a description of a problem's data requirements (developed during the Analysis phase) and its solution algorithm (developed during the Design phase), summarizes your intentions and thought processes
  • Building Programs from Existing Information
    • You can use this documentation as a starting point in coding your program
    • For example, you can begin by editing the data requirements to conform the C syntax for constant macro definitions and variable declarations
    • This approach is especially helpful if the documentation was created with a word processor and is in a file that you can edit
    • To develop the executable statements in the main function, first use the initial algorithm and its refinements as program comments
    • The comments describe each algorithm step and provide program documentation that guides your C code
    • After the comments are in place in the main function, you can begin to write the C statements
    • Place the C code for an unrefined step directly under that step
    • For a step that is refined, either edit the refinement to change it from English to C or replace it with C code
  • Finding the Area and Circumference of a Circle
    1. Get the radius of a circle
    2. Compute and display the circle's area
    3. Compute and display the circle's circumference
  • Analysis
    Clearly, the problem input is the circle's radius. Two outputs are requested: the circle's area and circumference. These variables should be type double because the inputs and outputs may contain fractional parts. The geometric relationships of a circle's radius to its area and circumference are listed on the next slide, along with the data requirements.
  • Finding the Area and Circumference of a Circle
    1. Get the circle radius
    2. Calculate the area
    3. Calculate the circumference
    4. Display the area and the circumference
  • Finding the Area and Circumference of a Circle
    1. Assign PI * radius * radius to area
    2. Assign 2 * PI * radius to circum
  • The next slide shows the C program so far. The main function lists the initial algorithm and its refinements as comments. To write the final program, convert the refinements (step 2.1 and 3.1) to C and write C code for the unrefined steps (steps 1 and 4).
  • Predefined Functions and Code Reuse

    • A primary goal of software engineering is to write error-free code
    • Code reuse, reusing program fragments that have already been written and tested whenever possible, is one way to accomplish this goal
    • C promotes reuse by providing many predefined functions that can be used to perform mathematical computations
  • sqrt function
    C's standard math library defines a function named sqrt that performs the square root computation
  • Predefined Functions and Code Reuse
    1. The function call activates the code function by writing a function call
    2. After the function executes, the function result is substituted for the function call
  • Predefined Functions and Code Reuse
    The function can be thought of as a black box that receives one or more input values and automatically returns a single output value
  • Sample Exercise: Square Root Program
    1. Display the square root of two numbers provided as input data (first and second) and the square root of their sum
    2. To do so, it must call the C functions sqrt three times: for the first two calls, the function arguments are variables (first and second), the third call shows that a function argument can also be an expression (first + second)
    3. For all three calls, the result returned by function sqrt is assigned to a variable
    4. Because the definition of the standard sqrt function is found in the standard math library, the program begins with an additional #include directive (math.h)
  • If you look closely at the Square Root Program solution, you will see that each statement contains a call to a library function (printf, scanf, sqrt) – we have used C's predefined functions as building blocks to construct a new program.
  • Some Mathematical Library Functions
    • abs
    • fabs
    • sqrt
    • sin
    • cos
    • tan
  • Functions Without Arguments
    • One way that the programmers implement top-down design in their programs is by defining their own functions
    • Often, a programmer will write one function subprogram for each subproblem
    • In this section, we will show how to use and define your own functions, focusing on simple functions that have no arguments and return value
  • Functions Without Arguments
    1. As an example of top-down design with functions, you could use the main function to draw the stick figure of a person
    2. In the next slide, three algorithm steps are coded as calls to three function subprograms
    3. For example, the statement draw_circle(); calls a function (draw_circle) that implements the algorithm step Draw a circle
    4. We call function draw_circle just like we call function printf
    5. The empty parentheses after the function name indicate that draw_circle requires no arguments
  • Function Prototypes
    • Just like other identifiers in C, a function must be declared before it can be referenced
    • One way to declare a function is to insert a function prototype, before the main function
    • A function prototype tells the C compiler the data type of the function, the function name, and information about the arguments that the function expects
    • The data type of a function is determined by the type of value returned by the function
    • The functions declared in the previous slides are void functions (that is, their type is void) because they do not return a value