Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

team B solution #9

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stdio.h>
int findMin (int arr[], int n);
int find_Max_Profit(int array[], int n);

int max_p=0, min_p, max_v, min_v, max_profit=0;
/* max_p store the position of max num in the array
max_v store the value of max num in the array
min_p store the position of min num in the array
min_v store the value of min num in the array
*/


int main() {
int n;// n is the number of element
printf("How many numbers do you want to enter?\n");
scanf("%d", &n);
int array[n];
for (int i=0; i<n; i++){
printf("Enter your element:\n");
scanf(" %d", &array[i]);
}
// assume last element in the array is the max number in the array, and store it's potision
max_v=array[n-1];
max_p=n-1;
printf("\nresult is %d\n",find_Max_Profit(array, n));

for (int z=0; z<max_p; z++){
if (min_v==array[z] && z+1<max_p){
min_p=z+1;
break;
}
}
if (max_profit!=0)
printf("Description: Buy on day %d (price = %d) and sell on day %d (price = %d), profit = %d-%d = %d. ",min_p,min_v,max_p,max_v,max_v,min_v,max_profit);
else
printf("no action should be taken")

}

int find_Max_Profit(int array[], int n){
for (int i=n-1; i>0; i--){
if ((array[i]-findMin(array, i-1))>max_profit){
max_profit=array[i]-findMin(array, i-1);
max_p=i+1;
max_v=array[i];
min_v=findMin(array, i-1);
}
}
return max_p;
}

// find the minimum value in the given range n in the array
int findMin (int arr[], int n){
int i, min=arr[n];
for (int k=n-1; k>=0; k--){
if (arr[k]<min){
min=arr[k];
}
}
return min;
}