Search code examples
arrayscloopsdiscountsubtotal

data types different in C


how to do we declare parallel arrays in C. And also , how to add more than one data type in a single array. for example, i want to declare int and char data type in the same array

#include <stdio.h>

int main()
    

     
     
     double linetotal= qty*price;
     printf("line total =%lf\n",linetotal);
     double subtotal = line total - discount ;

    
      
    }
     }
    
    
    
    
    
    

Solution

  • Check the solution now.

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define NUM_CODE 10
    const char *CODE[NUM_CODE] = {"B100","B122","B134","B138","B145","B160","B165","B178","B186","B194"};
    double PRICE[NUM_CODE]={12.8,18.7,20.5,11.5,25.5,20.55,25.65,14.85,22.2,24.25};
    int main()
    {
        char name[20];
        
        printf("Enter buyer name: ");
        scanf("%s",name);
    
        int entry;
    
        printf("Enter number of entries for the invoice: ");
        scanf("%d",&entry);
        
        char code[10];
        int quantity;
        double price,linetotal=0;
        int i; 
        for(i=1;i<=entry;i++)
        {
            printf("Enter  Code: ");
            scanf("%s",code);
            printf("Enter Quantity: ");
            scanf("%d",&quantity);
            printf("Enter Unit Price: ");
            scanf("%lf",&price);
        
            int j;
            int flag=0;
            for(j=1;j<=NUM_CODE;j++){
                // how do i print values like B100 without getting errors//
                printf("%s ",CODE[j-1]);
    
                //  how to match the user input with parallel arrays?//
                if(strcmp(CODE[j-1],code)==0 && PRICE[j-1]==price){
                    //If correct code and price found do
                    printf("\nValid Code and price found\n");
                    //set the flag and break from the loop
                    flag=1;
                    break;
                }
            }
            if(flag==0){
                //Exit the program with invalid entry
                printf("\nInvalid Code and price found\n");
                printf("The program will close now...\n");
                exit(1);
            }
            
            //  how do calculate line total,and subtotal after discount//
            // discount is 10% if line total is greater than Rs5000//
            linetotal = price * quantity + linetotal;
            printf("\n");
        }
        double subtotal;
        if(linetotal>5000){
            subtotal = 0.9*linetotal;
        }
        else if(linetotal>=1000 && linetotal<=5000){
            subtotal = 0.95*linetotal;
        }
        else{
            subtotal = linetotal;
        }
    
        printf("Discount: %0.2lf%%\nTotal Price: %lf\n",((linetotal-subtotal)/linetotal)*100,subtotal);
    
        return 0;
    }