0% found this document useful (0 votes)
18 views15 pages

pf ass 2

The document contains a series of programming assignments and solutions related to various topics including prime number checking, array summation, matrix transposition, airline reservation systems, calendar printing, inventory management, and student data handling. Each assignment outlines specific requirements and provides corresponding C programming code as solutions. The document is structured with questions followed by answers for each programming task.

Uploaded by

aghanadeem336
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)
18 views15 pages

pf ass 2

The document contains a series of programming assignments and solutions related to various topics including prime number checking, array summation, matrix transposition, airline reservation systems, calendar printing, inventory management, and student data handling. Each assignment outlines specific requirements and provides corresponding C programming code as solutions. The document is structured with questions followed by answers for each programming task.

Uploaded by

aghanadeem336
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/ 15

NAME:

SYED NADEEM
RIZVI
REGISTRATION NO

SP24-BCS-192

ASSIGNMENT
2
31 DECEMBER 2024

ADDRESS: PHONE: WEB:

Programing Sir Jawad khan Comsat University


Fundamental Abbotabad
QUESTIONS

Write a function prime that returns 1 if its argument is a prime


01 number and returns 0 otherwise.

.Write a function called arraySum that takes two arguments: an


02 integer array and the number of elements in the array. Have the
function return as its result the sum of the elements in the array.

A matrix M with i rows, j columns can be transposed into a


03 matrix N having j rows and i columns by simply setting the value
of Na,b equal to the value of Mb,a for all relevant values of a and
b.
a. Write a function transposeMatrix that takes as an argument a
4 x 5 matrix
and a 5 x 4 matrix. Have the function transpose the 4 x 5 matrix
and store
the results in the 5 x 4 matrix. Also write a main routine to test
the function.
b. Using variable-length arrays, rewrite the transposeMatrix
function that take the number of rows and columns as
arguments,and to transpose the matrix of the specified
dimensions.

Write a program to pick up the largest number from any 5 row


04 by 5 column matrix.

SYED NADEEM
QUESTIONS

(Airline Reservations System) A small airline has just


05 purchased a computer for its new automated reservations
system. The president has asked you to program the new
system. You’ll write a program to assign seats on each flight
of the airline’s only plane (capacity: 50 seats).
Your program should display the following menu of
alternatives:
Please type 1 for "first class"
Please type 2 for "economy"
If the person types 1, then your program should assign a seat
in the first class section (seats 1–
20). If the person types 2, then your program should assign a
seat in the economy section (seats 21–
50). Your program should then print a boarding pass
indicating the person's seat number and
whether it’s in the first class or economy section of the plane.
Use a single-subscripted array to represent the seating chart
of the plane. Initialize all the elements
of the array to 0 to indicate that all seats are empty. As each
seat is assigned, set the corresponding
element of the array to 1 to indicate that the seat is no longer
available.
Your program should, of course, never assign a seat that has
already been assigned. When the
first class section is full, your program should ask the person
if it’s acceptable to be placed in the
economy section (and vice versa). If yes, then make the
appropriate seat assignment. If no, then
print the message "Next flight leaves in 3 hours.".

SYED NADEEM
QUESTIONS

Develop a program that receives the month and year from the
06 keyboard as integers and prints the calendar in the following
format.

A factory has 3 division and stocks 4 categories of products. An


07 inventory table is updated for each division and for each product
as they are received. There are three independent suppliers of
products to the factory:
(a) Design a data format to represent each transaction.
(b) Write a program to take a transaction and update the
inventory.
(c) If the cost per item is also given write a program to calculate
the total inventory values.

SYED NADEEM
QUESTIONS

Write a structure program with name (student) that contains


08 name, roll and marks as its data member. Then, create an array
of structure of 10 elements. Ask user to enter 10 element of data
(name, roll and marks) and stored in array of structure. Finally,
the data entered by user is displayed.

SYED NADEEM
ANSWERS 1

#include <stdio.h>
int isPrime(int n) {
if (n <= 1) {
return 0;
}

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


if (n % i == 0) {
return 0;
}
}
return 1;
}

int main() {
printf("SYED NADEEM BCS 192\n");
int n;
printf("ENTER THE NUMBER : ");
scanf("%d", &n);
int result;
result = isPrime(n);
printf("%d\n", result);
return 0;
}
ANSWERS 2

#include <stdio.h>
int main() {
printf("SYED NADEEM BCS 192\n");
int arr[3][3];
int i, j;

printf("ENTER MATRIX:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &arr[i][j]);
}
}

printf("\nOriginal Matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%4d", arr[i][j]);
}
printf("\n");
}

printf("\nTransposed Matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%4d", arr[j][i]);
}
printf("\n");
}
return 0;
}
ANSWERS 3

#include <stdio.h>
void transposeMatrix(int rows, int cols, int m[rows][cols], int
n[cols][rows]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
n[j][i] = m[i][j];
}
}
}
int main() {
printf("SYED NADEEM BCS 192\n");
int rows, cols;

printf("Enter rows and columns: ");


scanf("%d %d", &rows, &cols);

int matrix[rows][cols];
int transposed[cols][rows];
printf("Enter matrix elements:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}

transposeMatrix(rows, cols, matrix, transposed);

printf("Transposed Matrix:\n");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d ", transposed[i][j]);
}
printf("\n");
}
return 0;
}
ANSWERS 4

#include <stdio.h>
int main() {
printf("SYED NADEEM BCS 192\n");
int matrix[5][5] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}
};

int max = matrix[0][0]; // Initialize max to the first element


for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (matrix[i][j] > max) {
max = matrix[i][j];
}
}
}
printf("The largest number is %d\n", max);
return 0;
}
ANSWERS 5

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define TOTAL_SEATS 50
#define FIRST_CLASS_CAPACITY 20

void first_class(bool seats[]);


void economy_class(bool seats[]);
int main() {
printf("SYED NADEEM BCS 192\n");
bool seats[TOTAL_SEATS] = {false};
int choice;

printf("WELCOME TO AIRLINE RESERVATION SYSTEM\n\n");


while (true) {
printf("Enter your choice: (1 for First Class, 2 for Economy, 3 for exit): ");
if (scanf("%d", &choice) != 1) {
printf("Invalid input. Please enter a number.\n");
while (getchar() != '\n');
continue;
}

switch (choice) {
case 1:
first_class(seats);
break;
case 2:
economy_class(seats);
break;
case 3:
printf("Exiting the program.\n");
exit(0);
default:
printf("Invalid choice. Please enter 1, 2, or 3.\n");
break;
}
}
return 0;
ANSWERS 5

void first_class(bool seats[]) {


printf("Entered in first class\n");
for (int i = 0; i < FIRST_CLASS_CAPACITY; i++) {
if (!seats[i]) {
seats[i] = true;
printf("Seat no %d assigned in First Class. Go and check for boarding.\n", i + 1);
return;
}
}

printf("First class is full. Would you like economy class (y/n)?\n");


char con;
scanf(" %c", &con);
if (con == 'y' || con == 'Y') {
economy_class(seats);
} else if (con == 'n' || con == 'N') {
printf("Next flight in 3 hours\n");
} else {
printf("Invalid input.\n");
}
}

void economy_class(bool seats[]) {


printf("Entered in economy class\n");
for (int i = FIRST_CLASS_CAPACITY; i < TOTAL_SEATS; i++) {
if (!seats[i]) {
seats[i] = true;
printf("Seat no %d assigned in economy class. Go and check for boarding.\n", i + 1);
return;
}
}

printf("Economy class is full. Would you like first class (y/n)?\n");


char con;
scanf(" %c", &con);
if (con == 'y' || con == 'Y') {
first_class(seats);
} else if (con == 'n' || con == 'N') {
printf("Next flight in 3 hours\n");
} else {
printf("Invalid input.\n");
}
}
ANSWERS 6

#include <stdio.h>
void printCalendar(int month, int year) {
int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char *months[] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {


daysInMonth[1] = 29;
}
int y = year, m = month;
if (m < 3) {
m += 12;
y--;
}
int startingDay = (1 + (13 * (m + 1)) / 5 + y + (y / 4) - (y / 100) + (y / 400)) % 7;
startingDay = (startingDay + 5) % 7;

printf("\n %s %d\n", months[month - 1], year);


printf(" Sun Mon Tue Wed Thu Fri Sat\n");
for (int i = 0; i < startingDay; i++) {
printf(" ");
}

for (int i = 1; i <= daysInMonth[month - 1]; i++) {


printf("%4d", i);
if ((i + startingDay) % 7 == 0) {
printf("\n");
}
}
printf("\n");
}
ANSWERS 6

int main() {
printf("SYED NADEEM BCS 192\n");
int month, year;
printf("Enter month (1-12): ");
if (scanf("%d", &month) != 1) {
printf("Invalid month input. Please enter a number.\n");
return 1;
}
printf("Enter year: ");
if (scanf("%d", &year) != 1) {
printf("Invalid year input. Please enter a number.\n");
return 1;
}

if (month < 1 || month > 12 || year < 1) {


printf("Invalid month or year!\n");
return 1;
}

printCalendar(month, year);
return 0;
}
ANSWERS 7

#include <stdio.h>
#define DIVISIONS 3
#define PRODUCTS 4
typedef struct {
int divisionId, productId, quantity, supplierId;
float costPerItem;
} Transaction;
void updateInventory(int inv[DIVISIONS][PRODUCTS], Transaction t) {
if (t.divisionId < 0 || t.divisionId >= DIVISIONS || t.productId < 0 || t.productId >=
PRODUCTS) {
printf("Invalid transaction.\n");
return;
}
inv[t.divisionId][t.productId] += t.quantity;
}

int main() {
printf("SYED NADEEM BCS 192\n");
int inv[DIVISIONS][PRODUCTS] = {0}, numTrans;

printf("Enter no. of transactions: ");


if (scanf("%d", &numTrans) != 1 || numTrans < 0) return 1;

Transaction trans[numTrans];
for (int i = 0; i < numTrans; i++) {
printf("\nTransaction %d:\n", i + 1);
printf("Division (0-%d): ", DIVISIONS - 1);
if (scanf("%d", &trans[i].divisionId) != 1) return 1;
printf("Product (0-%d): ", PRODUCTS - 1);
if (scanf("%d", &trans[i].productId) != 1) return 1;
printf("Quantity: ");
if (scanf("%d", &trans[i].quantity) != 1) return 1;
printf("Cost: ");
if (scanf("%f", &trans[i].costPerItem) != 1) return 1;
updateInventory(inv, trans[i]);
}

printf("\nInventory:\n");
for (int i = 0; i < DIVISIONS; i++) {
printf("Div %d: ", i);
for (int j = 0; j < PRODUCTS; j++) {
printf("%d ", inv[i][j]);
ANSWERS 8

#include <stdio.h>
struct student
{
int roll;
char name[50];
float marks;
};

int main()
{
printf("SYED NADEEM BCS 192\n");
int i, n;
printf("\nEnter the Total Number Of Student: ");
scanf("%d", &n);

struct student s[n];

printf("\nEnter Student Details (Enter Marks in Float Ie.


78.00):\n");
for (i = 0; i < n; i++)
{
printf("\nEnter Student Roll No: ");
scanf("%d", &(s[i].roll));

printf("Enter Student Name: ");


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

printf("Enter Student Marks: ");


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

printf("\n\nDisplay Student Details:\n");

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


{
printf("\nStudent Roll No: %d", s[i].roll);
printf("\nStudent Name: %s", s[i].name);
printf("\nStudent Marks: %f", s[i].marks);
printf("\n\n");
}

return 0;

You might also like