C++ 数组中的最大/最小值?
目前,我正在解决一个家庭作业问题,希望能得到一些提示,说明我的代码为什么不起作用/我遗漏了什么。家庭作业问题要求您制作一个数组,该数组保存一个商店的一串萨尔萨香精,从用户输入中收集销售数量(罐)的数据,然后显示每种香精的总销售额。我记下了所有这些,但问题继续问你如何检索畅销书和最差的畅销书。我已经尝试并且可以让我的代码使用两种风格,但它们完全不正确。感谢您的帮助!这是我的密码:C++ 数组中的最大/最小值?,c++,C++,目前,我正在解决一个家庭作业问题,希望能得到一些提示,说明我的代码为什么不起作用/我遗漏了什么。家庭作业问题要求您制作一个数组,该数组保存一个商店的一串萨尔萨香精,从用户输入中收集销售数量(罐)的数据,然后显示每种香精的总销售额。我记下了所有这些,但问题继续问你如何检索畅销书和最差的畅销书。我已经尝试并且可以让我的代码使用两种风格,但它们完全不正确。感谢您的帮助!这是我的密码: #include <iostream> #include <string>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
// variables
const int SIZE = 5;
string salsas[SIZE] = { "Mild", "Medium", "Sweet", "Hot", "Zesty"};
//names of salsas
int jars[SIZE]; //holds number of jars user enters
int sales[SIZE]; //holds sales numbers
int count; //counts for a loop
int largest; //largest sale
int smallest; //smallest sale
cout << "Enter the monthly number of jars sold for each type of
salsa.\n\n";
// Display the salsas in the array
for (int count = 0; count < SIZE; count++)
{
cout << salsas[count] << ": ";
cin >> jars[count];
}
// Display salsa sales
cout << "\nEach jar of salsa, no matter the type, costs $5.50.\n";
cout << "Here are the monthly sales numbers for each type of salsa.
\n\n";
cout << fixed << showpoint << setprecision(2);
for (int count = 0; count < SIZE; count++)
{
double sales = jars[count] * 5.50;
cout << salsas[count] << ": $";
cout << sales << endl;
}
//Gets highest sale
{
int count;
int largest = sales[0];
int index = 0;
for (count = 0; count < SIZE; count++)
{
if (sales[count] > largest)
{
largest = sales[count];
index = count;
}
}
cout << "\nBest Seller: " << salsas[index] << endl;
}
//Gets lowest sale
{
int count;
int smallest = sales[0];
int index = 0;
for (count = 0; count < SIZE; count++)
{
if (sales[count] < smallest)
{
smallest = sales[count];
index = count;
}
}
cout << "\nWorst Seller: " << salsas[index] << endl;
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
//变数
常数int SIZE=5;
字符串salsas[SIZE]={“温和”、“中等”、“甜”、“热”、“活泼”};
//萨尔萨的名字
int jars[SIZE];//保存用户输入的jar数
int sales[SIZE];//保存销售数字
int count;//循环的计数
int最大;//最大销售额
int最小;//最小销售额
cout调试自己的代码的最大障碍之一是命名变量。如果在main()
函数的开头声明了count
,最大的,和最小的变量,但稍后在单独的块中重新声明它们。这是完全冗余的
实际的错误在于,您没有实际计算和填充sales[]
数组。稍后比较最大的和最小的将无法达到预期效果
在下面的代码中查找这一行
sales[count] = jars[count] * 5.50;
LéCode
那么,sales
(数组)代表还剩多少,或者卖出了多少?它们都以相同的原始编号开始吗?对不起,我遗漏了一些信息,用户(店主)输入一个月内销售的特定萨尔萨香精的罐数。数字可以组合。销售额应表示一种特定香精的罐数乘以罐价(每罐5.50美元).你真是帮了大忙,得到答案很好,但在每件事上加上解释纯粹是仁慈。非常感谢!如果你觉得我的答案有用,请。谢谢:-)
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
// variables
const int SIZE = 5;
string salsas[SIZE] = { "Mild", "Medium", "Sweet", "Hot", "Zesty"};
//names of salsas
int jars[SIZE]; //holds number of jars user enters
int sales[SIZE]; //holds sales numbers
// int count; //counts for a loop // no need
// int largest; //largest sale // no need
// int smallest; //smallest sale // no need
cout << "Enter the monthly number of jars sold for each type of salsa.\n\n";
// Display the salsas in the array
for (int count = 0; count < SIZE; count++)
{
cout << salsas[count] << ": ";
cin >> jars[count];
}
// Display salsa sales
cout << "\nEach jar of salsa, no matter the type, costs $5.50.\n";
cout << "Here are the monthly sales numbers for each type of salsa. \n\n";
cout << fixed << showpoint << setprecision(2);
for (int count = 0; count < SIZE; count++) // declare `count` where it is used
{
sales[count] = jars[count] * 5.50; // calculate sales[count] IMPORTANT
cout << salsas[count] << ": $";
cout << sales[count] << endl; // show appropriate output
}
//Gets highest sale
int largest = sales[0]; // declare largest and index closest to where they are used
int index = 0;
for (int count = 0; count < SIZE; count++)
{
if (sales[count] > largest)
{
largest = sales[count];
index = count;
}
}
cout << "\nBest Seller: " << salsas[index] << endl;
//Gets lowest sale
int smallest = sales[0]; // declare smallest closest to where it is used
index = 0; // reuse your old variable
for (int count = 0; count < SIZE; count++)
{
if (sales[count] < smallest)
{
smallest = sales[count];
index = count;
}
}
cout << "\nWorst Seller: " << salsas[index] << endl;
return 0;
}