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

C++ Programming Exercises on Arrays

The document outlines a worksheet for CSSE-133 Programming Fundamentals I at MicroLink Information Technology College, detailing programming tasks involving arrays, pointers, and strings. It includes exercises such as replacing negative numbers with their absolute values, finding minimum values, removing items from arrays, and creating histograms. Each task requires writing and testing C++ programs to demonstrate understanding of fundamental programming concepts.

Uploaded by

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

C++ Programming Exercises on Arrays

The document outlines a worksheet for CSSE-133 Programming Fundamentals I at MicroLink Information Technology College, detailing programming tasks involving arrays, pointers, and strings. It includes exercises such as replacing negative numbers with their absolute values, finding minimum values, removing items from arrays, and creating histograms. Each task requires writing and testing C++ programs to demonstrate understanding of fundamental programming concepts.

Uploaded by

frodereje
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

MicroLink Information Technology College

Department of Computer Science


CSSE-133 Programming Fundamentals I
Worksheet 6 (Array/Pointer/String)
1. Write and test a program that replaces all the negative numbers in an array of integers with
their absolute values.

2. Write and test a program that returns the minimum value stored in an array.

3. Write and test a program that returned the index of the minimum value stored in an array.

4. Write and test a C++ program that attempts to remove an item from an array:

5. Write and test a program that adds element-wise 2 one-dimensional int arrays of the same
size. For example, if the two given arrays are
22 33 44 55
and
7 4 1 -2
then the third array would be assigned
29 37 45 53
6. Write a C++ program that accepts any word from keyboard to display the word’s reverse.

Example: Input : Ethiopia

Output: aipoihtE

7. Write a program that reads a sequence of words from the console until the word stop is
entered, and then prints the reversed sequence (without the final stop).

Example: Input: Gallia est omnis divisa in partes tres stop

Output: tres partes in divisa omnis est Gallia

8. Write a program that reads a sequence of positive integers (until the user enters a non-
positive number) and then prints a histogram of all the integer value between the smallest and
the largest integer the user has entered, i.e., for each such value the number of occurrences of
that value in the sequence entered by the user.

Example: Input: 5 10 12 7 10 10 5 11 7 12 10 0

Output:
MicroLink Information Technology College
Department of Computer Science
CSSE-133 Programming Fundamentals I
Worksheet 6 (Array/Pointer/String)
1. Write and test a program that replaces all the negative numbers in an array of integers with
their absolute values.

2. Write and test a program that returns the minimum value stored in an array.

3. Write and test a program that returned the index of the minimum value stored in an array.

4. Write and test a C++ program that attempts to remove an item from an array:

5. Write and test a program that adds element-wise 2 one-dimensional int arrays of the same
size. For example, if the two given arrays are
22 33 44 55
and
7 4 1 -2
then the third array would be assigned
29 37 45 53
6. Write a C++ program that accepts any word from keyboard to display the word’s reverse.

Example: Input : Ethiopia

Output: aipoihtE

7. Write a program that reads a sequence of words from the console until the word stop is
entered, and then prints the reversed sequence (without the final stop).

Example: Input: Gallia est omnis divisa in partes tres stop

Output: tres partes in divisa omnis est Gallia

8. Write a program that reads a sequence of positive integers (until the user enters a non-
positive number) and then prints a histogram of all the integer value between the smallest and
the largest integer the user has entered, i.e., for each such value the number of occurrences of
that value in the sequence entered by the user.

Example: Input: 5 10 12 7 10 10 5 11 7 12 10 0

Output:

Common questions

Powered by AI

The algorithm involves reading a sequence of words, storing them until a designated stop word is entered, and then reversing their order before printing. This approach can be useful in applications involving linguistic processing, such as reversing word orders to analyze syntactic structures or exploring patterns in streams of data. The algorithm's time complexity is O(n), where n is the number of words, as it requires traversing the list twice—once to collect the words and once to reverse them .

To print a histogram representing occurrences of integer values, first gather all input numbers and determine the range by identifying the smallest and largest integers. Use a frequency array or map to count occurrences within this range. Efficient handling involves creating bins for each integer and incrementing counts as numbers are processed. Challenges include managing space efficiently for large ranges and ensuring that the data structure can quickly accommodate dynamic data without repetitive traversal, maintaining an overall time complexity of O(n).

To remove an item from an array, locate the index of the desired item, then shift all subsequent elements one position to the left to overwrite the removed item. This effectively reduces the logical size of the array by one element. This approach modifies the original array structure, which may lead to fragmentation if not handled with care. Array-based data structures do not support dynamic resizing, so physically reducing the array size is not possible without creating a new array. The operation has a worst-case time complexity of O(n), where n is the number of elements in the array due to the need to shift elements .

Element-wise addition of two arrays involves iterating through both arrays simultaneously and summing corresponding elements to produce a third array. The computational implication is that this operation can be performed in O(n) time, where n is the length of the arrays. The primary benefit is efficiency in computations, as it leverages parallel processing capabilities and ensures that each pair of elements is processed independently, making it suitable for large-scale vectorized operations .

To replace all negative numbers in an array with their absolute values, you can iterate over the array and conditionally change each element to its absolute value using a loop. The algorithm iterates through each element, checks if it is negative, and replaces it with its positive equivalent if necessary. The time complexity of this approach is O(n), where n is the number of elements in the array, because you need to process each element once .

To display the reverse of a word, you can implement a program that iterates over the characters of the word from the end to the beginning and appends each character to a new string. Alternatively, languages with built-in string functions can use a direct method to reverse strings. The main space complexity consideration is O(n), where n is the number of characters in the word, due to the creation of a new reversed string in memory .

Building a histogram with arrays suits scenarios where the range of data values is known and contiguous, allowing for fast access and updates in constant time O(1). This can be inefficient for large ranges with sparse data. Hashmaps excel with sparse and dynamic ranges, though they incur a greater overhead due to hash operations, with average time complexity of O(1) but potential for O(n) in poor hash distributions. Arrays are preferable for dense ranges of small, known bounds, while hashmaps are better suited for data with unpredictable, sparse distributions .

Efficiently reversing a sequence of words involves reading the words into a data structure supporting dynamic insertion in reverse order, such as a stack. Words are pushed onto the stack as they are read, and popping them returns them in reverse order. This stack-based algorithm has a time complexity of O(n), where n is the number of words, due to single-pass reading and stack operations. Using a list to collect words for subsequent reverse iteration provides similar efficiency; the choice depends on language-specific structure implementations .

To develop a program that returns both the minimum value and its index in an array, iterate through the array while keeping track of the minimum value found and its corresponding index. Initialize two variables to hold the minimum value and index; as you traverse the array, update these variables whenever you encounter a smaller value. The challenge is ensuring that both values are updated simultaneously and correctly, especially in cases where multiple occurrences of the minimum value exist. The implementation should have a time complexity of O(n) as it involves a single pass through the array .

Pointers and arrays offer different advantages for sequence and string manipulation. Pointers provide flexibility and control over memory, enabling dynamic memory management and efficient traversal through address arithmetic. However, they are prone to errors such as memory leaks and undefined behavior. Arrays, while statically sized and less flexible, offer simplicity for operations where bounds are known and immutable. Choice depends on project needs; arrays suit fixed-size, clear-bound scenarios, while pointers excel in dynamic, flexible memory-intensive tasks. Choosing between them involves trade-offs in complexity, safety, and performance .

You might also like