0% found this document useful (0 votes)
11 views6 pages

CS100 Pa5

This document outlines Assignment 5 for a computational problem-solving course in C++, focusing on string manipulation and loops. It includes seven programming tasks with specific requirements, such as creating patterns, calculating sums, and generating prime numbers, along with submission guidelines and learning outcomes. The assignment emphasizes the importance of original work and prohibits plagiarism.

Uploaded by

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

CS100 Pa5

This document outlines Assignment 5 for a computational problem-solving course in C++, focusing on string manipulation and loops. It includes seven programming tasks with specific requirements, such as creating patterns, calculating sums, and generating prime numbers, along with submission guidelines and learning outcomes. The assignment emphasizes the importance of original work and prohibits plagiarism.

Uploaded by

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

Computational Problem Solving Assignment 5

String manipulation and loops in C++


Deadline: 11:55PM, 9 March
Lead TAs: Amna Hassan and Haider Dawood Khan
Learning Outcomes

• Implement loops and string manipulation to solve computational problems.

• Translate problem descriptions into functional C++ code.

Submission Guidelines

• Attempt each task in a separate C++ source file (task#.cpp).

• Combine all .cpp files into a folder, zip it, and rename as 2XXXXXXX PA5.zip.

• Submit your zipped file via the designated LMS portal before the deadline.

Important Notes

• Copying or plagiarism is strictly prohibited.

• This assignment is a practical exercise to enhance your learning; dishonest practices will
impede your progress.

Problem 1: Number Triangle Pattern [20]


Write a program in C++ that takes an integer input rows and prints a right-angle triangle
pattern using numbers. The program should:

• Prompt the user to enter an integer.

• Print a right-angle triangle where each row contains the row number repeated accord-
ingly.

• Display the pattern based on the input number of rows.

Sample Output

Input number of rows: 5


1
22
333
4444
55555

1
Problem 2: Sum of First and Last Digits [10]
Write a program in C++ to find the sum of the first and last digits of any integer. The
program should:

• Prompt the user to enter an integer.

• Extract the first digit of the number.

• Extract the last digit of the number.

• Calculate the sum of the first and last digits.

• Display the first digit, the last digit, and their sum.

Sample Output

Input any number: 12345


The first digit of 12345 is: 1
The last digit of 12345 is: 5
The sum of first and last digit of 12345 is: 6

Problem 3: Summation of Odd Digits Using Loops [10]


Write a program that repeatedly asks the user for a positive integer and, for each input,
calculates the sum of all the odd digits in that number using loops. The program should
continue prompting the user until they enter a non-positive integer, at which point it should
terminate.

• Use a loop to repeatedly prompt the user for a positive integer.


• If the entered number is non-positive, terminate the program.
• Use another loop to process the digits of the number:
– Extract each digit (e.g., using % 10 and / 10).
– Check if the digit is odd; if so, add it to a sum.
• After processing all digits, output the sum of odd digits for the given number.
• Repeat the process until the user enters a non-positive integer.

Example
Input: 12345
Output: 9 (since 1 + 3 + 5 = 9)

Input: 24689
Output: 9

Input: 0
Output: (end program)

2
Problem 4: Power Calculation Without Built-In Functions [10]
Write a program in C++ to calculate and display the powers of a given number up to a
specified maximum exponent without using any built-in functions (such as pow). The program
should:

• Prompt the user for a base number.

• Prompt the user for the maximum exponent.

• Use loops to compute the power for each exponent from 0 up to the maximum exponent.

• Display the results in the format: base ^ exponent = result.

Sample Output

Input the base: 2


Input the max exponent: 5
2 ^ 0 = 1
2 ^ 1 = 2
2 ^ 2 = 4
2 ^ 3 = 8
2 ^ 4 = 16
2 ^ 5 = 32

Problem 5: Prime Number Generator [20]


Write a program that:

• Prompts the user to enter a positive integer N (1 ≤ N ≤ 100).

• Uses a nested loop to determine whether a number is prime.

• Prints the first N prime numbers in a single line, separated by spaces.

Example 1

Input:
Enter N: 10

Output:
2 3 5 7 11 13 17 19 23 29

Example 2

Input:
Enter N: 5

Output:
2 3 5 7 11

3
Hint A prime number is a number greater than 1 that has no divisors other than 1 and
itself. Use a nested loop to check divisibility and determine if a number is prime.

Problem 6: Frequency of Each Digit [20]

Write a program in C++ to determine the frequency of each digit (0 through 9) in a given
integer. The program should:

• Prompt the user to enter an integer.

• Analyze the number to count how many times each digit appears.

• Display the frequency for each digit from 0 to 9.

• NOTE: You must do this using nested loops without declaring a separate count variable
for each digit.

Sample Output

Input any number: 122345


The frequency of 0 = 0
The frequency of 1 = 1
The frequency of 2 = 2
The frequency of 3 = 1
The frequency of 4 = 1
The frequency of 5 = 1
The frequency of 6 = 0
The frequency of 7 = 0
The frequency of 8 = 0
The frequency of 9 = 0

Problem 7: String Manipulation: Replacing Spaces [30]


Part A: Replace Spaces and Display Characters

Write a C++ program that performs basic string manipulation. The program should:

1. Use getline(cin, str) to read an entire line of text (since cin stops at spaces).

2. Use a loop to iterate over the string and print each character on a new line. Use
str.length() to get the length of the string.

3. Use indexing (i.e., str[i]) to access each character.

4. Identify any spaces in the string and print the indices at which they are found.

5. Lastly, replace all spaces with an asterisk (*).

Guidelines and Examples:

4
• Reading Input: Use the following code to read a full line from the user:

string str;
getline(cin, str);

• Accessing Characters Using Indexing: To access and print each character of a string,
you can use a loop with indexing that iterates over the string’s length. str[0] gives
the first character in the string, str[1] gives the second character and so on and so
forth. If str is of length n, then the last character is at index n-1.
For example:

for (int i = 0; i < str.length(); i++)


{
cout << str[i];
}

This loop iterates over the entire string and prints each character (including spaces).

• Identifying Spaces and Replacing Them: You can check if a character is a space by
comparing it to ’ ’. To replace a space with an asterisk, use:

if (str[i] == ’ ’)
{
str[i] = ’*’;
}

Note: When you change str[i], the string itself is modified.

Sample Output:
Input a string: Hello World!
H
e
l
l
o

W
o
r
l
d
!
Spaces found at indices: 5
Modified string: Hello*World!

5
Part B: Print the String in Reverse

Based on what you learned in Part A, extend your program to print the entire (modified)
string in reverse order. Use a loop to access characters in reverse (starting from the last
index and moving to the first).

Sample Output (using the modified string from Part A)

Reversed String: !dlroW*olleH

You might also like