Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C Programming language, not C++) Companies and people often buy and sell stocks

ID: 3673408 • Letter: #

Question

(C Programming language, not C++)

Companies and people often buy and sell stocks. Often they buy the same stock for different prices at different times. Say one owns 1000 shares a certain stock (such as Checkpoint), one may have bought the stock in amounts of 100 shares over 10 different times with 10 different prices. We will analyze two different methods of accounting, FIFO and LIFO accounting used for determining the “cost” of a stock. This is also known as the dollar cost average basis. This information is typically calculated when a stock is sold to determine if a profit or loss was made. In our version of FIFO accounting, the price of a commodity is averaged starting with the first purchase of that item. Say we sell 250 shares of a stock, according to this method the dollar cost averaged purchase price is determined by averaging the prices on the first 250 shares bought. In our version of LIFO accounting, the price of a commodity is averaged starting with the last purchase of that item. Say we sell 250 shares of a stock, according to this method the dollar cost average purchase price is determined by averaging the prices on the last 250 shares bought. In this assignment, you will be using a queue for storing data for FIFO accounting, and a stack for LIFO accounting. Implement you queue using link list and implement your stack using a link list. Both your stack and queue should have records with the following fields: The stock Tticker symbol (a string). The Stock Name ( a string). The number of shares of a stock (an integer). The purchase price for those shares (decimal). You can assume that the first element of the structure is the security bought first, the second was bought second, etc. Your program should have the user able to enter information about various stocks, the amount of shares, and the price. The user can then enter a query about a certain stock and the cost according to the LIFO and FIFO accounting methods for a certain number of shares.

The following could be your menu:

Press 1 to enter a new stock

Press 2 to find the LIFO and FIFO dollar cost average for the number of shares sold.

If 1 is pressed, the user needs to enter the stock symbol, Stock name, the number of shares bought, and the price per share when purchased.

If 2 is pressed, the user needs to enter the stock symbol being queried and the number of shares you wish to sell.

Once that information is entered the dollar cost averaged price per share for both the LIFO method and the FIFO method should be displayed. Implement the program described above using the c programming language to meet the overall requirements described above.

Use the blow data.

Ticker:XOM Name:Exon Mobile

Purchases in the order below.

1. 100 shares at 82.65

2. 50 sharees at 80.23

3. 200 shares at 84.50

4. 100 shares at 81.58

Ticker:D Name:Dominion Resources

Purchases in the order below.

1. 350 shares at69.98

2. 200 shares at 70.72

3. 100 shares at 69.00

4. 180 shares at 71.90

After you enter the XOM data have the software calculate and display the dollar cost average basis for both the LIFO and FIFO methods when selling 400 shares.

After you enter the D data have the software calculate and display the dollar cost average basis using both the LIFO and FIFO methods when selling 400 shares.

Explanation / Answer

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct stockShare
{
   char tickerSymbol[20];
    char tickername[20];
    int noOfShares[4];
   double sharePrices[4];
};

typedef struct ShareNode{
    struct stockShare sh;
    struct ShareNode* nextShare;
};

void insertShare(ShareNode *&h, stockShare *s){
ShareNode *t ;
t->sh = s;
t->nextShare = NULL;
if(h == NULL){
h = t;
return;
}
ShareNode *t2 = h;
while(t2->nextShare != NULL){
t2 = t2->nextShare;
}
t2->nextShare = t;
}

double getLIFOCost(stockShare **shareArr, int c, int share,char *shareSymbol){
   
       
   int k2;
   float sharesCost=0;
   int shareCount=0;
    for(int k = c - 1; k >= 0; --k)
   {
       if(strcmp(shareArr[k].tickerSymbol,shareSymbol)==0)
       {
           for(int k1=0;k1<4;k1++)
           {
               if(share <= shareCount)
               {
                   return shareCost/shareCount;
               }
               else
               {
                   shareCount+=shareArr[k].noOfShares[k1];
                   shareCost+=(shareArr[k].sharePrices[k1]*shareArr[k].noOfShares[k1]);
               }
           }
       }
   }
   return 0;

}

double getFIFOCost(ShareNode *h, int share,char *shareSymbol)
{   
    ShareNode *t = head;
   int k2;
   float sharesCost=0;
   int shareCount=0;
    while(t != NULL)
   {
        if(strcmp(t->sh->tickerSymbol,shareSymbol)==0)
       {
           for(int k1=0;k1<4;k1++)
           {
               if(share <= shareCount)
               {
                   return shareCost/shareCount;
               }
               else
               {
                   shareCount+=t->sh.noOfShares[k1];
                   shareCost+=(t->sh.sharePrices[k1]*t->sh.noOfShares[k1]);
               }
           }
       }
       else
           t=t->nextShare;
    }
   return 0;
}
void showLIFOArray(stochShare **shareArr, int c)
{
   int k;
   int k2;
   for(k=0;k<c;k++)
   {
       printf("Ticker Symbol: %s ", shareArr[k].tickerSymbol);
       printf("Ticker Name: %s ", shareArr[k].tickername);
       printf("Purchase Order: ");
       for(k2=0;k2<4;k2++)
       {
           printf(" %d. %d shares at %f",k2+1, shareArr[k].noOfShares[k2],shareArr[k].sharePrices[k2]);
       }
       printf(" ");
   }
}

int main()
{
   stockShare **shareArr;
    ShareNode *h = NULL;
    stockShare s1 ;
   char *name;
   int sCount=0;
   int ot1;
    while(1)
   {
        printf("1.ENTER NEW STOCK ");
        printf("2. AVERAFE FOR NUMBER OF SHARES TO SOLD ");
      printf("3.EXIT ");
        printf("ENTER YOUR CHOICE ");
        scanf("%d",&ot1);
        switch(option){
case 1:
      
    printf("enter symbol,name: ");
   scanf("%s%s",s1.tickerSymbol,s1.tickername);
       printf("Enter noof shares and Purchse Price:");
       for(int k=0;k<4;k++)
       {
           scanf("%d%f",&s1.noOfShares[k],&s1.sharePrices[k]);
       }
       insertShare( h,s1);   
    shareArr[sCount++] = s1;
    break;
case 2:
    printf("enter symbol to sell 400 shares: ");
   scanf("%s",name);      
    printf("Cost (lifo): %f", getLIFOCost(shareArr, 10, 400,name) );
    printf("cost (fifo): %f" , getFIFOCost(h, 400,name) );
    break;
case 3:
return 0;
break;
default:
    printf("INVALID" );
}
}
return 0;
}