├── .devcontainer ├── Dockerfile ├── devcontainer.json └── reinstall-cmake.sh ├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── main.yml ├── .gitignore ├── .vscode └── settings.json ├── CH00 └── 00_02 │ └── 00_02-hello1.c ├── CH01 ├── 01_01 │ └── 01_01-first1.c ├── 01_02 │ └── 01_02-challenge1.c ├── 01_03 │ └── 01_03-solution1.c ├── 01_04 │ ├── 01_04-hello1.asm │ └── 01_04-hello1.c ├── 01_05 │ ├── 01_05-sample1.c │ ├── 01_05-sample2.c │ └── 01_05-sample3.c ├── 01_06 │ ├── 01_06-cdev1.c │ └── 01_06-cdev2.c ├── 01_07 │ └── 01_07-challenge1.c ├── 01_08 │ └── 01_08-solution1.c ├── 01_09 │ ├── 01_09-define1.c │ ├── 01_09-define2.c │ ├── 01_09-define3.c │ └── 01_09-include1.c ├── 01_10 │ └── 01_10-math1.c ├── 01_11 │ ├── 01_11-comments1.c │ ├── 01_11-comments2.c │ ├── 01_11-comments3.c │ ├── 01_11-comments4.c │ └── 01_11-comments5.c ├── 01_12 │ └── 01_12-challenge1.c └── 01_13 │ └── 01_13-solution1.c ├── CH02 ├── 02_01 │ ├── 02_01-datatypes1.c │ └── 02_01-string1.c ├── 02_02 │ ├── 02_02-variables1.c │ ├── 02_02-variables2.c │ └── 02_02-variables3.c ├── 02_03 │ ├── 02_03-variable1.c │ ├── 02_03-variable2.c │ ├── 02_03-variable3.c │ └── 02_03-variable4.c ├── 02_04 │ ├── 02_04-printf1.c │ ├── 02_04-printf2.c │ ├── 02_04-printf3.c │ └── 02_04-printf4.c ├── 02_05 │ ├── 02_05-constant1.c │ ├── 02_05-constant2.c │ ├── 02_05-constant3.c │ ├── 02_05-constant4.c │ └── 02_05-constant5.c ├── 02_06 │ └── 02_06-challenge1.c ├── 02_07 │ ├── 02_07-solution1.c │ └── 02_07-solution2.c ├── 02_08 │ ├── 02_08-auto1.c │ ├── 02_08-extern1.c │ └── 02_08-static1.c ├── 02_09 │ ├── 02_09-time_t1.c │ └── 02_09-typedef1.c ├── 02_10 │ ├── 02_10-singlechar1.c │ ├── 02_10-singlechar2.c │ └── 02_10-string1.c ├── 02_11 │ ├── 02_11-inputtest1.c │ ├── 02_11-values1.c │ ├── 02_11-values2.c │ ├── 02_11-values3.c │ └── 02_11-values4.c ├── 02_12 │ ├── 02_12-typecast1.c │ └── 02_12-typecast2.c ├── 02_13 │ └── 02_13-challenge1.c └── 02_14 │ └── 02_14-solution1.c ├── CH03 ├── 03_01 │ ├── 03_01-math1.c │ ├── 03_01-math2.c │ ├── 03_01-math3.c │ └── 03_01-math4.c ├── 03_02 │ └── 03_02-chalenge1.c ├── 03_03 │ └── 03_03-solution1.c ├── 03_04 │ ├── 03_04-assignment1.c │ └── 03_04-assignment2.c ├── 03_05 │ └── 03_05-order1.c ├── 03_06 │ └── 03_06-challenge1.c ├── 03_07 │ └── 03_07-solution1.c ├── 03_08 │ ├── 03_08-relational1.c │ ├── 03_08-relational2.c │ └── 03_08-relational3.c ├── 03_09 │ ├── 03_09-logical1.c │ └── 03_09-logical2.c ├── 03_10 │ ├── 03_10-bitwise1.c │ ├── 03_10-bitwise2.c │ ├── 03_10-bitwise3.c │ └── 03_10-bitwise4.c ├── 03_11 │ ├── 03_11-shift1.c │ ├── 03_11-shift2.c │ └── 03_11-shift3.c ├── 03_12 │ ├── 03_12-not.c │ ├── 03_12-plusminus1.c │ ├── 03_12-plusminus2.c │ └── 03_12-sizeof.c ├── 03_13 │ └── 03_13-challenge1.c └── 03_14 │ └── 03_14-solution1.c ├── CH04 ├── 04_01 │ ├── 04_01-flow1.c │ ├── 04_01-flow2.c │ ├── 04_01-if1.c │ ├── 04_01-if2.c │ ├── 04_01-if3.c │ └── 04_01-if4.c ├── 04_02 │ ├── 04_02-else1.c │ ├── 04_02-elseif1.c │ └── 04_02-elseif2.c ├── 04_03 │ ├── 04_03-comparison1.c │ ├── 04_03-ternary1.c │ └── 04_03-ternary2.c ├── 04_04 │ ├── 04_04-switchcase1.c │ ├── 04_04-switchcase2.c │ └── 04_04-switchcase3.c ├── 04_05 │ └── 04_05-challenge1.c ├── 04_06 │ └── 04_06-solution1.c ├── 04_07 │ ├── 04_07-for1.c │ ├── 04_07-for2.c │ ├── 04_07-for3.c │ └── 04_07-for4.c ├── 04_08 │ ├── 04_08-dowhile1.c │ ├── 04_08-while1.c │ └── 04_08-while2.c ├── 04_09 │ └── 04_09-challenge1.c ├── 04_10 │ ├── 04_10-solution-for1.c │ └── 04_10-solution-while1.c ├── 04_11 │ ├── 04_11-nested1.c │ └── 04_11-nested2.c ├── 04_12 │ ├── 04_12-break1.c │ ├── 04_12-break2.c │ ├── 04_12-break3.c │ └── 04_12-continue1.c ├── 04_13 │ ├── 04_13-goto1.c │ └── 04_13-goto2.c ├── 04_14 │ └── 04_14-challenge1.c └── 04_15 │ └── 04_15-solution1.c ├── CH05 ├── 05_01 │ ├── 05_01-arguments1.c │ ├── 05_01-arguments2.c │ ├── 05_01-functypes1.c │ └── 05_01-main1.c ├── 05_02 │ ├── 05_02-function1.c │ ├── 05_02-function2.c │ ├── 05_02-function3.c │ └── 05_02-function4.c ├── 05_03 │ └── 05_03-challenge1.c ├── 05_04 │ ├── 05_04-solution1.c │ ├── 05_04-solution2.c │ └── 05_04-solution3.c ├── 05_05 │ ├── 05_05-function1.c │ └── 05_05-function2.c ├── 05_06 │ └── 05_06-challenge1.c ├── 05_07 │ └── 05_07-solution1.c ├── 05_08 │ ├── 05_08-function1.c │ ├── 05_08-function2.c │ └── 05_08-function3.c ├── 05_09 │ └── 05_09-challenge1.c ├── 05_10 │ └── 05_10-solution1.c ├── 05_11 │ └── 05_11-arguments1.c ├── 05_12 │ ├── 05_12-function1.c │ └── 05_12-function2.c ├── 05_13 │ ├── 05_13-recursive1.c │ ├── 05_13-recursive2.c │ ├── 05_13-recursive3.c │ └── 05_13-recursive4.c ├── 05_14 │ └── 05_14_challenge1.c └── 05_15 │ └── 05_15-solution1.c ├── CH06 ├── 06_01 │ └── 06_01-highscores1.c ├── 06_02 │ ├── 06_02-highscores1.c │ ├── 06_02-highscores2.c │ ├── 06_02-highscores3.c │ └── 06_02-highscores4.c ├── 06_03 │ └── 06_03-challenge1.c ├── 06_04 │ ├── 06_04-solution1.c │ └── 06_04-solution2.c ├── 06_05 │ ├── 06_05-passarray1.c │ ├── 06_05-passelement1.c │ └── 06_05-returnarray1.c ├── 06_06 │ ├── 06_06-grid1.c │ ├── 06_06-grid2.c │ ├── 06_06-grid3.c │ └── 06_06-grid4.c ├── 06_07 │ ├── 06_07-structure1.c │ ├── 06_07-structure2.c │ └── 06_07-structure3.c ├── 06_08 │ ├── 06_08-nested1.c │ ├── 06_08-nested2.c │ ├── 06_08-structinit1.c │ └── 06_08-structinit2.c ├── 06_09 │ └── 06_09-challenge1.c ├── 06_10 │ ├── 06_10-solution1.c │ └── 06_10-solution2.c ├── 06_11 │ ├── 06_11-structarray1.c │ ├── 06_11-structarray2.c │ └── 06_11-structarray3.c ├── 06_12 │ ├── 06_12-passall1.c │ ├── 06_12-passall2.c │ ├── 06_12-passmember1.c │ └── 06_12-returnstruct1.c ├── 06_13 │ └── 06_13-challenge1.c └── 06_14 │ └── 06_14-solution1.c ├── CH07 ├── 07_01 │ ├── 07_01-getchar1.c │ ├── 07_01-getchar2.c │ ├── 07_01-putchar1.c │ └── 07_01-typewriter1.c ├── 07_02 │ ├── 07_02-case1.c │ ├── 07_02-case2.c │ ├── 07_02-testchar1.c │ ├── 07_02-testchar2.c │ └── 07_02-testchar3.c ├── 07_03 │ └── 07_03-challenge1.c ├── 07_04 │ └── 07_04-solution1.c ├── 07_06 │ ├── 07_06-yourname1.c │ ├── 07_06-yourname2.c │ └── 07_06-yourname3.c ├── 07_07 │ ├── 07_07-printf1.c │ ├── 07_07-printf2.c │ ├── 07_07-puts1.c │ ├── 07_07-string1.c │ ├── 07_07-string2.c │ ├── 07_07-string3.c │ └── 07_07-string4.c ├── 07_08 │ └── 07_08-challenge1.c ├── 07_09 │ └── 07_09-solution1.c ├── 07_10 │ ├── 07_10-strcmp1.c │ └── 07_10-strlen1.c ├── 07_11 │ ├── 07_11-strbuild1.c │ ├── 07_11-strbuild2.c │ └── 07_11-strbuild3.c ├── 07_12 │ └── 07_12-challenge1.c └── 07_13 │ └── 07_13-solution1.c ├── CH08 ├── 08_01 │ └── 08_01-variableinfo1.c ├── 08_02 │ ├── 08_02-pointerdata1.c │ ├── 08_02-pointerdata2.c │ └── 08_02-pointerdata3.c ├── 08_03 │ ├── 08_03-pointermath1.c │ ├── 08_03-pointermath2.c │ ├── 08_03-pointermath3.c │ ├── 08_03-pointermath4.c │ └── 08_03-pointermath5.c ├── 08_04 │ ├── 08_04-parray1.c │ ├── 08_04-parray2.c │ ├── 08_04-parray3.c │ ├── 08_04-parray4.c │ └── 08_04-parray5.c ├── 08_05 │ └── 08_05-challenge1.c ├── 08_06 │ ├── 08_06-solution1.c │ └── 08_06-solution2.c ├── 08_07 │ ├── 08_07-allocate1.c │ └── 08_07-allocate2.c ├── 08_08 │ ├── 08_08-arrayp1.c │ ├── 08_08-arrayp2.c │ ├── 08_08-arrayp3.c │ ├── 08_08-arrayp4.c │ └── 08_08-arrayp5.c ├── 08_09 │ ├── 08_09-structurepointer1.c │ ├── 08_09-structurepointer2.c │ └── 08_09-structurepointer3.c ├── 08_10 │ └── 08_10-challenge1.c ├── 08_11 │ └── 08_11-solution1.c ├── 08_12 │ ├── 08_12-passpointer1.c │ └── 08_12-passpointer2.c ├── 08_13 │ ├── 08_13-returnp1.c │ └── 08_13-returnp2.c ├── 08_14 │ └── 08_14-challenge1.c └── 08_15 │ └── 08_15-solution1.c ├── CH09 ├── 09_01 │ ├── 09_01-reading1.c │ ├── 09_01-reading2.c │ └── sonnet18.txt ├── 09_02 │ ├── 09_02-writing1.c │ ├── 09_02-writing2.c │ └── 09_02-writing3.c ├── 09_03 │ └── 09_03-challenge1.c ├── 09_04 │ └── 09_04-solution1.c ├── 09_05 │ ├── 09_05-readf1.c │ ├── 09_05-readf2.c │ └── 09_05-writef1.c ├── 09_06 │ ├── 09_06-createpres1.c │ └── 09_06-readrand1.c ├── 09_07 │ └── 09_07-challenge1.c ├── 09_08 │ ├── 09_08-solution1.c │ ├── 09_08-solution2.c │ └── presidents.dat ├── 09_09 │ ├── 09_09-copyfile1.c │ ├── 09_09-rename1.c │ ├── 09_09-unlink1.c │ ├── alpha.txt │ ├── beta.txt │ └── delete_me.txt ├── 09_10 │ ├── 09_10-chdir1.c │ └── 09_10-getcwd1.c ├── 09_11 │ ├── 09_11-readdir1.c │ ├── 09_11-readdir2.c │ └── 09_11-readdir3.c ├── 09_12 │ ├── 09_12-filestat1.c │ ├── 09_12-filestat2.c │ ├── 09_12-filestat3.c │ └── alpha.txt ├── 09_13 │ └── 09_13-challenge1.c └── 09_14 │ └── 09_14-solution1.c ├── CH10 ├── 10_01 │ ├── 10_01-popen1.c │ └── 10_01-system1.c ├── 10_02 │ ├── 10_01-pow1.c │ └── 10_01-sqrt1.c ├── 10_03 │ ├── 10_02-time1.c │ ├── 10_02-time2.c │ ├── 10_02-time3.c │ └── 10_02-time4.c ├── 10_04 │ ├── 10_04-random1.c │ ├── 10_04-random2.c │ ├── 10_04-random3.c │ └── 10_04-random4.c ├── 10_05 │ ├── 10_05-bubblesort1.c │ └── 10_05-quicksort1.c ├── 10_06 │ └── 10_06-challenge1.c └── 10_07 │ └── 10_07-solution1.c ├── CONTRIBUTING.md ├── LICENSE ├── NOTICE ├── README.md └── src └── codehere.txt /.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.236.0/containers/cpp/.devcontainer/base.Dockerfile 2 | 3 | # [Choice] Debian / Ubuntu version (use Debian 11, Ubuntu 18.04/22.04 on local arm64/Apple Silicon): debian-11, debian-10, ubuntu-22.04, ubuntu-20.04, ubuntu-18.04 4 | ARG VARIANT="bullseye" 5 | FROM mcr.microsoft.com/vscode/devcontainers/cpp:0-${VARIANT} 6 | 7 | # [Optional] Install CMake version different from what base image has already installed. 8 | # CMake reinstall choices: none, 3.21.5, 3.22.2, or versions from https://cmake.org/download/ 9 | ARG REINSTALL_CMAKE_VERSION_FROM_SOURCE="none" 10 | 11 | # Optionally install the cmake for vcpkg 12 | COPY ./reinstall-cmake.sh /tmp/ 13 | RUN if [ "${REINSTALL_CMAKE_VERSION_FROM_SOURCE}" != "none" ]; then \ 14 | chmod +x /tmp/reinstall-cmake.sh && /tmp/reinstall-cmake.sh ${REINSTALL_CMAKE_VERSION_FROM_SOURCE}; \ 15 | fi \ 16 | && rm -f /tmp/reinstall-cmake.sh 17 | 18 | # [Optional] Uncomment this section to install additional vcpkg ports. 19 | # RUN su vscode -c "${VCPKG_ROOT}/vcpkg install " 20 | 21 | # [Optional] Uncomment this section to install additional packages. 22 | # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ 23 | # && apt-get -y install --no-install-recommends 24 | -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | // For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: 2 | // https://github.com/microsoft/vscode-dev-containers/tree/v0.236.0/containers/cpp 3 | { 4 | "name": "C++", 5 | "build": { 6 | "dockerfile": "Dockerfile", 7 | // Update 'VARIANT' to pick an Debian / Ubuntu OS version: debian-11, debian-10, ubuntu-22.04, ubuntu-20.04, ubuntu-18.04 8 | // Use Debian 11, Ubuntu 18.04 or Ubuntu 22.04 on local arm64/Apple Silicon 9 | "args": { "VARIANT": "debian-11" } 10 | }, 11 | "runArgs": ["--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined"], 12 | "extensions": [ 13 | "GitHub.github-vscode-theme" 14 | 15 | ], 16 | // Configure tool-specific properties. 17 | "customizations": { 18 | // Configure properties specific to VS Code. 19 | "vscode": { 20 | // Add the IDs of extensions you want installed when the container is created. 21 | "extensions": [ 22 | "ms-vscode.cpptools", 23 | "ms-vscode.cmake-tools" 24 | 25 | ] 26 | } 27 | }, 28 | 29 | // Use 'forwardPorts' to make a list of ports inside the container available locally. 30 | // "forwardPorts": [], 31 | 32 | // Use 'postCreateCommand' to run commands after the container is created. 33 | // "postCreateCommand": "gcc -v", 34 | "onCreateCommand" : "echo PS1='\"$ \"' >> ~/.bashrc", //Set Terminal Prompt to $ 35 | 36 | // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. 37 | "remoteUser": "vscode", 38 | "features": { 39 | "powershell": "latest" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /.devcontainer/reinstall-cmake.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | #------------------------------------------------------------------------------------------------------------- 3 | # Copyright (c) Microsoft Corporation. All rights reserved. 4 | # Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. 5 | #------------------------------------------------------------------------------------------------------------- 6 | # 7 | set -e 8 | 9 | CMAKE_VERSION=${1:-"none"} 10 | 11 | if [ "${CMAKE_VERSION}" = "none" ]; then 12 | echo "No CMake version specified, skipping CMake reinstallation" 13 | exit 0 14 | fi 15 | 16 | # Cleanup temporary directory and associated files when exiting the script. 17 | cleanup() { 18 | EXIT_CODE=$? 19 | set +e 20 | if [[ -n "${TMP_DIR}" ]]; then 21 | echo "Executing cleanup of tmp files" 22 | rm -Rf "${TMP_DIR}" 23 | fi 24 | exit $EXIT_CODE 25 | } 26 | trap cleanup EXIT 27 | 28 | 29 | echo "Installing CMake..." 30 | apt-get -y purge --auto-remove cmake 31 | mkdir -p /opt/cmake 32 | 33 | architecture=$(dpkg --print-architecture) 34 | case "${architecture}" in 35 | arm64) 36 | ARCH=aarch64 ;; 37 | amd64) 38 | ARCH=x86_64 ;; 39 | *) 40 | echo "Unsupported architecture ${architecture}." 41 | exit 1 42 | ;; 43 | esac 44 | 45 | CMAKE_BINARY_NAME="cmake-${CMAKE_VERSION}-linux-${ARCH}.sh" 46 | CMAKE_CHECKSUM_NAME="cmake-${CMAKE_VERSION}-SHA-256.txt" 47 | TMP_DIR=$(mktemp -d -t cmake-XXXXXXXXXX) 48 | 49 | echo "${TMP_DIR}" 50 | cd "${TMP_DIR}" 51 | 52 | curl -sSL "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/${CMAKE_BINARY_NAME}" -O 53 | curl -sSL "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/${CMAKE_CHECKSUM_NAME}" -O 54 | 55 | sha256sum -c --ignore-missing "${CMAKE_CHECKSUM_NAME}" 56 | sh "${TMP_DIR}/${CMAKE_BINARY_NAME}" --prefix=/opt/cmake --skip-license 57 | 58 | ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake 59 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Codeowners for these exercise files: 2 | # * (asterisk) denotes "all files and folders" 3 | # Example: * @producer @instructor 4 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 7 | 8 | ## Issue Overview 9 | 10 | 11 | ## Describe your environment 12 | 13 | 14 | ## Steps to Reproduce 15 | 16 | 1. 17 | 2. 18 | 3. 19 | 4. 20 | 21 | ## Expected Behavior 22 | 23 | 24 | ## Current Behavior 25 | 26 | 27 | ## Possible Solution 28 | 29 | 30 | ## Screenshots / Video 31 | 32 | 33 | ## Related Issues 34 | 35 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Copy To Branches 2 | on: 3 | workflow_dispatch: 4 | jobs: 5 | copy-to-branches: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v2 9 | with: 10 | fetch-depth: 0 11 | - name: Copy To Branches Action 12 | uses: planetoftheweb/copy-to-branches@v1.2 13 | env: 14 | key: main 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .tmp 4 | npm-debug.log 5 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.bracketPairColorization.enabled": true, 3 | "editor.cursorBlinking": "solid", 4 | "editor.fontFamily": "ui-monospace, Menlo, Monaco, 'Cascadia Mono', 'Segoe UI Mono', 'Roboto Mono', 'Oxygen Mono', 'Ubuntu Monospace', 'Source Code Pro', 'Fira Mono', 'Droid Sans Mono', 'Courier New', monospace", 5 | "editor.fontLigatures": false, 6 | "editor.fontSize": 22, 7 | "editor.formatOnPaste": true, 8 | "editor.formatOnSave": true, 9 | "editor.lineNumbers": "on", 10 | "editor.matchBrackets": "always", 11 | "editor.minimap.enabled": false, 12 | "editor.smoothScrolling": true, 13 | "editor.tabSize": 2, 14 | "editor.useTabStops": true, 15 | "emmet.triggerExpansionOnTab": true, 16 | "explorer.openEditors.visible": 0, 17 | "files.autoSave": "afterDelay", 18 | "screencastMode.onlyKeyboardShortcuts": true, 19 | "terminal.integrated.fontSize": 18, 20 | "workbench.activityBar.visible": true, 21 | "workbench.colorTheme": "Visual Studio Dark", 22 | "workbench.fontAliasing": "antialiased", 23 | "workbench.statusBar.visible": true 24 | } 25 | -------------------------------------------------------------------------------- /CH00/00_02/00_02-hello1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("Hello, world!"); 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /CH01/01_01/01_01-first1.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /CH01/01_02/01_02-challenge1.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /CH01/01_03/01_03-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | puts("I am a C programmer."); 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /CH01/01_04/01_04-hello1.asm: -------------------------------------------------------------------------------- 1 | section .data 2 | 3 | msg db "Greetings, human!", 0xa 4 | msglen equ $-msg ; string length in bytes 5 | STDOUT equ 1 ; standard output device 6 | SYS_write equ 1 ; write message 7 | SYS_exit equ 60 ; exit program 8 | EXIT_OK equ 0 ; return value 9 | 10 | section .text 11 | global _start 12 | _start: 13 | 14 | mov rax, SYS_write ; rax holds the function 15 | mov rdi, STDOUT ; output device 16 | mov rsi, msg ; text address 17 | mov rdx, msglen ; string length 18 | syscall ; system interrupt 19 | ;exit 20 | mov rax,SYS_exit ; exit function 21 | mov rdi,EXIT_OK ; exit return value 22 | syscall ; system interrupt 23 | ;done 24 | -------------------------------------------------------------------------------- /CH01/01_04/01_04-hello1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char string[] = "Greetings, human!\n"; 6 | int index; 7 | 8 | index = 0; 9 | while( string[index] != '\0' ) 10 | { 11 | putchar( string[index] ); 12 | index++; 13 | } 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH01/01_05/01_05-sample1.c: -------------------------------------------------------------------------------- 1 | /* input and loop demo */ 2 | #include 3 | 4 | int main() 5 | { 6 | int a,b; 7 | 8 | printf("Type a positive value: "); 9 | scanf("%d",&b); 10 | for(a=0;a 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | printf("Type a positive value: "); 8 | scanf("%d",&b); 9 | for(a=0;a 2 | int main(){int a,b;printf("Type a positive value: ");scanf("%d",&b);for(a=0;a 2 | #include 3 | 4 | int main() 5 | { 6 | printf("Hello, world!\n"); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /CH01/01_06/01_06-cdev2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("I bet you can ") 6 | prinft("find the error!\n"); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /CH01/01_07/01_07-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | printf("Type a positive value: ); 8 | scanf("%d",&b); 9 | for(a=0;a 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | printf("Type a positive value: "); 8 | scanf("%d",&b); 9 | for(a=0;a 2 | #include 3 | #include 4 | 5 | #define COUNT 10 6 | 7 | int main() 8 | { 9 | int a; 10 | 11 | srand( (unsigned)time(NULL) ); 12 | for(a=0;a 2 | 3 | #define GREETING "Howdy do!" 4 | 5 | int main() 6 | { 7 | puts(GREETING); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /CH01/01_09/01_09-define3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define charout(a) putc(a,stdout) 4 | 5 | int main() 6 | { 7 | charout('H'); 8 | charout('i'); 9 | charout('!'); 10 | charout('\n'); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /CH01/01_09/01_09-include1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | int a; 8 | 9 | srand( (unsigned)time(NULL) ); 10 | for(a=0;a<10;a++) 11 | printf("%3d ",rand()%100+1); 12 | putchar('\n'); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /CH01/01_10/01_10-math1.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* The math header file contains definitions for M_PI 3 | and the sin() function. It does not contain the 4 | code for the sin() function. The code lies in the 5 | math library file */ 6 | #include 7 | 8 | int main() 9 | { 10 | int w; 11 | for( w=0; w<10; w++) 12 | { 13 | printf("%.2f\n",sin(M_PI/(w+1)) ); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /CH01/01_11/01_11-comments1.c: -------------------------------------------------------------------------------- 1 | /* 2 | This code outputs a message 3 | */ 4 | #include //You can add this type of comment at the end of a line 5 | 6 | /* This code contains only the main() function */ 7 | int main() 8 | { 9 | puts("I can generate text all day long."); /* pithy advice */ 10 | 11 | return(0); 12 | } 13 | -------------------------------------------------------------------------------- /CH01/01_11/01_11-comments2.c: -------------------------------------------------------------------------------- 1 | /************************************ 2 | * Extremely complex code * 3 | * Dan Gookin * 4 | * Original Version: 2018 09 06 * 5 | * 2019 07 01 Fixed bugs * 6 | * 2020 06 20 Fixed more bugs * 7 | * 2023 11 27 Fixed last bug? * 8 | ***********************************/ 9 | #include 10 | 11 | int main() 12 | { 13 | /* the point is to output text for the user */ 14 | puts("I can generate text all day long."); 15 | 16 | /* write code here to output a happy face */ 17 | 18 | return(0); 19 | } 20 | -------------------------------------------------------------------------------- /CH01/01_11/01_11-comments3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char happy_face = 191; /* isn't this the correct code? */ 6 | 7 | puts("I can generate text all day long."); 8 | /* printf("%c\n",happy_face); */ 9 | 10 | return(0); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /CH01/01_11/01_11-comments4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | /* this code features a nested loop 8 | /* but this nested comment isn't good */ 9 | */ 10 | for( a=0; a<10; a++ ) 11 | { 12 | for( b='A'; b<='J'; b++ ) 13 | printf("%d%c ",a,b); 14 | putchar('\n'); 15 | } 16 | 17 | return(0); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /CH01/01_11/01_11-comments5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | puts("This string /* char array */ is too long"); 6 | 7 | return(0); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /CH01/01_12/01_12-challenge1.c: -------------------------------------------------------------------------------- 1 | main program 2 | 3 | initialization 4 | 5 | initial message 6 | 7 | main program loop 8 | 9 | output results 10 | -------------------------------------------------------------------------------- /CH01/01_13/01_13-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* main program */ 5 | int main() 6 | { 7 | /* initialization */ 8 | 9 | /* initial message */ 10 | 11 | /* main program loop */ 12 | 13 | /* output results */ 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /CH02/02_01/02_01-datatypes1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char c; 6 | int i; 7 | float f; 8 | double d; 9 | 10 | /* input */ 11 | printf("Type a character: "); 12 | scanf("%c",&c); 13 | printf("Type an integer: "); 14 | scanf("%d",&i); 15 | printf("Type a small value: "); 16 | scanf("%f",&f); 17 | printf("Type a huge value: "); 18 | scanf("%lg",&d); 19 | 20 | /* output */ 21 | printf("\nCharacter: %c\n",c); 22 | printf("Integer: %d\n",i); 23 | printf("Single-precision: %f\n",f); 24 | printf("Double-precision: %lg\n",d); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /CH02/02_01/02_01-string1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char buffer[32]; 6 | 7 | printf("Type your name: "); 8 | fgets(buffer,32,stdin); 9 | printf("Hello, %s",buffer); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /CH02/02_02/02_02-variables1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char c; 6 | int i; 7 | float f; 8 | 9 | /* input */ 10 | printf("Type a character: "); 11 | scanf("%c",&c); 12 | printf("Type an integer: "); 13 | scanf("%d",&i); 14 | printf("Type a small value: "); 15 | scanf("%f",&f); 16 | printf("Type a huge value: "); 17 | double d; 18 | scanf("%lg",&d); 19 | 20 | /* output */ 21 | printf("\nCharacter: %c\n",c); 22 | printf("Integer: %d\n",i); 23 | printf("Single-precision: %f\n",f); 24 | printf("Double-precision: %lg\n",d); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /CH02/02_02/02_02-variables2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | /* valid variable names */ 6 | int i123456789; /* must start with a letter */ 7 | int looping_value; /* can include underscores, not spaces */ 8 | int modemOnlineDetect; /* camel case */ 9 | 10 | /* naughty variable names */ 11 | char return; /* 'return' is a keyword */ 12 | int puts; /* 'puts' is a function name */ 13 | float new; /* 'new' is a C++ reserved word */ 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /CH02/02_02/02_02-variables3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char a = 'X'; 6 | int count = 25; 7 | double longitude = 47.677878; 8 | 9 | printf("%c\n%d\n%f\n", 10 | a, 11 | count, 12 | longitude 13 | ); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /CH02/02_03/02_03-variable1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char a; 6 | int b; 7 | float c; 8 | double d; 9 | 10 | a = 'A'; 11 | b = 2; 12 | c = 0.25; 13 | d = 2.5e6; 14 | 15 | printf("a is %c\n",a); 16 | printf("b is %d\n",b); 17 | printf("c is %f\n",c); 18 | printf("d is %f\n",d); 19 | 20 | return(0); 21 | } 22 | -------------------------------------------------------------------------------- /CH02/02_03/02_03-variable2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char a = 'A'; 6 | int b = 2; 7 | float c = 0.25; 8 | double d = 2500000.0; 9 | 10 | printf("a is %c\n",a); 11 | printf("b is %d\n",b); 12 | printf("c is %f\n",c); 13 | printf("d is %f\n",d); 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH02/02_03/02_03-variable3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char a,b,c,d,e; 6 | 7 | a = 'H'; 8 | b = 'e'; 9 | c = 'l'; 10 | d = 'o'; 11 | e = '\n'; 12 | 13 | printf("%c%c%c%c%c%c",a,b,c,c,d,e); 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH02/02_03/02_03-variable4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float x; 6 | 7 | printf("The value of %f is unknown\n",x); 8 | 9 | return(0); 10 | } 11 | -------------------------------------------------------------------------------- /CH02/02_04/02_04-printf1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | printf("Hello, world!\n"); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /CH02/02_04/02_04-printf2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 65; 6 | 7 | printf("The value of 'a' is %d\n",a); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /CH02/02_04/02_04-printf3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char ch = 'A'; 6 | unsigned x = 100; 7 | float pi = 3.14159; 8 | 9 | printf("ch = %c, x = %u, pi = %f\n",ch,x,pi); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /CH02/02_04/02_04-printf4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float pi = 3.14159; 6 | 7 | printf("The value of pi: %.2f\n",pi); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /CH02/02_05/02_05-constant1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | a = 4+5; 8 | printf("4 + 5 = %d\n",a); 9 | 10 | return(0); 11 | } 12 | -------------------------------------------------------------------------------- /CH02/02_05/02_05-constant2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | putchar('H'); 6 | putchar('e'); 7 | putchar('l'); 8 | putchar('l'); 9 | putchar('o'); 10 | putchar('\n'); 11 | 12 | return(0); 13 | } 14 | -------------------------------------------------------------------------------- /CH02/02_05/02_05-constant3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | puts("Greetings, programmer!"); 6 | 7 | return(0); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /CH02/02_05/02_05-constant4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const int count = 4; 6 | 7 | printf("Please count to %d\n",count); 8 | printf("Do not count to %d\n",count+1); 9 | printf("Do not stop at %d\n",count-1); 10 | 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /CH02/02_05/02_05-constant5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX 20 4 | 5 | /* generate a line */ 6 | void line(int v) 7 | { 8 | int x; 9 | 10 | for( x=0; x= MAX ) 13 | break; 14 | putchar('-'); 15 | } 16 | putchar('\n'); 17 | } 18 | 19 | int main() 20 | { 21 | int value; 22 | 23 | printf("Enter a positive value less than %d: ",MAX); 24 | scanf("%d",&value); 25 | line(value); 26 | return(0); 27 | } 28 | -------------------------------------------------------------------------------- /CH02/02_06/02_06-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("Here is the value of A: %d\n",a); 6 | printf("Here is the value of B: %c\n",b); 7 | 8 | return(0); 9 | } 10 | -------------------------------------------------------------------------------- /CH02/02_07/02_07-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | const char b = 'Z'; 7 | 8 | a = 16; 9 | printf("Here is the value of A: %d\n",a); 10 | printf("Here is the value of B: %c\n",b); 11 | 12 | return(0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /CH02/02_07/02_07-solution2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 16; 6 | const char b = 'Z'; 7 | 8 | printf("Here is the value of A: %d\n",a); 9 | printf("Here is the value of B: %c\n",b); 10 | 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /CH02/02_08/02_08-auto1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void sum(void) 4 | { 5 | auto int a,b; 6 | 7 | a = 91; b = 7; 8 | printf("%d + %d = %d\n",a,b,a+b); 9 | } 10 | 11 | int main() 12 | { 13 | puts("Calling the sum() function:"); 14 | sum(); 15 | puts("done"); 16 | 17 | return(0); 18 | } 19 | -------------------------------------------------------------------------------- /CH02/02_08/02_08-extern1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int a,b; 4 | 5 | void sum(void) 6 | { 7 | extern int a,b; 8 | 9 | a = 91; b = 7; 10 | printf("%d + %d = %d\n",a,b,a+b); 11 | } 12 | 13 | int main() 14 | { 15 | extern int a,b; 16 | 17 | puts("Calling the sum() function:"); 18 | sum(); 19 | puts("And in main():"); 20 | printf("%d + %d = %d\n",a,b,a+b); 21 | 22 | return(0); 23 | } 24 | -------------------------------------------------------------------------------- /CH02/02_08/02_08-static1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char *myname(void) 4 | { 5 | char me[] = "Dan Gookin"; 6 | 7 | return(me); 8 | } 9 | 10 | int main() 11 | { 12 | printf("This course's author is %s\n", myname() ); 13 | 14 | return(0); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /CH02/02_09/02_09-time_t1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | time_t now; 7 | 8 | time(&now); 9 | printf("%s",ctime(&now)); 10 | 11 | return(0); 12 | } 13 | -------------------------------------------------------------------------------- /CH02/02_09/02_09-typedef1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | typedef float radius; 6 | 7 | radius a; 8 | 9 | a = 27.5; 10 | 11 | printf("A circle with radius %.2f has an area of %.2f\n", 12 | a, 13 | a*a*3.1415926 14 | ); 15 | 16 | return(0); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /CH02/02_10/02_10-singlechar1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char a; 6 | 7 | printf("Type a single character: "); 8 | scanf("%c",&a); 9 | 10 | printf("You typed \"%c\", which is code %d\n", 11 | a, 12 | a 13 | ); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /CH02/02_10/02_10-singlechar2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char greetings[4] = { 'H', 'i', '\x21', '\0' }; 6 | 7 | puts(greetings); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /CH02/02_10/02_10-string1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char string[] = "I'm literally a string.\n"; 6 | 7 | puts(string); 8 | } 9 | -------------------------------------------------------------------------------- /CH02/02_11/02_11-inputtest1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char buffer[16]; 7 | int lv; 8 | 9 | printf("Type a large value: "); 10 | fgets(buffer,16,stdin); 11 | lv = strtol(buffer,NULL,10); 12 | printf("You typed %d\n",lv); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /CH02/02_11/02_11-values1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int mega = 1000000; /* no commas! */ 6 | float quarter = 0.25; /* leading zero */ 7 | double large = 1000000000.0; /* trailing zero */ 8 | 9 | printf("Here are the values:\n"); 10 | printf("Mega - %d\n",mega); 11 | printf("Quarter - %f\n",quarter); 12 | printf("Large = %f\n",large); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /CH02/02_11/02_11-values2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("%f\n",2.5E6); 6 | printf("%f\n",2.5e-6); 7 | 8 | return(0); 9 | } 10 | -------------------------------------------------------------------------------- /CH02/02_11/02_11-values3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int x,o; 6 | 7 | /* output hexadecimal, base 16 values */ 8 | for(x=0x1 ; x<=0x10; x++) 9 | printf("0x%X = %d\n",x,x); 10 | /* output octal, base 8 values */ 11 | for(o=01 ; o<=010; o++) 12 | printf("0%o = %d\n",o,o); 13 | 14 | return(0); 15 | } 16 | -------------------------------------------------------------------------------- /CH02/02_11/02_11-values4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | long x = 100L; 6 | unsigned y = 17U; 7 | float z = 3.0F; 8 | 9 | printf("X = %ld\n",x); 10 | printf("Y = %d\n",y); 11 | printf("Z = %f\n",z); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /CH02/02_12/02_12-typecast1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | a = 100; 8 | b = 7; 9 | 10 | printf("%d / %d = %d\n",a,b,a/b); 11 | 12 | return(0); 13 | } 14 | -------------------------------------------------------------------------------- /CH02/02_12/02_12-typecast2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | int x,r[5]; 8 | 9 | srand((unsigned)time(NULL)); 10 | 11 | for(x=0;x<5;x++) 12 | r[x] = rand()%10+1; 13 | 14 | printf("Here are your %d random values:\n",5); 15 | for(x=0;x<5;x++) 16 | printf("%3d\n",r[x]); 17 | 18 | return(0); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /CH02/02_13/02_13-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | //declare constant ratio equal to 3/4 7 | //declare char a 8 | //declare integer b 9 | //declare float c 10 | 11 | //assign char 12 | //assign integer 13 | //assign float 14 | 15 | //output char value, e.g., "the value of variable a is '?'" 16 | //output integer value, e.g., "the value of variable b is ??" 17 | //output float value, e.g., "the value of variable c is ???.??" 18 | //output value of ratio, e.g., "the value of constant ratio is ???.??" 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /CH02/02_14/02_14-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | const float ratio = 0.75; /* or =3.0/4.0 */ 7 | char a; 8 | int b; 9 | float c; 10 | 11 | a = 'A'; 12 | b = 45; 13 | c = 123.45; 14 | 15 | printf("The value of variable a is '%c'\n",a); 16 | printf("The value of variable b is %d\n",b); 17 | printf("The value of variable c is %.2f\n",c); 18 | printf("The value of constant ratio is %.2f\n",ratio); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /CH03/03_01/03_01-math1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | a = 18; 8 | b = 3; 9 | 10 | puts("Basic math"); 11 | printf("%d + %d = %d\n",a,b,a+b); 12 | printf("%d - %d = %d\n",a,b,a-b); 13 | printf("%d * %d = %d\n",a,b,a*b); 14 | printf("%d / %d = %d\n",a,b,a/b); 15 | 16 | return(0); 17 | } 18 | -------------------------------------------------------------------------------- /CH03/03_01/03_01-math2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | a = 10; 8 | b = a; 9 | 10 | printf("a is %d, b is %d\n",a,b); 11 | a++; 12 | b--; 13 | printf("a is %d, b is %d\n",a,b); 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH03/03_01/03_01-math3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | a = 10; 8 | b = a; 9 | 10 | printf("a is %d, b is %d\n",a++,b--); 11 | printf("a is %d, b is %d\n",a,b); 12 | 13 | return(0); 14 | } 15 | -------------------------------------------------------------------------------- /CH03/03_01/03_01-math4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int mod,x,a; 6 | 7 | printf("Enter a value from 2 to 10: "); 8 | scanf("%d",&mod); 9 | for(x=1;x<=10;x++) 10 | { 11 | a = x % mod; 12 | printf("%2d %% %d = %d\n",x,mod,a); 13 | } 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH03/03_02/03_02-chalenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | /* variable declarations */ 6 | 7 | /* variable assignments */ 8 | 9 | /* output */ 10 | printf("Value of variable A = %d\n",a); 11 | printf("Value of variable B = %f\n",b); 12 | printf("value of vairable C = %d\n",c); 13 | 14 | return(0); 15 | } 16 | -------------------------------------------------------------------------------- /CH03/03_03/03_03-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | /* variable declarations */ 6 | int a,c; 7 | float b; 8 | 9 | /* variable assignments */ 10 | a = 15; 11 | b = a/2.0; 12 | c = b * 4; 13 | 14 | /* output */ 15 | printf("Value of variable A = %d\n",a); 16 | printf("Value of variable B = %f\n",b); 17 | printf("value of vairable C = %d\n",c); 18 | 19 | return(0); 20 | } 21 | -------------------------------------------------------------------------------- /CH03/03_04/03_04-assignment1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 25; 6 | 7 | printf("The value of a is %d\n",a); 8 | a = a + 5; 9 | printf("The value of a is %d\n",a); 10 | a = a - 5; 11 | printf("The value of a is %d\n",a); 12 | a = a * 5; 13 | printf("The value of a is %d\n",a); 14 | a = a / 5; 15 | printf("The value of a is %d\n",a); 16 | 17 | return(0); 18 | } 19 | -------------------------------------------------------------------------------- /CH03/03_04/03_04-assignment2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 25; 6 | 7 | printf("The value of a is %d\n",a); 8 | a += 5; 9 | printf("The value of a is %d\n",a); 10 | a -= 5; 11 | printf("The value of a is %d\n",a); 12 | a *= 5; 13 | printf("The value of a is %d\n",a); 14 | a /= 5; 15 | printf("The value of a is %d\n",a); 16 | 17 | return(0); 18 | } 19 | -------------------------------------------------------------------------------- /CH03/03_05/03_05-order1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | a = 6 / 2 * (1 + 2); 8 | printf("The people who said %d are correct.\n",a); 9 | 10 | return(0); 11 | } 12 | -------------------------------------------------------------------------------- /CH03/03_06/03_06-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int artifacts,rooms,paintings,sculptures; 6 | 7 | /* code your calculations here */ 8 | 9 | printf("This museum has %d artifacts\n",artifacts); 10 | 11 | return(0); 12 | } 13 | -------------------------------------------------------------------------------- /CH03/03_07/03_07-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int artifacts,rooms,paintings,sculptures; 6 | 7 | /* code your calculations here */ 8 | rooms = 12; 9 | paintings = 16; 10 | sculptures = 4; 11 | 12 | artifacts = rooms * (paintings + sculptures); 13 | 14 | printf("This museum has %d artifacts\n",artifacts); 15 | 16 | return(0); 17 | } 18 | -------------------------------------------------------------------------------- /CH03/03_08/03_08-relational1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | printf("Type a value: "); 8 | scanf("%d",&a); 9 | if( a < 10 ) 10 | { 11 | puts("You typed a value less than 10"); 12 | } 13 | puts("Thank you"); 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH03/03_08/03_08-relational2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | for(a=1; a<=20; a++) 8 | { 9 | puts("Help! I'm trapped in the computer!"); 10 | } 11 | 12 | return(0); 13 | } 14 | -------------------------------------------------------------------------------- /CH03/03_08/03_08-relational3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 0; 6 | 7 | while( a <= 20 ) 8 | { 9 | puts("Good day to you!"); 10 | a++; 11 | if( a == 10 ) 12 | break; 13 | } 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH03/03_09/03_09-logical1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 1; 6 | 7 | while(a <= 20) 8 | { 9 | if( a > 6 && a < 15 ) 10 | putchar('*'); 11 | printf("%d\n",a); 12 | a++; 13 | } 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH03/03_09/03_09-logical2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 1; 6 | 7 | while( a <= 20) 8 | { 9 | if( a <= 5 || a >= 16 || a == 10 ) 10 | putchar('*'); 11 | printf("%d\n",a); 12 | a++; 13 | } 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH03/03_10/03_10-bitwise1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* convert an integer into a string of 1s and 0s */ 4 | char *binString(unsigned short n) 5 | { 6 | static char bin[17]; 7 | int x; 8 | 9 | for(x=0;x<16;x++) 10 | { 11 | bin[x] = n & 0x8000 ? '1' : '0'; 12 | n <<= 1; 13 | } 14 | bin[16] = '\0'; 15 | 16 | return(bin); 17 | } 18 | 19 | int main() 20 | { 21 | unsigned short a,b,c; 22 | 23 | a = 0xAAAA; 24 | b = 0x00FF; 25 | 26 | printf("Start: %04X - %s\n",a,binString(a)); 27 | printf(" & %04X - %s\n",b,binString(b)); 28 | c = a & b; 29 | printf(" End: %04X - %s\n",c,binString(c)); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /CH03/03_10/03_10-bitwise2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* convert an integer into a string of 1s and 0s */ 4 | char *binString(unsigned short n) 5 | { 6 | static char bin[17]; 7 | int x; 8 | 9 | for(x=0;x<16;x++) 10 | { 11 | bin[x] = n & 0x8000 ? '1' : '0'; 12 | n <<= 1; 13 | } 14 | bin[16] = '\0'; 15 | 16 | return(bin); 17 | } 18 | 19 | int main() 20 | { 21 | unsigned short a,b,c; 22 | 23 | a = 0xAAAA; 24 | b = 0x5555; 25 | 26 | printf("Start: %04X - %s\n",a,binString(a)); 27 | printf(" | %04X - %s\n",b,binString(b)); 28 | c = a | b; 29 | printf(" End: %04X - %s\n",c,binString(c)); 30 | 31 | return(0); 32 | } 33 | -------------------------------------------------------------------------------- /CH03/03_10/03_10-bitwise3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* convert an integer into a string of 1s and 0s */ 4 | char *binString(unsigned short n) 5 | { 6 | static char bin[17]; 7 | int x; 8 | 9 | for(x=0;x<16;x++) 10 | { 11 | bin[x] = n & 0x8000 ? '1' : '0'; 12 | n <<= 1; 13 | } 14 | bin[16] = '\0'; 15 | 16 | return(bin); 17 | } 18 | 19 | int main() 20 | { 21 | unsigned short a,b,c; 22 | 23 | a = 0xAAAA; 24 | b = 0x55FF; 25 | 26 | printf("Start: %04X - %s\n",a,binString(a)); 27 | printf(" ^ %04X - %s\n",b,binString(b)); 28 | c = a ^ b; 29 | printf(" ^ %04X - %s\n",c,binString(c)); 30 | a = c ^ b; 31 | printf(" End: %04X - %s\n",a,binString(a)); 32 | 33 | return(0); 34 | } 35 | -------------------------------------------------------------------------------- /CH03/03_10/03_10-bitwise4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* convert an integer into a string of 1s and 0s */ 4 | char *binString(unsigned short n) 5 | { 6 | static char bin[17]; 7 | int x; 8 | 9 | for(x=0;x<16;x++) 10 | { 11 | bin[x] = n & 0x8000 ? '1' : '0'; 12 | n <<= 1; 13 | } 14 | bin[16] = '\0'; 15 | 16 | return(bin); 17 | } 18 | 19 | int main() 20 | { 21 | unsigned short a,b; 22 | 23 | a = 0xAAAA; 24 | 25 | printf("Start: %04X - %s\n",a,binString(a)); 26 | b = ~a; 27 | printf(" End: %04X - %s\n",b,binString(b)); 28 | 29 | return(0); 30 | } 31 | -------------------------------------------------------------------------------- /CH03/03_11/03_11-shift1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* convert an integer into a string of 1s and 0s */ 4 | char *binString(unsigned short n) 5 | { 6 | static char bin[17]; 7 | int x; 8 | 9 | for(x=0;x<16;x++) 10 | { 11 | bin[x] = n & 0x8000 ? '1' : '0'; 12 | n <<= 1; 13 | } 14 | bin[16] = '\0'; 15 | 16 | return(bin); 17 | } 18 | 19 | int main() 20 | { 21 | unsigned short a; 22 | int x; 23 | 24 | a = 0x0003; 25 | 26 | for(x=0;x<16;x++) 27 | { 28 | printf("%04X - %s\n",a,binString(a)); 29 | a = a << 1; 30 | } 31 | 32 | return(0); 33 | } 34 | -------------------------------------------------------------------------------- /CH03/03_11/03_11-shift2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 1; 6 | 7 | while(a < 33000) 8 | { 9 | printf("%d\n",a); 10 | a = a << 1; 11 | } 12 | 13 | return(0); 14 | } 15 | -------------------------------------------------------------------------------- /CH03/03_11/03_11-shift3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 1000000; 6 | 7 | while(a > 0) 8 | { 9 | printf("%d\n",a); 10 | a = a >> 1; 11 | } 12 | 13 | return(0); 14 | } 15 | -------------------------------------------------------------------------------- /CH03/03_12/03_12-not.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | for(a=0;a<20;a++) 8 | { 9 | if( a<10 ) 10 | putchar('*'); 11 | printf("%d\n",a); 12 | } 13 | 14 | return(0); 15 | } 16 | -------------------------------------------------------------------------------- /CH03/03_12/03_12-plusminus1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | a = +50; 8 | b = -10; 9 | 10 | printf("%d + %d = %d\n",a,b,a+b); 11 | 12 | return(0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /CH03/03_12/03_12-plusminus2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | a = 50; 8 | b = 10; 9 | 10 | printf("%d + %d = %d\n",+a,-b,+a+-b); 11 | 12 | return(0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /CH03/03_12/03_12-sizeof.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char a; 6 | int b; 7 | float c; 8 | double d; 9 | char string[] = "Hello!"; 10 | int buffer[10]; 11 | 12 | printf("char variable 'a' uses %lu bytes\n",sizeof(a)); 13 | printf("int variable 'b' uses %lu bytes\n",sizeof(b)); 14 | printf("float variable 'c' uses %lu bytes\n",sizeof(c)); 15 | printf("double variable 'd' uses %lu bytes\n",sizeof(d)); 16 | printf("string array 'string' uses %lu bytes\n",sizeof(string)); 17 | printf("int buffer 'buffer' uses %lu bytes\n",sizeof(buffer)); 18 | 19 | return(0); 20 | } 21 | -------------------------------------------------------------------------------- /CH03/03_13/03_13-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | printf("Enter two values, separated by a space: "); 8 | scanf("%d",&a); 9 | scanf("%d",&b); 10 | 11 | // add 12 | // subtract 13 | // multiply 14 | // divide 15 | // modulus 16 | // bit shift right 17 | // bit shift left 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /CH03/03_14/03_14-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | printf("Enter two values, separated by a space: "); 8 | scanf("%d",&a); 9 | scanf("%d",&b); 10 | 11 | printf("%d + %d = %d\n",a,b,a+b); 12 | printf("%d - %d = %d\n",a,b,a-b); 13 | printf("%d * %d = %d\n",a,b,a*b); 14 | printf("%d / %d = %f\n",a,b,(float)a/b); 15 | printf("%d %% %d = %d\n",a,b,a%b); 16 | printf("%d >> %d = %d\n",a,b,a>>b); 17 | printf("%d << %d = %d\n",a,b,a< 2 | 3 | int main() 4 | { 5 | puts("This code runs top-down"); 6 | puts("Each line executed, one after the other"); 7 | puts("Up until the function ends"); 8 | puts("For the main() function, a 'return' ends the flow"); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /CH04/04_01/04_01-flow2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int get_value() 5 | { 6 | char buffer[3]; 7 | 8 | fgets(buffer,3,stdin); 9 | return(atoi(buffer)); 10 | } 11 | 12 | void newline(void) 13 | { 14 | putchar('\n'); 15 | } 16 | 17 | void separator_row(int w, char c) 18 | { 19 | int x; 20 | 21 | for( x=0; x18 ) 34 | rows= 18; 35 | 36 | c = 0; 37 | for( a=0; a 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | printf("Type a value: "); 8 | scanf("%d",&a); 9 | if( a < 10 ) 10 | { 11 | puts("You typed a value less than 10"); 12 | } 13 | puts("Thank you"); 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH04/04_01/04_01-if2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | printf("Type a value: "); 8 | scanf("%d",&a); 9 | if( a < 10 ) 10 | { 11 | puts("You typed a value less than 10"); 12 | a -= 5; 13 | printf("The value is now %d\n",a); 14 | } 15 | puts("Thank you"); 16 | 17 | return(0); 18 | } 19 | -------------------------------------------------------------------------------- /CH04/04_01/04_01-if3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 1; 6 | 7 | while(a <= 20) 8 | { 9 | if( a > 6 ) 10 | { 11 | if( a < 15 ) 12 | putchar('*'); 13 | } 14 | printf("%d\n",a); 15 | a++; 16 | } 17 | 18 | return(0); 19 | } 20 | -------------------------------------------------------------------------------- /CH04/04_01/04_01-if4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 1; 6 | 7 | while(a <= 20) 8 | { 9 | if( a > 6 && a < 15 ) 10 | putchar('*'); 11 | printf("%d\n",a); 12 | a++; 13 | } 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH04/04_02/04_02-else1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | printf("Enter value 1: "); 8 | scanf("%d",&a); 9 | printf("Enter value 2: "); 10 | scanf("%d",&b); 11 | if( a > b ) 12 | printf("%d is greater than %d\n",a,b); 13 | else 14 | printf("%d is greater than %d\n",b,a); 15 | 16 | return(0); 17 | } 18 | -------------------------------------------------------------------------------- /CH04/04_02/04_02-elseif1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | printf("Enter value 1: "); 8 | scanf("%d",&a); 9 | printf("Enter value 2: "); 10 | scanf("%d",&b); 11 | if( a > b ) 12 | printf("%d is greater than %d\n",a,b); 13 | else if( b > a ) 14 | printf("%d is greater than %d\n",b,a); 15 | else 16 | printf("%d is equal to %d\n",a,b); 17 | 18 | return(0); 19 | } 20 | -------------------------------------------------------------------------------- /CH04/04_02/04_02-elseif2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char a; 6 | 7 | printf("Type a character: "); 8 | scanf("%c",&a); 9 | if( a >= 'a' && a <= 'z' ) 10 | puts("You typed a lowercase letter"); 11 | else if( a >= 'A' && a <= 'Z' ) 12 | puts("You typed an uppercase letter"); 13 | else if( a >= '0' && a <= '9' ) 14 | puts("You typed a number"); 15 | else if( a == ' ' ) 16 | puts("You typed a space"); 17 | else 18 | printf("You typed the %c symbol\n",a); 19 | 20 | return(0); 21 | } 22 | -------------------------------------------------------------------------------- /CH04/04_03/04_03-comparison1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b,c; 6 | 7 | a = 10; 8 | b = 14; 9 | if( a > b ) 10 | c = a; 11 | else 12 | c = b; 13 | printf("%d is the greater of %d and %d\n",c,a,b); 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH04/04_03/04_03-ternary1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b,c; 6 | 7 | a = 10; 8 | b = 14; 9 | c = ( a > b ) ? a : b; 10 | printf("%d is the greater of %d and %d\n",c,a,b); 11 | 12 | return(0); 13 | } 14 | -------------------------------------------------------------------------------- /CH04/04_03/04_03-ternary2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int x; 6 | 7 | for(x=1;x<=10;x++) 8 | { 9 | printf("%d - %s\n",x,x % 2 ? "odd" : "even"); 10 | } 11 | 12 | return(0); 13 | } 14 | -------------------------------------------------------------------------------- /CH04/04_04/04_04-switchcase1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | printf("Select item 1, 2, or 3: "); 8 | scanf("%d",&a); 9 | 10 | switch(a) 11 | { 12 | case 1: 13 | puts("This is the first item"); 14 | break; 15 | case 2: 16 | puts("This is the second item"); 17 | break; 18 | case 3: 19 | puts("You chose the third item"); 20 | break; 21 | default: 22 | puts("Invalid choice"); 23 | } 24 | 25 | return(0); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /CH04/04_04/04_04-switchcase2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char a; 6 | 7 | puts("Menu"); 8 | puts("A - Bugs"); 9 | puts("B - Snake Surprise"); 10 | puts("C - Eyeball Soup"); 11 | printf("Your choice: "); 12 | scanf("%c",&a); 13 | 14 | switch(a) 15 | { 16 | case 'A': 17 | puts("Delicious! A hearty snack."); 18 | break; 19 | case 'B': 20 | puts("A family favorite!"); 21 | break; 22 | case 'C': 23 | puts("From a secret recipe."); 24 | break; 25 | default: 26 | puts("Invalid choice"); 27 | } 28 | 29 | return(0); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /CH04/04_04/04_04-switchcase3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char a; 6 | 7 | puts("Menu"); 8 | puts("A - Bugs"); 9 | puts("B - Snake Surprise"); 10 | puts("C - Eyeball Soup"); 11 | printf("Your choice: "); 12 | scanf("%c",&a); 13 | 14 | switch(a) 15 | { 16 | case 'A': 17 | case 'a': 18 | puts("Delicious! A hearty snack."); 19 | break; 20 | case 'B': 21 | case 'b': 22 | puts("A family favorite!"); 23 | break; 24 | case 'C': 25 | case 'c': 26 | puts("From a secret recipe."); 27 | break; 28 | default: 29 | puts("Invalid choice"); 30 | } 31 | 32 | return(0); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /CH04/04_05/04_05-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | printf("Select item 1, 2, or 3: "); 8 | scanf("%d",&a); 9 | 10 | if( a == 1 ) 11 | puts("This is the first item"); 12 | else if( a == 2 ) 13 | puts("This is the second item"); 14 | else if( a == 3) 15 | puts("This is the third item"); 16 | else 17 | puts("Invalid choice!"); 18 | 19 | return(0); 20 | } 21 | -------------------------------------------------------------------------------- /CH04/04_06/04_06-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | printf("Select item 1, 2, or 3: "); 8 | scanf("%d",&a); 9 | 10 | switch(a) 11 | { 12 | case 1: 13 | puts("This is the first item"); 14 | break; 15 | case 2: 16 | puts("This is the second item"); 17 | break; 18 | case 3: 19 | puts("This is the third item"); 20 | break; 21 | default: 22 | puts("Invalid choice!"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /CH04/04_07/04_07-for1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | for( a = 0 ; a < 10 ; a++ ) 8 | { 9 | puts("I must do this 10 times"); 10 | } 11 | 12 | return(0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /CH04/04_07/04_07-for2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | printf("Type a positive value: "); 8 | scanf("%d",&b); 9 | for( a=0 ; a 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | for( a=1, b=10 ; a<=10 ; a++, b-- ) 8 | printf("%2d %2d\n",a,b); 9 | 10 | return(0); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /CH04/04_07/04_07-for4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | for(;;) 6 | { 7 | printf("I am eternal "); 8 | } 9 | 10 | return(0); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /CH04/04_08/04_08-dowhile1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | printf("Enter a value: "); 8 | scanf("%d",&a); 9 | 10 | do 11 | { 12 | puts("Here we go!"); 13 | a--; 14 | } 15 | while(a > 0); 16 | 17 | return(0); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /CH04/04_08/04_08-while1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | a = 0; 8 | while( a < 10 ) 9 | { 10 | puts("I must do this 10 times"); 11 | a++; 12 | } 13 | 14 | return(0); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /CH04/04_08/04_08-while2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int ch; 6 | 7 | printf("Type '!' to exit: "); 8 | while(1) 9 | { 10 | ch = getchar(); 11 | if( ch=='!' ) 12 | break; 13 | } 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH04/04_09/04_09-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | printf("How long is the line? "); 8 | scanf("%d",&a); 9 | 10 | /* write the loop here */ 11 | 12 | return(0); 13 | } 14 | -------------------------------------------------------------------------------- /CH04/04_10/04_10-solution-for1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | printf("How long is the line? "); 8 | scanf("%d",&a); 9 | 10 | for( ; a>0 ; a--) 11 | putchar('-'); 12 | putchar('\n'); 13 | 14 | return(0); 15 | } 16 | -------------------------------------------------------------------------------- /CH04/04_10/04_10-solution-while1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | printf("How long is the line? "); 8 | scanf("%d",&a); 9 | 10 | while(a>0) 11 | { 12 | putchar('-'); 13 | a--; 14 | } 15 | putchar('\n'); 16 | 17 | return(0); 18 | } 19 | -------------------------------------------------------------------------------- /CH04/04_11/04_11-nested1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int row; 6 | char column; 7 | 8 | for(row=1;row<=10;row++) 9 | { 10 | for(column='A';column<='J';column++) 11 | { 12 | printf("%2d%c ",row,column); 13 | } 14 | putchar('\n'); 15 | } 16 | 17 | return(0); 18 | } 19 | -------------------------------------------------------------------------------- /CH04/04_11/04_11-nested2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int row; 6 | char column; 7 | 8 | row = 1; 9 | while(row<=10) 10 | { 11 | column = 'A'; 12 | while(column<='J') 13 | { 14 | printf("%2d%c ",row,column); 15 | column++; 16 | } 17 | putchar('\n'); 18 | row++; 19 | } 20 | 21 | return(0); 22 | } 23 | -------------------------------------------------------------------------------- /CH04/04_12/04_12-break1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | printf("Type a positive value: "); 8 | scanf("%d",&b); 9 | for(a=0;a 2 | 3 | int main() 4 | { 5 | int row; 6 | char column; 7 | 8 | for(row=1;row<=10;row++) 9 | { 10 | for(column='A';column<='J';column++) 11 | { 12 | printf("%2d%c ",row,column); 13 | if(column=='E') 14 | break; 15 | } 16 | putchar('\n'); 17 | } 18 | 19 | return(0); 20 | } 21 | -------------------------------------------------------------------------------- /CH04/04_12/04_12-break3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int ch; 6 | 7 | printf("Type '!' to end:"); 8 | while(1) 9 | { 10 | ch = getchar(); 11 | if( ch == '!') 12 | break; 13 | } 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH04/04_12/04_12-continue1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | a = 0; 8 | while(1) 9 | { 10 | a++; 11 | printf("%d\n",a); 12 | if( a < 10 ) 13 | continue; 14 | a++; 15 | if( a > 20 ) 16 | break; 17 | } 18 | 19 | return(0); 20 | } 21 | -------------------------------------------------------------------------------- /CH04/04_13/04_13-goto1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 10; 6 | 7 | here: 8 | printf("%d\n",a); 9 | a--; 10 | if( a > 0 ) 11 | goto here; 12 | 13 | return(0); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /CH04/04_13/04_13-goto2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 10; 6 | 7 | here: 8 | printf("%d\n",a); 9 | a--; 10 | if( a == 4 ) 11 | goto there; 12 | if( a > 0 ) 13 | goto here; 14 | there: 15 | 16 | return(0); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /CH04/04_14/04_14-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define FALSE 0 5 | #define TRUE 1 6 | 7 | int main() 8 | { 9 | // variable declarations 10 | 11 | /* main program loop */ 12 | done = FALSE; 13 | while(!done) 14 | { 15 | // prompt for input 16 | // process input 17 | // if R is input, output "move right" 18 | // if L is input, output "move left" 19 | // if B is input, output "move back" 20 | // if Q is input, break the loop 21 | // if H or ? is input, output valid commands 22 | // identify invalid input 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /CH04/04_15/04_15-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define FALSE 0 5 | #define TRUE 1 6 | 7 | int main() 8 | { 9 | int done,ch; 10 | 11 | /* main program loop */ 12 | done = FALSE; 13 | while(!done) 14 | { 15 | printf("Command: "); 16 | ch = getchar(); 17 | switch( ch ) 18 | { 19 | case 'R': 20 | case 'r': 21 | puts("Move right"); 22 | break; 23 | case 'L': 24 | case 'l': 25 | puts("Move left"); 26 | break; 27 | case 'B': 28 | case 'b': 29 | puts("Move back"); 30 | break; 31 | case 'Q': 32 | case 'q': 33 | done = TRUE; 34 | puts("Goodbye!"); 35 | break; 36 | case 'H': 37 | case 'h': 38 | case '?': 39 | puts("You may go (R)ight, (L)eft, or (B)ack"); 40 | puts("Or you can (Q)uit"); 41 | break; 42 | default: 43 | puts("I don't know that command"); 44 | } 45 | while( (ch=getchar()) !='\n' ) 46 | ; 47 | } 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /CH05/05_01/05_01-arguments1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void repeat(int r) 4 | { 5 | int a; 6 | 7 | for( a=0; a 2 | 3 | int total(int a, int b, int c, int d, int e) 4 | { 5 | return a+b+c+d+e; 6 | } 7 | 8 | char *title(void) 9 | { 10 | return("Here is the total:"); 11 | } 12 | 13 | int main() 14 | { 15 | printf("%s %d\n", 16 | title(), 17 | total(1,2,3,4,5) 18 | ); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /CH05/05_01/05_01-functypes1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char valchar(void) 4 | { 5 | return 'A'; 6 | } 7 | 8 | int valint(void) 9 | { 10 | return 7; 11 | } 12 | 13 | float valfloat(void) 14 | { 15 | return 3.141; 16 | } 17 | 18 | double valdouble(void) 19 | { 20 | return 6.023e23; 21 | } 22 | 23 | void valvoid(void) 24 | { 25 | puts("void function returns nothing!"); 26 | } 27 | 28 | int main() 29 | { 30 | printf("char function returns %c\n",valchar() ); 31 | printf("int function returns %d\n",valint() ); 32 | printf("float function returns %f\n",valfloat() ); 33 | printf("double function returns %f\n",valdouble() ); 34 | valvoid(); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /CH05/05_01/05_01-main1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("I am the main() function\n"); 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /CH05/05_02/05_02-function1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void sum(void); 4 | 5 | int main() 6 | { 7 | puts("Calling the sum() function:"); 8 | sum(); 9 | puts("Done"); 10 | 11 | return(0); 12 | } 13 | 14 | void sum(void) 15 | { 16 | int a,b,sum; 17 | 18 | a = 91; b = 7; 19 | sum = a + b; 20 | printf("%d + %d = %d\n",a,b,sum); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /CH05/05_02/05_02-function2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void sum(void) 4 | { 5 | int a,b,sum; 6 | 7 | a = 91; b = 7; 8 | sum = a + b; 9 | printf("%d + %d = %d\n",a,b,sum); 10 | } 11 | 12 | int main() 13 | { 14 | puts("Calling the sum() function:"); 15 | sum(); 16 | puts("Done"); 17 | 18 | return(0); 19 | } 20 | -------------------------------------------------------------------------------- /CH05/05_02/05_02-function3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | puts("Carefully read these directions!"); 6 | printf("Press any key to continue: "); 7 | getchar(); 8 | puts("Are you sure you read the directions?"); 9 | printf("Press any key to continue: "); 10 | getchar(); 11 | puts("Okay. I'm sure you're sure."); 12 | printf("Press any key to continue: "); 13 | getchar(); 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH05/05_02/05_02-function4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void pressAny(void) 4 | { 5 | printf("Press any key to continue: "); 6 | getchar(); 7 | } 8 | 9 | int main() 10 | { 11 | puts("Carefully read these directions!"); 12 | pressAny(); 13 | puts("Are you sure you read the directions?"); 14 | pressAny(); 15 | puts("Okay. I'm sure you're sure."); 16 | pressAny(); 17 | 18 | return(0); 19 | } 20 | -------------------------------------------------------------------------------- /CH05/05_03/05_03-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | puts("How to Fight Off a Robot Attack"); 6 | line(); 7 | puts("A Survival Guide for the 21st Century"); 8 | line(); 9 | 10 | return(0); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /CH05/05_04/05_04-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void line(void) 4 | { 5 | int a; 6 | 7 | for(a=0;a<40;a++) 8 | putchar('-'); 9 | putchar('\n'); 10 | } 11 | 12 | int main() 13 | { 14 | puts("How to Fight Off a Robot Attack"); 15 | line(); 16 | puts("A Survival Guide for the 21st Century"); 17 | line(); 18 | 19 | return(0); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /CH05/05_04/05_04-solution2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void line(void); 4 | 5 | int main() 6 | { 7 | puts("How to Fight Off a Robot Attack"); 8 | line(); 9 | puts("A Survival Guide for the 21st Century"); 10 | line(); 11 | 12 | return(0); 13 | } 14 | 15 | void line(void) 16 | { 17 | int a; 18 | 19 | for(a=0;a<40;a++) 20 | putchar('-'); 21 | putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /CH05/05_04/05_04-solution3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void line(void) 4 | { 5 | puts("----------------------------------------"); 6 | } 7 | 8 | int main() 9 | { 10 | puts("How to Fight Off a Robot Attack"); 11 | line(); 12 | puts("A Survival Guide for the 21st Century"); 13 | line(); 14 | 15 | return(0); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /CH05/05_05/05_05-function1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | char randchar(void) 6 | { 7 | char r; 8 | 9 | r = rand() % 26; 10 | r += 'A'; 11 | 12 | return(r); 13 | } 14 | 15 | int main() 16 | { 17 | int a; 18 | 19 | /* seed the randomizer */ 20 | srand( (unsigned)time(NULL) ); 21 | 22 | printf("Today's random word: "); 23 | for(a=0;a<7;a++) 24 | putchar( randchar() ); 25 | putchar('\n'); 26 | 27 | return(0); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /CH05/05_05/05_05-function2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void greetings(void) 5 | { 6 | time_t now; 7 | struct tm *today; 8 | 9 | time(&now); 10 | today = localtime(&now); 11 | if( today->tm_hour > 16 ) 12 | { 13 | puts("Good Evening"); 14 | return; 15 | } 16 | puts("It's not evening"); 17 | } 18 | 19 | int main() 20 | { 21 | greetings(); 22 | 23 | return(0); 24 | } 25 | -------------------------------------------------------------------------------- /CH05/05_06/05_06-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | int r,dice1,dice2,dice3; 8 | 9 | /* seed the randomizer */ 10 | srand( (unsigned)time(NULL) ); 11 | 12 | puts("Roll them bones!"); 13 | r = rand() % 6; 14 | r += 1; 15 | dice1 = r; 16 | r = rand() % 6; 17 | r += 1; 18 | dice2 = r; 19 | r = rand() % 6; 20 | r += 1; 21 | dice3 = r; 22 | printf("You rolled %d - %d - %d\n",dice1,dice2,dice3); 23 | 24 | return(0); 25 | } 26 | -------------------------------------------------------------------------------- /CH05/05_07/05_07-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int roll(void) 6 | { 7 | int r; 8 | 9 | r = rand() % 6; 10 | r += 1; 11 | 12 | return(r); 13 | } 14 | 15 | int main() 16 | { 17 | int dice1,dice2,dice3; 18 | 19 | /* seed the randomizer */ 20 | srand( (unsigned)time(NULL) ); 21 | 22 | dice1 = roll(); 23 | dice2 = roll(); 24 | dice3 = roll(); 25 | printf("You rolled %d - %d - %d\n",dice1,dice2,dice3); 26 | 27 | return(0); 28 | } 29 | -------------------------------------------------------------------------------- /CH05/05_08/05_08-function1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void repeat(int count) 4 | { 5 | int x; 6 | 7 | for(x=0;x 2 | 3 | float average(float a, float b, float c) 4 | { 5 | float avg; 6 | 7 | avg = ( a + b + c ) / 3; 8 | 9 | return(avg); 10 | } 11 | 12 | int main() 13 | { 14 | float a; 15 | 16 | a = average( 29.0, 37.0, 30.2 ); 17 | printf("The average of 29.0, 37.0 and 30.2 is %.2f\n",a); 18 | 19 | return(0); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /CH05/05_08/05_08-function3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float average(float a, float b, float c, int count) 4 | { 5 | float avg; 6 | 7 | avg = ( a + b + c ) / count; 8 | 9 | return(avg); 10 | } 11 | 12 | int main() 13 | { 14 | float a; 15 | 16 | a = average( 29.0, 37.0, 30.2, 3 ); 17 | printf("The average of 29.0, 37.0 and 30.2 is %.2f\n",a); 18 | 19 | return(0); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /CH05/05_09/05_09-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | puts("Report on Extraterrestrial Activity"); 6 | line(35,'-'); 7 | 8 | return(0); 9 | } 10 | -------------------------------------------------------------------------------- /CH05/05_10/05_10-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void line(int length, char c) 4 | { 5 | for( ; length>0; length--) 6 | putchar(c); 7 | putchar('\n'); 8 | } 9 | 10 | int main() 11 | { 12 | puts("Report on Extraterrestrial Activity"); 13 | line(35,'-'); 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH05/05_11/05_11-arguments1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char *argv[]) 4 | { 5 | int x; 6 | 7 | printf("%d arguments found:\n",argc); 8 | for( x=0; x 2 | 3 | void funct(void) 4 | { 5 | int a = 16; 6 | 7 | printf("In funct(), A = %d\n",a); 8 | } 9 | 10 | int main() 11 | { 12 | int a = 10; 13 | 14 | printf("In main(), A = %d\n",a); 15 | funct(); 16 | printf("In main(), A = %d\n",a); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /CH05/05_12/05_12-function2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int funct(void) 4 | { 5 | int a = 0; 6 | 7 | a += 16; 8 | printf("In funct(), A = %d\n",a); 9 | 10 | return(a); 11 | } 12 | 13 | int main() 14 | { 15 | int a; 16 | 17 | a = funct(); 18 | printf("In main(), A = %d\n",a); 19 | a = funct(); 20 | printf("In main(), A = %d\n",a); 21 | a = funct(); 22 | printf("In main(), A = %d\n",a); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /CH05/05_13/05_13-recursive1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void recurse(int a) 4 | { 5 | a++; 6 | printf("%d\n",a); 7 | recurse(a); 8 | } 9 | 10 | int main() 11 | { 12 | recurse(0); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /CH05/05_13/05_13-recursive2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void recurse(int a) 4 | { 5 | a++; 6 | if( a>10 ) 7 | return; 8 | printf("%d\n",a); 9 | recurse(a); 10 | } 11 | 12 | int main() 13 | { 14 | recurse(0); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /CH05/05_13/05_13-recursive3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void recurse(int v) 4 | { 5 | int a; 6 | 7 | for(a=0;a Wind-up!"); 10 | if( v == 10 ) 11 | return; 12 | recurse(v+1); 13 | for(a=0;a 2 | 3 | int factorial(int f) 4 | { 5 | if( f == 1 ) 6 | return(f); 7 | return( f * factorial( f-1 ) ); 8 | } 9 | 10 | int main() 11 | { 12 | int a,b; 13 | 14 | printf("Enter a positive value: "); 15 | scanf("%d",&a); 16 | if( a < 1 ) 17 | { 18 | printf("%d is not a positive value\n",a); 19 | return(1); 20 | } 21 | b = factorial(a); 22 | printf("The factorial of %d! is %d\n",a,b); 23 | 24 | return(0); 25 | } 26 | -------------------------------------------------------------------------------- /CH05/05_14/05_14_challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b; 6 | 7 | printf("Enter two values, separated by a space: "); 8 | scanf("%d",&a); 9 | scanf("%d",&b); 10 | 11 | add(a,b); 12 | subtract(a,b); 13 | multiply(a,b); 14 | printf("%d / %d = %f\n",a,b,divide(a,b)); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /CH05/05_15/05_15-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void add(int x, int y) 4 | { 5 | printf("%d + %d = %d\n",x,y,x+y); 6 | } 7 | 8 | void subtract(int x, int y) 9 | { 10 | printf("%d - %d = %d\n",x,y,x-y); 11 | } 12 | 13 | void multiply(int x, int y) 14 | { 15 | printf("%d * %d = %d\n",x,y,x*y); 16 | } 17 | 18 | float divide(int x, int y) 19 | { 20 | return((float)x/y); 21 | } 22 | 23 | int main() 24 | { 25 | int a,b; 26 | 27 | printf("Enter two values, separated by a space: "); 28 | scanf("%d",&a); 29 | scanf("%d",&b); 30 | 31 | add(a,b); 32 | subtract(a,b); 33 | multiply(a,b); 34 | printf("%d / %d = %f\n",a,b,divide(a,b)); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /CH06/06_01/06_01-highscores1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float highscore1,highscore2,highscore3,highscore4,highscore5; 6 | 7 | highscore1 = 993.86; 8 | highscore2 = 682.01; 9 | highscore3 = 639.60; 10 | highscore4 = 310.45; 11 | highscore5 = 123.25; 12 | 13 | puts("Here are the high scores:"); 14 | printf("#1 %.2f\n",highscore1); 15 | printf("#2 %.2f\n",highscore2); 16 | printf("#3 %.2f\n",highscore3); 17 | printf("#4 %.2f\n",highscore4); 18 | printf("#5 %.2f\n",highscore5); 19 | 20 | return(0); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /CH06/06_02/06_02-highscores1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float highscore[5]; 6 | 7 | highscore[0] = 993.86; 8 | highscore[1] = 682.01; 9 | highscore[2] = 639.60; 10 | highscore[3] = 310.45; 11 | highscore[4] = 123.25; 12 | 13 | puts("Here are the high scores:"); 14 | printf("#1 %.2f\n",highscore[0]); 15 | printf("#2 %.2f\n",highscore[1]); 16 | printf("#3 %.2f\n",highscore[2]); 17 | printf("#4 %.2f\n",highscore[3]); 18 | printf("#5 %.2f\n",highscore[4]); 19 | 20 | return(0); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /CH06/06_02/06_02-highscores2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float highscore[5]; 6 | int x; 7 | 8 | highscore[0] = 993.86; 9 | highscore[1] = 682.01; 10 | highscore[2] = 639.60; 11 | highscore[3] = 310.45; 12 | highscore[4] = 123.25; 13 | 14 | puts("Here are the high scores:"); 15 | for(x=0;x<5;x++) 16 | printf("#%d %.2f\n",x+1,highscore[x]); 17 | 18 | return(0); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /CH06/06_02/06_02-highscores3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float highscore[5] = { 993.86, 682.01, 639.60, 310.45, 123.25 }; 6 | int x; 7 | 8 | puts("Here are the high scores:"); 9 | for(x=0;x<5;x++) 10 | printf("#%d %.2f\n",x+1,highscore[x]); 11 | 12 | return(0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /CH06/06_02/06_02-highscores4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float highscore[5]; 6 | int x; 7 | 8 | for(x=0;x<5;x++) 9 | { 10 | printf("High score #%d: ",x+1); 11 | scanf("%f",&highscore[x]); 12 | } 13 | 14 | puts("Here are the high scores:"); 15 | for(x=0;x<5;x++) 16 | printf("#%d %.2f\n",x+1,highscore[x]); 17 | 18 | return(0); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /CH06/06_03/06_03-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /CH06/06_04/06_04-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int age[5]; 6 | int x; 7 | 8 | age[0] = 65; 9 | age[1] = 48; 10 | age[2] = 31; 11 | age[3] = 12; 12 | age[4] = 1; 13 | 14 | for( x=0; x<5; x++ ) 15 | printf("I know someone who is %d years old\n",age[x]); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /CH06/06_04/06_04-solution2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int age[] = { 65, 48, 31, 12, 1 }; 6 | int x; 7 | 8 | for( x=0; x<5; x++ ) 9 | printf("I know someone who is %d years old\n",age[x]); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /CH06/06_05/06_05-passarray1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void incshow(char array[]) 4 | { 5 | int x; 6 | 7 | for(x=0;x<6;x++) 8 | { 9 | array[x]++; 10 | putchar(array[x]); 11 | } 12 | } 13 | 14 | int main() 15 | { 16 | char text[] = "Gdkkn "; 17 | 18 | incshow(text); 19 | putchar('\n'); 20 | 21 | return(0); 22 | } 23 | -------------------------------------------------------------------------------- /CH06/06_05/06_05-passelement1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void incshow(char a) 4 | { 5 | a++; 6 | putchar(a); 7 | } 8 | 9 | int main() 10 | { 11 | char text[] = "Gdkkn "; 12 | int x; 13 | 14 | for(x=0;x<6;x++) 15 | incshow(text[x]); 16 | putchar('\n'); 17 | 18 | return(0); 19 | } 20 | -------------------------------------------------------------------------------- /CH06/06_05/06_05-returnarray1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int makearray(void) 6 | { 7 | int array[5]; 8 | int x; 9 | 10 | for(x=0;x<5;x++) 11 | array[x] = rand() % 10 + 1; 12 | 13 | return(array); 14 | } 15 | 16 | int main() 17 | { 18 | int x,r[5]; 19 | 20 | srand( (unsigned)time(NULL) ); 21 | puts("Here are your 5 random numbers:"); 22 | r = makearray(); 23 | for(x=0;x<5;x++) 24 | printf("%d\n",r[x]); 25 | 26 | return(0); 27 | } 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /CH06/06_06/06_06-grid1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int grid[5][2]; 6 | int x; 7 | 8 | for(x=0;x<5;x++) 9 | { 10 | grid[x][0] = x+1; 11 | grid[x][1] = grid[x][0] * 10; 12 | } 13 | 14 | for(x=0;x<5;x++) 15 | printf("%d,%d\n",grid[x][0],grid[x][1]); 16 | 17 | return(0); 18 | } 19 | -------------------------------------------------------------------------------- /CH06/06_06/06_06-grid2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char tictactoe[3][3]; 6 | int x,y; 7 | 8 | /* initialize */ 9 | for(x=0;x<3;x++) 10 | for(y=0;y<3;y++) 11 | tictactoe[x][y] = '.'; 12 | 13 | /* display */ 14 | puts("Tic Tac Toe"); 15 | for(x=0;x<3;x++) 16 | { 17 | for(y=0;y<3;y++) 18 | printf(" %c ",tictactoe[x][y]); 19 | putchar('\n'); 20 | } 21 | 22 | return(0); 23 | } 24 | -------------------------------------------------------------------------------- /CH06/06_06/06_06-grid3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char tictactoe[3][3][3]; 6 | int x,y,z; 7 | 8 | /* initialize */ 9 | for(x=0;x<3;x++) 10 | for(y=0;y<3;y++) 11 | for(z=0;z<3;z++) 12 | tictactoe[x][y][z] = '.'; 13 | 14 | /* display */ 15 | puts("3D Tic Tac Toe"); 16 | for(x=0;x<3;x++) 17 | { 18 | printf("%*c",x+1,' '); /* The * is a placeholder for the width */ 19 | for(y=0;y<3;y++) 20 | { 21 | for(z=0;z<3;z++) 22 | printf(" %c ",tictactoe[x][y][z]); 23 | printf(" "); /* 5 spaces */ 24 | } 25 | putchar('\n'); 26 | } 27 | 28 | return(0); 29 | } 30 | -------------------------------------------------------------------------------- /CH06/06_06/06_06-grid4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char tictactoe[27]; 6 | int x,y,z; 7 | 8 | /* initialize */ 9 | for(x=0; x<27; x++ ) 10 | tictactoe[x] = '.'; 11 | 12 | /* display */ 13 | puts("3D Tic Tac Toe"); 14 | for(x=0;x<3;x++) 15 | { 16 | printf("%*c",x+1,' '); 17 | for(y=0;y<3;y++) 18 | { 19 | for(z=0;z<3;z++) 20 | printf(" %c ",tictactoe[x*9+y*3+z]); /* math required to accurately plot out elements */ 21 | printf(" "); /* 5 spaces */ 22 | } 23 | putchar('\n'); 24 | } 25 | 26 | return(0); 27 | } 28 | -------------------------------------------------------------------------------- /CH06/06_07/06_07-structure1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | struct pixel { 6 | int horz; 7 | int vert; 8 | char color; 9 | }; 10 | struct pixel center; 11 | 12 | center.horz = 320; 13 | center.vert = 240; 14 | center.color = 'r'; 15 | 16 | printf("The pixel at %d,%d is colored ", 17 | center.horz, 18 | center.vert 19 | ); 20 | switch(center.color) 21 | { 22 | case 'r': 23 | puts("red"); 24 | break; 25 | case 'g': 26 | puts("green"); 27 | break; 28 | case 'b': 29 | puts("blue"); 30 | break; 31 | default: 32 | puts("Invalid"); 33 | } 34 | 35 | return(0); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /CH06/06_07/06_07-structure2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | struct pixel { 6 | int horz; 7 | int vert; 8 | char color; 9 | } center; 10 | 11 | center.horz = 320; 12 | center.vert = 240; 13 | center.color = 'r'; 14 | 15 | printf("The pixel at %d,%d is colored ", 16 | center.horz, 17 | center.vert 18 | ); 19 | switch(center.color) 20 | { 21 | case 'r': 22 | puts("red"); 23 | break; 24 | case 'g': 25 | puts("green"); 26 | break; 27 | case 'b': 28 | puts("blue"); 29 | break; 30 | default: 31 | puts("Invalid"); 32 | } 33 | 34 | return(0); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /CH06/06_07/06_07-structure3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | struct pixel { 6 | int horz; 7 | int vert; 8 | char color; 9 | } center = { 320, 240, 'r' }; 10 | 11 | printf("The pixel at %d,%d is colored ", 12 | center.horz, 13 | center.vert 14 | ); 15 | switch(center.color) 16 | { 17 | case 'r': 18 | puts("red"); 19 | break; 20 | case 'g': 21 | puts("green"); 22 | break; 23 | case 'b': 24 | puts("blue"); 25 | break; 26 | default: 27 | puts("Invalid"); 28 | } 29 | 30 | return(0); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /CH06/06_08/06_08-nested1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | struct date { 7 | int month; 8 | int day; 9 | int year; 10 | }; 11 | struct person { 12 | struct date birthday; 13 | char name[32]; 14 | }; 15 | struct person president; 16 | 17 | president.birthday.month = 2; 18 | president.birthday.day = 22; 19 | president.birthday.year = 1732; 20 | strcpy(president.name,"George Washington"); 21 | 22 | printf("%s was born on %d/%d/%d\n", 23 | president.name, 24 | president.birthday.month, 25 | president.birthday.day, 26 | president.birthday.year 27 | ); 28 | 29 | return(0); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /CH06/06_08/06_08-nested2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | struct date { 6 | int month; 7 | int day; 8 | int year; 9 | }; 10 | struct person { 11 | struct date birthday; 12 | char name[32]; 13 | } president = { 14 | { 2, 22, 1732 }, 15 | "George Washington" 16 | }; 17 | 18 | printf("%s was born on %d/%d/%d\n", 19 | president.name, 20 | president.birthday.month, 21 | president.birthday.day, 22 | president.birthday.year 23 | ); 24 | 25 | return(0); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /CH06/06_08/06_08-structinit1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | struct date { 6 | int month; 7 | int day; 8 | int year; 9 | }; 10 | struct date birthday; 11 | 12 | birthday.month = 2; 13 | birthday.day = 22; 14 | birthday.year = 1732; 15 | 16 | printf("George Watshington was born on %d/%d/%d\n", 17 | birthday.month, 18 | birthday.day, 19 | birthday.year 20 | ); 21 | 22 | return(0); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /CH06/06_08/06_08-structinit2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | struct date { 6 | int month; 7 | int day; 8 | int year; 9 | } birthday = { 2, 22, 1732 }; 10 | 11 | printf("George Watshington was born on %d/%d/%d\n", 12 | birthday.month, 13 | birthday.day, 14 | birthday.year 15 | ); 16 | 17 | return(0); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /CH06/06_09/06_09-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /CH06/06_10/06_10-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | struct date { 7 | int month; 8 | int day; 9 | int year; 10 | }; 11 | struct person { 12 | struct date birthday; 13 | float height; 14 | char name[32]; 15 | }; 16 | struct person me; 17 | 18 | me.birthday.month = 10; 19 | me.birthday.day = 19; 20 | me.birthday.year = 1960; 21 | me.height = 0.001829; 22 | strcpy(me.name,"Dan Gookin"); 23 | 24 | printf("%s was born on %d/%d/%d and is %f kilometers tall\n", 25 | me.name, 26 | me.birthday.month, 27 | me.birthday.day, 28 | me.birthday.year, 29 | me.height 30 | ); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /CH06/06_10/06_10-solution2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | struct date { 6 | int month; 7 | int day; 8 | int year; 9 | }; 10 | struct person { 11 | struct date birthday; 12 | float height; 13 | char name[32]; 14 | }; 15 | struct person me = { 16 | { 10, 19, 1960 }, 17 | 0.01829, 18 | "Dan Gookin" 19 | }; 20 | 21 | printf("%s was born on %d/%d/%d and is %f kilometers tall\n", 22 | me.name, 23 | me.birthday.month, 24 | me.birthday.day, 25 | me.birthday.year, 26 | me.height 27 | ); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /CH06/06_11/06_11-structarray1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | struct pixel { 6 | int horz; 7 | int vert; 8 | char color; 9 | }; 10 | struct pixel box[4]; 11 | int x; 12 | 13 | box[0].horz = 100; 14 | box[0].vert = 50; 15 | box[0].color = 'r'; 16 | box[1].horz = 200; 17 | box[1].vert = 50; 18 | box[1].color = 'r'; 19 | box[2].horz = 100; 20 | box[2].vert = 100; 21 | box[2].color = 'r'; 22 | box[3].horz = 200; 23 | box[3].vert = 100; 24 | box[3].color = 'r'; 25 | 26 | for(x=0;x<4;x++) 27 | { 28 | printf("Pixel %d: %d, %d = %c\n", 29 | x+1, 30 | box[x].horz, 31 | box[x].vert, 32 | box[x].color 33 | ); 34 | } 35 | 36 | return(0); 37 | } 38 | -------------------------------------------------------------------------------- /CH06/06_11/06_11-structarray2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | struct pixel { 6 | int horz; 7 | int vert; 8 | char color; 9 | }; 10 | struct pixel box[4] = { 11 | { 100, 50, 'r' }, 12 | { 200, 50, 'r' }, 13 | { 100, 100, 'r' }, 14 | { 200, 100, 'r' } 15 | }; 16 | int x; 17 | 18 | for(x=0;x<4;x++) 19 | { 20 | printf("Pixel %d: %d, %d = %c\n", 21 | x+1, 22 | box[x].horz, 23 | box[x].vert, 24 | box[x].color 25 | ); 26 | } 27 | 28 | return(0); 29 | } 30 | -------------------------------------------------------------------------------- /CH06/06_11/06_11-structarray3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | struct pixel { 6 | int horz; 7 | int vert; 8 | char color; 9 | } box[4] = { 10 | { 100, 50, 'r' }, 11 | { 200, 50, 'r' }, 12 | { 100, 100, 'r' }, 13 | { 200, 100, 'r' } 14 | }; 15 | int x; 16 | 17 | for(x=0;x<4;x++) 18 | { 19 | printf("Pixel %d: %d, %d = %c\n", 20 | x+1, 21 | box[x].horz, 22 | box[x].vert, 23 | box[x].color 24 | ); 25 | } 26 | 27 | return(0); 28 | } 29 | -------------------------------------------------------------------------------- /CH06/06_12/06_12-passall1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void show_pixel(struct pixel p) 4 | { 5 | printf("Pixel found at %d,%d, color = ", 6 | p.horz, 7 | p.vert 8 | ); 9 | switch(p.color) 10 | { 11 | case 'r': 12 | puts("red"); 13 | break; 14 | case 'g': 15 | puts("green"); 16 | break; 17 | case 'b': 18 | puts("blue"); 19 | break; 20 | default: 21 | puts("Invalid"); 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | struct pixel { 28 | int horz; 29 | int vert; 30 | char color; 31 | } center = { 320, 240, 'r' }; 32 | 33 | show_pixel(center); 34 | 35 | return(0); 36 | } 37 | -------------------------------------------------------------------------------- /CH06/06_12/06_12-passall2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct pixel { 4 | int horz; 5 | int vert; 6 | char color; 7 | }; 8 | 9 | void show_pixel(struct pixel p) 10 | { 11 | printf("Pixel found at %d,%d, color = ", 12 | p.horz, 13 | p.vert 14 | ); 15 | switch(p.color) 16 | { 17 | case 'r': 18 | puts("red"); 19 | break; 20 | case 'g': 21 | puts("green"); 22 | break; 23 | case 'b': 24 | puts("blue"); 25 | break; 26 | default: 27 | puts("Invalid"); 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | struct pixel center = { 320, 240, 'r' }; 34 | 35 | show_pixel(center); 36 | 37 | return(0); 38 | } 39 | -------------------------------------------------------------------------------- /CH06/06_12/06_12-passmember1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char *show_color(char c) 4 | { 5 | switch(c) 6 | { 7 | case 'r': 8 | return("red"); 9 | case 'g': 10 | return("green"); 11 | case 'b': 12 | return("blue"); 13 | default: 14 | return("Invalid"); 15 | } 16 | } 17 | 18 | int main() 19 | { 20 | struct pixel { 21 | int horz; 22 | int vert; 23 | char color; 24 | } center = { 320, 240, 'r' }; 25 | 26 | printf("The pixel at %d,%d is colored %s\n", 27 | center.horz, 28 | center.vert, 29 | show_color(center.color) 30 | ); 31 | 32 | return(0); 33 | } 34 | -------------------------------------------------------------------------------- /CH06/06_12/06_12-returnstruct1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct pixel { 4 | int horz; 5 | int vert; 6 | char color; 7 | }; 8 | 9 | struct pixel screenmid(int h, int v) 10 | { 11 | struct pixel c; 12 | 13 | c.horz = h/2; 14 | c.vert = v/2; 15 | c.color = 'r'; 16 | 17 | return(c); 18 | } 19 | 20 | int main() 21 | { 22 | struct pixel midscreen; 23 | 24 | midscreen = screenmid(640,480); 25 | printf("The center pixel is found at %d,%d\n", 26 | midscreen.horz, 27 | midscreen.vert 28 | ); 29 | 30 | return(0); 31 | } 32 | -------------------------------------------------------------------------------- /CH06/06_13/06_13-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define SIZE 32 5 | 6 | struct person fill(void); 7 | void output(struct person y); 8 | 9 | struct person { 10 | char name[SIZE]; 11 | int id; 12 | }; 13 | 14 | /* main program */ 15 | int main() 16 | { 17 | struct person you; 18 | 19 | /* fill the structure */ 20 | you = fill(); 21 | 22 | /* output the structure */ 23 | output(you); 24 | 25 | return 0; 26 | } 27 | 28 | struct person fill(void) 29 | { 30 | } 31 | 32 | void output(struct person y) 33 | { 34 | } 35 | -------------------------------------------------------------------------------- /CH06/06_14/06_14-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define SIZE 32 5 | 6 | struct person fill(void); 7 | void output(struct person y); 8 | 9 | struct person { 10 | char name[SIZE]; 11 | int id; 12 | }; 13 | 14 | /* main program */ 15 | int main() 16 | { 17 | struct person you; 18 | 19 | /* fill the structure */ 20 | you = fill(); 21 | 22 | /* output the structure */ 23 | output(you); 24 | 25 | return 0; 26 | } 27 | 28 | struct person fill(void) 29 | { 30 | static struct person y; 31 | 32 | printf("Your name: "); 33 | fgets(y.name,SIZE,stdin); 34 | printf("Your id: "); 35 | scanf("%d",&y.id); 36 | 37 | return(y); 38 | } 39 | 40 | void output(struct person y) 41 | { 42 | printf("ID %d belongs to %s\n", 43 | y.id, 44 | y.name 45 | ); 46 | } 47 | -------------------------------------------------------------------------------- /CH07/07_01/07_01-getchar1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int ch; 6 | 7 | printf("Type a character: "); 8 | ch = getchar(); 9 | printf("Character '%c' received.\n",ch); 10 | 11 | return(0); 12 | } 13 | -------------------------------------------------------------------------------- /CH07/07_01/07_01-getchar2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int ch1,ch2; 6 | 7 | printf("Type a character: "); 8 | ch1 = getchar(); 9 | printf("Type a character: "); 10 | ch2 = getchar(); 11 | printf("Characters '%c' and '%c' received.\n",ch1,ch2); 12 | 13 | return(0); 14 | } 15 | -------------------------------------------------------------------------------- /CH07/07_01/07_01-putchar1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | putchar('H'); 6 | putchar('e'); 7 | putchar('l'); 8 | putchar('l'); 9 | putchar('o'); 10 | putchar('\n'); 11 | 12 | return(0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /CH07/07_01/07_01-typewriter1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a; 6 | 7 | while(1) 8 | { 9 | a = getchar(); 10 | if(a == EOF) 11 | break; 12 | putchar(a); 13 | }; 14 | 15 | return(0); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /CH07/07_02/07_02-case1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char string[] = "My aunt types her email in ALL CAPS.\n"; 7 | int ch; 8 | int x = 0; 9 | 10 | while( (ch = string[x]) != '\0' ) 11 | { 12 | putchar( tolower(ch) ); 13 | x++; 14 | } 15 | 16 | return(0); 17 | } 18 | -------------------------------------------------------------------------------- /CH07/07_02/07_02-case2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char string[] = "My aunt types her email in ALL CAPS.\n"; 7 | int ch; 8 | int x = 0; 9 | 10 | while( (ch = string[x]) != '\0' ) 11 | { 12 | putchar( toupper(ch) ); 13 | x++; 14 | } 15 | 16 | return(0); 17 | } 18 | -------------------------------------------------------------------------------- /CH07/07_02/07_02-testchar1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char string[] = "Nifty TEXT! 123\n"; 7 | char ch; 8 | int x = 0; 9 | 10 | while( (ch = string[x]) != '\0' ) 11 | { 12 | if( isalpha(ch) ) 13 | putchar('_'); 14 | else 15 | putchar(ch); 16 | x++; 17 | } 18 | 19 | return(0); 20 | } 21 | -------------------------------------------------------------------------------- /CH07/07_02/07_02-testchar2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char string[] = "Nifty TEXT! 123\n"; 7 | char ch; 8 | int x = 0; 9 | 10 | while( (ch = string[x]) != '\0' ) 11 | { 12 | if( isdigit(ch) ) 13 | putchar('_'); 14 | else 15 | putchar(ch); 16 | x++; 17 | } 18 | 19 | return(0); 20 | } 21 | -------------------------------------------------------------------------------- /CH07/07_02/07_02-testchar3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char string[] = "Nifty TEXT! 123\n"; 7 | char ch; 8 | int x = 0; 9 | 10 | while( (ch = string[x]) != '\0' ) 11 | { 12 | if( isupper(ch) ) 13 | putchar('_'); 14 | else 15 | putchar(ch); 16 | x++; 17 | } 18 | 19 | return(0); 20 | } 21 | -------------------------------------------------------------------------------- /CH07/07_03/07_03-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char string[] = "Nifty TEXT! 123\n"; 6 | 7 | return(0); 8 | } 9 | -------------------------------------------------------------------------------- /CH07/07_04/07_04-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char string[] = "Nifty TEXT! 123\n"; 7 | int ch; 8 | int x = 0; 9 | 10 | while( (ch = string[x]) != '\0' ) 11 | { 12 | if( isupper(ch) ) 13 | putchar( tolower(ch) ); 14 | else if( islower(ch) ) 15 | putchar( toupper(ch) ); 16 | else 17 | putchar(ch); 18 | x++; 19 | } 20 | 21 | return(0); 22 | } 23 | -------------------------------------------------------------------------------- /CH07/07_06/07_06-yourname1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char input[10]; 6 | 7 | printf("Your name? "); 8 | scanf("%s",input); 9 | printf("Pleased to meet you, %s!\n",input); 10 | 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /CH07/07_06/07_06-yourname2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char input[10]; 6 | 7 | printf("Your name? "); 8 | fgets(input,10,stdin); 9 | printf("Pleased to meet you, %s!\n",input); 10 | 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /CH07/07_06/07_06-yourname3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char input[10]; 6 | int i; 7 | 8 | printf("Your name? "); 9 | fgets(input,10,stdin); 10 | for(i=0;i<10;i++) 11 | { 12 | if(input[i] == '\n') 13 | input[i] = '\0'; 14 | } 15 | printf("Pleased to meet you, %s!\n",input); 16 | 17 | return(0); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /CH07/07_07/07_07-printf1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char string[] = "Yet another string literal"; 6 | 7 | printf("A string literal"); 8 | printf(string); 9 | 10 | return(0); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /CH07/07_07/07_07-printf2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char string[] = "Yet another string literal\n"; 6 | 7 | printf("A string literal\n"); 8 | printf("%s",string); 9 | 10 | return(0); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /CH07/07_07/07_07-puts1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char string[] = "Yet another string literal"; 6 | 7 | puts("A string literal"); 8 | puts(string); 9 | 10 | return(0); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /CH07/07_07/07_07-string1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char string[] = "I'm such a handsome program!\n"; 6 | int a; 7 | 8 | a = 0; 9 | while( string[a] != '\0' ) 10 | { 11 | putchar( string[a] ); 12 | a++; 13 | } 14 | 15 | return(0); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /CH07/07_07/07_07-string2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char string[] = "I'm such a handsome program!\n"; 6 | int a; 7 | 8 | a = 0; 9 | while( putchar(string[a]) != '\0' ) 10 | { 11 | a++; 12 | } 13 | 14 | return(0); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /CH07/07_07/07_07-string3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char string[] = "I'm such a handsome program!\n"; 6 | int a; 7 | 8 | a = 0; 9 | while( putchar(string[a]) ) 10 | { 11 | a++; 12 | } 13 | 14 | return(0); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /CH07/07_07/07_07-string4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char string[] = "I'm such a handsome program!\n"; 6 | int a; 7 | 8 | a = 0; 9 | while( putchar(string[a++]) ) 10 | ; 11 | 12 | return(0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /CH07/07_08/07_08-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /CH07/07_09/07_09-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char item[] = "fish umbrellas"; 6 | int quantity = 12; 7 | float price = 78.66; 8 | 9 | printf("You have purchased %d %s at $%.2f each\n", 10 | quantity, 11 | item, 12 | price 13 | ); 14 | printf("The total cost is $%.2f\n",quantity*price); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /CH07/07_10/07_10-strcmp1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char password[] = "secret\n"; 7 | char input[10]; 8 | 9 | printf("Password: "); 10 | fgets(input,10,stdin); 11 | 12 | if( strcmp(password,input)==0 ) 13 | puts("Entry granted!"); 14 | else 15 | puts("Incorrect password!"); 16 | 17 | return(0); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /CH07/07_10/07_10-strlen1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char string[] = "Hello, you happy planet."; 7 | int len; 8 | 9 | len = strlen(string); 10 | printf("The string \"%s\" is %d characters long.\n",string,len); 11 | 12 | return(0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /CH07/07_11/07_11-strbuild1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char first[16] = "George"; 7 | char last[16] = "Washington"; 8 | char full[32]; 9 | 10 | /* build the string */ 11 | 12 | printf("Pleased to meet you, %s\n",full); 13 | 14 | return(0); 15 | } 16 | -------------------------------------------------------------------------------- /CH07/07_11/07_11-strbuild2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char first[16] = "George"; 7 | char last[16] = "Washington"; 8 | char full[32]; 9 | 10 | /* build the string */ 11 | strcpy(full,first); 12 | strcat(full,last); 13 | 14 | printf("Pleased to meet you, %s\n",full); 15 | 16 | return(0); 17 | } 18 | -------------------------------------------------------------------------------- /CH07/07_11/07_11-strbuild3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char first[16] = "George"; 7 | char last[16] = "Washington"; 8 | char full[32]; 9 | 10 | /* build the string */ 11 | strcpy(full,first); 12 | strcat(full," "); 13 | strcat(full,last); 14 | 15 | printf("Pleased to meet you, %s\n",full); 16 | 17 | return(0); 18 | } 19 | -------------------------------------------------------------------------------- /CH07/07_12/07_12-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /* main program */ 7 | int main() 8 | { 9 | // variable declaration 10 | 11 | while(1) 12 | { 13 | printf("Command: "); 14 | // fetch input 15 | // remove newline 16 | // convert to uppercase 17 | // output "Processing command 'cmd'" 18 | // break the loop on `QUIT` 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /CH07/07_13/07_13-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /* main program */ 7 | int main() 8 | { 9 | const int size = 32; 10 | int x; 11 | char input[size]; 12 | 13 | while(1) 14 | { 15 | x = 0; 16 | printf("Command: "); 17 | fgets(input,size,stdin); 18 | while( input[x] != '\0' ) 19 | { 20 | if( input[x]=='\n' ) 21 | { 22 | input[x]='\0'; 23 | break; 24 | } 25 | input[x] = toupper(input[x]); 26 | x++; 27 | } 28 | printf("Processing command '%s'\n",input); 29 | if( strcmp(input,"QUIT")==0 ) 30 | break; 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /CH08/08_01/08_01-variableinfo1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int alpha; 6 | 7 | alpha = 27; 8 | printf("Integer variable `alpha` holds the value %d\n",alpha); 9 | printf("Variable `alpha` occupies %lu bytes of storage\n",sizeof(alpha)); 10 | printf("And it sits at address %p\n",&alpha); 11 | 12 | return(0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /CH08/08_02/08_02-pointerdata1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int alpha; 6 | int *ptr; 7 | 8 | alpha = 99; 9 | ptr = α 10 | printf("The address of variable alpha is %p\n",&alpha); 11 | printf("The value stored in pointer ptr is %p\n",ptr); 12 | 13 | return(0); 14 | } 15 | -------------------------------------------------------------------------------- /CH08/08_02/08_02-pointerdata2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int alpha; 6 | int *ptr; 7 | 8 | alpha = 99; 9 | ptr = α 10 | printf("The address of variable alpha is %p\n",&alpha); 11 | printf("The value stored in pointer ptr is %p\n",ptr); 12 | printf("The content of variable alpha is %d\n",alpha); 13 | printf("The content of memory address %p is %d\n",ptr,*ptr); 14 | 15 | return(0); 16 | } 17 | -------------------------------------------------------------------------------- /CH08/08_02/08_02-pointerdata3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int alpha; 6 | int *ptr; 7 | 8 | alpha = 99; 9 | ptr = α 10 | printf("The address of variable alpha is %p\n",&alpha); 11 | printf("The value stored in pointer ptr is %p\n",ptr); 12 | printf("The content of variable alpha is %d\n",alpha); 13 | printf("The content of memory address %p is %d\n",ptr,*ptr); 14 | *ptr = 66; 15 | printf("Variable alpha = %d\n",alpha); 16 | 17 | return(0); 18 | } 19 | -------------------------------------------------------------------------------- /CH08/08_03/08_03-pointermath1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int alpha; 6 | int *pa; 7 | 8 | pa = α 9 | printf("%p\n",pa); 10 | printf("%p\n",pa+1); 11 | 12 | return(0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /CH08/08_03/08_03-pointermath2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int twos[5] = { 2, 4, 6, 8, 10 }; 6 | int *pt; 7 | 8 | pt = twos; 9 | printf("%p\n",pt); 10 | printf("%p\n",pt+1); 11 | 12 | return(0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /CH08/08_03/08_03-pointermath3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char letters[5] = { 'a', 'b', 'c', 'd', 'e' }; 6 | char *pt; 7 | 8 | pt = letters; 9 | printf("%p\n",pt); 10 | printf("%p\n",pt+1); 11 | 12 | return(0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /CH08/08_03/08_03-pointermath4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | double twos[5] = { 2.2, 4.4, 6.6, 8.8, 10.1 }; 6 | double *pt; 7 | 8 | pt = twos; 9 | printf("%p\n",pt); 10 | printf("%p\n",pt+1); 11 | 12 | return(0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /CH08/08_03/08_03-pointermath5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("Size of variable storage:\n"); 6 | printf("char\t%lu\n",sizeof(char)); 7 | printf("int\t%lu\n",sizeof(int)); 8 | printf("float\t%lu\n",sizeof(float)); 9 | printf("double\t%lu\n",sizeof(double)); 10 | 11 | return(0); 12 | } 13 | -------------------------------------------------------------------------------- /CH08/08_04/08_04-parray1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char text[10]; 6 | 7 | printf("Type something: "); 8 | scanf("%s",text); 9 | printf("You typed: %s\n",text); 10 | 11 | return(0); 12 | } 13 | -------------------------------------------------------------------------------- /CH08/08_04/08_04-parray2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int text[10]; 6 | int *pt; 7 | 8 | printf("Type something: "); 9 | scanf("%s",text); 10 | printf("You typed: %s\n",text); 11 | pt = text; 12 | printf("Address of text = %p\n",text); 13 | printf("Address of pt = %p\n",pt); 14 | 15 | return(0); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /CH08/08_04/08_04-parray3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int twos[5] = { 2, 4, 6, 8, 10 }; 6 | int *pt; 7 | int x; 8 | 9 | pt = twos; 10 | for(x=0;x<5;x++) 11 | { 12 | printf("%p = %p\n",pt+x,&twos[x]); 13 | } 14 | 15 | return(0); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /CH08/08_04/08_04-parray4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int twos[5] = { 2, 4, 6, 8, 10 }; 6 | 7 | printf("%d\n",twos[0]); 8 | printf("%d\n",twos[1]); 9 | printf("%d\n",twos[2]); 10 | printf("%d\n",twos[3]); 11 | printf("%d\n",twos[4]); 12 | 13 | return(0); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /CH08/08_04/08_04-parray5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int twos[5] = { 2, 4, 6, 8, 10 }; 6 | int *pt; 7 | 8 | pt = twos; 9 | printf("%d\n",*(pt+0)); 10 | printf("%d\n",*(pt+1)); 11 | printf("%d\n",*(pt+2)); 12 | printf("%d\n",*(pt+3)); 13 | printf("%d\n",*(pt+4)); 14 | 15 | return(0); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /CH08/08_05/08_05-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char title[] = "Pointers don't intimidate me!\n"; 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /CH08/08_06/08_06-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char title[] = "Pointers don't intimidate me!\n"; 6 | char *t; 7 | 8 | t = title; 9 | while( *t != '\0' ) 10 | { 11 | putchar(*t); 12 | t++; 13 | } 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /CH08/08_06/08_06-solution2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char title[] = "Pointers don't intimidate me!\n"; 6 | char *t; 7 | int x; 8 | 9 | t = title; 10 | x = 0; 11 | while( *(t+x) != '\0' ) 12 | { 13 | putchar(*(t+x)); 14 | x++; 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /CH08/08_07/08_07-allocate1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char *buffer; 7 | 8 | buffer = malloc( sizeof(char) * 128 ); 9 | if( buffer == NULL ) 10 | { 11 | puts("Unable to allocate memory"); 12 | exit(1); 13 | } 14 | puts("Buffer allocated"); 15 | free(buffer); 16 | puts("Buffer freed"); 17 | 18 | return(0); 19 | } 20 | -------------------------------------------------------------------------------- /CH08/08_07/08_07-allocate2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int *buffer; 7 | int x; 8 | 9 | /* allocate storage for 20 integers */ 10 | buffer = malloc( sizeof(int) * 20 ); 11 | if( buffer == NULL ) 12 | { 13 | puts("Unable to allocate memory"); 14 | exit(1); 15 | } 16 | 17 | /* fill and display the buffer */ 18 | for(x=0;x<20;x++) 19 | { 20 | *(buffer+x) = 10 * (x + 1); 21 | printf("%d\n",*(buffer+x)); 22 | } 23 | 24 | /* exit */ 25 | free(buffer); 26 | return(0); 27 | } 28 | -------------------------------------------------------------------------------- /CH08/08_08/08_08-arrayp1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char *veeps[5] = { "Adams", "Jefferson", "Burr", "Clinton", "Gerry" }; 6 | int x; 7 | 8 | for(x=0;x<5;x++) 9 | printf("%s\n",veeps[x]); 10 | 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /CH08/08_08/08_08-arrayp2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char *veeps[5] = { "Adams", "Jefferson", "Burr", "Clinton", "Gerry" }; 6 | int x; 7 | 8 | for(x=0;x<5;x++) 9 | printf("%s\n",*(veeps+x)); 10 | 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /CH08/08_08/08_08-arrayp3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char *veeps[5] = { "Adams", "Jefferson", "Burr", "Clinton", "Gerry" }; 6 | char *temp; 7 | int x; 8 | 9 | /* swap 2 and 3 */ 10 | temp = veeps[1]; 11 | veeps[1] = veeps[2]; 12 | veeps[2] = temp; 13 | 14 | for(x=0;x<5;x++) 15 | printf("%s\n",*(veeps+x)); 16 | 17 | return(0); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /CH08/08_08/08_08-arrayp4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char *veeps[5] = { "Adams", "Jefferson", "Burr", "Clinton", "Gerry" }; 6 | int x; 7 | 8 | for(x=0;x<5;x++) 9 | printf("%c\n",**(veeps+x)); 10 | 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /CH08/08_08/08_08-arrayp5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char *veeps[5] = { "Adams", "Jefferson", "Burr", "Clinton", "Gerry" }; 6 | int x; 7 | 8 | for(x=0;x<5;x++) 9 | printf("%c\n",*(*(veeps+x)+1)); 10 | 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /CH08/08_09/08_09-structurepointer1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | struct person { 8 | char *name; 9 | int age; 10 | } you; 11 | 12 | /* allocate storage */ 13 | you.name = malloc( sizeof(char) * 32); 14 | if( you.name == NULL) 15 | { 16 | puts("Unable to allocate storage"); 17 | exit(1); 18 | } 19 | 20 | /* gather input */ 21 | printf("Enter your name: "); 22 | fgets(you.name,32,stdin); 23 | printf("Enter your age: "); 24 | scanf("%d",&you.age); 25 | 26 | printf("You are %s",you.name); 27 | printf("You are %d years old\n",you.age); 28 | 29 | /* clean-up */ 30 | free(you.name); 31 | return(0); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /CH08/08_09/08_09-structurepointer2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | struct person { 7 | char name[32]; 8 | int age; 9 | } *you; 10 | 11 | /* allocate storage for the structure */ 12 | you = malloc( sizeof(struct person) ); 13 | if( you== NULL) 14 | { 15 | puts("Unable to allocate storage"); 16 | exit(1); 17 | } 18 | 19 | printf("Enter your name: "); 20 | fgets(you->name,32,stdin); 21 | /* get your age */ 22 | printf("Enter your age: "); 23 | scanf("%d",&you->age); 24 | 25 | printf("You are %s",you->name); 26 | printf("You are %d years old\n",you->age); 27 | 28 | /* clean-up */ 29 | free(you); 30 | return(0); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /CH08/08_09/08_09-structurepointer3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | struct person { 8 | char *name; 9 | int age; 10 | } *you; 11 | 12 | /* allocate storage */ 13 | you = malloc( sizeof(struct person) ); 14 | if( you== NULL) 15 | { 16 | puts("Unable to allocate storage"); 17 | exit(1); 18 | } 19 | you->name = malloc( sizeof(char) * 32 ); 20 | if( you->name == NULL) 21 | { 22 | puts("Unable to allocate storage"); 23 | exit(1); 24 | } 25 | 26 | /* gather input */ 27 | printf("Enter your name: "); 28 | fgets(you->name,32,stdin); 29 | printf("Enter your age: "); 30 | scanf("%d",&you->age); 31 | 32 | printf("You are %s",you->name); 33 | printf("You are %d years old\n",you->age); 34 | 35 | /* clean-up */ 36 | free(you->name); 37 | free(you); 38 | return(0); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /CH08/08_10/08_10-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /CH08/08_11/08_11-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | struct data { 7 | int v; 8 | char c; 9 | } *alpha[26]; 10 | int x; 11 | 12 | /* both allocate and assign the structures */ 13 | for( x=0; x<26; x++ ) 14 | { 15 | alpha[x] = malloc( sizeof(struct data) ); 16 | if( alpha[x]==NULL ) 17 | { 18 | puts("Memory allocation error"); 19 | exit(1); 20 | } 21 | alpha[x]->v = x; 22 | alpha[x]->c = 'A'+x; 23 | } 24 | 25 | /* output */ 26 | for( x=0; x<26; x++ ) 27 | printf("%d, %c\n",alpha[x]->v,alpha[x]->c); 28 | 29 | /* clean-up */ 30 | for( x=0; x<26; x++ ) 31 | free(alpha[x]); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /CH08/08_12/08_12-passpointer1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void doubler(int *alpha) 4 | { 5 | *alpha += *alpha; 6 | } 7 | 8 | int main() 9 | { 10 | int a = 25; 11 | 12 | printf("Value before: %d\n",a); 13 | doubler(&a); 14 | printf("Value after: %d\n",a); 15 | 16 | return(0); 17 | } 18 | -------------------------------------------------------------------------------- /CH08/08_12/08_12-passpointer2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void three(float a, float *b, float *c) 4 | { 5 | *b = a/3.0; 6 | *c = a/7.0; 7 | } 8 | 9 | int main() 10 | { 11 | float x,y; 12 | 13 | three(20.0,&x,&y); 14 | printf("20 / 3 = %.1f\n",x); 15 | printf("20 / 7 = %.1f\n",y); 16 | 17 | return(0); 18 | } 19 | -------------------------------------------------------------------------------- /CH08/08_13/08_13-returnp1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char *allocate(int size) 5 | { 6 | char *p; 7 | 8 | p = malloc(sizeof(char) * size); 9 | if( p == NULL ) 10 | { 11 | puts("Unable to allocate memory"); 12 | exit(1); 13 | } 14 | return(p); 15 | } 16 | 17 | int main() 18 | { 19 | char *a,*b; 20 | 21 | printf("Allocating 256 bytes: "); 22 | a = allocate(256); 23 | puts("Done"); 24 | printf("Allocating 512 bytes: "); 25 | b = allocate(512); 26 | puts("Done"); 27 | 28 | /* clean-up */ 29 | free(a); 30 | free(b); 31 | return(0); 32 | } 33 | -------------------------------------------------------------------------------- /CH08/08_13/08_13-returnp2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int *makearray(int size) 5 | { 6 | int *p; 7 | int x; 8 | 9 | /* allocate storage for `size` integers */ 10 | p = malloc( sizeof(int) * size ); 11 | if( p == NULL ) 12 | { 13 | puts("Unable to allocate memory"); 14 | exit(1); 15 | } 16 | 17 | /* fill the array */ 18 | for(x=0;x 2 | #include 3 | 4 | // write the get_input() function here 5 | 6 | int main() 7 | { 8 | char *your_name,*your_city; 9 | 10 | printf("Your name: "); 11 | your_name = get_input(32); 12 | printf("Your city: "); 13 | your_city = get_input(32); 14 | 15 | printf("%s lives in %s.\n",your_name,your_city); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /CH08/08_15/08_15-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char *get_input(int size) 5 | { 6 | char *a; 7 | int x = 0; 8 | 9 | /* allocate storage */ 10 | a = malloc( sizeof(char) * size ); 11 | if( a==NULL ) 12 | { 13 | puts("Unable to allocate memory"); 14 | exit(1); 15 | } 16 | 17 | /* read input */ 18 | fgets(a,size,stdin); 19 | /* remove newline */ 20 | while( *(a+x) != '\0' ) 21 | { 22 | if( *(a+x)=='\n' ) 23 | { 24 | *(a+x) = '\0'; 25 | break; 26 | } 27 | x++; 28 | } 29 | 30 | return(a); 31 | } 32 | 33 | int main() 34 | { 35 | char *your_name,*your_city; 36 | 37 | printf("Your name: "); 38 | your_name = get_input(32); 39 | printf("Your city: "); 40 | your_city = get_input(32); 41 | 42 | printf("%s lives in %s.\n",your_name,your_city); 43 | 44 | free(your_name); 45 | free(your_city); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /CH09/09_01/09_01-reading1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const char filename[] = "sonnet18.txt"; 6 | FILE *fh; 7 | 8 | /* open the file */ 9 | fh = fopen(filename,"r"); 10 | if( fh == NULL ) 11 | { 12 | printf("Unable to read from file %s\n",filename); 13 | return(1); 14 | } 15 | 16 | printf("File '%s' opened\n",filename); 17 | 18 | /* clean-up */ 19 | fclose(fh); 20 | return(0); 21 | } 22 | -------------------------------------------------------------------------------- /CH09/09_01/09_01-reading2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const char filename[] = "sonnet18.txt"; 6 | FILE *fh; 7 | int ch; 8 | 9 | /* open the file */ 10 | fh = fopen(filename,"r"); 11 | if( fh == NULL ) 12 | { 13 | printf("Unable to read from file %s\n",filename); 14 | return(1); 15 | } 16 | 17 | while(1) 18 | { 19 | ch = fgetc(fh); 20 | if( ch == EOF ) 21 | break; 22 | putchar(ch); 23 | } 24 | 25 | /* clean-up */ 26 | fclose(fh); 27 | return(0); 28 | } 29 | -------------------------------------------------------------------------------- /CH09/09_01/sonnet18.txt: -------------------------------------------------------------------------------- 1 | Shall I compare thee to a summer's day? 2 | Thou art more lovely and more temperate: 3 | Rough winds do shake the darling buds of May, 4 | And summer's lease hath all too short a date: 5 | Sometime too hot the eye of heaven shines, 6 | And often is his gold complexion dimmed; 7 | And every fair from fair sometime declines, 8 | By chance, or nature's changing course, untrimmed: 9 | But thy eternal summer shall not fade, 10 | Nor lose possession of that fair thou ow'st; 11 | Nor shall Death brag thou wander'st in his shade 12 | When in eternal lines to time thou grow'st: 13 | So long as men can breathe or eyes can see, 14 | So long lives this, and this gives life to thee. 15 | -------------------------------------------------------------------------------- /CH09/09_02/09_02-writing1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | FILE *fh; 6 | 7 | /* open/create the file */ 8 | fh = fopen("output.txt","w"); 9 | if( fh == NULL ) 10 | { 11 | puts("Failed to create file"); 12 | return(1); 13 | } 14 | 15 | /* output */ 16 | fprintf(fh,"Hello there, file!\n"); 17 | 18 | /* close the file */ 19 | fclose(fh); 20 | 21 | return(0); 22 | } 23 | -------------------------------------------------------------------------------- /CH09/09_02/09_02-writing2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | FILE *fh; 6 | int a; 7 | 8 | /* open/create the file */ 9 | fh = fopen("alphabet.txt","w"); 10 | if( fh == NULL ) 11 | { 12 | puts("Failed to create file"); 13 | return(1); 14 | } 15 | 16 | /* output */ 17 | for(a='A';a<='Z';a++) 18 | fputc(a,fh); 19 | 20 | /* close the file */ 21 | fclose(fh); 22 | 23 | return(0); 24 | } 25 | -------------------------------------------------------------------------------- /CH09/09_02/09_02-writing3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | FILE *fh; 6 | 7 | /* open the file for appending */ 8 | fh = fopen("alphabet.txt","a"); 9 | if( fh == NULL ) 10 | { 11 | puts("Failed to open file"); 12 | return(1); 13 | } 14 | 15 | /* output */ 16 | fputc('\n',fh); 17 | 18 | /* close the file */ 19 | fclose(fh); 20 | 21 | return(0); 22 | } 23 | -------------------------------------------------------------------------------- /CH09/09_03/09_03-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const char filename[] = "hello.txt"; 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /CH09/09_04/09_04-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const char filename[] = "hello.txt"; 6 | char name[16]; 7 | char input[64]; 8 | FILE *fh; 9 | 10 | /* create the file */ 11 | fh = fopen(filename,"w"); 12 | if( fh==NULL ) 13 | { 14 | puts("Error creating file"); 15 | return(1); 16 | } 17 | 18 | /* prompt for input */ 19 | printf("Your name: "); 20 | scanf("%s",name); 21 | 22 | /* write to the file */ 23 | fprintf(fh,"%s has written to this file\n",name); 24 | 25 | /* close */ 26 | fclose(fh); 27 | 28 | /* open and read the file */ 29 | fh = fopen(filename,"r"); 30 | if( fh==NULL ) 31 | { 32 | puts("Error opening file"); 33 | return(1); 34 | } 35 | 36 | /* read the text */ 37 | fgets(input,64,fh); 38 | printf("The file '%s' contains this text:\n",filename); 39 | puts(input); 40 | 41 | /* clean-up */ 42 | fclose(fh); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /CH09/09_05/09_05-readf1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const char filename[] = "integers.dat"; 6 | FILE *fp; 7 | int x,a; 8 | 9 | /* open the file for reading */ 10 | fp = fopen(filename,"r"); 11 | if( fp == NULL ) 12 | { 13 | printf("Unable to create %s\n",filename); 14 | return(1); 15 | } 16 | 17 | /* read integers */ 18 | for(x=0;x<10;x++) 19 | { 20 | fread(&a, sizeof(int), 1, fp ); 21 | printf("%d\n",a); 22 | } 23 | 24 | /* close */ 25 | fclose(fp); 26 | 27 | return(0); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /CH09/09_05/09_05-readf2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const char filename[] = "integers.dat"; 6 | FILE *fp; 7 | int x,a[10]; 8 | 9 | /* open the file for reading */ 10 | fp = fopen(filename,"r"); 11 | if( fp == NULL ) 12 | { 13 | printf("Unable to create %s\n",filename); 14 | return(1); 15 | } 16 | 17 | /* read integers */ 18 | fread(a, sizeof(int), 10, fp ); 19 | for(x=0;x<10;x++) 20 | printf("%d\n",a[x]); 21 | 22 | /* close */ 23 | fclose(fp); 24 | 25 | return(0); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /CH09/09_05/09_05-writef1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const char filename[] = "integers.dat"; 6 | FILE *fp; 7 | int x,a; 8 | 9 | /* open and create the file */ 10 | fp = fopen(filename,"w"); 11 | if( fp == NULL ) 12 | { 13 | printf("Unable to create %s\n",filename); 14 | return(1); 15 | } 16 | 17 | /* write the integers */ 18 | a = 10; 19 | for(x=0;x<10;x++) 20 | { 21 | fwrite(&a, sizeof(int), 1, fp); 22 | a += 5; 23 | } 24 | 25 | /* close-up */ 26 | fclose(fp); 27 | return(0); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /CH09/09_06/09_06-createpres1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const char filename[] = "presidents.dat"; 6 | FILE *fp; 7 | struct person { 8 | char name[32]; 9 | int inaguration; 10 | int age; 11 | } presidents[10] = { 12 | { "George Washington", 1789, 57 }, 13 | { "John Adams", 1797, 62 }, 14 | { "Thomas Jefferson", 1801, 58 }, 15 | { "James Madison", 1809, 58 }, 16 | { "James Monroe", 1817, 59 }, 17 | { "John Quincy Adams", 1825, 58 }, 18 | { "Andrew Jackson", 1829, 62 }, 19 | { "Martin Van Buren", 1837, 55 }, 20 | { "William Henry Harrison", 1841, 68 }, 21 | { "John Tyler", 1841, 51 } 22 | }; 23 | 24 | /* open the file */ 25 | fp = fopen(filename,"w"); 26 | if( fp == NULL ) 27 | { 28 | printf("Unable to create %s\n",filename); 29 | return(1); 30 | } 31 | 32 | /* write the 10 structures */ 33 | fwrite( presidents, sizeof(struct person), 10, fp); 34 | 35 | /* and remember to close */ 36 | fclose(fp); 37 | 38 | return(0); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /CH09/09_06/09_06-readrand1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const char filename[] = "presidents.dat"; 6 | struct person { 7 | char name[32]; 8 | int inauguration; 9 | int age; 10 | } president; 11 | int x; 12 | FILE *fp; 13 | 14 | /* open the file */ 15 | fp = fopen(filename,"r"); 16 | if( fp == NULL ) 17 | { 18 | printf("Unable to open %s\n",filename); 19 | return(1); 20 | } 21 | 22 | /* prompt to read a specific record */ 23 | printf("Enter record to read (1-10): "); 24 | scanf("%d",&x); 25 | /* validate the input */ 26 | if( x<1 || x>10 ) 27 | { 28 | puts("Invalid input"); 29 | return(1); 30 | } 31 | /* decrement x to reference the proper offset */ 32 | x--; 33 | 34 | /* Set the file position indicator */ 35 | fseek(fp, sizeof(struct person)*x, SEEK_SET); 36 | /* fetch the record */ 37 | fread(&president, sizeof(struct person), 1, fp); 38 | 39 | /* print the result */ 40 | printf("President %s was %d years old when inaugurated in %d\n", 41 | president.name, 42 | president.age, 43 | president.inauguration 44 | ); 45 | 46 | /* close-up */ 47 | fclose(fp); 48 | return(0); 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /CH09/09_07/09_07-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const char filename[] = "presidents.dat"; 6 | struct person { 7 | char name[32]; 8 | int inauguration; 9 | int age; 10 | } president; 11 | int x; 12 | FILE *fp; 13 | 14 | /* open the file */ 15 | fp = fopen(filename,"r"); 16 | if( fp == NULL ) 17 | { 18 | printf("Unable to open %s\n",filename); 19 | return(1); 20 | } 21 | 22 | /* read records from back to front */ 23 | 24 | /* clean-up */ 25 | fclose(fp); 26 | return(0); 27 | } 28 | 29 | 30 | -------------------------------------------------------------------------------- /CH09/09_08/09_08-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const char filename[] = "presidents.dat"; 6 | struct person { 7 | char name[32]; 8 | int inauguration; 9 | int age; 10 | } president; 11 | int x; 12 | FILE *fp; 13 | 14 | /* open the file */ 15 | fp = fopen(filename,"r"); 16 | if( fp == NULL ) 17 | { 18 | printf("Unable to open %s\n",filename); 19 | return(1); 20 | } 21 | 22 | /* read records from back to front */ 23 | for( x=9; x>=0; x-- ) 24 | { 25 | fseek(fp, sizeof(struct person)*x, SEEK_SET); 26 | fread(&president, sizeof(struct person), 1, fp); 27 | /* print the result */ 28 | printf("President %s was %d years old when inaugurated in %d\n", 29 | president.name, 30 | president.age, 31 | president.inauguration 32 | ); 33 | } 34 | 35 | /* clean-up */ 36 | fclose(fp); 37 | return(0); 38 | } 39 | 40 | 41 | -------------------------------------------------------------------------------- /CH09/09_08/09_08-solution2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const char filename[] = "presidents.dat"; 6 | struct person { 7 | char name[32]; 8 | int inauguration; 9 | int age; 10 | } president; 11 | int x; 12 | FILE *fp; 13 | 14 | /* open the file */ 15 | fp = fopen(filename,"r"); 16 | if( fp == NULL ) 17 | { 18 | printf("Unable to open %s\n",filename); 19 | return(1); 20 | } 21 | 22 | /* read records from back to front */ 23 | for( x=-1; x>-11; x-- ) 24 | { 25 | fseek(fp, sizeof(struct person)*x, SEEK_END); 26 | fread(&president, sizeof(struct person), 1, fp); 27 | /* print the result */ 28 | printf("President %s was %d years old when inaugurated in %d\n", 29 | president.name, 30 | president.age, 31 | president.inauguration 32 | ); 33 | } 34 | 35 | /* clean-up */ 36 | fclose(fp); 37 | return(0); 38 | } 39 | 40 | 41 | -------------------------------------------------------------------------------- /CH09/09_08/presidents.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/complete-guide-to-c-programming-foundations-4504398/ecd8e55c5d283fc6d4930d45b5947e90ecdfde0e/CH09/09_08/presidents.dat -------------------------------------------------------------------------------- /CH09/09_09/09_09-copyfile1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int filecopy(char *org,char *dup) 4 | { 5 | FILE *o,*d; 6 | int c; 7 | 8 | /* open/create the files */ 9 | o = fopen(org,"r"); 10 | d = fopen(dup,"w"); 11 | if( org==NULL || dup==NULL) 12 | { 13 | return(-1); 14 | } 15 | 16 | /* read/write to copy the file */ 17 | while(1) 18 | { 19 | c = fgetc(o); 20 | if( c == EOF ) 21 | break; 22 | fputc(c,d); 23 | } 24 | 25 | /* clean-up */ 26 | fclose(o); 27 | fclose(d); 28 | return(0); 29 | } 30 | 31 | int main() 32 | { 33 | char original[] = "beta.txt"; 34 | char duplicate[] = "betacopy.txt"; 35 | int r; 36 | 37 | r = filecopy(original,duplicate); 38 | if( r==-1 ) 39 | puts("Unable to copy files"); 40 | else 41 | printf("%s copied to %s\n",original,duplicate); 42 | 43 | return(0); 44 | } 45 | -------------------------------------------------------------------------------- /CH09/09_09/09_09-rename1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int r; 6 | 7 | r = rename("alpha.txt","a.txt"); 8 | if( r == 0 ) 9 | puts("Rename operation successful"); 10 | else 11 | puts("Rename failed"); 12 | 13 | return(0); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /CH09/09_09/09_09-unlink1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int r; 7 | 8 | r = unlink("delete_me.txt"); 9 | if( r == 0 ) 10 | puts("File deleted"); 11 | else 12 | puts("Something went wrong!"); 13 | 14 | return(0); 15 | } 16 | -------------------------------------------------------------------------------- /CH09/09_09/alpha.txt: -------------------------------------------------------------------------------- 1 | This is the alpha file. 2 | -------------------------------------------------------------------------------- /CH09/09_09/beta.txt: -------------------------------------------------------------------------------- 1 | Here you find the contents of the file beta. 2 | -------------------------------------------------------------------------------- /CH09/09_09/delete_me.txt: -------------------------------------------------------------------------------- 1 | This file will be deleted. 2 | -------------------------------------------------------------------------------- /CH09/09_10/09_10-chdir1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char path[512]; 7 | 8 | chdir(".."); 9 | getcwd(path,512); 10 | printf("This program is running in the %s directory\n",path); 11 | 12 | return(0); 13 | } 14 | -------------------------------------------------------------------------------- /CH09/09_10/09_10-getcwd1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char path[512]; 7 | 8 | getcwd(path,512); 9 | printf("This program is running in the %s directory\n",path); 10 | 11 | return(0); 12 | } 13 | -------------------------------------------------------------------------------- /CH09/09_11/09_11-readdir1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | DIR *directory; 7 | 8 | /* open the directory */ 9 | directory = opendir("."); 10 | if( directory == NULL ) 11 | { 12 | puts("Unable to open current directory"); 13 | return(1); 14 | } 15 | 16 | /* do something */ 17 | puts("Opened current directory"); 18 | 19 | /* close the directory */ 20 | closedir(directory); 21 | 22 | return(0); 23 | } 24 | -------------------------------------------------------------------------------- /CH09/09_11/09_11-readdir2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | DIR *directory; 7 | struct dirent *file; 8 | 9 | /* open the directory */ 10 | directory = opendir("."); 11 | if( directory == NULL ) 12 | { 13 | puts("Unable to open current directory"); 14 | return(1); 15 | } 16 | 17 | /* do something */ 18 | file = readdir(directory); 19 | printf("Found the file %s\n",file->d_name); 20 | 21 | /* close the directory */ 22 | closedir(directory); 23 | 24 | return(0); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /CH09/09_11/09_11-readdir3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | DIR *directory; 7 | struct dirent *file; 8 | 9 | /* open the directory */ 10 | directory = opendir("."); 11 | if( directory == NULL ) 12 | { 13 | puts("Unable to open current directory"); 14 | return(1); 15 | } 16 | 17 | /* do something */ 18 | while( (file=readdir(directory)) != NULL ) 19 | printf("Found the file %s\n",file->d_name); 20 | 21 | /* close the directory */ 22 | closedir(directory); 23 | 24 | return(0); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /CH09/09_12/09_12-filestat1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | const char filename[] = "alpha.txt"; 8 | struct stat fstat; 9 | 10 | stat(filename,&fstat); 11 | printf("%s is %ld bytes long\n", 12 | filename, 13 | fstat.st_size 14 | ); 15 | printf("It was modified on %s",ctime(&fstat.st_mtime)); 16 | 17 | return(0); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /CH09/09_12/09_12-filestat2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | DIR *directory; 9 | struct dirent *file; 10 | struct stat fstat; 11 | 12 | /* open the directory */ 13 | directory = opendir("."); 14 | if( directory == NULL ) 15 | { 16 | puts("Unable to open current directory"); 17 | return(1); 18 | } 19 | 20 | /* do something */ 21 | printf("%-25s %-10s %s\n","Filename","Size","Timestamp"); 22 | while( (file=readdir(directory)) != NULL ) 23 | { 24 | stat(file->d_name,&fstat); 25 | printf("%25s %10lld ", 26 | file->d_name, 27 | fstat.st_size 28 | ); 29 | printf("%s",ctime(&fstat.st_mtime)); 30 | } 31 | 32 | /* close the directory */ 33 | closedir(directory); 34 | 35 | return(0); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /CH09/09_12/09_12-filestat3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | DIR *directory; 9 | struct dirent *file; 10 | struct stat fstat; 11 | 12 | /* open the directory */ 13 | directory = opendir("."); 14 | if( directory == NULL ) 15 | { 16 | puts("Unable to open current directory"); 17 | return(1); 18 | } 19 | 20 | /* do something */ 21 | printf("%-5s %-20s %-10s %s\n","Type","Filename","Size","Timestamp"); 22 | while( (file=readdir(directory)) != NULL ) 23 | { 24 | stat(file->d_name,&fstat); 25 | if( S_ISDIR(fstat.st_mode) ) 26 | printf(" Dir "); 27 | else 28 | printf("File "); 29 | printf("%20s %10ld ", 30 | file->d_name, 31 | fstat.st_size 32 | ); 33 | printf("%s",ctime(&fstat.st_mtime)); 34 | } 35 | 36 | /* close the directory */ 37 | closedir(directory); 38 | 39 | return(0); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /CH09/09_12/alpha.txt: -------------------------------------------------------------------------------- 1 | This is the alpha file. 2 | -------------------------------------------------------------------------------- /CH09/09_13/09_13-challenge1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void create_file(const char *f) 6 | { 7 | } 8 | 9 | void read_file(const char *f) 10 | { 11 | } 12 | 13 | void delete_file(const char *f) 14 | { 15 | } 16 | 17 | int main() 18 | { 19 | const char filename[] = "stuff.dat"; 20 | 21 | create_file(filename); 22 | read_file(filename); 23 | delete_file(filename); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /CH09/09_14/09_14-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void create_file(const char *f) 6 | { 7 | FILE *fh; 8 | 9 | /* open/create the file */ 10 | fh = fopen(f,"w"); 11 | if( fh==NULL ) 12 | { 13 | printf("Unable to create '%s'\n",f); 14 | exit(1); 15 | } 16 | printf("File '%s' created\n",f); 17 | 18 | /* write your name to the file */ 19 | fprintf(fh,"Dan Gookin"); 20 | puts("Data written"); 21 | 22 | /* close the file */ 23 | fclose(fh); 24 | printf("File '%s' closed\n",f); 25 | } 26 | 27 | void read_file(const char *f) 28 | { 29 | FILE *fh; 30 | char buffer[32]; 31 | 32 | /* open/read the file */ 33 | fh = fopen(f,"r"); 34 | if( fh==NULL ) 35 | { 36 | printf("Unable to open '%s'\n",f); 37 | exit(1); 38 | } 39 | printf("File '%s' opened\n",f); 40 | 41 | /* read your name from the file */ 42 | fgets(buffer,32,fh); 43 | printf("The file '%s' contains the text: %s\n", 44 | f, 45 | buffer 46 | ); 47 | 48 | /* close the file */ 49 | fclose(fh); 50 | printf("File '%s' closed\n",f); 51 | } 52 | 53 | void delete_file(const char *f) 54 | { 55 | int r; 56 | 57 | r = unlink(f); 58 | if( r==0 ) 59 | printf("File '%s' deleted\n",f); 60 | else 61 | puts("Something went wrong!"); 62 | } 63 | 64 | int main() 65 | { 66 | const char filename[] = "stuff.dat"; 67 | 68 | create_file(filename); 69 | read_file(filename); 70 | delete_file(filename); 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /CH10/10_01/10_01-popen1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | FILE *p; 6 | int c; 7 | 8 | /* open the process */ 9 | p = popen("uname","r"); /* linux */ 10 | if( p == NULL ) 11 | { 12 | puts("Unable to create process"); 13 | return(1); 14 | } 15 | 16 | /* read from the process */ 17 | while( (c=fgetc(p)) != EOF ) 18 | { 19 | putchar(c); 20 | } 21 | 22 | /* close the process */ 23 | pclose(p); 24 | 25 | return(0); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /CH10/10_01/10_01-system1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | printf("Press Enter to clear the screen: "); 7 | getchar(); 8 | system("clear"); /* linux */ 9 | puts("Thank you"); 10 | 11 | return(0); 12 | } 13 | -------------------------------------------------------------------------------- /CH10/10_02/10_01-pow1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | double a; 7 | 8 | for(a=0.0;a<20.0;a+=1.0) 9 | printf("%.0f\n",pow(2.0,a) ); 10 | 11 | return(0); 12 | } 13 | -------------------------------------------------------------------------------- /CH10/10_02/10_01-sqrt1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | double a = 4294967296.0; 7 | 8 | while(a >= 2.0) 9 | { 10 | printf("%.f\n",a); 11 | a = sqrt(a); 12 | } 13 | 14 | return(0); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /CH10/10_03/10_02-time1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | time_t now; 7 | 8 | time(&now); 9 | printf("It's now %ld\n",now); 10 | 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /CH10/10_03/10_02-time2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | time_t now; 7 | 8 | time(&now); 9 | printf("It's now %s",ctime(&now)); 10 | 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /CH10/10_03/10_02-time3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | time_t now; 7 | struct tm *timestamp; 8 | 9 | time(&now); 10 | timestamp = localtime(&now); 11 | printf("It's now %d:%02d:%02d\n", 12 | timestamp->tm_hour, 13 | timestamp->tm_min, 14 | timestamp->tm_sec 15 | ); 16 | 17 | return(0); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /CH10/10_03/10_02-time4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | time_t now; 7 | struct tm *timestamp; 8 | const char *weekday[] = { 9 | "Sunday", "Monday", "Tuesday", "Wednesday", 10 | "Thursday", "Friday", "Saturday" 11 | }; 12 | const char *month[] = { 13 | "January", "February", "March", "April", 14 | "May", "June", "July", "August", 15 | "September", "October", "November", "December" 16 | }; 17 | 18 | time(&now); 19 | timestamp = localtime(&now); 20 | printf("Today is %s, %s %d %d\n", 21 | weekday[timestamp->tm_wday], 22 | month[timestamp->tm_mon], 23 | timestamp->tm_mday, 24 | timestamp->tm_year+1900 25 | ); 26 | 27 | return(0); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /CH10/10_04/10_04-random1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int x,r; 7 | 8 | for(x=0;x<10;x++) 9 | { 10 | r = rand(); 11 | printf("%d\n",r); 12 | } 13 | 14 | return(0); 15 | } 16 | -------------------------------------------------------------------------------- /CH10/10_04/10_04-random2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int x,r; 7 | 8 | for(x=0;x<10;x++) 9 | { 10 | r = rand() % 100 + 1; 11 | printf("%d\n",r); 12 | } 13 | 14 | return(0); 15 | } 16 | -------------------------------------------------------------------------------- /CH10/10_04/10_04-random3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int x,r; 7 | unsigned s; 8 | 9 | printf("Enter the seed: "); 10 | scanf("%u",&s); 11 | srand(s); 12 | 13 | for(x=0;x<10;x++) 14 | { 15 | r = rand() % 100 + 1; 16 | printf("%d\n",r); 17 | } 18 | 19 | return(0); 20 | } 21 | -------------------------------------------------------------------------------- /CH10/10_04/10_04-random4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | int x,r; 8 | 9 | srand( (unsigned)time(NULL) ); 10 | 11 | for(x=0;x<100;x++) 12 | { 13 | r = rand() % 100 + 1; 14 | printf("%4d",r); 15 | } 16 | putchar('\n'); 17 | 18 | return(0); 19 | } 20 | -------------------------------------------------------------------------------- /CH10/10_05/10_05-bubblesort1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define SIZE 100 6 | 7 | int main() 8 | { 9 | int list[SIZE]; 10 | int a,b,temp; 11 | 12 | /* seed the randomizer */ 13 | srand( (unsigned)time(NULL) ); 14 | 15 | /* build and display the random values */ 16 | printf("%d random values, unsorted:\n",SIZE); 17 | for(a=0;a b, swap the values */ 30 | if( list[a] > list[b] ) 31 | { 32 | temp = list[a]; 33 | list[a] = list[b]; 34 | list[b] = temp; 35 | } 36 | } 37 | } 38 | 39 | /* display the sorted list */ 40 | printf("%d random values, sorted:\n",SIZE); 41 | for(a=0;a 2 | #include 3 | #include 4 | 5 | #define SIZE 100 6 | 7 | int compare(const void *a, const void *b) 8 | { 9 | return( *(int *)a - *(int *)b); 10 | } 11 | 12 | int main() 13 | { 14 | int list[SIZE]; 15 | int a; 16 | 17 | /* seed the randomizer */ 18 | srand( (unsigned)time(NULL) ); 19 | 20 | /* build and display the random values */ 21 | printf("%d random values, unsorted:\n",SIZE); 22 | for(a=0;a 2 | #include 3 | #include 4 | 5 | int compare(const void *a, const void *b) 6 | { 7 | return( *(int *)a - *(int *)b); 8 | } 9 | 10 | int main() 11 | { 12 | // seed the randomizer 13 | // allocate storage 14 | // populate the grid with random values, 0 through 99 15 | // quicksort the grid 16 | // output the grid 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /CH10/10_07/10_07-solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int compare(const void *a, const void *b) 6 | { 7 | return( *(int *)a - *(int *)b); 8 | } 9 | 10 | int main() 11 | { 12 | const int grow = 5; 13 | const int gcol = 5; 14 | const int gsize = grow * gcol; 15 | int *grid,x,y; 16 | 17 | /* seed the randomizer */ 18 | srand( (unsigned)time(NULL) ); 19 | 20 | /* allocate storage */ 21 | grid = malloc( sizeof(int) * gsize ); 22 | if( grid==NULL ) 23 | { 24 | puts("Unable to allocate storage"); 25 | exit(1); 26 | } 27 | 28 | /* populate the grid with random values, 0 through 99 */ 29 | for( x=0; x