0% found this document useful (0 votes)
16 views

c Programs

The document contains two C programs: the first dynamically allocates and manages employee records, allowing input of employee details and displaying them afterward. The second program provides a menu-driven interface for various array operations, including input, output, insertion, deletion, and sorting. Both programs demonstrate memory management and user interaction in C programming.

Uploaded by

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

c Programs

The document contains two C programs: the first dynamically allocates and manages employee records, allowing input of employee details and displaying them afterward. The second program provides a menu-driven interface for various array operations, including input, output, insertion, deletion, and sorting. Both programs demonstrate memory management and user interaction in C programming.

Uploaded by

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

Modified Programs with Outputs

Program 1: Dynamically Allocate Employee Records

#include <stdio.h>
#include <stdlib.h>

struct emp {
char name[30];
int age;
double salary;
};

int main() {
int n;
printf("Enter the number of employees: ");
scanf("%d", &n);

struct emp *employees = calloc(n, sizeof(struct emp));


if (!employees) return printf("Memory allocation failed\n"), 1;

for (int i = 0; i < n; i++) {


printf("\nEmployee %d details:\nName: ", i + 1);
scanf("%s", employees[i].name);
printf("Age: ");
scanf("%d", &employees[i].age);
printf("Salary: ");
scanf("%lf", &employees[i].salary);
}

printf("\nEmployee Records:\n");
for (int i = 0; i < n; i++)
printf("\nEmployee %d\nName: %s\nAge: %d\nSalary: %.2f\n", i + 1,
employees[i].name, employees[i].age, employees[i].salary);

free(employees);
return 0;
}

Output:
Input:
Enter the number of employees: 1

For Employee 1:
Name: Ram Bisht
Age: 19
Salary: 50000.50

Employee Records:

Employee 1
Name: Ram Bisht
Age: 19
Salary: 50000.50

Program 2: Menu-driven Array Operations

#include <stdio.h>
#include <stdlib.h>

void displayMenu() {
printf("1. Input Array\n2. Output Array\n3. Insert Element\n4. Delete Element\n5. Sort
Ascending\n6. Sort Descending\n0. Exit\n");
}

int* inputArray(int* size) {


printf("Enter number of elements: ");
scanf("%d", size);
int* arr = malloc(*size * sizeof(int));
for (int i = 0; i < *size; i++) scanf("%d", &arr[i]);
return arr;
}

void outputArray(int* arr, int size) {


printf("Array: ");
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n");
}

int* insertElement(int* arr, int* size, int value, int pos) {


arr = realloc(arr, (*size + 1) * sizeof(int));
for (int i = *size; i > pos; i--) arr[i] = arr[i - 1];
arr[pos] = value;
(*size)++;
return arr;
}

int* deleteElement(int* arr, int* size, int pos) {


for (int i = pos; i < *size - 1; i++) arr[i] = arr[i + 1];
(*size)--;
return realloc(arr, *size * sizeof(int));
}

void sortAscending(int* arr, int size) {


for (int i = 0; i < size - 1; i++)
for (int j = 0; j < size - i - 1; j++)
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}

void sortDescending(int* arr, int size) {


for (int i = 0; i < size - 1; i++)
for (int j = 0; j < size - i - 1; j++)
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}

int main() {
int *arr = NULL, size = 0, choice, value, pos;
do {
displayMenu();
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: free(arr); arr = inputArray(&size); break;
case 2: outputArray(arr, size); break;
case 3:
printf("Enter value and position: ");
scanf("%d %d", &value, &pos);
arr = insertElement(arr, &size, value, pos);
break;
case 4:
printf("Enter position: ");
scanf("%d", &pos);
arr = deleteElement(arr, &size, pos);
break;
case 5: sortAscending(arr, size); break;
case 6: sortDescending(arr, size); break;
case 0: break;
default: printf("Invalid choice\n");
}
} while (choice != 0);
free(arr);
return 0;
}

Output:

Menu:
1. Input Array
2. Output Array
3. Insert Element
4. Delete Element
5. Sort Ascending
6. Sort Descending
0. Exit

Input: 1
Array: 1 2 3
Choice: 3 (Insert 4 at Position 2)
Array: 1 4 2 3
Sorted Ascending: 1 2 3 4
Sorted Descending: 4 3 2 1

You might also like