Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Problem solving and programming Assignments

The document presents a C program that allows users to manage an array through a menu-driven interface. Users can read, print, sum elements, find the minimum number, and search for an element in the array. The program includes error handling for invalid inputs and provides a structured algorithm and flowchart to illustrate its functionality.

Uploaded by

ashrafsuhail1414
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Problem solving and programming Assignments

The document presents a C program that allows users to manage an array through a menu-driven interface. Users can read, print, sum elements, find the minimum number, and search for an element in the array. The program includes error handling for invalid inputs and provides a structured algorithm and flowchart to illustrate its functionality.

Uploaded by

ashrafsuhail1414
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Name : Ashraf Ahmed

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 2: // Print Array


if (size > 0) {
printf("The elements of the array are:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
} else {
printf("Array is empty. Please read the array
first.\n");
}
break;

case 3: // Print Addition of Array Elements


if (size > 0) {
sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
printf("The sum of the array elements is: %d\n",
sum);
} else {
printf("Array is empty. Please read the array
first.\n");
}
break;

case 4: // Find Minimum Number from Array


if (size > 0) {
min = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
printf("The minimum number in the array is: %d\n",
min);
} else {
printf("Array is empty. Please read the array
first.\n");
}
break;

case 5: // Search an Element in Array


if (size > 0) {
printf("Enter the element to search: ");
scanf("%d", &key);
found = 0;
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
printf("Element %d found at index %d
(position %d)\n", key, i, i + 1);
found = 1;
break;
}
}
if (!found) {
printf("Element %d not found in the array.\n",
key);
}
} else {
printf("Array is empty. Please read the array
first.\n");
}
break;

case 6: // Exit
printf("Exiting program. Goodbye!\n");
break;

default:
printf("Invalid choice! Please try again.\n");
}

} while (choice != 6);

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

Step Action Condition Update/Loop Output

1 Start - - -

2 Menu - - Show options

Read choice
3 - - -
=1

4 Read size = 5 Valid - -

Read arr = [4, 2, 8, 1,


5 - -
Elements 3]

6 Menu - - Show options


Step Action Condition Update/Loop Output

Read choice
7 - - -
=2

Check size >


8 - -
0

Loop i = 0 →
9 Print arr[i] 42813
4

10 Menu - - Show options

Read choice
11 - - -
=3

Check size >


12 sum = 0 -
0

Loop i = 0 →
13 sum += arr[i] sum = 18
4

14 Print sum - - Sum = 18

15 Menu - - Show options

Read choice
16 - - -
=4

Check size >


17 min = arr[0] = 4 -
0

Loop i = 1 → Update min if


18 min = 1
4 arr[i] < min
Step Action Condition Update/Loop Output

19 Print min - - Min = 1

20 Menu - - Show options

Read choice
21 - - -
=5

22 Read key = 8 - found = 0 -

Loop i = 0 → Check arr[i] ==


23 Found at i=2
4 key

Print key Found at 2


24 -
index (pos 3)

25 Menu - - Show options

Read choice
26 - Goodbye!
=6

27 End - - -

Loop in Sum Calculation (choice = 3)

i arr[i] sum (before) sum (updated)

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

Loop in Minimum Calculation (choice = 4)

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

Loop in Search (choice = 5, key = 8)

i arr[i] arr[i] == key? Found?

0 4

1 2

2 8 (Stop)
OUTPUT :
FLOWCHART :

You might also like