0% found this document useful (0 votes)
40 views14 pages

BhavikaJainAssignment 1

The document is the code for a C program that performs matrix operations (addition, subtraction, multiplication, division) on two 3x3 matrices. It begins by including header files, declaring variables to store the matrix elements, and getting user input for the two matrices. It then shows the code for each matrix operation and printing the results. The key steps are: 1) getting input for two 3x3 matrices, 2) performing the four basic operations on the matrices, and 3) printing the results.

Uploaded by

harleensomal28
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)
40 views14 pages

BhavikaJainAssignment 1

The document is the code for a C program that performs matrix operations (addition, subtraction, multiplication, division) on two 3x3 matrices. It begins by including header files, declaring variables to store the matrix elements, and getting user input for the two matrices. It then shows the code for each matrix operation and printing the results. The key steps are: 1) getting input for two 3x3 matrices, 2) performing the four basic operations on the matrices, and 3) printing the results.

Uploaded by

harleensomal28
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/ 14

COMP 2130

Introduction to Computer Systems


Assignment - 1
Bhavika Jain
T00684244

Answer 1:
#include <stdio.h>

int main() {

int r = 3, c = 3;

int a[100][100], b[100][100], sum[100][100], diff[100][100],


product[100][100], i, j;

float quotient[100][100];

// user inputs the elements of matrix 1

printf("Enter elements of 1st matrix\n");

for (i = 0; i < r; i++){

for (j = 0; j < c; j++) {

printf("Enter element a%d%d: ", i + 1, j + 1);

scanf("%d", &a[i][j]);

// user inputs the elements of matrix 2

printf("\nEnter elements of 2nd matrix\n");

for (i = 0; i < r; i++){

for (j = 0; j < c; j++) {

printf("Enter element b%d%d: ", i + 1, j + 1);

scanf("%d", &b[i][j]);

}
// adding two matrices

for (i = 0; i < r; i++){

for (j = 0; j < c; j++) {

sum[i][j] = a[i][j] + b[i][j];

// printing the addition result

printf("\nSum of two matrices: \n");

for (i = 0; i < r; i++){

for (j = 0; j < c; j++) {

printf("%d ", sum[i][j]);

if (j == c-1) {

printf("\n\n");

// subtracting the two matrices

for (i = 0; i < r; i++){

for (j = 0; j < c; j++) {

diff[i][j] = b[i][j] - a[i][j];

// printing sbtraction result

printf("\nDifference of two matrices: \n");

for (i = 0; i < r; i++){

for (j = 0; j < c; j++) {

printf("%d ", diff[i][j]);

if (j == c-1) {

printf("\n\n");

}
}

// multiplying both matrices

for (i = 0; i < r; i++){

for (j = 0; j < c; j++) {

product[i][j] = a[i][j] * b[i][j];

// printing the multiplication result

printf("\nProduct of two matrices: \n");

for (i = 0; i < r; i++){

for (j = 0; j < c; j++) {

printf("%d ", product[i][j]);

if (j == c-1) {

printf("\n\n");

// dividing two matrices

for (i = 0; i < r; i++){

for (j = 0; j < c; j++) {

quotient[i][j] = a[i][j] / b[i][j];

// printing the division result

printf("\nDivision of two matrices: \n");

for (i = 0; i < r; i++){

for (j = 0; j < c; j++) {

printf("%.1f ", quotient[i][j]);

if (j == c-1) {

printf("\n\n");
}

return 0;

}
Answer 2
#include <stdio.h>

// structure declaration

struct student {
int roll;

char name[50];

int sem;

char course[50];

char address[100];

long long contact;

float fees;

};

int main() {

int i, n;

// inputting the number of students

printf("Enter the number of students: ");

scanf("%d", &n);

// creating a structure varaible of student called s

struct student s[n];

// calling function to input all the information

studentInfo(s, n);

printf("\nDisplaying information:\n\n");

// displaying information

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

printf("\nRoll number: %d\n", i + 1);

printf("Name: %s\n", s[i].name);

printf("Semester: %d\n", s[i].sem);

printf("Course: %s\n", s[i].course);

printf("Address: %s\n", s[i].address);

printf("Contact Number: %d\n", s[i].contact);

printf("Fees: %.3f\n", s[i].fees);

}
return 0;

// function to store student information

void studentInfo(struct student s[], int n){

int i;

printf("\nEnter information of students");

// storing information

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

s[i].roll = i + 1;

printf("\nFor roll number %d,\n", s[i].roll);

printf("Enter name: ");

scanf("%s", s[i].name);

printf("Enter semester: ");

scanf("%d", &s[i].sem);

printf("Enter course: ");

scanf("%s", &s[i].course);

printf("Enter address: ");

scanf("%s", &s[i].address);

printf("Enter contact number: ");

scanf("%d", &s[i].contact);

printf("Enter fees: ");

scanf("%f", &s[i].fees);

}
Answer 3
#include <stdio.h>

int main() {

int num[] = {50, 14, 25, 68, 78, 3};


int *ptr = num; // pointer to the first element of the array

int large = *ptr; // assigning the first element to largest

// for loop to loop through the array

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

// compare the current element with largest

if (*(ptr + i) > large) {

large = *(ptr + i); // update largest

// printing the largest element

printf("The largest element in the array is: %d\n", large);

return 0;

Answer 4
#include <stdio.h>

#include <time.h>

struct Employee {

char name[100];

int dept_no;

char dept_name[100];

char designation[100];

// nested structure

struct Date {

int day;

int month;

int year;

} dob, doj;
};

int main() {

// creating a structure varaible of Employee called emp

struct Employee emp;

time_t t = time(NULL);

//localtime() function

struct tm tm = *localtime(&t);

int age, working_days;

// inputting all the employee information

printf("Enter employee name: ");

scanf("%s", emp.name);

printf("Enter department number: ");

scanf("%d", &emp.dept_no);

printf("Enter department name: ");

scanf("%s", emp.dept_name);

printf("Enter designation: ");

scanf("%s", emp.designation);

printf("Enter date of birth (DD MM YYYY): ");

scanf("%d %d %d", &emp.dob.day, &emp.dob.month, &emp.dob.year);

printf("Enter date of joining (DD MM YYYY): ");

scanf("%d %d %d", &emp.doj.day, &emp.doj.month, &emp.doj.year);

// Calculate age

age = tm.tm_year + 1900 - emp.dob.year; // struct tm is the number of


years since 1900
if (tm.tm_mon + 1 < emp.dob.month || (tm.tm_mon + 1 == emp.dob.month &&
tm.tm_mday < emp.dob.day)) {

age--;

printf("\n\nCurrent age: %d\n", age);

// Calculate number of working days

struct tm start_tm = {0};

start_tm.tm_year = emp.doj.year - 1900;

start_tm.tm_mon = emp.doj.month - 1;

start_tm.tm_mday = emp.doj.day;

time_t start_time = mktime(&start_tm);

double diff_time = difftime(t, start_time);

working_days = diff_time / (60 * 60 * 24);

printf("Number of working days: %d\n", working_days);

return 0;

Answer 5
#include <stdio.h>

#include <stdlib.h>

#include <time.h>

struct Employee {

char name[100];
int dept_no;

char dept_name[100];

char designation[100];

// additional field member for union (salary)

union {

float yearly_salary;

} salary;

struct Date {

int day;

int month;

int year;

} dob, doj;

};

int main() {

// creating a structure varaible of Employee called emp

struct Employee emp[10];

int i, n;

// declare and intialize the highest salary as zero

float highest_salary = 0.0;

// inputting the number of employees

printf("Enter number of employees: ");

scanf("%d", &n);

// inputting employee information from users

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

printf("\nEnter details for employees %d:\n", i + 1);

printf("Name: ");

scanf("%s", emp[i].name);

printf("Department number: ");

scanf("%d", &emp[i].dept_no);

printf("Department name: ");


scanf("%s", emp[i].dept_name);

printf("Designation: ");

scanf("%s", emp[i].designation);

printf("Yearly salary: ");

scanf("%f", &emp[i].salary.yearly_salary);

printf("Date of birth (DD MM YYYY): ");

scanf("%d %d %d", &emp[i].dob.day, &emp[i].dob.month,


&emp[i].dob.year);

printf("Date of joining (DD MM YYYY): ");

scanf("%d %d %d", &emp[i].doj.day, &emp[i].doj.month,


&emp[i].doj.year);

// calculate highest salary

if (emp[i].salary.yearly_salary > highest_salary) {

highest_salary = emp[i].salary.yearly_salary;

// displaying information

printf("\nEmployee details:\n");

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

printf("\nEmployee Information %d:\n", i + 1);

printf("Name: %s\n", emp[i].name);

printf("Department number: %d\n", emp[i].dept_no);

printf("Department name: %s\n", emp[i].dept_name);

printf("Designation: %s\n", emp[i].designation);

printf("Yearly salary: $%.2f\n", emp[i].salary.yearly_salary);

printf("Date of birth: %d/%d/%d\n", emp[i].dob.month,


emp[i].dob.day, emp[i].dob.year);

printf("Date of joining: %d/%d/%d\n", emp[i].doj.month,


emp[i].doj.day, emp[i].doj.year);

// diplaying the highest salary

printf("\nHighest salary: $%.2f\n", highest_salary);

You might also like