CPSC 1420: Programming and Problem Solving I: 5 credit class
About
Introduction to programming and computing fundamentals, with emphasis on problem solving, design and style.
Prior programming experience is not required.
This course introduces fundamental programming concepts such as functional decomposition, stepwise refinement, control structures, loops, functions, primitive and aggregate data types.
Prerequisites
MATH 1021 or higher (C- or better) - Must be completed prior to taking this course.
Primitive Data Types
Primitive data types specify the size and type of variable values.
They are the building blocks of data manipulation and cannot be further divided into simpler data types.
This includes Integer, Character, Boolean, Floating Point, Double Floating Point, Valueless or Void, and Wide Character.
Integer: 1, 2, 3...
Character: a, b, c...
Boolean: True, False
Floating Point: 3.14, 0.5, 0.10...
Double Floating Point: 1.7E - 308 to 1.7E + 308
Aggregate Data Types
An aggregate type is a class, structure, union, or array type.
It can hold multiple values of different or same types.
Array: int arr[5] = {1, 2, 3, 4, 5};
Struct: struct Person {string name; int age; double height;};
Class: class Car {public: string make; string model; int year;};
Union: union Data {int intValue; float floatValue; char charValue;};
Loops
Loops are control structures that allow you to execute a block of code repeatedly based on a condition.
C++ supports several types of loops: for, while, and do-while.
'for' loop: is commonly used when the number of iterations is known beforehand.
'while' loop: is used when the number of iterations is not known beforehand and depends on a condition.
'do-while' loop: is similar to the while loop, but it guarantees that the code block will be executed at least once.
for (int i = 0; i < 5; i++) {
std::cout << "i = " << i << std::endl;
}
int i = 0;
while (i < 5) {
std::cout << "i = " << i << std::endl;
i++;
}
int i = 0;
do {
std::cout << "i = " << i << std::endl;
i++;
} while (i < 5);
Functions
A function is a block of code that performs a specific task.
Functions allow you to break down a program into smaller, manageable pieces.
This modular approach helps improve code readability, reusability, and maintainability.
Function Components:
Return Type: Specifies the type of value the function returns. If the function does not return a value, the return type is void.
Function Name: An identifier for the function.
Parameters: Input values passed to the function. These are optional and defined within parentheses.
Function Body: The block of code that defines what the function does. Enclosed in curly braces {}.
Syntax:
return_type function_name(parameter_list) {
// Function body
}