Question

In: Computer Science

Explain What is being executed in this code. State all changes in values when traversing loops....

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;

}

Solutions

Expert Solution

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

  • productList[0].prodId = 78
  • productList[0].numberInStock = 8
  • productList[0].unitPrice = 78
  • productList[1].prodId = 78
  • productList[1].numberInStock = 8
  • productList[1].unitPrice = 78

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:

  • sum:7
  • id:8

In the second loop the values of variables are as follows:

  • sum:56
  • id:0

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:

  • sum:9
  • id:5

In the second loop the values of variables are as follows:

  • sum:45
  • id:0

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!!!


Related Solutions

What makes the state of being and the state of existing identical? Explain!
What makes the state of being and the state of existing identical? Explain!
True and False 1. The instruction register stores machine code for the instruction being executed. 2....
True and False 1. The instruction register stores machine code for the instruction being executed. 2. Before a digital computer may execute an instruction, the instruction code must be fetched from memory. 3. A pointer is a binary code for data in the arithmetic logic unit. 4. Von Neumann computer architecture stores data and instruction codes in the same memory. 5. Complex instruction set computers have instructions with greater speed than those in reduced instruction set computers.
What changes are seen in the amino acids when hemoglobin changes its state from t state...
What changes are seen in the amino acids when hemoglobin changes its state from t state to r state? Explain all tge interactions of the subunits.
Microsoft Word is being executed by the CPU, and it becomes blocked on IO. What are...
Microsoft Word is being executed by the CPU, and it becomes blocked on IO. What are the next steps taken by the system? Give a brief description of each step.
2. The HTML document on the following page consists of JavaScript code executed when the document...
2. The HTML document on the following page consists of JavaScript code executed when the document is loaded. It prompts the user for his/her name (first and last names) then outputs the greeting “Hello Mr. _____!” or “Hello Ms. _____!” (where _____ is the user’s last name) depending on whether the first name is recognized as a male name. (In the code, only three male names are checked; a realistic program would check many more names.) We allow for the...
As a process executes, it changes states. Explain what are ready state, running state, and waiting...
As a process executes, it changes states. Explain what are ready state, running state, and waiting state. Also, describe when a process moves between these three states
Thinking in Assembly language What values will be written to the array when the following code...
Thinking in Assembly language What values will be written to the array when the following code executes? .data array DWORD 4 DUP(0) .code main PROC mov eax,10 mov esi,0 call proc_1 add esi,4 add eax,10 mov array[esi],eax INVOKE ExitProcess,0 main ENDP proc_1 PROC call proc_2 add esi,4 add eax,10 mov array[esi],eax ret proc_1 ENDP proc_2 PROC call proc_3 add esi,4 add eax,10 mov array[esi],eax ret proc_2 ENDP proc_3 PROC mov array[esi],eax ret proc_3 ENDP
1. What will be the value of numbers[1] after the following code is executed? int[] numbers...
1. What will be the value of numbers[1] after the following code is executed? int[] numbers = {22, 33, 44}; for(int k = 0; k < 3; k++) { numbers[k] = numbers[k] + 5; } } 2. What will be the results of the following code? final int ARRAY_SIZE = 5; double[] x = new double[ARRAY_SIZE]; for(int i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10.0; } 3.What is the value of numbers [3] after the following line...
explain in a detailed paragraph please  - What is meant by the genetic code being universal and...
explain in a detailed paragraph please  - What is meant by the genetic code being universal and redundant?
State how derivatives and changes in their values are reported under US GAAP and IFRS.
State how derivatives and changes in their values are reported under US GAAP and IFRS.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT