Saturday, August 18, 2018

STRUCTURE POINTERS

  • Problem Description

    1. Create a Structure "grocery" with following data members:
    a. qty - int
    b. price - float
    c. amount - float
    d. itemname - char

    2. In main method declare structure variable as "itm"
    Hint: struct grocery itm

    3. Create an another structure pointer variable 
    Hint: struct grocery *pitem

    4. Assign the pointer assignment of itm to pitem
    Hint: pitem=&itm;

    5. Input the values of product name, price, quantity
    Hint: pitm->itemname

    6. Calculate the total amount as:
    pitem->amount =(float)pitem->qty * pitem->price;

    7. Display the details of itemname, price, quantity and totalamount


    Note: The structure variables, data members and structure name are CASE Sensitive.

    Follow the same case mentioned in the mandatory
  • CODING ARENA
  • #include <stdio.h>
    struct grocery
      {
      int qty; 
      float price,amount;
      char itemname[50];
      };
    int main()
    {
      struct grocery itm; 
      struct grocery *pitem; 
      pitem=&itm; 
      scanf("%s",pitem->itemname);
      scanf("%f",&pitem->price); 
      scanf("%d",&pitem->qty); 
      pitem->amount=(float)pitem->qty*pitem->price; 
      printf("Name=%s",pitem->itemname);
      printf("\nPrice=%f",pitem->price);
      printf("\nQuantity=%d",pitem->qty);
      printf("\nTotal Amount=%.2f",pitem->amount);

    return 0;
    }
  • Test Case 1

    Input (stdin)
    Pen
    
    5.5
    
    10
    
    
    Expected Output
    Name=Pen
    
    Price=5.500000
    
    Quantity=10
    
    Total Amount=55.00
  • Test Case 2

    Input (stdin)
    Chocolate
    
    15.5
    
    102
    
    
    Expected Output
    Name=Chocolate
    
    Price=15.500000
    
    Quantity=102
    
    Total Amount=1581.00

No comments:

Post a Comment