0% found this document useful (0 votes)
35 views9 pages

Assigement

Uploaded by

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

Assigement

Uploaded by

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

Assigement:

Clo_2:
Code:

#include <stdio.h>

#include <stdlib.h>

// Structure for an Employee

Struct Employee {

Int id;

Char name[50];

Float salary;

};

// Function to input employee data

Void inputEmployeeData(struct Employee *emp, int n) {

Printf(“Enter employee details:\n”);

For (int I = 0; I < n; i++) {

Printf(“Employee %d:\n”, I + 1);

Printf(“Enter ID: “);

Scanf(“%d”, &emp[i].id);

Printf(“Enter Name: “);

Scanf(“ %[^\n]”, emp[i].name);

Printf(“Enter Salary: “);

Scanf(“%f”, &emp[i].salary);
Printf(“\n”);

// Function to display employee data

Void displayEmployeeData(struct Employee *emp, int n) {

Printf(“Employee Details:\n”);

Printf(“------------------------------------\n”);

Printf(“ID\tName\tSalary\n”);

Printf(“------------------------------------\n”);

For (int I = 0; I < n; i++) {

Printf(“%d\t%s\t%.2f\n”, emp[i].id, emp[i].name, emp[i].salary);

Printf(“------------------------------------\n”);

// Function to sort employees by salary (Bubble Sort)

Void sortEmployeesBySalary(struct Employee *emp, int n) {

For (int I = 0; I < n – 1; i++) {

For (int j = 0; j < n – I – 1; j++) {

If (emp[j].salary > emp[j + 1].salary) {

// Swap employees

Struct Employee temp = emp[j];

Emp[j] = emp[j + 1];

Emp[j + 1] = temp;

}
}

// Function to swap two employees

Void swap(struct Employee *a, struct Employee *b) {

Struct Employee temp = *a;

*a = *b;

*b = temp;

Int main() {

Int n;

Printf(“Enter the number of employees: “);

Scanf(“%d”, &n);

// Allocate memory for the employee array

Struct Employee *emp = (struct Employee *)malloc(n * sizeof(struct Employee));

// Input employee data

inputEmployeeData(emp, n);

// Sort employees by salary

sortEmployeesBySalary(emp, n);

// Display sorted employee data


displayEmployeeData(emp, n);

// Free allocated memory

Free(emp);

Return 0;

Output:

Enter the number of employees: 3

Enter employee details:

Employee 1:

Enter ID: 123456

Enter Name: suga

Enter Salary: 50000

Employee 2:

Enter ID: 234567

Enter Name: jhope

Enter Salary: 56000

Employee 3:

Enter ID: 345678

Enter Name: rm

Enter Salary: 78000

Employee Details:
ID Name Salary

123456 suga 50000.00

234567 jhope 56000.00

345678 rm 78000.00

=== Code Execution Successful ===0

Clo_3

1. Issue: size=sizeof(arr):

Size=sizeof(arr);

Explanation: sizeof(arr) returns the total size of the array in bytes, not the number of
elements. Since arr is an array of int, this will be 8 * sizeof(int), not the number of elements.

Fix: Change size calculation to size = sizeof(arr) / sizeof(arr[0]); to get the number of
elements in the array.

2. Input array(arr,size):

inputArray(arr, size);

Explanation: The function inputArray is called but is not defined anywhere in the code.

Fix: Remove this line as it is unnecessary. The array is already initialized, so no additional
input is required.

3. Issue: size in sortArray and :


sortArray(arr, size);

Size = removeElement(arr, size, 5);

Explanation: size should be correctly updated and passed as the number of elements after
changes (like removing elements).

Fix: With the size correction from step 1, this will be handled correctly.

4. Issue: displayArray Function

Void displayArray(int arr[], int size)

Explanation: The displayArray function assumes that size accurately reflects the number of
elements. Ensure size is correctly calculated and updated throughout the program.

Fix: Ensure that the size passed to displayArray is correctly calculated, reflecting the
number of elements in the array after operations like sorting or removing elements.

Full Corrected Code:

#include <stdio.h>

Void displayArray(int arr[], int size);

Void sortArray(int arr[], int size);

Int removeElement(int arr[], int size, int element);

Int main()

Int arr[] = { 25, 16, 6, 5, 35, 5, 9, 12 };

Int size;

Size = sizeof(arr) / sizeof(arr[0]); // Fixed calculation of number of elements


Printf(“Size is %d\n”, size);

sortArray(arr, size);

printf(“Sorted array: “);

displayArray(arr, size);

size = removeElement(arr, size, 5);

printf(“\nArray after removing 5: “);

displayArray(arr, size);

return 0;

Void displayArray(int arr[], int size)

For (int I = 0; I < size; i++)

Printf(“%d “, arr[i]);

Printf(“\n”);

Void sortArray(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 removeElement(int arr[], int size, int element)

Int newSize = 0;

For (int I = 0; I < size; i++)

If (arr[i] != element)

Arr[newSize++] = arr[i];

Return newSize;

Output:

Size is 8

Sorted array: 5 5 6 9 12 16 25 35

Array after removing 5: 6 9 12 16 25 35


=== Code Execution Successful ===

You might also like