Six stock prices (buying price) are stored into a 2 x 6 two-dimensional array. T
ID: 3804426 • Letter: S
Question
Six stock prices (buying price) are stored into a 2 x 6 two-dimensional array. The user inputs the current price of each stock. This program will compare the buying price and the current price of each stock. It will print a table:
Stock Buying Price Current Price Profit If Sold
1 12.25 15.00 2.75
etc. After the table is displayed, print out the stock with the highest profit if sold and its position in the array[row][column]. Please provide C code for above problem. Thank you
Explanation / Answer
#include <stdio.h>
int main(void) {
int stocks[2][6],profits[6];
int i,j,profit,maxProfit = -999,maxIndex; // Init maxProfit with -999, an arbitrary high negative value
// Consider the first row is having buying prices of the six stocks
// The second row would have the current price of the six stocks
for(i=0;i<2;i++){
for(j=0;j<6;j++){
scanf("%d",&stocks[i][j]);
}
}
// Let us calculate and store the profits in the array
// We will also calculate the stock having highest profit
for(j=0;j<6;j++){
profit = stocks[1][j] - stocks[0][j];
printf(" Stock: %d, Buying price: %d, Current Price: %d, Profit: %d ",j+1,stocks[0][j],stocks[1][j],profit);
if(profit > maxProfit){
maxProfit = profit;
maxIndex = j;
}
}
printf(" Stock %d is having the highest profit.",maxIndex+1);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.