Problem solving and programming Assignments
Problem solving and programming Assignments
Roll No.: 09
PRN : 12412991
Class : CSDS-A (FY)
Assignment 4
#include <stdio.h>
int main() {
int arr[100], size = 0, choice, sum, min, key, found;
do {
// Display the menu
printf("\nMenu:\n");
printf("1. Read Array\n");
printf("2. Print Array\n");
printf("3. Print Addition of Array Elements\n");
printf("4. Find Minimum Number from Array\n");
printf("5. Search an Element in Array\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Read Array
printf("Enter the size of the array: ");
scanf("%d", &size);
if (size <= 0 || size > 100) {
printf("Invalid size! Please enter a value between 1
and 100.\n");
break;
}
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
break;
case 6: // Exit
printf("Exiting program. Goodbye!\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
return 0;
}
Algorithm :
1. Start
2. Initialize an array arr[100] and variable size = 0.
3. Repeat until user chooses Exit (choice = 6):
• Display Menu with options (Read, Print, Sum, Minimum,
Search, Exit).
• Take user input for choice.
4. Perform action based on choice:
• Case 1: Read Array
1. Take input for size.
2. If size is valid, take size number of inputs and store in
arr[].
• Case 2: Print Array
1. If array is not empty, print all elements.
2. Else, show "Array is empty" message.
• Case 3: Sum of Array Elements
1. If array is not empty, initialize sum = 0.
2. Loop through array, add each element to sum.
3. Print sum.
• Case 4: Find Minimum Number
1. If array is not empty, set min = arr[0].
2. Loop through array and update min if a smaller value
is found.
3. Print min.
• Case 5: Search an Element
1. If array is not empty, take input key.
2. Loop through array and check if arr[i] == key.
3. If found, print position and exit loop.
4. If not found, show "Element not found" message.
• Case 6: Exit
o Print "Goodbye" and End program.
5. End
Step-by-Step
1 Start - - -
Read choice
3 - - -
=1
Read choice
7 - - -
=2
Loop i = 0 →
9 Print arr[i] 42813
4
Read choice
11 - - -
=3
Loop i = 0 →
13 sum += arr[i] sum = 18
4
Read choice
16 - - -
=4
Read choice
21 - - -
=5
Read choice
26 - Goodbye!
=6
27 End - - -
0 4 0 4
1 2 4 6
2 8 6 14
3 1 14 15
i arr[i] sum (before) sum (updated)
4 3 15 18
min (updated if
i arr[i] min (before)
smaller)
1 2 4 2
2 8 2 2
3 1 2 1
4 3 1 1
0 4
1 2
2 8 (Stop)
OUTPUT :
FLOWCHART :