Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 69

Ruhan Khalid

23k-5512

Assignment # 2

Programming for Business


Question: 01 [20 points]
You are given a 2D array that represents the batting
performance of a cricket team. Each row in
the array corresponds to a different batsman, and
each column represents an inning played by
that batsman. The values in the array represent the
runs scored by each batsman in each inning.
Write a C program to perform the following tasks:
1) Input the number of batsmen and the number of
innings from the user.
2) Input the batting performance for each batsman
in each inning.

3) Calculate and display the following statistics for


each batsman.
● Total runs scored.
● Average runs per inning
● Highest score in a single inning
● Number of centuries (innings with a score of 100
or more)
● Number of half-centuries (innings with a score of
50-99)
Your program should display the statistics for each
batsman individually.
Ensure that the program provides a user-friendly
interface and handles errors gracefully, such as
invalid inputs or attempts to perform operations on
non-existent products. Implement this program in
C, focusing on readability, modularity, and efficient
memory usage.
Answer code:
#include <stdio.h>
int main(){
printf("Ruhan Khalid 23k-5512\n\n\n");
int nob,noi;
printf("Enter number of batsmen: ");
scanf("%d", &nob);
printf("Enter number of innings: ");
scanf("%d", &noi);
printf("\n");
int rs[nob][noi];
int i,j;
for(i = 0; i< nob;i++){
for(j = 0; j < noi; j++){
printf("Enter runs scored by batsman
no.%d in inning no.%d :", i+1,j+1);
scanf("%d", &rs[i][j]);
}
printf("\n");
}
for(i = 0; i < nob ; i++){
int total = 0, cen = 0,hcen = 0,highest =
0,avg;
for(j = 0; j< noi; j++){
total += rs[i][j];
if(rs[i][j]> highest){
highest = rs[i][j];
}
if(rs[i][j]>=100){
cen++;
}
if(rs[i][j]>=50 && rs[i][j]<=99){
hcen++;
}
}
avg = total/noi;
printf("\nBatman no.%d:\n",i+1);
printf("Total runs scored: %d\nAverage runs
per innning: %d\n",total,avg);
printf("Highest score: %d", highest);
printf("\nNo. of centuries: %d\n", cen);
printf("No. of half centuries: %d\n",hcen);
}
return 0;
}
Output:
Question: 02 [15 points]
You&#39;ve been tasked with developing a simple
inventory management system for a small retail
store using C programming language. Write a
program that allows the store manager to perform
various operations on the inventory using loops and
arrays exclusively. The program should
offer the following functionalities:

1. Add a new product to the inventory with details


such as name, code, quantity, and price.
2. Update the details of an existing product,
including its name, code, quantity, or price.
3. Remove a product from the inventory.
4. Display the details of all products in the
inventory.
5. Search for a product by its code and display its
details.
Ensure proper error handling and provide a user-
friendly interface for the store manager to
interact with the inventory. Remember, you&#39;re
not allowed to use pointers for parameter passing
or variable manipulation.
Answer Code:
#include <stdio.h>
int main(){
printf("Ruhan Khalid 23k-5512\n\n\n");
int MAX_PRODUCTS = 100;
char names[MAX_PRODUCTS][50];
int codes[MAX_PRODUCTS];
int quantities[MAX_PRODUCTS];
float prices[MAX_PRODUCTS];
int count = 0;
int choice,i,j;
do{
printf("\nInventory Management System\n");
printf("1. Add a new product\n");
printf("2. Update an existing product\n");
printf("3. Remove a product\n");
printf("4. Display all products\n");
printf("5. Search for a product by code\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
printf("\n");
switch(choice) {
case 1:{
if(count >= MAX_PRODUCTS) {
printf("Inventory is full! Cannot add
more products.\n");
break;
}
printf("Enter product details:\n");
printf("Name: ");
scanf("%s", names[count]);
printf("Code: ");
scanf("%d", &codes[count]);
printf("Quantity: ");
scanf("%d", &quantities[count]);
printf("Price: ");
scanf("%f", &prices[count]);
count++;
printf("Product added successfully.\n");
break;
}
case 2:{
int code;
printf("Enter the product code to update:
");
scanf("%d", &code);
for(i = 0; i < count; i++) {
if (codes[i] == code) {
printf("Enter new details:\n");
printf("Name: ");
scanf("%s", names[i]);
printf("Quantity: ");
scanf("%d", &quantities[i]);
printf("Price: ");
scanf("%f", &prices[i]);
printf("Product details updated
successfully.\n");
break;
}
if(i == count - 1) {
printf("Product with code %d not
found.\n", code);
}
}
break;
}
case 3:{
int code;
printf("Enter the product code to remove:
");
scanf("%d", &code);
int found = 0;
for (i = 0; i < count; i++) {
if(codes[i] == code) {
for (j = i; j < count - 1; j++) {
codes[j] = codes[j + 1];
quantities[j] = quantities[j + 1];
prices[j] = prices[j + 1];
}
count--;
printf("Product removed successfully.\
n");
found = 1;
break;
}
}
if(!found) {
printf("Product with code %d not found.\
n", code);
}
break;
}
case 4: {
if(count == 0) {
printf("Inventory is empty.\n");
break;
}
printf("Product Details:\n");
printf("Code\tName\tQuantity\tPrice\n");
for(i = 0; i < count; i++) {
printf("%d\t%s\t%d\t%.2f\n", codes[i],
names[i], quantities[i], prices[i]);
}
break;
}
case 5:{
int code;
printf("Enter the product code to search:
");
scanf("%d", &code);
int found = 0;
for(i = 0; i < count; i++) {
if(codes[i] == code) {
printf("Product found:\n");
printf("Code: %d\nName: %s\
nQuantity: %d\nPrice: %.2f\n", codes[i], names[i],
quantities[i], prices[i]);
found = 1;
break;
}
}
if(!found) {
printf("Product with code %d not found.\
n", code);
}
break;
}
case 6:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while(choice != 6);
return 0;
}
Output:
Question: 03 [20 points]
You&#39;ve been hired by a local supermarket
chain to develop a program to manage their
inventory
of perishable goods, such as fruits and vegetables.
The supermarket has multiple branches,
each with its own inventory, and they need a
system to keep track of the stock levels and expiry
dates of these perishable items. You have access to
a 2D array representing the inventory
status for different branches and items. Below is an
example of the inventory status:

Each row represents a branch, and each column


represents a type of perishable item. The
numbers indicate the quantity of each item
currently in stock.
1. Analyze the inventory status for each branch and
item. Determine which branch has the
highest quantity of each item in stock.
2. The supermarket wants to identify the branches
with low stock levels for a specific item (e.g.,
Oranges) so they can redistribute stock accordingly.
Determine which branches have less than a
certain threshold (e.g., 50 units) of a specified item.
3. The supermarket is planning to introduce a new
item (e.g., Strawberries) and wants to find the
branch with the highest overall stock level to
initially introduce this item. Identify the branch with
the highest total stock quantity across all items.
4. The supermarket is conducting a routine check of
their inventory and wants to identify any
items that are close to their expiry date. They
consider any item with less than a week until
expiry as close to expiry. Determine which items in
each branch are close to expiry and need
attention.
Answer code:
#include <stdio.h>
int main(){
printf("Ruhan Khalid 23k-5512\n\n\n");
int item[5][7] = {{55,33,80,45,66,23,55},
{55,33,66,23,64,24,89},
{65,66,56,87,36,70,43},
{44,55,30,56,76,87,120},
{23,102,52,90,87,67,66}};
int expiry[5][7] = {{5, 10, 2, 7, 3, 8, 4},
{6, 8, 3, 5, 2, 9, 7},
{4, 7, 9, 6, 8, 3, 5},
{8, 3, 6, 2, 7, 4, 9},
{9, 5, 4, 7, 3, 6, 2}};
int i,j,k,max_ind,count,high,choice,hi;
char item_names[7][20] = {"Oranges", "Water
melons", "Peaches", "Pears", "Bananas", "Apples",
"Grapes"};
char branch;

do{
printf("1.Check Inventory details\n2.Check
branch with highest stock\n3.Check branches with
low stock\n");
printf("4.Check branch with highest overall
stock\n5.Check item close to expiry\n");
scanf("%d",&choice);
switch(choice){
case 1:
for(i=0;i<5;i++){
char branch = 'A' + i;
printf("\nBranch %c:\n",branch);
for(j=0;j<7;j++){
printf("%d %s\n", item[i][j],
item_names[j]); // Print item quantity and name

}
printf("\n\n");
}
break;

case 2:
printf("\nBranches with highest stock of each
item:\
n_________________________________________
__\n");
for(i = 0;i<5;i++){
branch = 'A' + i;
printf("Branch %c: ",branch);
hi = item[i][0];
for(j = 1; j< 7;j++){
if(item[i][j]>hi){
hi = item[i][j];
max_ind = j;
}
}
puts(item_names[max_ind]);
}
printf("\n");

break;

case 3:
printf("\n\nBranches with low stock of each
item:\n__________________________\n");
for(i = 0;i<5;i++){
branch = 'A' + i;
count = 0;
for(j = 0; j< 7;j++){
if(item[i][j]<50){
if(count == 0){
printf("Branch %c: \n",branch);
}
puts(item_names[j]);
count++;
}

}
printf("\n");
}

break;

case 4:
printf("\nBranch with highest overall stock: \
n_______________________________\n");
for(i=0;i<5;i++) {
int sum = 0;
for(j=0;j<7;j++) {
sum += item[i][j];
}
if(sum > high){
high = sum;
branch = 'A' + i;
}
}
printf("Branch %c\nTotal Stock: %d\n", branch,
high);
break;

case 5:
printf("\nItems that are close to expiry:\
n____________________________\n\n");
for(i = 0;i<5;i++){
count = 0;
branch = 'A' + i;
for(j = 0; j< 7;j++){
if(expiry[i][j]<7){
if(count == 0){
printf("Branch %c:\n\n",branch);
}
puts(item_names[j]);
printf(" left %d days to expire\
n",expiry[i][j]);
}
}
printf("\n\n");
}
break;
default:
printf("Invalid input! Please try again.");
}
}
while(choice >6 || choice < 1);
}
Output:
Question 04 [20 points]
You&#39;ve been assigned the task of creating a
program to manage a library&#39;s bookshelves
using C
programming language. The library has multiple
bookshelves, each containing books of different
genres. You have access to a 2D array representing
the bookshelf inventory, where each row
corresponds to a bookshelf, and each column
corresponds to a book genre. The inventory is
represented as follows:
char bookshelves[N][M] = {
{&#39;F&#39;, &#39;N&#39;, &#39;N&#39;,
&#39;F&#39;, &#39;F&#39;}, // Fiction, Non-fiction
{&#39;N&#39;, &#39;N&#39;, &#39;F&#39;,
&#39;F&#39;, &#39;N&#39;}, // Non-fiction, Fiction
{&#39;F&#39;, &#39;F&#39;, &#39;N&#39;,
&#39;N&#39;, &#39;N&#39;}, // Fiction, Fiction,
Non-fiction
{&#39;N&#39;, &#39;N&#39;, &#39;N&#39;,
&#39;F&#39;, &#39;N&#39;}, // Non-fiction, Fiction
{&#39;F&#39;, &#39;N&#39;, &#39;F&#39;,
&#39;N&#39;, &#39;N&#39;} // Fiction, Non-fiction,
Fiction
};

Each character in the array represents a book


genre: &#39;F&#39; for Fiction and &#39;N&#39;
for Non-fiction.
1. Analyze the given bookshelf inventory
represented by a 2D array. Describe the structure of
the inventory and explain the meanings of
&#39;F&#39; and &#39;N&#39; in the context of
the library&#39;s bookshelves.
2. Develop a C program to search for a specific book
genre in the bookshelf inventory and
display the indexes of the bookshelves containing
that genre.
3. Discuss the logic behind your program design.
How does the program search for a specific
book genre in the inventory? Explain any algorithms
or data structures used.
4. Consider scenarios where a book genre might be
present on multiple bookshelves. How
would your program handle such cases?
Answer code:
#include <stdio.h>
int main() {
printf("Ruhan Khalid 23k-5512\n\n\n");
char bookshelves[5][5] =
{{'F', 'N', 'N', 'F', 'F'},
{'N', 'N', 'F', 'F', 'N'},
{'F', 'F', 'N', 'N', 'N'},
{'N', 'N', 'N', 'F', 'N'},
{'F', 'N', 'F', 'N', 'N'}};
char gen;
int i,j;
jumphere:
do{
printf("Enter the genre to search (F for
Fiction, N for Non-fiction): ");
scanf(" %c", &gen);
if(gen == 'F' || gen == 'N'){
printf("%c genre found at:\n", gen);
for (i = 0; i < 5; i++) {
int flag = 0;
printf("Bookshelf %d Indexes: ", i+1);
for (j = 0; j < 5; j++) {
if (bookshelves[i][j] == gen) {
if(flag>0){
printf(" , ");
}
printf("%d", j + 1);
flag++;
}
}
printf("\n");
}
}
else{
printf("Invalid input! Please try again.\n\n");
}
}
while(gen != 'F' || gen != 'N');
return 0;
}
Output:

3. The program checks each bookshelf to find the entered genre. It uses
a loop to go through each bookshelf and another loop to check each
book's genre. When it finds a match, it prints the bookshelf's index. This
process repeats until the user enters a valid genre.
4. If a genre appears on multiple bookshelves, the program lists all the
bookshelves where it's found.
It uses a flag to ensure proper formatting, separating the indexes with
commas.
Question 05 [20 points]
Ramanujan-Hardy Numbers 2-way are numbers that
are the sum of two cubes two different ways.
Following are the first few Ramanujan-Hardy 2-way
numbers:
1729, 4104, 13832, 20683, 32832, 39312, 40033,
46683, 64232, 65728, 110656, 110808, 134379,
149389, 165464, 171288, 195841, 216027, 216125,
262656, 314496, 320264, 327763, 373464, 402597,
439101, 443889, 513000, 513856, 515375, 525824,
558441, 593047, 684019, 704977
E.g., 1729 = 12^3 + 1^3 = 9^3 + 10^3.
A. Write a C program to find all Ramanujan-Hardy
numbers less than n^3.
B. Draw a trace table on a paper for the input 1729.
Answer code:
#include <stdio.h>
int main(){
printf("Ruhan Khalid 23k-5512\n\n\n");
int limit,num;
printf("Enter the limit (n): ");
scanf("%d", &limit);
printf("Ramanujan-Hardy numbers less than
%d^3 are:\n", limit);
for(num=2;num<limit*limit*limit;num++){
int pairCount=0;
int firstRoot,secondRoot;
for(firstRoot=1;firstRoot*firstRoot*firstRoot<nu
m;firstRoot++){
for(secondRoot=firstRoot+1;secondRoot*sec
ondRoot*secondRoot<num;secondRoot++){
int sumOfCubes = firstRoot * firstRoot *
firstRoot + secondRoot * secondRoot * secondRoot;
if (sumOfCubes == num) {
pairCount++;
if (pairCount== 2) {
printf("%d\n",num);
break;
}
}
}
if(pairCount== 2) {
break;
}
}
}
return 0;
}
Output:
Trace table:
Question 06 [10 points]
Given a 1D array of N integers, and a target integer
t.
Write a ‘C’ program to find all different number
pairs in the array that have sum equal to t.
Sample Input:
List: 1, 8, 10, 12, 7, 4, 3 and t: 11
Sample Output:
Pairs: (1, 10), (7,4), (8,3)
Answer code:
#include <stdio.h>
int main(){
printf("Ruhan Khalid 23k-5512\n\n\n");
int n,flag = 0;
printf("Enter size of array:");
scanf("%d",&n);
int arr[n];
printf("Enter elements of the array:\n");
int i,j;
for(i = 0; i < n; i++){
scanf("%d",&arr[i]);
}
printf("Enter target number: ");
int t;
scanf("%d",&t);
printf("Pairs for sum of %d are:\n",t);
for(i = 0; i < n; i++){
for(j=i+1;j<n;j++){
if(arr[i]+arr[j] == t){
printf("(%d,%d) ",arr[i],arr[j]);
flag = 1;
}
}
}
if(!flag){
printf("No pairs found!");
}
}
Output:
Question 07: [10 points]
You&#39;ve been assigned the task of developing a
program to manage and sort shoe details for a
footwear store. The shoe details include the size
and brand for each shoe. Write a C program
that sorts the shoe details based on size in
descending order and, within the same size, sorts
them based on brand in ascending order.

You are given a 2D array: sizes (representing the


size of each shoe) and brands (representing
the brand of each shoe). Assume any number of
shoes and their brands by yourself.

Your expected output will show two different


sorted lists: one sorted in descending order with
respect to size and the other sorted in ascending
order with respect to brand.
1. Analyze the given 2D arrays representing the
sizes and brands of shoes. Describe the
structure of the arrays and explain the significance
of sizes and brands in the context of
managing shoe details for the footwear store.

2. Develop a C program to sort the shoe details


based on size in descending order and, within
the same size, sort them based on brand in
ascending order. Implement any sorting algorithm
of
your choice to achieve this.

3. Discuss the logic behind your program design.


How does the program sort the shoe details
based on size and brand? Explain any data
structures or algorithms used in the sorting process.
4. Consider scenarios where multiple shoes have
the same size but different brands. How would your
program handle such cases during the sorting
process?
Answer code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_BRANDS 8
#define NUM_SIZES 5

int main(){
char shoe_brands[NUM_BRANDS][20] = {
"Bata", "Ndure", "PUMA", "Nike", "Clarks",
"Brooks", "Servis", "Adidas"
};
int shoe_sizes[NUM_BRANDS][NUM_SIZES] = {
{7, 5, 9, 8, 6},
{6, 9, 5, 8, 7},
{7, 9, 5, 7, 6},
{6, 5, 8, 7, 9},
{9, 5, 7, 5, 6},
{8, 9, 5, 6, 7},
{7, 5, 9, 6, 8},
{7, 9, 5, 6, 8}
};
int quantity[NUM_BRANDS] = {10, 5, 30, 40, 15,
20, 25, 35};
int i,j,k;
printf("List of shoes along with sizes:\n");
for(i=0;i<NUM_BRANDS;i++) {
printf("%s\t", shoe_brands[i]);
for (j = 0; j < NUM_SIZES; j++) {
printf("%d ", shoe_sizes[i][j]);
}
printf("\n");
}
printf("\nBrand : Qty\n");
for(i=0; i<NUM_BRANDS; i++) {
printf("%s : %d\n", shoe_brands[i], quantity[i]);
}
int option;
printf("\nHow would you like to sort the shoes?");
printf("\n1. Sort by Brands (Ascending Order)");
printf("\n2. Sort by Size (Descending Order)");
printf("\nEnter your choice (1 or 2):");
scanf("%d", &option);
switch(option) {
case 1:
for (i = 0; i < NUM_BRANDS - 1; i++) {
for (j = i + 1; j < NUM_BRANDS; j++) {
if (strcmp(shoe_brands[i],
shoe_brands[j]) > 0) {
char temp_brand[20];
strcpy(temp_brand, shoe_brands[i]);
strcpy(shoe_brands[i],
shoe_brands[j]);
strcpy(shoe_brands[j], temp_brand);
for (k = 0; k < NUM_SIZES; k++) {
int temp_size = shoe_sizes[i][k];
shoe_sizes[i][k] = shoe_sizes[j][k];
shoe_sizes[j][k] = temp_size;
}
int temp_quantity = quantity[i];
quantity[i] = quantity[j];
quantity[j] = temp_quantity;
}
}
}
printf("\nSorted by brands (Ascending
Order):\n");
break;
case 2:
for(i=0;i<NUM_BRANDS;i++){
for(j = 0; j < NUM_SIZES - 1; j++) {
for(k = j + 1; k < NUM_SIZES; k++) {
if(shoe_sizes[i][j] < shoe_sizes[i][k]) {
int temp_size = shoe_sizes[i][j];
shoe_sizes[i][j] = shoe_sizes[i][k];
shoe_sizes[i][k] = temp_size;
}
}
}
}
printf("\nSorted by size (Descending Order):\
n");
break;
default:
printf("\nInvalid Option. Please choose 1 or
2.");
break;
}
printf("Brand\tSizes\n");
for(i=0;i<NUM_BRANDS; i++) {
printf("%s\t", shoe_brands[i]);
for(j=0;j<NUM_SIZES;j++) {
printf("%d ", shoe_sizes[i][j]);
}
printf("\n");
}
printf("\nBrand : Qty\n");
for(i=0;i<NUM_BRANDS;i++) {
printf("%s : %d\n", shoe_brands[i], quantity[i]);
}

}
Logic:
The program uses the selection sort algorithm to
organize both the brands and sizes. This algorithm
iterates through the elements and selects the smallest or
largest item in each iteration, then swaps it with the
current position. This process continues until the entire
list is sorted.
When sorting by brand, the program ensures that shoes
of the same size are grouped together within each brand.
However, it may not order them within each size group.
This means that within a specific size, shoes from
different brands may not be sorted in any particular
order.
Output:

You might also like