Currently working on a project where I have two arrays (one 1-D and another 2-D). One contains names and another contains a set of numbers. I'm working on a function that finds the lowest value in the table and outputs it. However, I need to attribute the name of the monkey to the amount of food he has eaten. So "Monkey 1 has eaten the least amount of food only eating 11 pounds on Day 1". Below is the function to find the lowest value which is working fine, however I'm not sure how to implement the monkey's name (without just typing it in) and the day of the week. If the rest of my code is needed please let me know.
void leastAmt(string names[], int food[][NUM_DAYS])
{
int lowest = food[0][0];
for (int monkey = 0; monkey < NUM_MONKEYS; monkey++)
{
for (int day = 0; day < NUM_DAYS; day++)
{
if (food[monkey][day] < lowest)
lowest = food[monkey][day];
}
}
cout << "Least amount of food: " << lowest << endl;
}
create two variables for storing monkey and money values.
int lowest_monkey, least_day;
Whenever lowest value is modified update these values with current values of monkey and day variables.
Replace the last line with the following
cout << "Monkey " << lowest_monkey << " has eaten least amount of food only eating " << lowest << " on " << least_day << endl;
Store least_eating_monkey in an array, storing monkey names for each day.
int leastEatingMonkey[NUM_DAYS];
for (int day = 0; day < NUM_DAYS; day++)
{
lowest = INT_MAX;
for (int monkey = 0; monkey < NUM_MONKEYS; monkey++)
{
if (food[monkey][day] < lowest){
leastEatingMonkey[day] = monkey;
lowest = food[monkey][day];
}
}
}
for (int day = 0; day < NUM_DAYS; day++)
{
cout << "monkey " << names[day] << " ate least amount of food on day " << day << endl;
}
Related
I am doing a a problem using Multi-dimensional Arrays. I call the function in main but I get this Error message. This is the Original Problem I have:
A local zoo wants to keep track of how many pounds of food each of its three tigers eats each day during
a typical week. Write a program that stores this information in a two-dimensional 3 x 7 array, where
each row represents a different tiger and each column represents a different day of the week. The
program should first have the user input the data for each tiger. Then it should create a report that
includes the following information:
Average amount of food eaten by each of the tigers during the week.
Average amount of food eaten by all the tigers per day for each day.
The least amount of food eaten during the week by any tiger.
The greatest amount of food eaten during the week by any tiger.
the code:
#include <iostream>
using namespace std;
const int TIGERS = 3;
const int DAYS = 7;
int food[TIGERS][DAYS];
void inputArray(int food[TIGERS][DAYS]);
float AvgTig(float);
float Least(float);
float most(float);
int main()
{
float maximum;
float minimum;
float total = 0.0f;
float average = 0.0f;
float result = 0;
inputArray(food);
maximum = food[0][0];
minimum = food[0][0];
cout << "\n \n";
AvgTig(result);
cout << "\n \n";
Least(minimum);
cout << "\n \n";
most(maximum);
cout << "\n \n";
system("PAUSE");
return 0;
//end program
}
void inputArray(int food[TIGERS][DAYS])
{
int total = 0;
for (int tig = 0; tig < TIGERS; tig++) {
for (int day = 0; day < DAYS; day++) {
cout << "TIGER " << (day + 1) << ", day " << (day + 1) << ": ";
cin >> food[tig][day];
}
cout << endl;
}
}
float AvgTig(float output)
{
int total = 0;
for (int i = 0; i < TIGERS; i++) {
for (int day = 0; day < DAYS; day++) {
total += food[i][day];
}
cout << endl;
}
output = total / (TIGERS * DAYS);
cout << "Average food for Tigers in the days is :" << output << " ";
return output;
}
float Least(float minimum)
{
for (int least = 0; least < TIGERS; least++) {
for (int day = 0; day < DAYS; day++) {
if (food[least][day] < minimum)
minimum = food[least][day];
}
}
cout << "Minimum food eaten: " << minimum << " ";
return minimum;
}
float most(float maximum)
{
for (int most = 0; most < TIGERS; most++) {
for (int day = 0; day < DAYS; day++) {
if (food[most][day] > maximum)
maximum = food[most][day];
}
}
cout << " Maximum number of food is: " << maximum << " ";
return maximum;
}
On this line:
inputArray(food[TIGERS][DAYS]);
in main, you are calling inputArray with a single int, and not the entire food array. In fact, even reading this position in food is UB.
You need to call the function like this:
inputArray(food);
The program should prompt the user to enter the number of jars sold for each type.The program should produce a report that displays sales for each salsa type, total sales, and the names of the highest selling and lowest selling products.
#include <iostream>
#include<string>
using namespace std;
int main()
{
const int SIZE=5;
string salsa_names[SIZE] = { "mild","medium","sweet","hot","zesty" },name1,name2;
double number_of_jars_sold[SIZE],total=0;
cout << "enter the number of jars sold for each of different types of salsa\n";
for (int i = 0; i < SIZE; i++)
{
cout <<"The salsa names are :"<<"'"<<salsa_names[i]<<"'";
cin >> number_of_jars_sold[i];
total += number_of_jars_sold[i];
}
double large=number_of_jars_sold[0],small=number_of_jars_sold[0];
cout << "The sales for each of salsa type is ="<<endl;
for (int i=0;i<SIZE;i++)
{
cout << salsa_names[i] << " : " << number_of_jars_sold[i] << endl;
}
cout << "total sale is equal to" << total << endl;
for (int i = 0; i < SIZE; i++)
{
if (large < number_of_jars_sold[i])
{
large = number_of_jars_sold[i];
name1=salsa_names[i];
}
}
for (int j = 0; j < SIZE; j++)
{
if (small > number_of_jars_sold[j])
{
small = number_of_jars_sold[j];
name2 = salsa_names[j];
}
}
cout << "The name of highest selling product is : " << name1 << endl;
cout << "The name of lowest selling product is : " << name2 << endl;
return 0;
}
Edited::
you should initialize small and large to zero first, add an if condition before this line if (small > number_of_jars_sold[j]):
if(small == 0), so you will have minimum value in small variable.
CAN ONLY USE LOOPS & IF/ELSE Statements
So for my assignment I'm given this: Write a program with for loops that asks the user to enter today’s sales for five stores. The program should then display a bar graph comparing each store’s sales. Create each bar in the bar graph by displaying a row of asterisks. Each asterisk should represent $100 of sales.
So it should like:
Store 1: *****
Store 2: ***
Store 3: *******
etc.
I have most of it written up and everything displays properly except for my asterisks. I can't seem to get them to display. Also, I've searched around and there are answers to this, but all of them include arrays and we aren't allowed to use them in this exercise. Here's my code so far:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int sales = 0, sale = 0; //Identfy Variables
for (int i = 1; i <= 5; i++) {
cout << "Enter today's sales for Store " << i << ":" << endl;
cin >> sale;
sales += sale;
sales /= 100; //in a similar manner to +=, /= can be used for division.
}
cout << "SALES BAR CHART:" << endl;
cout << "Each * = $100" << endl;
for (int x = 1; x <= 5; x++) {
cout << "Store " << x << ": ";
for (int s = 0; s < sales; s++) cout << "*";
cout << endl;
}
return 0;
}
Try this.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string star = "";
int sale;
for (int i = 1; i <= 3; i++)
{
cout << "Enter today's sales for Store " << i << ":" << endl;
cin >> sale;
star += "Store " + to_string(i) + string(":");
for(int j= 0; j<sale/100; j++) {
star += "*";
}
star += "\n";
}
cout<<star;
return 0;
}
The problem with your code is that you do not store the data for each store individually. You have one variable called sales, which stores the sales of only the last store the end-user entered. All numbers the user enters before that get written over.
Since you have five stores, sales needs to be an array of five items:
int sales[5], total = 0;
Change your for loop to iterate 0 through 4, inclusive. Print (i+1) to end-users to stay one-based. Read sales numbers into sales[i] array element, then use it in the second loop to print your bar graph:
for (int i = 0 ; i != 5 ; i++) {
cout << "Enter today's sales for Store " << (i+1) << ":" << endl;
cin >> sales[i];
sales[i] /= 100;
total += sales[i];
}
...
// Now print your bar graph using a loop on i, and sales[i]
Your for loop is missing a definition of s:
for (int s; s < sales; s++)
should be:
for (int s = 0; s < sales; s++)
Without this explicit initialization, a variable s is declared, and memory is allocated for it, but its value remains whatever was left in memory from the last time that address was used. It's likely that this left over data was equivalent to an integer larger than sales, and thus your for loop wouldn't run, not even once.
As #dasblinkenlight mentioned, you have a singular variable that stores the "sales" of every store. Each store needs a separate variable, since each store is going to sell a different amount.
You already have the answers, I just want to add two options that would make printing the *'s easier:
using std::fill_n:
std::fill_n(std::ostream_iterator<char>(cout), sales, '*');
cout << endl;
using std::string's constructor overload (2):
cout << std::string(sales, '*') << endl;
This function is not accurately returning the highest and lowest values entered into the array. I'm not sure what I code I entered for the program to do this. This program needs to return the average of all of the elements entered into the array (the average part works fine) as well as find the highest and lowest values among all of the values entered into the array. Please help!
#include <iostream>
using namespace std;
float temptotal = 0;
float averagetemp = 0;
float temperatures[50];
float average(int);
void highest(int);
void lowest(int);
int main()
{
int days = 0;
cout << "Enter the number of days: ";
cin >> days;
if (days > 50)
{
cout << "You may only enter temperatures for 50 days." << endl;
return 0;
}
average(days);
highest(days);
lowest(days);
}
float average(int days)
{
for (int i = 1; i <= days; i++)
{
cout << "Enter the temperature for day number " << i << ": ";
cin >> temperatures[i];
temptotal += temperatures[i];
}
averagetemp = temptotal / days;
cout << "The average temperature is: " << averagetemp << endl;
return averagetemp;
}
void highest(int days)
{
int count;
int highest;
highest = temperatures[50];
for (count = 1; count < days; count++)
{
if (temperatures[count] > highest)
highest = temperatures[count];
cout << "The highest temperature is: " << highest << endl;
}
}
void lowest(int days)
{
int count;
int lowest;
lowest = temperatures[50];
for (count = 1; count < days; count++)
{
if (temperatures[count] < lowest)
lowest = temperatures[count];
cout << "The lowest temperature is: " << lowest << endl;
}
}
This function
float average(int days)
{
for (int i = 1; i <= days; i++)
{
cout << "Enter the temperature for day number " << i << ": ";
cin >> temperatures[i];
temptotal += temperatures[i];
//...
is already wrong because element temperatures[0] will not be uninitialized. You have to write
float average(int days)
{
for (int i = 1; i <= days; i++)
{
cout << "Enter the temperature for day number " << i << ": ";
cin >> temperatures[i-1];
temptotal += temperatures[i-1];
//...
Or
float average(int days)
{
for (int i = 0; i < days; i++)
{
cout << "Enter the temperature for day number " << i + 1 << ": ";
cin >> temperatures[i];
temptotal += temperatures[i];
//...
Functions highest and lowest are also wrong. For example array temperatures has no element with index 50. Moreover the user can enter number of days less than 50. So this statement
highest = temperatures[50];
is wrong.
The functions can be written like this function definition
void highest( int days )
{
int highest = temperatures[0];
for ( int count = 1; count < days; count++ )
{
if ( highest < temperatures[count] ) highest = temperatures[count];
}
cout << "The highest temperature is: " << highest << endl;
}
Take into acccount that there are standard algorithms std::max_element, std::min_element, std::minmax_element declared in header <algorithm> that can be used instead of your functions.
For example function highest can be defined with using standard algorithm std::max_element the following way
#include <algorithm>
//...
void highest( int days )
{
cout << "The highest temperature is: "
<< *std::max_element( temperatures, temperatures + days )
<< endl;
}
Array indices start with a 0
Place the cout statements outside the for loop.
Arrays are indexed from 0 to n-1, where n is the total number of entries in the array. You started the count at 1 in your loop when you should start at 0. If the user entered 50 values, you would have had an out-of-bounds access.
The correction should be:
for (int i = 0; i < days; i++)
Then in your output statement, it should be:
cout << "Enter the temperature for day number " << i+1 << ": ";
The other issue is your highest function. The issue are a few things:
You declared the local variable highest as an int. It should be a float to match the type used in the temperature array.
You should set highest to a very small value, smaller than any you would expect. Or better yet, set it to the first temperature entry before the loop, and start the loop from the second entry.
You named your local variable highest, but your function name is also highest. This will lead to confusion when someone else looks at your code.
If you're interested in another solution, the following computes the highest:
#include <algorithm>
//...
void highest(int days)
{
float theHighest = *std::max_element(temperatures, temperatures + days);
cout << "The highest temperature is: " << theHighest << endl;
}
A little better version from the function 'highest':
void highest(int days)
{
if (days < 1)
{
cout << "No temperatures" << endl;
}
double highest = temperatures[0];
for (int count = 1; count < days; count++)
{
if (temperatures[count] > highest)
{
highest = temperatures[count];
}
}
cout << "The highest temperature is: " << temperatures[highest_index] << endl;
}
All arrays in C++ starting with index 0. In this implementation this point is considered by using the first element (index 0) as thie first highest value. After that the loop has only to deal with the rest, begining at index 1.
The variable 'highest' must be from the type double. Otherwise you may get wrong results, because the highest is a little lower than the real highest due to generaly rounding down (double to int). The next comparison may be assigned even if it is a little lower.
Array indices start with 0 but your loops start at 1. For your loop over days, for instance, this will iterate over the full range:
for (int i = 0; i < days; i++)
I need to create a program that enters daily sales for each day of the week. Once the values are entered I need to be able to display:
Sales for day 1 are ###
Sales for day 2 are ###
The lowest sales was XXX
The highest sales was XXX
The problem is that I can't get my code to cout:
Sales for day 1 are XXX
Sales for day 2 are XXX
All I can get it to say is
Sales are:
XXX
XXX
XXX
And I also don't know how to find the lowest and the highest sales. We haven't even begun working with MIN & MAX functions so I'm lost as to how to accomplish it.
My code that I have so far is:
const int DAYS_SALES = 7;
double sales[DAYS_SALES];
int sub;
double min = 0;
double max = 0;
for(sub = 0; sub < DAYS_SALES; ++sub)
{
cout << "Enter in the sales for day " << (sub + 1) << " ";
cin >> sales[sub];
}
cout << endl << "The sales for day are: " << endl;
for (sub = 0; sub < DAYS_SALES; ++sub)
cout << sales[sub] << " " << endl;
Any help would be appreciated!
Track the min and max values as your for loop goes over each value.
If the current value (sales[sub]) is less than the min so far, store that value as the new minimum value.
const int DAYS_SALES = 7;
double sales[DAYS_SALES];
int sub;
double min = 0.0;
double max = 0.0;
for(sub = 0; sub < DAYS_SALES; ++sub)
{
cout << "Enter in the sales for day " << (sub + 1) << " ";
cin >> sales[sub];
}
min = sales[0];
max = sales[0];
cout << endl << "The sales for day are: " << endl;
for (sub = 0; sub < DAYS_SALES; ++sub)
{
cout << endl << "The sales for day are: " << sales[sub] << " " << endl;
if (sales[sub] < min)
{ // If we find a smaller min. value, store that in min
min = sales[sub];
}
if (sales[sub] > max)
{ // If we find a bigger max. value, store that in max
max = sales[sub];
}
}
// Print out the Min and Max that we found.
cout<< "The lowest sales was " << min;
cout<< "The highest sales was " << max <<endl;
Ok, maybe you should enter your values to a std::vector of the appropriate type and then call std::minmax(...) on it.