0% found this document useful (0 votes)
2 views3 pages

Answer of Program

The document contains code snippets for two separate programs: one in JavaScript for swapping two numbers using a button click, and another in C for managing employee data and sorting them by salary. The C program includes functionality to input employee details, sort them, and print the sorted list, as well as to manage book records from a file, adding new entries and displaying those with prices over 900. Both programs demonstrate the use of structures and basic file handling in C.
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)
2 views3 pages

Answer of Program

The document contains code snippets for two separate programs: one in JavaScript for swapping two numbers using a button click, and another in C for managing employee data and sorting them by salary. The C program includes functionality to input employee details, sort them, and print the sorted list, as well as to manage book records from a file, adding new entries and displaying those with prices over 900. Both programs demonstrate the use of structures and basic file handling in C.
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/ 3

Q.

Write a JavaScript function to swap/exchange two numbers using botton void sortEmployees(struct Employee emp[], int n) {
click event for (int i = 0; i < n - 1; i++) {
<button id="swap">Click me</button> for (int j = 0; j < n - i - 1; j++) {
<input id="one" value="One"/> if (emp[j].salary < emp[j + 1].salary) {
<input id="two" value="Two"/> swap(&emp[j], &emp[j + 1]);
}
<script> }
var btn = document.getElementById("swap"); }
btn.addEventListener("click", function(e) { }
var from = document.getElementById("one"),
to = document.getElementById("two"); int main() {
if(!!from && !!to) { int n;
var _ = from.value; printf("Enter number of employees: ");
from.value = to.value; scanf("%d", &n);
to.value = _;
} else { struct Employee emp[n];
console.log("some input elements could not be found");
} // Input employee details
}); for (int i = 0; i < n; i++) {
</script> printf("\nEnter details for employee %d:\n", i + 1);
printf("ID: ");
Q. Write a C program using structure to input employee's id, name and salary scanf("%d", &emp[i].id);
then print the entered data in sorted order based on salary(from highest to printf("Name: ");
lowest) in simple way scanf(" %49[^"]", emp[i].name); // Read string with spaces
printf("Salary: ");
#include <stdio.h> scanf("%f", &emp[i].salary);
#include <string.h> }

// Structure to hold employee details // Sort employees by salary


struct Employee { sortEmployees(emp, n);
int id;
char name[50]; // Print sorted employee details
float salary; printf("\nEmployees sorted by salary (highest to lowest):\n");
}; for (int i = 0; i < n; i++) {
printf("ID: %d, Name: %s, Salary: %.2f\n", emp[i].id, emp[i].name,
// Function to swap two Employee records emp[i].salary);
void swap(struct Employee *a, struct Employee *b) { }
struct Employee temp = *a;
*a = *b; return 0;
*b = temp; }
}
// Function to sort employees in descending order of salary OR
#include <stdio.h> Q. Let a data file named “hissan.txt” contain information about books (name,
price, edition). Write a C program to add more data and then print all the
struct Employee { records of books having price >900.
int id; #include <stdio.h>
char name[50]; struct Book {
float salary; char name[100];
}; float price;
int edition;
int main() { };
int n;
printf("Enter number of employees: "); int main() {
scanf("%d", &n); struct Book book;
struct Employee emp[n]; FILE *file = fopen("hissan.txt", "a+"); // Open file for reading and appending
if (file == NULL) {
// Input employee details printf("Error opening file!\n");
for (int i = 0; i < n; i++) { return 1;
printf("Enter ID, Name, and Salary for employee %d: ", i + 1); }
scanf("%d %s %f", &emp[i].id, emp[i].name, &emp[i].salary);
} // Add new book data
printf("Enter book name: ");
// Sort employees by salary (descending order) scanf(" %99[^"]", book.name);
for (int i = 0; i < n - 1; i++) { printf("Enter book price: ");
for (int j = i + 1; j < n; j++) { scanf("%f", &book.price);
if (emp[i].salary < emp[j].salary) { printf("Enter book edition: ");
struct Employee temp = emp[i]; scanf("%d", &book.edition);
emp[i] = emp[j]; fprintf(file, "%s %.2f %d\n", book.name, book.price, book.edition);
emp[j] = temp; fclose(file);
}
} // Display books with price > 900
} file = fopen("hissan.txt", "r");
printf("\nBooks with price > 900:\n");
// Print sorted employee details while (fscanf(file, " %99[^"] %f %d", book.name, &book.price,
printf("\nEmployees sorted by salary (highest to lowest):\n"); &book.edition) != EOF) {
for (int i = 0; i < n; i++) { if (book.price > 900) {
printf("ID: %d, Name: %s, Salary: %.2f\n", emp[i].id, emp[i].name, printf("Name: %s, Price: %.2f, Edition: %d\n", book.name, book.price,
emp[i].salary); book.edition);
} }
}
return 0; fclose(file);
}
return 0;
}
OR
#include <stdio.h>

struct Book {
char name[100];
float price;
int edition;
};

int main() {
struct Book book;
FILE *file = fopen("hissan.txt", "a+");
if (!file) {
printf("Error opening file!\n");
return 1;
}

// Add new book


printf("Enter book name: ");
scanf(" %[^]", book.name);
printf("Enter book price: ");
scanf("%f", &book.price);
printf("Enter book edition: ");
scanf("%d", &book.edition);
fprintf(file, "%s %.2f %d\n", book.name, book.price, book.edition);
fclose(file);

// Display books with price > 900


file = fopen("hissan.txt", "r");
printf("\nBooks with price > 900:\n");
while (fscanf(file, " %99[^\n] %f %d", book.name, &book.price, &book.edition)
!= EOF) {
if (book.price > 900) {
printf("%s %.2f %d\n", book.name, book.price, book.edition);
}
}
fclose(file);

return 0;
}

You might also like