EXERCISE 5 PC File
EXERCISE 5 PC File
❖ Input Function
Define inputMatrix() to:
Accept rows and columns.
Use nested loops to input matrix elements.
❖Display Function
Define printMatrix() to:
Accept matrix and dimensions.
Use nested loops to print each element.
❖Add Function
Define addMatrices() to:
Take two matrices.
Perform element-wise addition.
Store and return result.
❖Subtract Function
Define subtractMatrices() to:
Take two matrices.
❖Transpose Function
Define transposeMatrix() to:
Swap rows and columns of input matrix.
3. In main() function:
4. If choice is 6:
Learning Outcomes:
2.By breaking down the logic into functions, understood how to write
modular, reusable, and maintainable code.
\
Output:
Problem Statement: Write a program to copy a string
Algorithm:
1. Start
2. Input a string from the user and store it in array a.
3. Initialize index i = 0.
4. Copy each character from a[i] to b[i] until a[i] =='\0'.
5. Terminate b with '\0 and display the copied string.
6. End
Source Code:
Output:
Problem Statement: Write a program to reverse a string.
Algorithm:
1. Start
2. Input a string from the user and store it in array a.
3. Find the length of the string by traversing until the null character
('\0').
4. Copy characters from the end of a to the beginning of b using
reverse indexing.
5. Add null character at the end of b, and display the reversed string.
6. End
Source Code:
Output:
EXERCISE 8
Problem Statement: Write a program to perform the following operations on
strings without using string functions.
Theory:
This C program is designed to perform various string operations—input, display, find
length, copy, and reverse—using modular functions and a menu-driven interface.
Each operation is encapsulated in a separate function to promote clarity, reusability,
and maintainability. The user interacts with the program through a looping menu that
calls the appropriate function based on the user's choice.
Algorithm:
1. Start
2. Define functions for string input, output, length calculation, copy, and
reverse.
3. Display menu repeatedly and read user’s choice:
4.If choice is:
1 → Input string
2 → Print string
3 → Calculate and display length
4 → Copy string and display copy
5 → Reverse string and display result
6 → Exit program
5. Use switch-case to call appropriate functions based on user input.
6. Loop until the user selects Exit.
7. End.
Source Code:
Output:
Learning Outcomes:
1.By breaking down the logic into functions, I understood how to write modular,
reusable, and maintainable code.
2.Gained a solid foundation in string concepts such as print, copy,reverse.