Write two statements to assign numApples with 10 and numOranges with 3. Sample o
ID: 643450 • Letter: W
Question
Write two statements to assign numApples with 10 and numOranges with 3. Sample output for given program:
#include <stdio.h>
#include <stdlib.h>
typedef struct bagContents_struct {
int numApples;
int numOranges;
} bagContents;
void bagContents_PrintBag(bagContents* itemPtr) {
if (itemPtr == NULL) return;
printf("Apples: %d ", itemPtr->numApples);
printf("Oranges: %d ", itemPtr->numOranges);
return;
}
int main(void) {
bagContents* groceryPtr = NULL;
groceryPtr = (bagContents*)malloc(sizeof(bagContents));
/* Your solution goes here */
bagContents_PrintBag(groceryPtr);
return 0;
}
Explanation / Answer
//C program that will display structure to set the values for sturcture variables from main method.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct bagContents_struct
{
int numApples;
int numOranges;
}bagContents;
//Method to ste the variables, numApples, numOranges
//to structure of bagContents_struct
void bagContents_PrintBag(bagContents* itemPtr)
{
if (itemPtr == NULL)
return;
printf("Apples: %d ", itemPtr->numApples);
printf("Oranges: %d ", itemPtr->numOranges);
return;
}
int main(void)
{
//Create a structure variable set to NULL
bagContents* groceryPtr = NULL;
//Create a memory of bagContents
groceryPtr = (bagContents*)malloc(sizeof(bagContents));
/*Call the structure notation on groceryPtr
structure variable to set the numApples to 10 */
groceryPtr->numApples=10;
/*Call the structure notation on groceryPtr
structure variable to set the numApples to 3 */
groceryPtr->numOranges=3;
//call method bagContents_PrintBag
bagContents_PrintBag(groceryPtr);
//pause the program output
getch();
return 0;
}
--------------------------------------------------------------------------------------------------------------
Sample output:
Apples: 10
Oranges: 3
Hope this helps you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.