一道有点难度的C语言题目 小弟在线等 急啊
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/19 16:01:45
一道有点难度的C语言题目 小弟在线等 急啊
A supermarket conveyor belt holds a range of groceries. The price of each product is [ 60 120 5 13 ] ; while the numbers of each product are [ 3 4 2 1 ]. Write a C program that calculates and prints the total bill.
Hint:
To declare a vector in C you need to specify its type. See how price is declared below.
int price[4] = {60, 120, 5, 13};
The first element of price is price[0], with value 60 (note that in C we use brackets to index the vector)
The second element of price is price[1], with value 120.
The third element of price is price[2], with value 5.
The last element of price is price[3], with value 13.
A supermarket conveyor belt holds a range of groceries. The price of each product is [ 60 120 5 13 ] ; while the numbers of each product are [ 3 4 2 1 ]. Write a C program that calculates and prints the total bill.
Hint:
To declare a vector in C you need to specify its type. See how price is declared below.
int price[4] = {60, 120, 5, 13};
The first element of price is price[0], with value 60 (note that in C we use brackets to index the vector)
The second element of price is price[1], with value 120.
The third element of price is price[2], with value 5.
The last element of price is price[3], with value 13.
#include
void main()
{
int price[4] = { 60, 120, 5, 13 };
int num[4] = { 3, 4, 2, 1 };
int sum = 0;
for (int i = 0; i < 4; i++)
{
sum += price[i]*num[i];
}
printf("The total money is %d.\n", sum);
}
……难点何在?还是我理解错意思了?
void main()
{
int price[4] = { 60, 120, 5, 13 };
int num[4] = { 3, 4, 2, 1 };
int sum = 0;
for (int i = 0; i < 4; i++)
{
sum += price[i]*num[i];
}
printf("The total money is %d.\n", sum);
}
……难点何在?还是我理解错意思了?