C Programming 1. Declare an array availablePizzas of 3 PizzaIngredients elements
ID: 3875924 • Letter: C
Question
C Programming
1. Declare an array availablePizzas of 3 PizzaIngredients elements.
#include <stdio.h>
#include <string.h>
typedef struct PizzaIngredients_struct {
char pizzaName[30];
char ingredients[70];
} PizzaIngredients;
int main(void) {
/* Your solution goes here */
strcpy(availablePizzas[0].pizzaName, "Barbecue");
strcpy(availablePizzas[0].ingredients, "Beef, chicken, bacon, barbecue sauce");
strcpy(availablePizzas[1].pizzaName, "Carbonara");
strcpy(availablePizzas[1].ingredients, "Mushrooms, onion, creamy sauce");
strcpy(availablePizzas[2].pizzaName, "Ham and Cheese");
strcpy(availablePizzas[2].ingredients, "Ham, cheese, bacon");
printf("%s: %s ", availablePizzas[0].pizzaName, availablePizzas[0].ingredients);
printf("%s: %s ", availablePizzas[1].pizzaName, availablePizzas[1].ingredients);
printf("%s: %s ", availablePizzas[2].pizzaName, availablePizzas[2].ingredients);
return 0;
}
2. Write a statement that calls a function named IncreaseItemQty, passing the variable addQty. Assign notebookInfo with the value returned by IncreaseItemQty.
#include <stdio.h>
#include <string.h>
typedef struct ProductInfo_struct {
char itemName[30];
int itemQty;
} ProductInfo;
ProductInfo IncreaseItemQty (ProductInfo productToStock, int increaseValue) {
productToStock.itemQty = productToStock.itemQty + increaseValue;
return productToStock;
}
int main(void) {
ProductInfo notebookInfo;
int addQty = 10;
scanf("%s", notebookInfo.itemName);
scanf("%d", ¬ebookInfo.itemQty);
/* Your solution goes here */
printf("Name: %s, stock: %d ", notebookInfo.itemName, notebookInfo.itemQty);
return 0;
}
3. For any element in keysList with a value greater than 40, print the corresponding value in itemsList, followed by a space.
Ex: If keysList = {32, 105, 101, 35} and itemsList = {10, 20, 30, 40}, print:
#include <stdio.h>
int main (void) {
const int SIZE_LIST = 4;
int keysList[SIZE_LIST];
int itemsList[SIZE_LIST];
int i;
keysList[0] = 13;
keysList[1] = 47;
keysList[2] = 71;
keysList[3] = 59;
itemsList[0] = 12;
itemsList[1] = 36;
itemsList[2] = 72;
itemsList[3] = 54;
/* Your solution goes here */
printf(" ");
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
typedef struct PizzaIngredients_struct {
char pizzaName[30];
char ingredients[70];
} PizzaIngredients;
int main(void) {
// declaration goes like StructureName VariableName[NumberOfItems]
PizzaIngredients availablePizzas[3];
strcpy(availablePizzas[0].pizzaName, "Barbecue");
strcpy(availablePizzas[0].ingredients, "Beef, chicken, bacon, barbecue sauce");
strcpy(availablePizzas[1].pizzaName, "Carbonara");
strcpy(availablePizzas[1].ingredients, "Mushrooms, onion, creamy sauce");
strcpy(availablePizzas[2].pizzaName, "Ham and Cheese");
strcpy(availablePizzas[2].ingredients, "Ham, cheese, bacon");
printf("%s: %s ", availablePizzas[0].pizzaName, availablePizzas[0].ingredients);
printf("%s: %s ", availablePizzas[1].pizzaName, availablePizzas[1].ingredients);
printf("%s: %s ", availablePizzas[2].pizzaName, availablePizzas[2].ingredients);
return 0;
}
Output
#include <stdio.h>
#include <string.h>
typedef struct ProductInfo_struct {
char itemName[30];
int itemQty;
} ProductInfo;
ProductInfo IncreaseItemQty (ProductInfo productToStock, int increaseValue) {
productToStock.itemQty = productToStock.itemQty + increaseValue;
return productToStock;
}
int main(void) {
ProductInfo notebookInfo;
int addQty = 10;
scanf("%s", notebookInfo.itemName);
scanf("%d", ¬ebookInfo.itemQty);
/* Calling function with notebookInfo and assigning the structure after incrementing the quantity */
notebookInfo = IncreaseItemQty(notebookInfo, addQty);
printf("Name: %s, stock: %d ", notebookInfo.itemName, notebookInfo.itemQty);
return 0;
}
Output
#include <stdio.h>
int main (void) {
const int SIZE_LIST = 4;
int keysList[SIZE_LIST];
int itemsList[SIZE_LIST];
int i;
keysList[0] = 13;
keysList[1] = 47;
keysList[2] = 71;
keysList[3] = 59;
itemsList[0] = 12;
itemsList[1] = 36;
itemsList[2] = 72;
itemsList[3] = 54;
/* Looping through each element
Checking if the value is greater than 40
printing value
*/
for(i=0; i<SIZE_LIST; i++)
{
if(keysList[i] > 40)
printf("%d ", itemsList[i]);
}
printf(" ");
return 0;
}
Output
#include <stdio.h>
#include <string.h>
typedef struct PizzaIngredients_struct {
char pizzaName[30];
char ingredients[70];
} PizzaIngredients;
int main(void) {
// declaration goes like StructureName VariableName[NumberOfItems]
PizzaIngredients availablePizzas[3];
strcpy(availablePizzas[0].pizzaName, "Barbecue");
strcpy(availablePizzas[0].ingredients, "Beef, chicken, bacon, barbecue sauce");
strcpy(availablePizzas[1].pizzaName, "Carbonara");
strcpy(availablePizzas[1].ingredients, "Mushrooms, onion, creamy sauce");
strcpy(availablePizzas[2].pizzaName, "Ham and Cheese");
strcpy(availablePizzas[2].ingredients, "Ham, cheese, bacon");
printf("%s: %s ", availablePizzas[0].pizzaName, availablePizzas[0].ingredients);
printf("%s: %s ", availablePizzas[1].pizzaName, availablePizzas[1].ingredients);
printf("%s: %s ", availablePizzas[2].pizzaName, availablePizzas[2].ingredients);
return 0;
}
Output
Barbecue: Beef, chicken, bacon, barbecue sauce Carbonara: Mushrooms, onion, creamy sauce Ham and Cheese: Ham, cheese, bacon
#include <stdio.h>
#include <string.h>
typedef struct ProductInfo_struct {
char itemName[30];
int itemQty;
} ProductInfo;
ProductInfo IncreaseItemQty (ProductInfo productToStock, int increaseValue) {
productToStock.itemQty = productToStock.itemQty + increaseValue;
return productToStock;
}
int main(void) {
ProductInfo notebookInfo;
int addQty = 10;
scanf("%s", notebookInfo.itemName);
scanf("%d", ¬ebookInfo.itemQty);
/* Calling function with notebookInfo and assigning the structure after incrementing the quantity */
notebookInfo = IncreaseItemQty(notebookInfo, addQty);
printf("Name: %s, stock: %d ", notebookInfo.itemName, notebookInfo.itemQty);
return 0;
}
Output
Chegg 20 Name: Chegg, stock: 30
#include <stdio.h>
int main (void) {
const int SIZE_LIST = 4;
int keysList[SIZE_LIST];
int itemsList[SIZE_LIST];
int i;
keysList[0] = 13;
keysList[1] = 47;
keysList[2] = 71;
keysList[3] = 59;
itemsList[0] = 12;
itemsList[1] = 36;
itemsList[2] = 72;
itemsList[3] = 54;
/* Looping through each element
Checking if the value is greater than 40
printing value
*/
for(i=0; i<SIZE_LIST; i++)
{
if(keysList[i] > 40)
printf("%d ", itemsList[i]);
}
printf(" ");
return 0;
}
Output
36 72 54
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.