Professional Documents
Culture Documents
Ruhan Khalid 23k-5512 Assignment # 2 Programming For Business
Ruhan Khalid 23k-5512 Assignment # 2 Programming For Business
23k-5512
Assignment # 2
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've been assigned the task of creating a
program to manage a library'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] = {
{'F', 'N', 'N',
'F', 'F'}, // Fiction, Non-fiction
{'N', 'N', 'F',
'F', 'N'}, // Non-fiction, Fiction
{'F', 'F', 'N',
'N', 'N'}, // Fiction, Fiction,
Non-fiction
{'N', 'N', 'N',
'F', 'N'}, // Non-fiction, Fiction
{'F', 'N', 'F',
'N', 'N'} // Fiction, Non-fiction,
Fiction
};
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'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.
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: