Project Report
Project Report
202045601
Problem Description:
Given a set of investment options (stocks) with associated returns, risks, and
prices, the task is to:
1. Select Stocks: Based on the user's risk tolerance (low, medium, high) and
budget, choose stocks to invest in.
Solution Approach:
2. Sorting: Sort stocks based on their profit-to-risk ratio within the specified
risk type range.
Algorithm Used:
202045601 Page 1
Design and Analysis of Algorithms
202045601
Source Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
typedef struct {
double profit;
double risk;
double price;
char name[20];
void knapsack(Stock stocks[], int n, double budget, int years, int num_stocks, char
risk_type[]) {
double budget_const=budget;
202045601 Page 2
Design and Analysis of Algorithms
202045601
if (strcmp(risk_type, "high") == 0) {
min_return = 0.20;
max_return = 0.40;
min_return = 0.05;
max_return = 0.15;
min_return = 0.15;
max_return = 0.20;
} else {
return;
// Sort stocks based on profit-to-risk ratio within the risk type range
stocks[j + 1] = temp;
202045601 Page 3
Design and Analysis of Algorithms
202045601
}
printf("\nSelected Stocks:\n");
budget -= stocks[i].price;
total_investment += stocks[i].price;
total_future_value += future_value;
total_expected_return += stocks[i].profit;
selected_count++;
} else {
total_future_value += future_value;
budget = 0;
selected_count++;
202045601 Page 4
Design and Analysis of Algorithms
202045601
}
int main() {
int n = 40;
Stock stocks[] = {
202045601 Page 5
Design and Analysis of Algorithms
202045601
{0.16, 0.35, 600, "HCL"},
202045601 Page 6
Design and Analysis of Algorithms
202045601
{0.15, 0.20, 620, "Panasonic"},
};
double budget;
char risk_type[10];
scanf("%lf", &budget);
scanf("%d", &years);
scanf("%d", &num_stocks);
scanf("%s", risk_type);
return 0;
202045601 Page 7
Design and Analysis of Algorithms
202045601
Output:
202045601 Page 8