Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Problem Solving in C and Python: Programming Exercises and Solutions, Part 1
Problem Solving in C and Python: Programming Exercises and Solutions, Part 1
Problem Solving in C and Python: Programming Exercises and Solutions, Part 1
Ebook279 pages1 hour

Problem Solving in C and Python: Programming Exercises and Solutions, Part 1

Rating: 4.5 out of 5 stars

4.5/5

()

Read preview

About this ebook

Problem Solving in C and Python: Programming Exercises and Solutions Part 1
Authors: Yana Kortsarts, Yulia Kempner, Leonid Kugel, Adam Fischbach (Editor)
Cover Designers: Zuny Jamatte & Michal Kortsarts
This book is a collection of exercises for the introductory programming course. We are focusing on two programming languages: Python and C. Each chapter provides a short overview of the necessary theoretical material for both programming languages, sample solutions written in C and Python 3 (version 3.5.1, the most recent version of Python at the time the book was written), and a list of practice exercises in increasing order of difficulty. This book covers basic programming concepts such as input/output, decision structures and repetition structures (loops).
The main purpose of the textbook is to promote effective development of the problem solving skills through an extended guided analysis phase of the software design cycle. Special consideration is devoted to word problems, the most challenging type of exercises, requiring translation of the “story” into programming language. Sample solutions in Python and C include detailed analysis and explanation of all steps in the problem solving process. All solutions follow the proposed problem solving approach which starts from extensive analysis of the input and output data, including their limitations, and then step by step transition from solution to final result.
This book is based on the extensive experience of the authors teaching introductory programming courses in Python and C.
How to Use this Book
For instructors, the books provides am extensive list of programming exercises that could be incorporated into laboratory or/and practice sessions accompanying introductory programming in Python or/and C courses.
For students, the book provides an extensive list of solved problems to learn from and practice problems.
If you are a student taking an introductory programming course in Python or/and C this book will serve as a great complimentary material to the lecture/theory material covered in class.
If you are an instructor teaching an introductory programming course in Python or/and C this book will serve as a great source for practice exercises, quizzes, and exams.
Chapter Structure
Chapter 1 covers input and output
Chapter 2 covers decision structures including switch programming structure in C
Chapter 3 covers repetition structures – loops, including nested loops.
Disclaimer: most of the problems are not original problems; in some cases the appropriate references are provided, but in many cases the problems are drawn from mathematical and programming folklore.

LanguageEnglish
Release dateJul 17, 2018
ISBN9780463579053
Problem Solving in C and Python: Programming Exercises and Solutions, Part 1

Related to Problem Solving in C and Python

Related ebooks

Programming For You

View More

Related articles

Reviews for Problem Solving in C and Python

Rating: 4.5 out of 5 stars
4.5/5

2 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Problem Solving in C and Python - Yana Kortsarts

    This book is a collection of exercises for the introductory programming course. We are focusing on two programming languages: Python and C. Each chapter provides a short overview of the necessary theoretical material for both programming languages, sample solutions written in C and Python 3 (version 3.5.1, the most recent version of Python at the time the book was written), and a list of practice exercises in increasing order of difficulty.

    Disclaimer: most of the problems are not original problems; in some cases the appropriate references are provided, but in many cases the problems are drawn from mathematical and programming folklore.

    Chapter 1: Variables, Input/Output, and Arithmetic Operators

    I. Theory Overview

    1. Variables

    Variables are placeholders for some values. Programs utilize variables to store data. Variables have three attributes: type, name, and value. In addition, each variable has a memory address. Memory addresses are used to access the variable to perform the following tasks: assign an initial value through input or declaration statements, retrieve the current value, and change the value. In most cases, the use of memory will be done automatically, but some statements require using specific syntax discussed later in this chapter.

    Variables in Python

    In Python, initialization is required. The type of the variable will be determined automatically based on the assigned value. There are two basic types: numeric and strings. Numeric types include integers (int) and floating point numbers (float). A string is a sequence of characters. String literals are enclosed in the single or double quotes. A character is not a separate data type but a string of exactly one character.

    Examples:

    Variables in C

    In C, declaration is required prior using the variable and initialization is optional. In C, there are four standard signed integer types: char, short, int, and long; there are three real floating point types: float, double, and long double.

    Declaration Statements Examples:

    Declaration and Initialization Statements Examples:

    2. Input/Output

    Input in Python

    The input function is used to perform standard input – input from the keyboard.

    Output in Python

    The print function is used to perform standard output – output to the screen. To print text, single ('), double (), or triple () quotes could be used. Triple quotes allow text to be printed on multiple lines. To print the value of the variable, use the name of the variable without any quotes. To format output, use the format function.

    Basic Escape Sequences in Python

    The backslash (\) character is used to escape characters that otherwise have a special meaning in Python.

    Examples:

    Examples:

    For more information on printing and formatting floating point numbers see: https://docs.python.org/3/tutorial/floatingpoint.html

    Input in C

    The scanf function is used to perform standard input – input from the keyboard. For the variable types we have introduced so far, the general syntax for a scanf statement is as follows:

    scanf("list of format specifiers", list of variables);

    Note:

    1. In the list of variables, each variable preceded by the ampersand sign (&) which denotes the address of the variable in the computer’s memory. For the variable types we have introduced so far, the ampersand sign is required before each variable name in order for scanf to be able to store the input value. Omitting the ampersand sign will not cause a compiler error, but instead can cause a segmentation fault, a run-time error which is a program error that occurs while the program is running. Run-time errors cause the immediate exit (termination) of the program.

    2. Most C compilers will not check the correspondence between the format specifier and the type of the variable. A format type mismatch will not cause a compiler error, but instead will result in a logic error. Logic errors will not terminate the program, but will cause the program to produce the wrong output.

    3. We advise not to include spaces or any other characters and numbers between format specifiers to avoid run-time and/or logic errors. Inconsistency between the scanf format string and the format your data is entered will result in run-time and/or logic errors. For example, the following statement,

    scanf(%d,%d,%d, &a, &b, &c);

    requires input integers to be separated by commas, otherwise a run-time and/or logic error occurs.

    4. For the variable types we have introduced so far, the scanf format specifiers, %d, %f, %lf, and %Lf, skip all leading whitespace characters and terminate on whitespace characters. In addition, %d terminates on any non-digit character. The format specifier, %c, does read whitespaces. This fact requires careful treatment while reading characters after numeric data.

    Basic scanf Formatting Options

    Examples:

    Reading characters after numeric data

    In addition, there are several functions that read a single character. In this book we will limit our use to the getchar() function that reads a character from standard input.

    Example:

    char c;

    c=getchar();

    Note: the storage sizes of the variables in C are platform dependent.

    Output in C

    The printf()function is used to perform standard output – output to the screen. To print text, the text must be enclosed in double quotes ("). To print the value of a variable, use the corresponding format specifier and the name of the variable without any quotes.

    The general syntax for the printf statement is as follows:

    printf("list of format specifiers", list of variables);

    Note: Most C compilers will not check the correspondence between format specifier and the type of the variable. A format type mismatch will result in logical errors.

    Basic printf Formatting Options

    Basic Escape Sequences in C

    The backslash (\) character is used to escape characters that otherwise have a special meaning in C.

    Enjoying the preview?
    Page 1 of 1