Home > database >  Need to fix a few bugs (preprocessor directives)
Need to fix a few bugs (preprocessor directives)

Time:03-25

task condition: Using the structure type and preprocessor directives, compile a program for input information about N types of computer equipment, which is known: manufacturer, type (printer, scanner, laptop, mouse, keyboard), color, model), get the price by the formula y = 3x ^ 2 4x-2, where x is the number of the option plus N. Sort the prices for computer equipment by the method of "bubbles" in ascending order.

#define N 5 
#define M 15
#define PRI(X) 3*X*X 4*X-2 
typedef struct Ctechnology 
{
    char firma[M];
    char type[M];
    int price[N];
} comp;
int main()
{
    comp a;
    printf("Firm, type, price - (y=3x^2 4x-2)\n ");
    for (int i = 0; i < N; i  )
    {
        a.price[N] = PRI(((i   1) N)); // there is a problem
        printf("%d) ", i   1); 
        scanf("%s %s", a.firma, a.type);
        printf("\n                              | [%d] | Firm s | Type s | Price d |\n", i   1, a.firma, a.type, a.price[N]);
    }
    return 0;
}

CodePudding user response:

Running

#define N 5 
#define M 15
#define PRI(X) 3*X*X 4*X-2 

price[N] = PRI(((i   1) N));

through gcc -E (which runs the preprocessor), we get

price[5] = 3*((i   1) 5)*((i   1) 5) 4*((i   1) 5)-2;

In general, you may want to add parentheses around the Xes in your directive, so they're correctly grouped no matter the input:

#define PRI(X) 3*(X)*(X) 4*(X)-2 

->

price[5] = 3*(((i   1) 5))*(((i   1) 5)) 4*(((i   1) 5))-2;

Then, of course, there's the matter of price[N] always being an out-of-bounds access.

  •  Tags:  
  • c
  • Related