0

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 ;

    
      
    }
     }
    
    
    
    
    
    
  • 2
    This won't work: `scanf("%s",&name);` because `name` is only a single character. You also have the wrong types for format specifiers for `scanf("%s",&name);`, `scanf("%f",&quantity);`, and `scanf("%f",&price);`. – Fred Larson Aug 04 '20 at 18:21
  • @FredLarson but that worked for me... –  Aug 04 '20 at 18:23
  • 2
    No, it only seemed to. Such is the nature of undefined behavior. It can *look* like it works, when in reality you have a time bomb waiting to go off. – Fred Larson Aug 04 '20 at 18:24
  • Another problem you are having, or will have, is [`scanf` leaving newline characters in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Fred Larson Aug 04 '20 at 18:25
  • @FredLarson yeah, thank you i noticed that. i can add a whitespace so the scanf will ignore the newline right? btw, can you please help me with the rest of the code? –  Aug 04 '20 at 18:30
  • `B100`, `B122`, etc. are not defined identifiers. What is your intent here? – Fred Larson Aug 04 '20 at 18:30
  • @FredLarson B100 is the book code and the corresponding price is Rs12.8. the data moves on respectively. I want the input entered by user for book code/price and the book code/price in my array to be the same. Then i want to calculate line total and subtotal after discount . –  Aug 04 '20 at 18:34
  • Then I think you mean `"B100"`, but that is not an `int`. – Fred Larson Aug 04 '20 at 18:34
  • @FredLarson Oh yes, "B100" but,how can i declare it –  Aug 04 '20 at 18:36
  • @FredLarson What i intend to do is print a receipt containing these details. If the code entered by user does not match the code in the array (like B100), it should print invalid code and ask user to re-enter code. –  Aug 04 '20 at 18:43
  • You probably want something like `char *CODE[10] = {"B100","B122","B134", ...` – William Pursell Aug 04 '20 at 19:36

1 Answers1

0

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;
}
Sourabh Choure
  • 723
  • 4
  • 15
  • Line Totals (total of purchase made by customer)and Subtotal should be calculated using Quantity and the Unit Price. If the Subtotal is greater than Rs 5000, then they will give a 10% discount on the subtotal. If the Subtotal is between $1000 and $5000, then they will give a 5% discount on the subtotal. Finally, the invoice should display the discount amount and the Total price has to be paid by the customer. –  Aug 04 '20 at 19:09
  • Okay, got it now. – Sourabh Choure Aug 04 '20 at 19:11
  • Oh, You can use the if-else block for error checking. – Sourabh Choure Aug 04 '20 at 19:27
  • @tiara Remember... If this answer helped you - give it an upvote. If this answer solved your problem - accept it. :-) – Support Ukraine Aug 04 '20 at 19:38
  • "Parallel arrays" is almost always an anti-pattern. What's better than N parallel arrays is one array of `struct` where the struct has N properties. – tadman Aug 04 '20 at 20:02