- "Define a structure of student having data member’s name, address, marks in C language, and marks in information system. Take data for n students in an array dynamically and find the total marks obtained."#include <stdio.h>#include <string.h>// Define the structure for a studentstruct Student {char name[50];char address[100];int marks;};int main() {// Declare a student variablestruct Student student1;// Assign values to the student1 fieldsstrcpy(student1.name, "Suman Adhikari");strcpy(student1.address, "Kathmandu, Nepal");student1.marks = 85;// Print the student detailsprintf("Name: %s\n", student1.name);printf("Address: %s\n", student1.address);printf("Marks: %d\n", student1.marks);return 0;}
- define structure of employee having data members name, address, age, and salary. Take data for n employee in an array dynamically and find the average salary."#include <stdio.h>#include <stdlib.h>// Define the Employee structurestruct Employee {char name[100];char address[100];int age;float salary;};int main() {int n;float sumSalary = 0.0, avgSalary;// Input the number of employeesprintf("Enter the number of employees: ");scanf("%d", &n);// Dynamically allocate memory for n employeesstruct Employee *employees = (struct Employee *)malloc(n * sizeof(struct Employee));// Check if memory has been allocated successfullyif (employees == NULL) {printf("Memory allocation failed.\n");return 1;}// Input data for each employeefor (int i = 0; i < n; i++) {printf("Enter details for employee %d:\n", i + 1);printf("Name: ");scanf(" %[^\n]%*c", employees[i].name); // to read string with spacesprintf("Address: ");scanf(" %[^\n]%*c", employees[i].address);printf("Age: ");scanf("%d", &employees[i].age);printf("Salary: ");scanf("%f", &employees[i].salary);// Add salary to sumsumSalary += employees[i].salary;}// Calculate the average salaryavgSalary = sumSalary / n;// Output the average salaryprintf("The average salary is: %.2f\n", avgSalary);// Free the allocated memoryfree(employees);return 0;}
- "Write a program to enter two 3x3 matrices and calculate the product of given matrices."#include <stdio.h>#define SIZE 3// Define a struct for a 3x3 matrixstruct Matrix {int data[SIZE][SIZE];};// Function prototypesvoid inputMatrix(struct Matrix *matrix, const char *name);void printMatrix(struct Matrix *matrix);void multiplyMatrices(struct Matrix *matrix1, struct Matrix *matrix2, struct Matrix *result);int main() {struct Matrix matrix1, matrix2, result;// Input matricesinputMatrix(&matrix1, "Matrix 1");inputMatrix(&matrix2, "Matrix 2");// Multiply matricesmultiplyMatrices(&matrix1, &matrix2, &result);// Output the result matrixprintf("Resultant matrix after multiplication:\n");printMatrix(&result);return 0;}// Function to input a matrixvoid inputMatrix(struct Matrix *matrix, const char *name) {printf("Enter elements of %s (3x3):\n", name);for (int i = 0; i < SIZE; i++) {for (int j = 0; j < SIZE; j++) {scanf("%d", &matrix->data[i][j]);}}}// Function to print a matrixvoid printMatrix(struct Matrix *matrix) {for (int i = 0; i < SIZE; i++) {for (int j = 0; j < SIZE; j++) {printf("%d ", matrix->data[i][j]);}printf("\n");}}// Function to multiply two matricesvoid multiplyMatrices(struct Matrix *matrix1, struct Matrix *matrix2, struct Matrix *result) {for (int i = 0; i < SIZE; i++) {for (int j = 0; j < SIZE; j++) {result->data[i][j] = 0;for (int k = 0; k < SIZE; k++) {result->data[i][j] += matrix1->data[i][k] * matrix2->data[k][j];}}}}
0 Comments had been done yet:
Post a Comment