0% found this document useful (0 votes)
9 views7 pages

Comp Assignment

The document contains programming examples in C++ covering various topics such as structures, variables, constants, boolean expressions, arrays, and control structures. It includes code snippets demonstrating user input, conditional logic, and array manipulation. Additionally, it explains the differences between sequential, selection, and repetition control structures.

Uploaded by

kinyuarisper2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Comp Assignment

The document contains programming examples in C++ covering various topics such as structures, variables, constants, boolean expressions, arrays, and control structures. It includes code snippets demonstrating user input, conditional logic, and array manipulation. Additionally, it explains the differences between sequential, selection, and repetition control structures.

Uploaded by

kinyuarisper2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

RISPER KINYUA

EB13/74168/24

QUESTION 1

#include <iostream>

#include <string>

using namespace std;

// Define the structure

struct Person {

string name;

int age;

};

int main() {

Person user; // Create an instance of the structure

// Get input from the user

cout << "Enter your name: ";

getline(cin, user.name);

cout << "Enter your age: ";

cin >> user.age;

// Display the stored information

cout << "\nStored Information:" << endl;

cout << "Name: " << user.name << endl;


cout << "Age: " << user.age << endl;

return 0;

QUESTION 2

1. Variable:

o A variable is a storage location whose value can change during the execution of a
program.

o It allows flexibility and is used to store dynamic data.

o Example:

o int age = 25; // 'age' is a variable and can be reassigned later

o age = 30; // Now the value of 'age' is updated to 30

2. Constant:

o A constant is a storage location whose value cannot change once it's defined.

o It provides fixed data that remains the same throughout program execution.

o Example:

o const double pi = 3.14159; // 'pi' is a constant and cannot be reassigned

o // Attempting to change 'pi' will result in a compilation error

Variables are dynamic, while constants are immutable. Let me know if you'd like to explore more
examples!

QUESTION 3

Boolean expressions are essential in programming as they evaluate to either true or false, enabling
conditional logic and decision-making. They are used in:

1. Control Structures: Driving if, while, and for loops.

2. Validation: Ensuring user inputs or program conditions meet specific requirements.

3. Optimizing Code: Simplifying complex logic into manageable expressions.


Boolean expressions are fundamental in making programs flexible, interactive, and capable of dynamic
responses.

Boolean Expression Example

bool isEven = (number % 2 == 0); // True if the number is even

bool isOdd = (number % 2 != 0); // True if the number is odd

Explanation:

 number % 2 == 0: Checks if the remainder of the number divided by 2 is zero (indicating it's
even).

 number % 2 != 0: Checks if the remainder is not zero (indicating it's odd).

You can use these expressions in a program like this:

#include <iostream>

using namespace std;

int main() {

int number;

cout << "Enter a number: ";

cin >> number;

if (number % 2 == 0) {

cout << number << " is even." << endl;

} else {

cout << number << " is odd." << endl;

}
return 0;

QUESTION 4

#include <iostream>

using namespace std;

// Function to initialize the array and calculate its sum

int calculateSum(int arr[], int size) {

int sum = 0;

for (int i = 0; i < size; i++) {

sum += arr[i];

return sum;

int main() {

const int arraySize = 10; // Size of each array

int arrays[10][arraySize]; // Declare 10 arrays, each of size 10

int sums[10]; // Array to store the sums of each array

// Initialize the arrays with values and calculate sums

for (int i = 0; i < 10; i++) {

cout << "Initializing array " << i + 1 << ":\n";

for (int j = 0; j < arraySize; j++) {

arrays[i][j] = i * 10 + j; // Example initialization


cout << arrays[i][j] << " ";

cout << endl;

sums[i] = calculateSum(arrays[i], arraySize); // Calculate sum

// Display results

cout << "\nSums of all arrays:\n";

for (int i = 0; i < 10; i++) {

cout << "Sum of array " << i + 1 << ": " << sums[i] << endl;

return 0;

QUESTION 5

1. Sequential Control Structure

 Description: Instructions are executed in the order they appear, one after another.

 Example:

 START

 Display "Enter two numbers:"

 Read number1, number2

 sum = number1 + number2

 Display "The sum is:", sum

 END
2. Selection Control Structure (Conditional)

 Description: Decisions are made based on conditions, using statements like if, else, or switch.

 Example:

 START

 Display "Enter a number:"

 Read number

 IF number > 0 THEN

 Display "Positive number"

 ELSE IF number < 0 THEN

 Display "Negative number"

 ELSE

 Display "Zero"

 ENDIF

 END

3. Repetition Control Structure (Looping)

 Description: Repeated execution of a block of code using loops like while, for, or do while.

 Example:

 START

 Set counter = 1

 WHILE counter <= 5 DO

 Display "Counter:", counter

 counter = counter + 1

 ENDWHILE

 END

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy