In: Computer Science
Explain What is being executed in this code. State all changes in values when traversing loops.
#include <stdio.h>
#define NUM_PRODUCTS 2
#define NUM_CATEGORIES 2
#define NUM_DIGITS 2
struct ProductInfo
{
int prodID;
int numberInStock;
double unitPrice;
};
int main(void)
{
struct ProductInfo productList[NUM_PRODUCTS] =
{
{78, 8, 19.95},
{95, 14, 22.98}
};
int categoryCount[NUM_CATEGORIES] = { 0 };
int i, j, sum, id, divisors[] = { 10, 1 };
for (i = 0; i < NUM_PRODUCTS; i++)
{
sum = 1;
id = productList[i].prodID;
for (j = 0; j < 2; j++)
{
sum *= id / divisors[j];
id = id % divisors[j];
}
categoryCount[sum % NUM_CATEGORIES] += productList[i].numberInStock;
}
for (i = 0; i < NUM_CATEGORIES; i++)
{
if (categoryCount[i] % 2 == 0)
{
printf("Category %d has %d items\n", i, categoryCount[i]);
}
}
return 0;
}
The main purpose of thia code is to count the number of items in stock in that category and print them.
In this code three constants have been declared , each with a value of 2.
#define NUM_PRODUCTS 2
#define NUM_CATEGORIES 2
#define NUM_DIGITS 2
The structure is created named ProductInfo with variables prodID,numberInStock and unitPrice.
struct ProductInfo
{
int prodID;
int numberInStock;
double unitPrice;
};
Now in the main loop ,
We created two objects of the structure ProductInfo and provided values for the variables in that structure for each object using the code snippet:
struct ProductInfo productList[NUM_PRODUCTS] =
{
{78, 8, 19.95},
{95, 14, 22.98}
};
So there are now two objects of the structure ProductInfo they are ProductInfo[0] and ProductInfo{1] and
The required variables for the forloop execution and the output generation is declared using the code snippet:
int categoryCount[NUM_CATEGORIES] = { 0 }; //two valued array as NUM_CATEGORIES = 2
int i, j, sum, id, divisors[] = { 10, 1 };
Now the changes taking place in this for loop snippet is as follows:
for (i = 0; i < NUM_PRODUCTS; i++)
{
sum = 1;
id = productList[i].prodID;
for (j = 0; j < 2; j++)
{
sum *= id / divisors[j];
id = id % divisors[j];
}
categoryCount[sum % NUM_CATEGORIES] += productList[i].numberInStock;
}
At first loop the sum is declared as 1 and id = productList[0].prodID = 78.
Now it enters the j loop which executes two times.
In the first loop the values of variables are as follows:
In the second loop the values of variables are as follows:
And Category count[0]=8
Now in the second loop ,sum is declared as 1 and id = productList[1].prodID = 95
Now it enters the j loop which executes two times.
In the first loop the values of variables are as follows:
In the second loop the values of variables are as follows:
And Category count[1]=14
Now the first for loop ends.
Now in the second for loop snippet:
for (i = 0; i < NUM_CATEGORIES; i++)
{
if (categoryCount[i] % 2 == 0)
{
printf("Category %d has %d items\n", i, categoryCount[i]);
}
}
If the categoryCount we got in the above is even then it is printed and hence when we run the program the output it generated is :
Hope this helps!!!