Problem Solving in C and Python: Programming Exercises and Solutions, Part 1
By Yana Kortsarts, Yulia Kempner, Leonid Kugel and
4.5/5
()
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.
Related to Problem Solving in C and Python
Related ebooks
Python Programming For Beginners: Learn The Basics Of Python Programming (Python Crash Course, Programming for Dummies) Rating: 5 out of 5 stars5/5Learning Python Rating: 5 out of 5 stars5/5Computer Programming In C Language Rating: 4 out of 5 stars4/5C language Programming: Simple, Short, and Straightforward Way of Learning C Programming Rating: 3 out of 5 stars3/5Learn C++ Rating: 4 out of 5 stars4/5C Programming Language Essentials Rating: 4 out of 5 stars4/5Python Programming: Your Advanced Guide To Learn Python in 7 Days Rating: 0 out of 5 stars0 ratingsPython: Advanced Guide to Programming Code with Python: Python Computer Programming, #4 Rating: 0 out of 5 stars0 ratingsPython: Programming For Intermediates: Learn The Basics Of Python In 7 Days! Rating: 0 out of 5 stars0 ratingsC++ Learn in 24 Hours Rating: 0 out of 5 stars0 ratingsC Programming Concepts Rating: 0 out of 5 stars0 ratingsProgramming Concepts in C++ Rating: 0 out of 5 stars0 ratingsPython 3 Programming: A Beginner Crash Course Guide to Learn Python 3 in 1 Week Rating: 3 out of 5 stars3/5The 1 Page Python Book Rating: 2 out of 5 stars2/5C++ Programming Language Rating: 0 out of 5 stars0 ratingsIntroduction to Programming Languages Rating: 4 out of 5 stars4/5Python for Beginners: An Introduction to Learn Python Programming with Tutorials and Hands-On Examples Rating: 4 out of 5 stars4/5Python Essentials Rating: 5 out of 5 stars5/5Computer Programming Using C Rating: 0 out of 5 stars0 ratingsPython Handbook For Beginners. A Hands-On Crash Course For Kids, Newbies and Everybody Else Rating: 0 out of 5 stars0 ratingsPython for Beginners: Learn It as Easy as Pie Rating: 0 out of 5 stars0 ratingsPython: Journey from Novice to Expert Rating: 5 out of 5 stars5/5Introduction to Algorithms Rating: 0 out of 5 stars0 ratingsPython Programming: Your Beginner’s Guide To Easily Learn Python in 7 Days Rating: 3 out of 5 stars3/5Python: Advanced Guide to Programming Code with Python Rating: 0 out of 5 stars0 ratingsC++: A Beginner's Guide, Second Edition Rating: 0 out of 5 stars0 ratingsBeginning C++ Programming Rating: 3 out of 5 stars3/5
Programming For You
Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Game Development with Unreal Engine 5: Learn the Basics of Game Development in Unreal Engine 5 (English Edition) Rating: 0 out of 5 stars0 ratingsHTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards Rating: 0 out of 5 stars0 ratingsThe Unofficial Guide to Open Broadcaster Software: OBS: The World's Most Popular Free Live-Streaming Application Rating: 0 out of 5 stars0 ratingsCoding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Learn JavaScript in 24 Hours Rating: 3 out of 5 stars3/5Python Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratingsLearn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsLinux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Web Designer's Idea Book, Volume 4: Inspiration from the Best Web Design Trends, Themes and Styles Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Python GUI Programming Cookbook - Second Edition Rating: 5 out of 5 stars5/5
Reviews for Problem Solving in C and Python
2 ratings0 reviews
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.