Cannot convert int to int[][] - c++

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);

Related

Your C++ program must use functions to input the scores, compute the average, and find the index of the highest score

I've been working on this for hours now and I'm almost done. I can't get the program to display the correct student ID.
Also the "highIndex" function is suppose to be an "int" but I started with a double. When I try to change it, everything else seems to fall apart.
How do I get the high score to be associated with the student ID so I can output it correctly? I also need to highIndex to be an int which means some other things needs to be changed.
Any help would be much appreciated :)
Here's what I have so far
void inputAnswers(int studentIds[], double scores[]);
double getAverage(double scores[])
{
double total = 0;
for (int count = 0; count <= 6; count++)
{
total += scores[count];
}
return total / 7;
}
double highIndex(double score[])
{
double highScore = score[0];
double indexHigh = 1;
for (int count = 0; count <= 6; count++)
{
if (score[count] > highScore)
{
highScore = score[count];
indexHigh = count;
}
}
return highScore;
}
int main()
{
const int ids = 7;
int student[ids] = { 1234, 2333, 4432, 3323, 2143, 3425, 4123 };
double scores[7];
double highScore[7];
// Gets the test score from user
inputAnswers(student, scores);
// Calculates the average
cout << "The average score is " << getAverage(scores) << endl;
// Calculates highest score
cout << "The high score was student " << highIndex(highScore) << " with a score of " << highIndex(scores);
return 0;
}
// Function gets student scores
void inputAnswers(int student[], double scores[])
{
for (int count = 0; count <= 6; count++)
{
cout << "Enter the score for student "<< student[count] << ": ";
cin >> scores[count];
}
}
As per my observation you haven't supplied any values to the Highscore array and it is not required as well.
If all you need is to find average score, highscore and id of student with highscore this slight change will do the trick just adjusted 3 values from your code and is documented at corresponding lines.
#include<iostream>
using namespace std;
double getAverage(double scores[])
{
double total = 0;
for (int count = 0; count <= 6; count++)
{
total += scores[count];
}
return total / 7;
}
void inputAnswers(int student[], double scores[])
{
for (int count = 0; count <= 6; count++)
{
cout << "Enter the score for student "<< student[count] << ": ";
cin >> scores[count];
}
}
int highIndex(double score[])
{
double highScore = score[0];
double indexHigh = 0; //as we are assigning from position 0
for (int count = 1; count <= 6; count++)
{
if (score[count] > highScore)
{
highScore = score[count];
indexHigh = count;
}
}
return indexHigh; //returns index of highscore
}
int main()
{
const int ids = 7;
int student[ids] = { 1234, 2333, 4432, 3323, 2143, 3425, 4123 };
double scores[7];
//double highScore[7]; no need
// Gets the test score from user
inputAnswers(student, scores);
// Calculates the average
cout << "The average score is " << getAverage(scores) << endl;
// Calculates highest score
cout << "The high score was student " << student[highIndex(scores)] << " with a score of " << scores[highIndex(scores)]; //uses returned index to find values from array
return 0;
}
Although i strongly recommend using class or structures for such data collection of any entitiy. Happy Coding :-)

Maximum and minimum values not printing in main () function

I am doing a code on tracking how much food tigers eat in 1 week and I am tracking 3 tigers.
I am supposed to print average, maximum and minimum. Whenever I run the code it doesn't print the max or minimum, only the initialized values I have in the function. I am assuming the int main() ignores my return values completely, but I can't see why is that. I have done many functions before and I do the same code every time and call it in main
Here is the code:
int main(){
cout << "Enter whether you want to find minimum for tiger 1 2 or 3. (Please
only enter 0, 1 or 2): ";
cin >> temp;
if (temp < 0) {
cout << "CAN'T RUN NEGATIVE NUMBERS";
exit(2);
}
least(food, temp, minimum);
cout << "\n";
cout << "The Tiger " << temp << " has minimum: " << minimum << " ";
cout << "\n \n ";
}
float least(float food[][DAYS], int temp, float min) //loop for days only
{
minimum = food[0][0];
//temp has to be less than 3
for (int j = 0; j < DAYS; ++j) {
if (min<food[temp][j]) {
min = food[temp][j];
}
}
cout << min << " ";
return max;
}
system("PAUSE");
return 0;
}
Since you are not using the return value, use the max and min argument as reference variable in your function definitions. Also the comparison in least & Most functions seems to be wrong. It should be the opposite way.
float least(float food[][DAYS], int temp, float &min) //loop for days only
{
min = food[0][0]; //temp has to be les
for (int j = 0; j < DAYS; ++j) {
if (min>food[temp][j]) {
min = food[temp][j];
}
}
cout << min << " ";
return min;
}
float Most(float food[][DAYS], int amb, float &max) //loop for days only
{
max = food[0][0];
//amb has to be less than 3
for (int j = 0; j < DAYS; ++j) {
if (max<food[amb][j]) {
max = food[amb][j];
}
}
cout << max << " ";
return max;
}
You do not use your methods' return values. Replace
Most(food, amb, maximum);
and
least(food, temp, minimum);
with
maximum = Most(food, amb, maximum);
and
minimum = least(food, temp, minimum);

Double variable outputs 32767 always instead of user input

I'm writing code that asks about rainfall each month, then outputs yearly total, monthly average, min, and max rain month (index). My code is almost complete, except that instead of outputting the max value the output always shows the max range of the variable type 32767 (e.g. if rain is 70mm and maximum, console shows 32767 always). Thanks for your help. I'm self-learning.
I have tried the following code.
#include<iostream>
using namespace std;
int main(){
const int numMonths = 3;
double monthlyRain[numMonths]={0,0,0};
string monthName[numMonths]= {"Jan", "Feb", "Mar"};
int count = 0;
int indexWettest = 0;
int indexDriest = 0;
double yearTotal=0, monthlyAverage=0;
double monthHighest;
double monthLowest;
monthLowest = monthlyRain[0];
monthHighest = monthlyRain[0];
// enter monthly rain;
for (int count=0; count < numMonths; count++){
cout << "Please enter amount of rain in " << monthName[count] << endl;
cin >> monthlyRain[count];
}
// print month and corresponding rain amount;
cout << "Month --- Rain(mm)" << endl;
for (int count = 0; count < numMonths; count++){
cout << monthName[count] << " " << monthlyRain[count] << endl;
}
// calculate year total;
for (int count = 0; count < numMonths; count++){
yearTotal += monthlyRain[count];
}
// calculate average monthly rainfall;
monthlyAverage = yearTotal/numMonths;
// find month with lowest rainfall;
// find month with highest rainfall;
for (int count = 0; count < numMonths; count++){
if (monthlyRain[count] > monthHighest){
monthHighest = monthlyRain[count+1];
indexWettest = count;
}
}
// PROBLEM IS HERE!;
for (int count = 0; count < numMonths; count++){
if (monthlyRain[count] < monthLowest){
monthLowest = monthlyRain[count];
indexDriest = count;
}
}
cout << "Total yearly rain fall is: " << yearTotal << endl;
cout << "Average monthly rainfall is: " << monthlyAverage << endl;
cout << "The driest month is " << monthName[indexDriest] << " with rain amount of " << monthLowest << endl;
cout << "The wettest month is " << monthName[indexWettest] << " with rain amount of " << monthHighest << endl; //<< monthName[indexWettest];
return 0;
}
Expected result is the user input.
Read compiler warnings.
main.cpp:35:19: warning: 'yearTotal' may be used uninitialized in this function [-
Wmaybe-uninitialized]
yearTotal += monthlyRain[count];
Line that declares yearTotal should be double yearTotal = 0;
Same with other variables. Always initialize them.

Largest / Smallest from an array?

currently stuck on a homework problem and hoping for some hints as to why my code isn't working / what I'm missing. The homework question asks you to make an array that holds a string of salsa flavors for a store, gathers data from the user input on how many were sold (jars) and then display the total sales from each flavor sold. Ive got all that down, but the question goes on to ask you to retrieve the best seller and the worst seller. I've attempted and can get my code to pull two flavors, but they're completely incorrect. Any help is appreciated! Here is my code:
#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;
}
One of the biggest hindrances in debugging your own code is in naming your variables. If you've declared count, largest, and smallest variables at the start of your main() function but you redeclare them in a separate block later on. It's downright redundant.
The actual bug is that you're not actually calculating and filling your sales[] array. Comparing them later on for largest and smallest wouldn't work as intended.
Look for this line in the code below
sales[count] = jars[count] * 5.50;
Lé Code
#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;
}
Sample Console Interaction
Enter the monthly number of jars sold for each type of salsa.
Mild: 20
Medium: 10
Sweet: 5
Hot: 2
Zesty: 1
Each jar of salsa, no matter the type, costs $5.50.
Here are the monthly sales numbers for each type of salsa.
Mild: $110
Medium: $55
Sweet: $27
Hot: $11
Zesty: $5
Best Seller: Mild
Worst Seller: Zesty

c++ sorting array function

double gross_pay(double pay[7]){
int i;
double add;
add = 0;
for(i = 0; i < 7; i++){
add += pay[i];
}
return add;
}
int find_lowest(double pay[7]){
double lowest;
int index, i;
index = 0;
lowest = pay[0];
for(i = 1; i < 7; i++){
if(pay[i] < lowest){
index = i;
lowest = pay[i];
}
}
return index;
}
int find_highest(double pay[7]){
double highest;
int index, i;
index = 0;
highest = pay[0];
for(i = 1; i < 7; i++){
if(pay[i] > highest){
index = i;
highest = pay[i];
}
}
return index;
}
int main()
{
string dayweek[7];
double dailypay[7];
int i;
double pay, add;
int lowest, highest;
dayweek[0] = "Monday";
dayweek[1] = "Tuesday";
dayweek[2] = "Wednesday";
dayweek[3] = "Thursday";
dayweek[4] = "Friday";
dayweek[5] = "Saturday";
dayweek[6] = "Sunday";
for(i = 0; i < 7; i++){
cout << "Please enter the gross sales ecieved on" << endl;
cout << dayweek[i] << ": ";
cin >> pay;
dailypay[i] = pay;
cout << endl << endl;
}
add = gross_pay(dailypay);
lowest = find_lowest(dailypay);
highest = find_highest(dailypay);
cout << endl;
cout << "Total sales for the week: " << "$" << add << endl << endl;
cout << "Total taxes withheld: $" << add*.0975 << "\n\n";
cout << "Net profit: $" << add*.9025 <<"\n\n";
cout << "Day with the highest sales amount was" << endl;
cout << dayweek[highest].c_str() <<" with a sales total of $" << dailypay[highest];
cout << "\n\nDay with the lowest sales amount was" << endl;
cout << dayweek[lowest].c_str() << " with a sales total of $" << dailypay[lowest];
return 0;
}
cant seem to figure out the code that will sort the array in order from lowest to highest for example
Sales sorted from lowest to highest:
Monday had gross sales of $101.00
Tuesday had gross sales of $135.64
Sunday had gross sales of $245.55
Wednesday had gross sales of $533.44
Thursday had gross sales of $922.42
Saturday had gross sales of $1555.22
Friday had gross sales of $2242.63
those are also the input values for gross sales,
this is my first time asking a question on this site, if i did anything wrong please let me know,thanks for all the help !
could you write one using this
void sortArray(double arySales[], int size, string aryDays[])
{
bool swap;
double tempSales;
string tempDays;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (arySales[count] > arySales[count + 1])
{
tempSales = arySales[count];
arySales[count] = arySales[count + 1];
arySales[count + 1] = tempSales;
tempDays = aryDays[count];
aryDays[count] = aryDays[count + 1];
aryDays[count + 1] = tempDays;
swap = true;
}
}
} while (swap);
}
You should put the day-of-week index (0 to 6) plus the value for that day into a struct, and then populate a std::vector of these structs. Then you can sort them easily:
struct PayForDay {
int day; // 0..6
double pay;
};
bool PayLess(const PayForDay& lhs, const PayForDay& rhs) {
return lhs.pay < rhs.pay;
}
std::vector<PayForDay> payments;
std::sort(payments.begin(), payments.end(), PayLess);